| author | blanchet | 
| Thu, 26 Apr 2012 20:09:38 +0200 | |
| changeset 47782 | 1678955ca991 | 
| parent 47404 | e6e5750f1311 | 
| child 49319 | f4b91a3a5f0f | 
| 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 | |
| 44265 
b94951f06e48
export Par_List.managed_results, to enable specific treatment of results apart from default Par_Exn.release_first;
 wenzelm parents: 
44247diff
changeset | 19 |   val managed_results: string -> ('a -> 'b) -> 'a list -> 'b Exn.result list
 | 
| 41711 | 20 |   val map_name: string -> ('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 | ||
| 41673 | 31 | fun managed_results name f xs = | 
| 41675 | 32 | if null xs orelse null (tl xs) orelse | 
| 33 | not (Multithreading.enabled ()) orelse Multithreading.self_critical () | |
| 34 | then map (Exn.capture f) xs | |
| 35 | 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: 
37865diff
changeset | 36 | let | 
| 44300 
349cc426d929
tuned signature -- treat structure Task_Queue as private to implementation;
 wenzelm parents: 
44299diff
changeset | 37 | val group = Future.new_group (Future.worker_group ()); | 
| 41672 
2f70b1ddd09f
more direct Future.bulk, which potentially reduces overhead for Par_List;
 wenzelm parents: 
40700diff
changeset | 38 | val futures = | 
| 44113 | 39 |         Future.forks {name = name, group = SOME group, deps = [], pri = 0, interrupts = true}
 | 
| 41673 | 40 | (map (fn x => fn () => f x) xs); | 
| 41672 
2f70b1ddd09f
more direct Future.bulk, which potentially reduces overhead for Par_List;
 wenzelm parents: 
40700diff
changeset | 41 | val results = Future.join_results futures | 
| 47404 
e6e5750f1311
simplified Future.cancel/cancel_group (again) -- running threads only;
 wenzelm parents: 
44300diff
changeset | 42 | handle exn => (if Exn.is_interrupt exn then Future.cancel_group group else (); reraise exn); | 
| 41675 | 43 | in results end; | 
| 28199 | 44 | |
| 44247 | 45 | fun map_name name f xs = Par_Exn.release_first (managed_results name f xs); | 
| 41711 | 46 | fun map f = map_name "Par_List.map" f; | 
| 28199 | 47 | |
| 48 | fun get_some f xs = | |
| 49 | let | |
| 50 | exception FOUND of 'b option; | |
| 51 | fun found (Exn.Exn (FOUND some)) = some | |
| 52 | | 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: 
37865diff
changeset | 53 | val results = | 
| 41673 | 54 | managed_results "Par_List.get_some" | 
| 55 | (fn x => (case f x of NONE => () | some => raise FOUND some)) xs; | |
| 28199 | 56 | in | 
| 57 | (case get_first found results of | |
| 58 | SOME y => SOME y | |
| 44247 | 59 | | NONE => (Par_Exn.release_first results; NONE)) | 
| 28199 | 60 | end; | 
| 61 | ||
| 62 | fun find_some P = get_some (fn x => if P x then SOME x else NONE); | |
| 63 | ||
| 64 | fun exists P = is_some o get_some (fn x => if P x then SOME () else NONE); | |
| 65 | fun forall P = not o exists (not o P); | |
| 66 | ||
| 67 | end; |