Bigarray_extSourceinclude module type of struct include Bigarray endBigarrays can contain elements of the following kinds:
Bigarray.float16_elt),Bigarray.float32_elt),Bigarray.float64_elt),Bigarray.complex32_elt),Bigarray.complex64_elt),Bigarray.int8_signed_elt or Bigarray.int8_unsigned_elt),Bigarray.int16_signed_elt or Bigarray.int16_unsigned_elt),Bigarray.int_elt),Bigarray.int32_elt),Bigarray.int64_elt),Bigarray.nativeint_elt).Each element kind is represented at the type level by one of the *_elt types defined below (defined with a single constructor instead of abstract types for technical injectivity reasons).
To facilitate interoperability with existing C and Fortran code, this library supports two different memory layouts for Bigarrays, one compatible with the C conventions, the other compatible with the Fortran conventions.
In the C-style layout, array indices start at 0, and multi-dimensional arrays are laid out in row-major format. That is, for a two-dimensional array, all elements of row 0 are contiguous in memory, followed by all elements of row 1, etc. In other terms, the array elements at (x,y) and (x, y+1) are adjacent in memory.
In the Fortran-style layout, array indices start at 1, and multi-dimensional arrays are laid out in column-major format. That is, for a two-dimensional array, all elements of column 0 are contiguous in memory, followed by all elements of column 1, etc. In other terms, the array elements at (x,y) and (x+1, y) are adjacent in memory.
Each layout style is identified at the type level by the phantom types Bigarray.c_layout and Bigarray.fortran_layout respectively.
The GADT type 'a layout represents one of the two supported memory layouts: C-style or Fortran-style. Its constructors are re-exported as values below for backward-compatibility reasons.
type 'a layout = 'a Bigarray.layout = | C_layout : c_layout layout| Fortran_layout : fortran_layout layoutReturn the generic Bigarray corresponding to the given zero-dimensional Bigarray.
Return the generic Bigarray corresponding to the given one-dimensional Bigarray.
Return the generic Bigarray corresponding to the given two-dimensional Bigarray.
Return the generic Bigarray corresponding to the given three-dimensional Bigarray.
Return the zero-dimensional Bigarray corresponding to the given generic Bigarray.
Return the one-dimensional Bigarray corresponding to the given generic Bigarray.
Return the two-dimensional Bigarray corresponding to the given generic Bigarray.
Return the three-dimensional Bigarray corresponding to the given generic Bigarray.
val reshape :
('a, 'b, 'c) Bigarray.Genarray.t ->
int array ->
('a, 'b, 'c) Bigarray.Genarray.treshape b [|d1;...;dN|] converts the Bigarray b to a N-dimensional array of dimensions d1...dN. The returned array and the original array b share their data and have the same layout. For instance, assuming that b is a one-dimensional array of dimension 12, reshape b [|3;4|] returns a two-dimensional array b' of dimensions 3 and 4. If b has C layout, the element (x,y) of b' corresponds to the element x * 3 + y of b. If b has Fortran layout, the element (x,y) of b' corresponds to the element x + (y - 1) * 4 of b. The returned Bigarray must have exactly the same number of elements as the original Bigarray b. That is, the product of the dimensions of b must be equal to i1 * ... * iN. Otherwise, Invalid_argument is raised.
Specialized version of Bigarray.reshape for reshaping to zero-dimensional arrays.
Specialized version of Bigarray.reshape for reshaping to one-dimensional arrays.
val reshape_2 :
('a, 'b, 'c) Bigarray.Genarray.t ->
int ->
int ->
('a, 'b, 'c) Bigarray.Array2.tSpecialized version of Bigarray.reshape for reshaping to two-dimensional arrays.
val reshape_3 :
('a, 'b, 'c) Bigarray.Genarray.t ->
int ->
int ->
int ->
('a, 'b, 'c) Bigarray.Array3.tSpecialized version of Bigarray.reshape for reshaping to three-dimensional arrays.
Care must be taken when concurrently accessing bigarrays from multiple domains: accessing a bigarray will never crash a program, but unsynchronized accesses might yield surprising (non-sequentially-consistent) results.
Every bigarray operation that accesses more than one array element is not atomic. This includes slicing, bliting, and filling bigarrays.
For example, consider the following program:
open Bigarray
let size = 100_000_000
let a = Array1.init Int C_layout size (fun _ -> 1)
let update f a () =
for i = 0 to size - 1 do a.{i} <- f a.{i} done
let d1 = Domain.spawn (update (fun x -> x + 1) a)
let d2 = Domain.spawn (update (fun x -> 2 * x + 1) a)
let () = Domain.join d1; Domain.join d2After executing this code, each field of the bigarray a is either 2, 3, 4 or 5. If atomicity is required, then the user must implement their own synchronization (for example, using Mutex.t).
If two domains only access disjoint parts of the bigarray, then the observed behaviour is the equivalent to some sequential interleaving of the operations from the two domains.
A data race is said to occur when two domains access the same bigarray element without synchronization and at least one of the accesses is a write. In the absence of data races, the observed behaviour is equivalent to some sequential interleaving of the operations from different domains.
Whenever possible, data races should be avoided by using synchronization to mediate the accesses to the bigarray elements.
Indeed, in the presence of data races, programs will not crash but the observed behaviour may not be equivalent to any sequential interleaving of operations from different domains.
Bigarrays have a distinct caveat in the presence of data races: concurrent bigarray operations might produce surprising values due to tearing. More precisely, the interleaving of partial writes and reads might create values that would not exist with a sequential execution. For instance, at the end of
let res = Array1.init Complex64 c_layout size (fun _ -> Complex.zero)
let d1 = Domain.spawn (fun () -> Array1.fill res Complex.one)
let d2 = Domain.spawn (fun () -> Array1.fill res Complex.i)
let () = Domain.join d1; Domain.join d2the res bigarray might contain values that are neither Complex.i nor Complex.one (for instance 1 + i).
type ('a, 'b) kind = | Float32 : (float, float32_elt) kind| Float64 : (float, float64_elt) kind| Int8_signed : (int, int8_signed_elt) kind| Int8_unsigned : (int, int8_unsigned_elt) kind| Int16_signed : (int, int16_signed_elt) kind| Int16_unsigned : (int, int16_unsigned_elt) kind| Int32 : (int32, int32_elt) kind| Int64 : (int64, int64_elt) kind| Int : (int, int_elt) kind| Nativeint : (nativeint, nativeint_elt) kind| Complex32 : (Complex.t, complex32_elt) kind| Complex64 : (Complex.t, complex64_elt) kind| Char : (char, int8_unsigned_elt) kind| Float16 : (float, float16_elt) kind| Bfloat16 : (float, bfloat16_elt) kind| Bool : (bool, bool_elt) kind| Int4_signed : (int, int4_signed_elt) kind| Int4_unsigned : (int, int4_unsigned_elt) kind| Float8_e4m3 : (float, float8_e4m3_elt) kind| Float8_e5m2 : (float, float8_e5m2_elt) kind| Complex16 : (Complex.t, complex16_elt) kind| Qint8 : (int, qint8_elt) kind| Quint8 : (int, quint8_elt) kindval create_int4_unsigned_genarray :
'c layout ->
int array ->
('a, 'b, 'c) Bigarray.Genarray.t