include module type of struct include Bigarray end
Element kinds
Bigarrays can contain elements of the following kinds:
- IEEE half precision (16 bits) floating-point numbers (
Bigarray.float16_elt
), - IEEE single precision (32 bits) floating-point numbers (
Bigarray.float32_elt
), - IEEE double precision (64 bits) floating-point numbers (
Bigarray.float64_elt
), - IEEE single precision (2 * 32 bits) floating-point complex numbers (
Bigarray.complex32_elt
), - IEEE double precision (2 * 64 bits) floating-point complex numbers (
Bigarray.complex64_elt
), - 8-bit integers (signed or unsigned) (
Bigarray.int8_signed_elt
or Bigarray.int8_unsigned_elt
), - 16-bit integers (signed or unsigned) (
Bigarray.int16_signed_elt
or Bigarray.int16_unsigned_elt
), - OCaml integers (signed, 31 bits on 32-bit architectures, 63 bits on 64-bit architectures) (
Bigarray.int_elt
), - 32-bit signed integers (
Bigarray.int32_elt
), - 64-bit signed integers (
Bigarray.int64_elt
), - platform-native signed integers (32 bits on 32-bit architectures, 64 bits on 64-bit architectures) (
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).
Array layouts
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.
Supported layouts
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.
Generic arrays (of arbitrarily many dimensions)
Zero-dimensional arrays
One-dimensional arrays
Two-dimensional arrays
Three-dimensional arrays
Coercions between generic Bigarrays and fixed-dimension Bigarrays
Return 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.
Re-shaping Bigarrays
reshape 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.
Specialized version of Bigarray.reshape
for reshaping to two-dimensional arrays.
Specialized version of Bigarray.reshape
for reshaping to three-dimensional arrays.
Bigarrays and concurrency safety
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.
Atomicity
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 d2
After 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
).
Data races
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.
Tearing
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 d2
the res
bigarray might contain values that are neither Complex.i
nor Complex.one
(for instance 1 + i
).