Module Parallel_arrays.Vec

type 'a t

of_vec and to_vec are the identity operation under the hood. This is memory safe because no other domain can get a Vec.t @ uncontended, so it cannot be resized in the middle of a parallel operation. It would be unsafe for Vec to implement concurrent incremental resizing in general, without updating existing code that manipulates it at uncontended. It would be similarly unsafe for any user of Vec to even Obj.magic_uncontended it and then resize it. Thus we are not introducing any additional memory unsafety in this library.

val of_vec : 'a. 'a Vec.t -> 'a t
val to_vec : 'a. 'a t -> 'a Vec.t
val length : 'a t @ shared -> Base.int

length t returns the number of elements in t.

To read a value from a parallel array, we must prove that it does not escape its capsule. This is the case if its type crosses contention, or if it is manipulated within a portable function.

val get : 'a. 'a t -> Base.int -> 'a

get t i reads the element at index i. Raises Invalid_arg if i is not in the range [0..length t).

val unsafe_get : 'a. 'a t -> Base.int -> 'a

unsafe_get t i unsafely reads the element at index i.

val extract : 'a t -> Base.int -> ('a -> 'b @ portable contended) @ local once portable -> 'b @ portable contended

extract t i f applies f with the element read from index i. Raises Invalid_arg if i is not in the range [0..length t).

val unsafe_extract : 'a t -> Base.int -> ('a -> 'b @ portable contended) @ local once portable -> 'b @ portable contended

unsafe_extract t i f applies f with the element unsafely read from index i.

To store a value in a parallel array, we must prove that it does not share unsynchronized state with any other elements. This is the case if its type crosses contention or it lives in a fresh capsule.

val set : 'a. 'a t -> Base.int -> 'a -> Base.unit @@ portable

set t i a stores the element a at index i. Raises Invalid_arg if i is not in the range [0..length t).

val unsafe_set : 'a. 'a t -> Base.int -> 'a -> Base.unit @@ portable

unsafe_set t i a unsafely stores the element a at index i.

val insert : 'a t -> Base.int -> (Base.unit -> 'a) @ local once portable -> Base.unit @@ portable

insert t i f stores f () at index i. Raises Invalid_arg if i is not in the range [0..length t).

val unsafe_insert : 'a t -> Base.int -> (Base.unit -> 'a) @ local once portable -> Base.unit @@ portable

unsafe_insert t i f unsafely stores f () at index i.

module Slice : Slice with type 'a array := 'a t
type 'a init = Base.int
val init : Parallel_kernel.t @ local -> ('a init -> (f:(Parallel_kernel.t @ local -> (Base.int -> 'a) @ local) @ shareable -> 'a t) @ local) @ local @@ portable

init parallel n ~f initializes an array with the result of f applied to the integers 0..n-1.

Mapping functions do not need to be templated over the mode of their output type. To work with a contended or shared 'b, return a 'b Modes.Contended.t or 'b Modes.Shared.t.

val map : Parallel_kernel.t @ local -> ('a t -> (f:(Parallel_kernel.t @ local -> ('a -> 'b) @ local) @ shareable -> 'b t) @ local) @ local

map parallel t ~f initializes an array with the result of f applied to each element of t.

val mapi : Parallel_kernel.t @ local -> ('a t -> (f: (Parallel_kernel.t @ local -> (Base.int -> ('a -> 'b) @ local) @ local) @ shareable -> 'b t) @ local) @ local

mapi parallel t ~f initializes an array with the result of f applied to each element of t and its index.

val map2_exn : Parallel_kernel.t @ local -> ('a t -> ('b t -> (f: (Parallel_kernel.t @ local -> ('a -> ('b -> 'c) @ local) @ local) @ shareable -> 'c t) @ local) @ local) @ local

map2_exn parallel t0 t1 ~f initializes an array with the result of f applied to each pair of elements of t0, t1. Raises if t0 and t1 do not have equal lengths.

val mapi2_exn : Parallel_kernel.t @ local -> ('a t -> ('b t -> (f: (Parallel_kernel.t @ local -> (Base.int -> ('a -> ('b -> 'c) @ local) @ local) @ local) @ shareable -> 'c t) @ local) @ local) @ local

mapi2_exn parallel t0 t1 ~f initializes an array with the result of f applied to each pair of element of t0, t1 and their index. Raises if t0 and t1 do not have equal lengths.

val iter : Parallel_kernel.t @ local -> ('a t -> (f:(Parallel_kernel.t @ local -> ('a -> Base.unit) @ local) @ shareable -> Base.unit) @ local) @ local

iter parallel t ~f applies f to each element of t.

val iteri : Parallel_kernel.t @ local -> ('a t -> (f: (Parallel_kernel.t @ local -> (Base.int -> ('a -> Base.unit) @ local) @ local) @ shareable -> Base.unit) @ local) @ local

iteri parallel t ~f applies f to each element of t and its index.

val find : Parallel_kernel.t @ local -> ('a t -> (f:(Parallel_kernel.t @ local -> ('a -> Base.bool) @ local) @ shareable -> 'a Base.option) @ local) @ local

find parallel t ~f returns the first element of t for which f returns true, if it exists. f will always be applied to every element of t.

val findi : Parallel_kernel.t @ local -> ('a t -> (f: (Parallel_kernel.t @ local -> (Base.int -> ('a -> Base.bool) @ local) @ local) @ shareable -> 'a Base.option) @ local) @ local

findi parallel t ~f returns the first element of t for which f returns true, if it exists. f will always be applied to every element of t and its index.

val reduce : Parallel_kernel.t @ local -> ('a t -> (f: (Parallel_kernel.t @ local -> ('a @ shared -> ('a @ shared -> 'a) @ local) @ local) @ shareable -> 'a Base.option) @ local) @ local

reduce parallel t ~f folds f over the elements of t. f must be associative. If t is empty, reduce returns None.

val min_elt : Parallel_kernel.t @ local -> ('a t -> (compare: (Parallel_kernel.t @ local -> ('a @ local shared -> ('a @ local shared -> Base.int) @ local) @ local) @ shareable -> 'a Base.option) @ local) @ local

min_elt parallel t ~compare is the minimum element of t according to compare. If t is empty, returns None.

val max_elt : Parallel_kernel.t @ local -> ('a t -> (compare: (Parallel_kernel.t @ local -> ('a @ local shared -> ('a @ local shared -> Base.int) @ local) @ local) @ shareable -> 'a Base.option) @ local) @ local

max_elt parallel t ~compare is the maximum element of t according to compare. If t is empty, returns None.

val fold : Parallel_kernel.t @ local -> ('a t @ shared -> (init:(Base.unit -> 'acc) @ portable -> (f: (Parallel_kernel.t @ local -> ('acc -> ('a @ shared -> 'acc) @ local) @ local) @ shareable -> (combine: (Parallel_kernel.t @ local -> ('acc -> ('acc -> 'acc) @ local) @ local) @ shareable -> 'acc) @ local) @ local) @ local) @ local @@ portable

fold parallel t ~init ~f ~combine folds combine over the result of map parallel t ~f. combine must be associative and combine init x must equal x.

val foldi : Parallel_kernel.t @ local -> ('a t @ shared -> (init:(Base.unit -> 'acc) @ portable -> (f: (Parallel_kernel.t @ local -> (Base.int -> ('acc -> ('a @ shared -> 'acc) @ local) @ local) @ local) @ shareable -> (combine: (Parallel_kernel.t @ local -> ('acc -> ('acc -> 'acc) @ local) @ local) @ shareable -> 'acc) @ local) @ local) @ local) @ local @@ portable

foldi parallel t ~init ~f ~combine folds combine over the result of mapi parallel t ~f. combine must be associative and combine init x must equal x.

val sort : Parallel_kernel.t @ local -> ('a t -> (compare: (Parallel_kernel.t @ local -> ('a @ local shared -> ('a @ local shared -> Base.int) @ local) @ local) @ shareable -> 'a t) @ local) @ local

sort parallel t ~compare initializes an array with the contents of t unstably sorted with respect to compare.

val stable_sort : Parallel_kernel.t @ local -> ('a t -> (compare: (Parallel_kernel.t @ local -> ('a @ local shared -> ('a @ local shared -> Base.int) @ local) @ local) @ shareable -> 'a t) @ local) @ local

stable_sort parallel t ~compare initializes an array with the contents of t stably sorted with respect to compare.

val scan : Parallel_kernel.t @ local -> ('a t -> (init:'a -> (f: (Parallel_kernel.t @ local -> ('a @ shared -> ('a @ shared -> 'a) @ local) @ local) @ shareable -> 'a t * 'a) @ local) @ local) @ local

scan parallel t ~init ~f initialises an array containing the exclusive prefix sums of t with respect to f. The first element is init and the full reduction of t is returned separately. f must be associative and f init x must equal x.

val scan_inclusive : Parallel_kernel.t @ local -> ('a t -> (init:'a -> (f: (Parallel_kernel.t @ local -> ('a @ shared -> ('a @ shared -> 'a) @ local) @ local) @ shareable -> 'a t) @ local) @ local) @ local

scan_inclusive parallel t ~init ~f initialises an array containing the inclusive prefix sums of t with respect to f. The first element is the first element of t. f must be associative and f init x must equal x.

val filter : Parallel_kernel.t @ local -> ('a t -> (f:(Parallel_kernel.t @ local -> ('a -> Base.bool) @ local) @ shareable -> 'a t) @ local) @ local

filter parallel t ~f initialises an array containing the elements of t that satisfy the predicate f.

val filteri : Parallel_kernel.t @ local -> ('a t -> (f: (Parallel_kernel.t @ local -> (Base.int -> ('a -> Base.bool) @ local) @ local) @ shareable -> 'a t) @ local) @ local

filteri parallel t ~f initialises an array containing the elements of t that, alongside their index, satisfy the predicate f.

val map_inplace : Parallel_kernel.t @ local -> ('a t -> (f:(Parallel_kernel.t @ local -> ('a -> 'a) @ local) @ shareable -> Base.unit) @ local) @ local @@ portable

map_inplace parallel t ~f overwrites an array with the result of f applied to each of its elements.

val mapi_inplace : Parallel_kernel.t @ local -> ('a t -> (f: (Parallel_kernel.t @ local -> (Base.int -> ('a -> 'a) @ local) @ local) @ shareable -> Base.unit) @ local) @ local @@ portable

mapi_inplace parallel t ~f overwrites an array with the result of f applied to each of its elements and their indices.

val init_inplace : Parallel_kernel.t @ local -> ('a t -> (f:(Parallel_kernel.t @ local -> (Base.int -> 'a) @ local) @ shareable -> Base.unit) @ local) @ local @@ portable

init_inplace parallel t ~f overwrites an array with the result of f applied to each array index. This can be much faster than using mapi_inplace since it does not need to read the array.

val sort_inplace : Parallel_kernel.t @ local -> ('a t -> (compare: (Parallel_kernel.t @ local -> ('a @ local shared -> ('a @ local shared -> Base.int) @ local) @ local) @ shareable -> Base.unit) @ local) @ local @@ portable

sort_inplace parallel t ~compare unstably sorts t with respect to compare.

val stable_sort_inplace : Parallel_kernel.t @ local -> ('a t -> (compare: (Parallel_kernel.t @ local -> ('a @ local shared -> ('a @ local shared -> Base.int) @ local) @ local) @ shareable -> Base.unit) @ local) @ local @@ portable

stable_sort_inplace parallel t ~compare stably sorts t with respect to compare.

val scan_inplace : Parallel_kernel.t @ local -> ('a t -> (init:'a -> (f: (Parallel_kernel.t @ local -> ('a @ shared -> ('a @ shared -> 'a) @ local) @ local) @ shareable -> 'a) @ local) @ local) @ local @@ portable

scan_inplace parallel t ~init ~f overwrites t to contain the its exclusive prefix sums with respect to f. The first element becomes init and the full reduction of t is returned. f must be associative and f init x must equal x.

val scan_inclusive_inplace : Parallel_kernel.t @ local -> ('a t -> (init:'a -> (f: (Parallel_kernel.t @ local -> ('a @ shared -> ('a @ shared -> 'a) @ local) @ local) @ shareable -> Base.unit) @ local) @ local) @ local @@ portable

scan_inclusive_inplace parallel t ~init ~f overwrites t to contain its inclusive prefix sums with respect to f. The first element is unchanged. f must be associative and f init x must equal x.