-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(fiber): add [Fiber.map_reduce_seq]
Map reduce a sequence in parallel without intermediate data structures <!-- ps-id: 3e833707-4b3b-41a0-bc11-5f363522d287 --> Signed-off-by: Rudi Grinberg <[email protected]>
- Loading branch information
Showing
3 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
open Stdune | ||
open Fiber.O | ||
|
||
let printf = Printf.printf | ||
let print_dyn dyn = Dyn.to_string dyn |> print_endline | ||
let () = Printexc.record_backtrace false | ||
|
||
module Scheduler = struct | ||
let t = Test_scheduler.create () | ||
let yield () = Test_scheduler.yield t | ||
let run f = Test_scheduler.run t f | ||
end | ||
|
||
let%expect_test "map_reduce_seq" = | ||
let test = | ||
let+ res = | ||
Fiber.map_reduce_seq | ||
(List.to_seq [ 1; 2; 3 ]) | ||
~f:(fun x -> | ||
printfn "x: %d" x; | ||
Fiber.return x) | ||
~empty:0 | ||
~commutative_combine:( + ) | ||
in | ||
printfn "final: %d" res | ||
in | ||
Scheduler.run test; | ||
[%expect {| | ||
x: 1 | ||
x: 2 | ||
x: 3 | ||
final: 6 |}]; | ||
let test = | ||
let ivars = List.init 3 ~f:(fun _ -> Fiber.Ivar.create ()) in | ||
Fiber.fork_and_join_unit | ||
(fun () -> | ||
let+ res = | ||
Fiber.map_reduce_seq | ||
(List.to_seq ivars) | ||
~f:(fun ivar -> | ||
let+ x = Fiber.Ivar.read ivar in | ||
printfn "x: %d" x; | ||
x) | ||
~empty:0 | ||
~commutative_combine:( + ) | ||
in | ||
printfn "final: %d" res) | ||
(fun () -> | ||
let i = ref 0 in | ||
Fiber.parallel_iter ivars ~f:(fun ivar -> | ||
incr i; | ||
printfn "filling ivar %d" !i; | ||
Fiber.Ivar.fill ivar !i)) | ||
in | ||
Scheduler.run test; | ||
[%expect | ||
{| | ||
filling ivar 1 | ||
filling ivar 2 | ||
filling ivar 3 | ||
x: 1 | ||
x: 2 | ||
x: 3 | ||
final: 6 |}] | ||
;; |