10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-06-17 02:35:18 +02:00

Changed input list in stream

This commit is contained in:
Anthony Scemama 2018-10-22 13:39:02 +02:00
parent 758c65e31c
commit 4bcab0647a
3 changed files with 19 additions and 22 deletions

View File

@ -25,20 +25,24 @@ let run_parallel_server stream =
in in
let send_task client_rank task_list = let send_task client_rank =
let task, rest = let task =
match task_list with try Some (Stream.next stream)
| [] -> None , [] with Stream.Failure -> None
| task :: rest -> Some task, rest
in in
Mpi.send task client_rank 0 Mpi.comm_world; Mpi.send task client_rank 0 Mpi.comm_world
rest
in in
let rec run result_list task_list = function let rec run result_list n_todo =
let n_todo =
match Stream.peek stream with
| None -> n_todo-1
| _ -> n_todo
in
match n_todo with
| 0 -> result_list | 0 -> result_list
| n_todo -> | _ ->
begin begin
let result, client = fetch_result () in let result, client = fetch_result () in
let new_result_list = let new_result_list =
@ -46,20 +50,14 @@ let run_parallel_server stream =
| None -> result_list | None -> result_list
| Some result -> result :: result_list | Some result -> result :: result_list
in in
let new_tasklist = send_task client task_list in send_task client;
run new_result_list new_tasklist (n_todo-1) run new_result_list n_todo
end end
in in
let result = let result =
let task_list = let n_todo = Mpi.comm_size Mpi.comm_world in
let rec to_list accu = run [] n_todo
try to_list (Stream.next stream :: accu)
with Stream.Failure -> List.rev accu
in to_list []
in
let n_todo = List.length task_list in
run [] task_list (n_todo + Parallel.size - 1)
|> Stream.of_list |> Stream.of_list
in in
Mpi.barrier Mpi.comm_world; Mpi.barrier Mpi.comm_world;

View File

@ -4,8 +4,7 @@ The input is a stream of input data, and the output is a stream of data.
*) *)
val run_parallel : ('a -> 'b) -> 'a Stream.t -> 'b Stream.t val run : ('a -> 'b) -> 'a Stream.t -> 'b Stream.t
val run_sequential : ('a -> 'b) -> 'a Stream.t -> 'b Stream.t
(** Run the [worker_function] on every process by popping elements from the (** Run the [worker_function] on every process by popping elements from the
input stream, and put the results on the output stream. The order of the input stream, and put the results on the output stream. The order of the
output is consistent with the order of the input. *) output is consistent with the order of the input. *)

View File

@ -26,6 +26,6 @@ let () =
let input = Stream.of_list let input = Stream.of_list
[ (1,2) ; (3,4) ; (5,6) ; (7,8) ; (9,10) ] [ (1,2) ; (3,4) ; (5,6) ; (7,8) ; (9,10) ]
in in
Farm.run_parallel f input Farm.run f input
|> Stream.iter (fun (x,y) -> Printf.printf "%d %d\n" x y) |> Stream.iter (fun (x,y) -> Printf.printf "%d %d\n" x y)