The bigarray library implements large, multi-dimensional, numerical arrays. These arrays are called “big arrays” to distinguish them from the standard OCaml arrays described in Module Array. The main differences between “big arrays” and standard OCaml arrays are as follows:
Programs that use the bigarray library must be linked as follows:
ocamlc other options bigarray.cma other files ocamlopt other options bigarray.cmxa other files
For interactive use of the bigarray library, do:
ocamlmktop -o mytop bigarray.cma ./mytop
or (if dynamic linking of C libraries is supported on your platform), start ocaml and type #load "bigarray.cma";;.
C stub code that interface C or Fortran code with OCaml code, as described in chapter 19, can exploit big arrays as follows.
The include file <caml/bigarray.h> must be included in the C stub file. It declares the functions, constants and macros discussed below.
If v is a OCaml value representing a big array, the expression Data_bigarray_val Caml_ba_data_val(v) returns a pointer to the data part of the array. This pointer is of type void * and can be cast to the appropriate C type for the array (e.g. double [], char [][10], etc).
Various characteristics of the OCaml big array can be consulted from C as follows:
C expression | Returns |
Caml_ba_array_val(v)->num_dims | number of dimensions |
Caml_ba_array_val(v)->dim[i] | i-th dimension |
Caml_ba_array_val(v)->flags & BIGARRAY_KIND_MASK | kind of array elements |
The kind of array elements is one of the following constants:
Constant | Element kind |
CAML_BA_FLOAT32 | 32-bit single-precision floats |
CAML_BA_FLOAT64 | 64-bit double-precision floats |
CAML_BA_SINT8 | 8-bit signed integers |
CAML_BA_UINT8 | 8-bit unsigned integers |
CAML_BA_SINT16 | 16-bit signed integers |
CAML_BA_UINT16 | 16-bit unsigned integers |
CAML_BA_INT32 | 32-bit signed integers |
CAML_BA_INT64 | 64-bit signed integers |
CAML_BA_CAML_INT | 31- or 63-bit signed integers |
CAML_BA_NATIVE_INT | 32- or 64-bit (platform-native) integers |
The following example shows the passing of a two-dimensional big array to a C function and a Fortran function.
extern void my_c_function(double * data, int dimx, int dimy); extern void my_fortran_function_(double * data, int * dimx, int * dimy); value caml_stub(value bigarray) { int dimx = Caml_ba_array_val(bigarray)->dim[0]; int dimy = Caml_ba_array_val(bigarray)->dim[1]; /* C passes scalar parameters by value */ my_c_function(Caml_ba_data_val(bigarray), dimx, dimy); /* Fortran passes all parameters by reference */ my_fortran_function_(Caml_ba_data_val(bigarray), &dimx, &dimy); return Val_unit; }
A pointer p to an already-allocated C or Fortran array can be wrapped and returned to OCaml as a big array using the alloc_bigarray caml_ba_alloc or alloc_bigarray_dims caml_ba_alloc_dims functions.
Return an OCaml big array wrapping the data pointed to by p. kind is the kind of array elements (one of the CAML_BA_ kind constants above). layout is CAML_BA_C_LAYOUT for an array with C layout and CAML_BA_FORTRAN_LAYOUT for an array with Fortran layout. numdims is the number of dimensions in the array. dims is an array of numdims long integers, giving the sizes of the array in each dimension.
Same as caml_ba_alloc, but the sizes of the array in each dimension are listed as extra arguments in the function call, rather than being passed as an array.
The following example illustrates how statically-allocated C and Fortran arrays can be made available to OCaml.
extern long my_c_array[100][200]; extern float my_fortran_array_[300][400]; value caml_get_c_array(value unit) { long dims[2]; dims[0] = 100; dims[1] = 200; return caml_ba_alloc(CAML_BA_NATIVE_INT | CAML_BA_C_LAYOUT, 2, my_c_array, dims); } value caml_get_fortran_array(value unit) { return caml_ba_alloc_dims(CAML_BA_FLOAT32 | CAML_BA_FORTRAN_LAYOUT, 2, my_fortran_array_, 300L, 400L); }