-
Notifications
You must be signed in to change notification settings - Fork 35
/
Parallel.gr
33 lines (29 loc) · 1.28 KB
/
Parallel.gr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
------
--- Module: Parallel
--- Description: Combinators for parallel programming in Granule
--- Authors: Michael Vollmer, Dominic Orchard
--- License: BSD3
--- Copyright: (c) Authors 2022
--- Issue-tracking: https://github.com/dorchard/granule/issues
--- Repository: https://github.com/dorchard/granule
------
module Parallel where
import List
--- basic parallel combinator (defined in terms of linear channels)
--- `par` expects two thunks, `f` and `g`, and returns their resulting values in a pair
par : forall {a : Type, b : Type} . (() -> a) -> (() -> b) -> (a, b)
par f g = let c = forkLinear (\c' -> close (send c' (f ()))); -- compute f in new thread
b = g (); -- compute g in current thread
(a, c') = recv c; -- get result of f from other thread
-- (shared memory, should be constant time to share pointer!)
_ = close c'
in (a, b)
--- parallel combinator for lists of computations
--- `parList` expects a list of thunks, and returns a list of values (computed in parallel)
parList : forall {a : Type} . List (() -> a) -> List a
parList lst =
case lst of
Empty -> Empty;
(Next f fs) -> case (par f (\x -> case x of
() -> parList fs)) of
(e, lst2) -> Next e lst2