1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-30 15:00:21 +02:00

Update `par-map' to use nested futures.

This allows it to actually use all CPU cores, instead of having the main
thread stuck on a `wait-condition-variable'.

* module/ice-9/threads.scm (par-mapper): Add a `cons' argument; update
  callers accordingly.  Rewrite using nested futures.
This commit is contained in:
Ludovic Courtès 2012-11-17 00:11:23 +01:00
parent 3e529bf02a
commit 2d37a93494

View file

@ -1,4 +1,5 @@
;;;; Copyright (C) 1996, 1998, 2001, 2002, 2003, 2006, 2010, 2011 Free Software Foundation, Inc. ;;;; Copyright (C) 1996, 1998, 2001, 2002, 2003, 2006, 2010, 2011,
;;;; 2012 Free Software Foundation, Inc.
;;;; ;;;;
;;;; This library is free software; you can redistribute it and/or ;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public ;;;; modify it under the terms of the GNU Lesser General Public
@ -33,6 +34,7 @@
(define-module (ice-9 threads) (define-module (ice-9 threads)
#:use-module (ice-9 futures) #:use-module (ice-9 futures)
#:use-module (ice-9 match)
#:export (begin-thread #:export (begin-thread
parallel parallel
letpar letpar
@ -87,16 +89,19 @@
(with-mutex (make-mutex) (with-mutex (make-mutex)
first rest ...)) first rest ...))
(define (par-mapper mapper) (define (par-mapper mapper cons)
(lambda (proc . arglists) (lambda (proc . lists)
(mapper touch (let loop ((lists lists))
(apply map (match lists
(lambda args (((heads tails ...) ...)
(future (apply proc args))) (let ((tail (future (loop tails)))
arglists)))) (head (apply proc heads)))
(cons head (touch tail))))
(_
'())))))
(define par-map (par-mapper map)) (define par-map (par-mapper map cons))
(define par-for-each (par-mapper for-each)) (define par-for-each (par-mapper for-each (const *unspecified*)))
(define (n-par-map n proc . arglists) (define (n-par-map n proc . arglists)
(let* ((m (make-mutex)) (let* ((m (make-mutex))