author | wenzelm |
Mon, 31 Jan 2011 23:10:16 +0100 | |
changeset 41675 | 0f0f6212d6c6 |
parent 41674 | 7da257539a8d |
child 41711 | 3422ae5aff3a |
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 |
|
19 |
val map: ('a -> 'b) -> 'a list -> 'b list |
|
20 |
val get_some: ('a -> 'b option) -> 'a list -> 'b option |
|
21 |
val find_some: ('a -> bool) -> 'a list -> 'a option |
|
22 |
val exists: ('a -> bool) -> 'a list -> bool |
|
23 |
val forall: ('a -> bool) -> 'a list -> bool |
|
24 |
end; |
|
25 |
||
29368 | 26 |
structure Par_List: PAR_LIST = |
28199 | 27 |
struct |
28 |
||
41673 | 29 |
fun managed_results name f xs = |
41675 | 30 |
if null xs orelse null (tl xs) orelse |
31 |
not (Multithreading.enabled ()) orelse Multithreading.self_critical () |
|
32 |
then map (Exn.capture f) xs |
|
33 |
else |
|
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
|
34 |
let |
41672
2f70b1ddd09f
more direct Future.bulk, which potentially reduces overhead for Par_List;
wenzelm
parents:
40700
diff
changeset
|
35 |
val group = Task_Queue.new_group (Future.worker_group ()); |
2f70b1ddd09f
more direct Future.bulk, which potentially reduces overhead for Par_List;
wenzelm
parents:
40700
diff
changeset
|
36 |
val futures = |
41674 | 37 |
Future.forks {name = name, group = SOME group, deps = [], pri = 0} |
41673 | 38 |
(map (fn x => fn () => f x) xs); |
41672
2f70b1ddd09f
more direct Future.bulk, which potentially reduces overhead for Par_List;
wenzelm
parents:
40700
diff
changeset
|
39 |
val results = Future.join_results futures |
2f70b1ddd09f
more direct Future.bulk, which potentially reduces overhead for Par_List;
wenzelm
parents:
40700
diff
changeset
|
40 |
handle exn => (if Exn.is_interrupt exn then Future.cancel_group group else (); reraise exn); |
41675 | 41 |
in results end; |
28199 | 42 |
|
41673 | 43 |
fun map f xs = Exn.release_first (managed_results "Par_List.map" f xs); |
28199 | 44 |
|
45 |
fun get_some f xs = |
|
46 |
let |
|
47 |
exception FOUND of 'b option; |
|
48 |
fun found (Exn.Exn (FOUND some)) = some |
|
49 |
| found _ = NONE; |
|
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
|
50 |
val results = |
41673 | 51 |
managed_results "Par_List.get_some" |
52 |
(fn x => (case f x of NONE => () | some => raise FOUND some)) xs; |
|
28199 | 53 |
in |
54 |
(case get_first found results of |
|
55 |
SOME y => SOME y |
|
28443 | 56 |
| NONE => (Exn.release_first results; NONE)) |
28199 | 57 |
end; |
58 |
||
59 |
fun find_some P = get_some (fn x => if P x then SOME x else NONE); |
|
60 |
||
61 |
fun exists P = is_some o get_some (fn x => if P x then SOME () else NONE); |
|
62 |
fun forall P = not o exists (not o P); |
|
63 |
||
64 |
end; |