ocaml-multicore / domainslib

Parallel Programming over Domains
ISC License
171 stars 30 forks source link

Unhandled exception in parallel_for #88

Closed artempyanykh closed 2 years ago

artempyanykh commented 2 years ago

I'm running ocaml5~alpha1 with flambda on an Apple M1. The code below consistently crashes with the following exception:

Fatal error: exception Unhandled
Raised by primitive operation at Domainslib__Task.parallel_for.work in file "lib/task.ml", line 198, characters 6-26
Called from Dune__exe__Domain_pool in file "domain_pool.ml", line 8, characters 2-66

The code (file: domain_pool.ml):

open Domainslib

let () =
  let n_workers = 4 in
  let pool = Task.setup_pool ~num_additional_domains:(n_workers - 1) () in
  Task.parallel_for pool ~start:0 ~finish:5 ~body:(fun _idx -> ());
  Task.teardown_pool pool;
  Printf.printf "Main done";
  ()

The error manifests both with the currently released version in opam and when pinning to the latest dev.

Sudha247 commented 2 years ago

Hi @artempyanykh, Task.parallel_for should be enclosed with a Task.run, as mentioned here: https://github.com/ocaml-multicore/domainslib/blob/master/lib/task.mli#L37-L40. Your code snippet would then be:

open Domainslib

let () =
  let n_workers = 4 in
  let pool = Task.setup_pool ~num_domains:(n_workers - 1) () in
  Task.run pool (fun () -> Task.parallel_for pool ~start:0 ~finish:5 ~body:(fun _idx -> ()));
  Printf.printf "Main done";
  ()
artempyanykh commented 2 years ago

Thanks for clarifying @Sudha247! This answers my question. I wonder, though, if the exception could be more specific rather than just Unhandled? Having an exception pointing to the fact a task has to be enclosed in run could help great with understandability/debuggability.