-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add create_int_array #7
Conversation
Wonderful. Thanks for including the API docs and example! Will merge once the CI turns green. |
I don't think this function quite works. Int arrays get scanned by the GC, and the GC will crash if it follows an uninitialised pointer. |
The tag remains |
Oh, I see. That's unsafe in a different way: if you call any of the generic-array functions (e.g. Array.get), they'll think it's a double array and box the result. |
Ok. I didn't consider that issue. I see two potential solutions (with their own issues): (1) Allocate the float array and change the tag to (2) Use |
I don't think this works either. There's still a race, and if the GC thread wins it'll clobber the I think a possible solution here is to use strings instead of arrays as the backing for the intarray type. There's already runtime support for allocating uninitialised strings, and there are no GC worries since the GC never scans strings. I've prototyped something along these lines here: https://gist.github.com/stedolan/f0cb0f5ab24f283d398e48763db1ae62 |
Ok. I missed that case. Concurrency is hard. @Sudha247 Stephen's solution is the best way forward as it does not mess about with |
Closing this in favour of Stephen's solution. I will make a separate PR for that. I have a question though, on @stedolan's code. The primitives |
I'm surprised it doesn't work in 4.06.1 - I think these primitives were added in 4.01 by ocaml/ocaml#5771 |
I might be incorrect; but I got this error message when I tried to build this on
I did a git grep on the source and did not find anything matching this. |
This patch adds a primitive
Task.create_int_array n
which returns an uninitialised integer array of length n.Stock OCaml and in-effect Multicore OCaml have
Array.create_float
which returns a fresh float array. There is no such primitive (yet) for integer arrays. TheArray.init
primitive walks through the array sequentially to initialise values in the array, which may be a bottleneck in Multicore OCaml programs.The idea is to use this primitive to allocate an uninitialised array and subsequently do the initialisation in parallel. An easy way to do this is by using the
parallel_for
primitive. One such example is provided intest/par_init.ml
.