author | wenzelm |
Mon, 02 Dec 2024 14:08:28 +0100 | |
changeset 81537 | d230683a35fc |
parent 78705 | fde0b195cb7d |
permissions | -rw-r--r-- |
28199 | 1 |
(* Title: Pure/Concurrent/par_list.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Parallel list combinators. |
|
5 |
||
6 |
Notes: |
|
7 |
||
8 |
* These combinators only make sense if the operator (function or |
|
9 |
predicate) applied to the list of operands takes considerable |
|
10 |
time. The overhead of scheduling is significantly higher than |
|
11 |
just traversing the list of operands sequentially. |
|
12 |
||
28358 | 13 |
* The order of operator application is non-deterministic. Watch out |
28199 | 14 |
for operators that have side-effects or raise exceptions! |
15 |
*) |
|
16 |
||
17 |
signature PAR_LIST = |
|
18 |
sig |
|
73686 | 19 |
val map': {name: string, sequential: bool} -> ('a -> 'b) -> 'a list -> 'b list |
58993
302104d8366b
prefer independent parallel map where user input is processed -- avoid non-deterministic feedback in error situations;
wenzelm
parents:
52945
diff
changeset
|
20 |
val map_independent: ('a -> 'b) -> 'a list -> 'b list |
28199 | 21 |
val map: ('a -> 'b) -> 'a list -> 'b list |
22 |
val get_some: ('a -> 'b option) -> 'a list -> 'b option |
|
23 |
val find_some: ('a -> bool) -> 'a list -> 'a option |
|
24 |
val exists: ('a -> bool) -> 'a list -> bool |
|
25 |
val forall: ('a -> bool) -> 'a list -> bool |
|
26 |
end; |
|
27 |
||
29368 | 28 |
structure Par_List: PAR_LIST = |
28199 | 29 |
struct |
30 |
||
73686 | 31 |
fun managed_results {name, sequential} f xs = |
32 |
if sequential orelse not (Future.relevant xs) then map (Exn.capture f) xs |
|
33 |
else |
|
34 |
Future.forked_results |
|
35 |
{name = if name = "" then "Par_List.map" else name, deps = []} |
|
36 |
(map (fn x => fn () => f x) xs); |
|
28199 | 37 |
|
73686 | 38 |
fun map' params f xs = Par_Exn.release_first (managed_results params f xs); |
39 |
fun map f = map' {name = "", sequential = false} f; |
|
78705
fde0b195cb7d
clarified signature: avoid association with potentially dangerous Exn.capture;
wenzelm
parents:
73686
diff
changeset
|
40 |
fun map_independent f = map (Exn.result f) #> Par_Exn.release_all; |
28199 | 41 |
|
42 |
fun get_some f xs = |
|
43 |
let |
|
59136
c2b23cb8a677
added Par_List in Scala, in accordance to ML version;
wenzelm
parents:
58993
diff
changeset
|
44 |
exception FOUND of 'b; |
40700
4b4dfe05b5d7
clarified Par_List.managed_results, with explicit propagation of outermost physical interrupt to forked futures (e.g. to make timeout apply here as expected and prevent zombies);
wenzelm
parents:
37865
diff
changeset
|
45 |
val results = |
73686 | 46 |
managed_results {name = "Par_List.get_some", sequential = false} |
59136
c2b23cb8a677
added Par_List in Scala, in accordance to ML version;
wenzelm
parents:
58993
diff
changeset
|
47 |
(fn x => (case f x of NONE => () | SOME y => raise FOUND y)) xs; |
28199 | 48 |
in |
59136
c2b23cb8a677
added Par_List in Scala, in accordance to ML version;
wenzelm
parents:
58993
diff
changeset
|
49 |
(case get_first (fn Exn.Exn (FOUND res) => SOME res | _ => NONE) results of |
c2b23cb8a677
added Par_List in Scala, in accordance to ML version;
wenzelm
parents:
58993
diff
changeset
|
50 |
NONE => (Par_Exn.release_first results; NONE) |
c2b23cb8a677
added Par_List in Scala, in accordance to ML version;
wenzelm
parents:
58993
diff
changeset
|
51 |
| some => some) |
28199 | 52 |
end; |
53 |
||
54 |
fun find_some P = get_some (fn x => if P x then SOME x else NONE); |
|
55 |
||
59137 | 56 |
fun exists P = is_some o find_some P; |
28199 | 57 |
fun forall P = not o exists (not o P); |
58 |
||
59 |
end; |