13 |
* Threads:: Multiple threads of execution. |
* Threads:: Multiple threads of execution. |
14 |
* Fluids:: Thread-local variables. |
* Fluids:: Thread-local variables. |
15 |
* Futures:: Delayed execution in new threads. |
* Futures:: Delayed execution in new threads. |
16 |
|
* Parallel Forms:: Parallel execution of forms. |
17 |
@end menu |
@end menu |
18 |
|
|
19 |
|
|
688 |
@end deffn |
@end deffn |
689 |
|
|
690 |
|
|
691 |
|
@node Parallel Forms |
692 |
|
@section Parallel Forms |
693 |
|
|
694 |
|
The functions described in this section are available from |
695 |
|
|
696 |
|
@example |
697 |
|
(use-modules (ice-9 threads)) |
698 |
|
@end example |
699 |
|
|
700 |
|
@deffn syntax parallel expr1 @dots{} exprN |
701 |
|
Evaluate each @var{expr} expression in parallel, each in a new thread. |
702 |
|
Return the results as a set of @var{N} multiple values |
703 |
|
(@pxref{Multiple Values}). |
704 |
|
@end deffn |
705 |
|
|
706 |
|
@deffn syntax letpar ((var1 expr1) @dots{} (varN exprN)) body@dots{} |
707 |
|
Evaluate each @var{expr} in parallel, each in a new thread, then bind |
708 |
|
the results to the corresponding @var{var} variables and evaluate |
709 |
|
@var{body}. |
710 |
|
|
711 |
|
@code{letpar} is like @code{let} (@pxref{Local Bindings}), but all the |
712 |
|
expressions for the bindings are evaluated in parallel. |
713 |
|
@end deffn |
714 |
|
|
715 |
|
@deffn {Scheme Procedure} par-map proc lst1 @dots{} lstN |
716 |
|
@deffnx {Scheme Procedure} par-for-each proc lst1 @dots{} lstN |
717 |
|
Call @var{proc} on the elements of the given lists. @code{par-map} |
718 |
|
returns a list comprising the return values from @var{proc}. |
719 |
|
@code{par-for-each} returns an unspecified value, but waits for all |
720 |
|
calls to complete. |
721 |
|
|
722 |
|
The @var{proc} calls are @code{(@var{proc} @var{elem1} @dots{} |
723 |
|
@var{elemN})}, where each @var{elem} is from the corresponding |
724 |
|
@var{lst}. Each @var{lst} must be the same length. The calls are |
725 |
|
made in parallel, each in a new thread. |
726 |
|
|
727 |
|
These functions are like @code{map} and @code{for-each} (@pxref{List |
728 |
|
Mapping}), but make their @var{proc} calls in parallel. |
729 |
|
@end deffn |
730 |
|
|
731 |
|
@deffn {Scheme Procedure} n-par-map n proc lst1 @dots{} lstN |
732 |
|
@deffnx {Scheme Procedure} n-par-for-each n proc lst1 @dots{} lstN |
733 |
|
Call @var{proc} on the elements of the given lists, in the same way as |
734 |
|
@code{par-map} and @code{par-for-each} above, but use no more than |
735 |
|
@var{n} new threads at any one time. The order in which calls are |
736 |
|
initiated within that threads limit is unspecified. |
737 |
|
|
738 |
|
These functions are good for controlling resource consumption if |
739 |
|
@var{proc} calls might be costly, or if there are many to be made. On |
740 |
|
a dual-CPU system for instance @math{@var{n}=4} might be enough to |
741 |
|
keep the CPUs utilized, and not consume too much memory. |
742 |
|
@end deffn |
743 |
|
|
744 |
|
@deffn {Scheme Procedure} n-for-each-par-map n sproc pproc lst1 @dots{} lstN |
745 |
|
Apply @var{pproc} to the elements of the given lists, and apply |
746 |
|
@var{sproc} to each result returned by @var{pproc}. The final return |
747 |
|
value is unspecified, but all calls will have been completed before |
748 |
|
returning. |
749 |
|
|
750 |
|
The calls made are @code{(@var{sproc} (@var{pproc} @var{elem1} @dots{} |
751 |
|
@var{elemN}))}, where each @var{elem} is from the corresponding |
752 |
|
@var{lst}. Each @var{lst} must have the same number of elements. |
753 |
|
|
754 |
|
The @var{pproc} calls are made in parallel, in new threads. No more |
755 |
|
than @var{n} new threads are used at any one time. The order in which |
756 |
|
@var{pproc} calls are initiated within that limit is unspecified. |
757 |
|
|
758 |
|
The @var{sproc} calls are made serially, in list element order, one at |
759 |
|
a time. @var{pproc} calls on later elements may execute in parallel |
760 |
|
with the @var{sproc} calls. Exactly which thread makes each |
761 |
|
@var{sproc} call is unspecified. |
762 |
|
|
763 |
|
This function is designed for individual calculations that can be done |
764 |
|
in parallel, but with results needing to be handled serially, for |
765 |
|
instance to write them to a file. The @var{n} limit on threads |
766 |
|
controls system resource usage when there are many calculations or |
767 |
|
when they might be costly. |
768 |
|
|
769 |
|
It will be seen that @code{n-for-each-par-map} is like a combination |
770 |
|
of @code{n-par-map} and @code{for-each}, |
771 |
|
|
772 |
|
@example |
773 |
|
(for-each sproc (n-par-map pproc lst1 ... lstN)) |
774 |
|
@end example |
775 |
|
|
776 |
|
@noindent |
777 |
|
But the actual implementation is more efficient since each @var{sproc} |
778 |
|
call, in turn, can be initiated once the relevant @var{pproc} call has |
779 |
|
completed, it doesn't need to wait for all to finish. |
780 |
|
@end deffn |
781 |
|
|
782 |
|
|
783 |
@c Local Variables: |
@c Local Variables: |
784 |
@c TeX-master: "guile.texi" |
@c TeX-master: "guile.texi" |
785 |
@c End: |
@c End: |