unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Delimited continuations to the rescue of futures
@ 2012-11-16 23:36 Ludovic Courtès
  2012-11-17  4:38 ` Mark H Weaver
  2013-01-14  2:34 ` Nala Ginrut
  0 siblings, 2 replies; 14+ messages in thread
From: Ludovic Courtès @ 2012-11-16 23:36 UTC (permalink / raw)
  To: guile-devel

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’.




^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2013-01-14 10:37 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-16 23:36 Delimited continuations to the rescue of futures Ludovic Courtès
2012-11-17  4:38 ` 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

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).