unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: ludo@gnu.org (Ludovic Courtès)
To: guile-devel@gnu.org
Subject: Delimited continuations to the rescue of futures
Date: Sat, 17 Nov 2012 00:36:56 +0100	[thread overview]
Message-ID: <87ip95unl3.fsf@gnu.org> (raw)

Hello!

As was reported recently by Mark and others, ‘par-map’ would only use
ncores - 1, because the main thread was stuck in a
‘wait-condition-variable’ while touching one of the futures.

The obvious fix is to write ‘par-map’ like this (as can be seen from
Chapter 2 of Marc Feeley’s PhD thesis):

  (define (par-mapper mapper cons)
    (lambda (proc . lists)
      (let loop ((lists lists))
        (match lists
          (((heads tails ...) ...)
           (let ((tail (future (loop tails)))
                 (head (apply proc heads)))
             (cons head (touch tail))))
          (_
           '())))))

However, our futures did not support “nested futures”.  That is, if a
future touched another future, it would also wait on a condition
variable until the latter completes.  Thus, the above code would only
use one core.

So the fix is to support nested futures, by properly scheduling futures
that are active, and adding those that are waiting to a wait queue.
Those added to the wait queue have their continuation captured (yeah!),
so that they can be later rescheduled when their “waitee” has completed.

But then there’s still the problem of the main thread: it’s silly to let
it just wait on a condition variable when it touches a future that has
not completed yet.  So now it behaves as a worker, processing futures
until the one it’s waiting for has completed.

Figures from my 2-core/4-thread laptop:

--8<---------------cut here---------------start------------->8---
$ time ./meta/guile -c '(begin (use-modules (ice-9 threads)) (define (fibo n) (if (<= n 1) n (+ (fibo (- n 1)) (fibo (- n 2))))) (pk "r" (map fibo (make-list 4 30))))'

;;; ("r" (832040 832040 832040 832040))

real    0m27.864s
user    0m27.773s
sys     0m0.031s

$ time ./meta/guile -c '(begin (use-modules (ice-9 threads)) (define (fibo n) (if (<= n 1) n (+ (fibo (- n 1)) (fibo (- n 2))))) (pk "r" (par-map fibo (make-list 4 30))))'

;;; ("r" (832040 832040 832040 832040))

real    0m10.899s
user    0m42.487s
sys     0m0.051s
--8<---------------cut here---------------end--------------->8---

The speedup is not optimal, but there’s room for optimization.

I’ve pushed the result in ‘wip-nested-futures’.  Please review and test!

Thanks,
Ludo’.




             reply	other threads:[~2012-11-16 23:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-16 23:36 Ludovic Courtès [this message]
2012-11-17  4:38 ` Delimited continuations to the rescue of futures Mark H Weaver
2012-11-17 13:43   ` Ludovic Courtès
2012-11-17 16:56     ` Mark H Weaver
2012-11-17 22:00       ` Ludovic Courtès
2012-11-18 22:19         ` Mark H Weaver
2012-11-20 19:20           ` Ludovic Courtès
2012-11-21 12:18             ` Peter TB Brett
2012-11-21 13:36               ` Ludovic Courtès
2012-11-21 16:45                 ` Peter TB Brett
2012-11-21 21:19                   ` Ludovic Courtès
2012-11-21 23:28         ` Ludovic Courtès
2013-01-14  2:34 ` Nala Ginrut
2013-01-14 10:37   ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ip95unl3.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).