The bigarray library
The bigarray library implements large, multi-dimensional, numerical
arrays. These arrays are called "big arrays" to distinguish them
from the standard arrays described in
Module Array.
The main differences between "big arrays" and standard arrays
are as follows:
-
Big arrays are not limited in size, unlike standard arrays
(real array are limited to 2097151 elements on a 32-bit platform,
other array types to 4194303 elements).
- Big arrays are multi-dimensional. Any number of dimensions
between 1 and 16 is supported. In contrast, standard arrays are
mono-dimensional and require encoding multi-dimensional arrays as
arrays of arrays.
- Big arrays can only contain integers and real
numbers, while standard arrays can contain arbitrary data types.
However, big arrays provide more space-efficient storage of integer
and real elements, in particular because they support
"small" types such as single-precision floats and 8 and 16-bit
integers, in addition to the standard types of double-precision
floats and 32 and 64-bit integers.
- The memory layout of big arrays is entirely compatible with that
of arrays in C and Fortran, allowing large arrays to be passed back
and forth between nML code and C / Fortran code with no data copying
at all.
- Big arrays support interesting high-level operations that normal
arrays do not provide efficiently, such as extracting sub-arrays and
"slicing" a multi-dimensional array along certain dimensions, all
without any copying.
Programs that use the bigarray library must be linked as follows:
nmlc other options bigarray.cma other files
nmlo other options bigarray.cmxa other files
For interactive use of the bigarray library, do:
ntopgen -o mytop bigarray.cma
./mytop
or (if dynamic linking of C libraries is supported on your platform),
start nml and type #load "bigarray.cma";;.
Module Bigarray
structure Bigarray = struct ... end
Large, multi-dimensional, numerical arrays.
This module implements multi-dimensional arrays of integers and
floating-point numbers, thereafter referred to as "big arrays."
The implementation allows efficient sharing of large numerical
arrays between nML code and C or Fortran numerical libraries.
Concerning the naming conventions, users of this module are encouraged
to do
open Bigarray
in their source, then refer to array types and
operations via short dot notation, e.g.
Array1.t
or
Array2.sub
.
Big arrays support all the polymorphic operations:
Big arrays can contain elements of the following kinds:
- IEEE single precision (32 bits) floating-point numbers
(
Bigarray.float32_elt
),
- IEEE double precision (64 bits) floating-point numbers
(
Bigarray.float64_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
),
- Caml integers (signed, 31 bits on 32-bit architectures,
63 bits on 64-bit architectures) (
Bigarray.int_elt
),
- 32-bit signed integer (
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 abstract types defined below.
type float32_elt
type float64_elt
type int8_signed_elt
type int8_unsigned_elt
type int16_signed_elt
type int16_unsigned_elt
type int_elt
type int32_elt
type int64_elt
type nativeint_elt
type ('a, 'b
) kind
To each element kind is associated a Caml type, which is
the type of Caml values that can be stored in the big array
or read back from it. This type is not necessarily the same
as the type of the array elements proper: for instance,
a big array whose elements are of kind float32_elt
contains
32-bit single precision floats, but reading or writing one of
its elements from Caml uses the Caml type float
, which is
64-bit double precision floats.
The abstract type ('a, 'b) kind
captures this association
of a Caml type 'a
for values read or written in the big array,
and of an element kind 'b
which represents the actual contents
of the big array. The following predefined values of type
kind
list all possible associations of Caml types with
element kinds:
val float32 : (float, float32_elt) kind
val float64 : (float, float64_elt) kind
val int8_signed : (int, int8_signed_elt) kind
val int8_unsigned : (int, int8_unsigned_elt) kind
val int16_signed : (int, int16_signed_elt) kind
val int16_unsigned : (int, int16_unsigned_elt) kind
val int : (int, int_elt) kind
val int32 : (int32, int32_elt) kind
val int64 : (int64, int64_elt) kind
val nativeint : (nativeint, nativeint_elt) kind
val char : (char, int8_unsigned_elt) kind
As shown by the types of the values above,
big arrays of kind float32_elt
and float64_elt
are
accessed using the Caml type float
. Big arrays of
integer kinds are accessed using the smallest Caml integer
type large enough to represent the array elements:
int
for 8- and 16-bit integer bigarrays, as well as Caml-integer
bigarrays; int32
for 32-bit integer bigarrays; int64
for 64-bit integer bigarrays; and nativeint
for
platform-native integer bigarrays. Finally, big arrays of
kind int8_unsigned_elt
can also be accessed as arrays of
characters instead of arrays of small integers, by using
the kind value char
instead of int8_unsigned
.
type c_layout
type fortran_layout
To facilitate interoperability with existing C and Fortran code,
this library supports two different memory layouts for big arrays,
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
abstract types
Bigarray.c_layout
and
fortran_layout
respectively.
type 'a
layout
The abstract values c_layout
and fortran_layout
represent
the two supported layouts at the level of values.
val c_layout : c_layout layout
val fortran_layout : fortran_layout layout
structure Genarray = struct ... end
Generic arrays (of arbitrarily many dimensions)
structure Array1 = struct ... end
One-dimensional arrays.
structure Array2 = struct ... end
Two-dimensional arrays.
structure Array3 = struct ... end
Three-dimensional arrays.
Coercions between generic big arrays and fixed-dimension big arrays
|
|
val genarray_of_array1 : ('a, 'b, 'c) Array1.t -> ('a, 'b, 'c) Genarray.t
Return the generic big array corresponding to the given one-dimensional big array.
val genarray_of_array2 : ('a, 'b, 'c) Array2.t -> ('a, 'b, 'c) Genarray.t
Return the generic big array corresponding to the given two-dimensional big array.
val genarray_of_array3 : ('a, 'b, 'c) Array3.t -> ('a, 'b, 'c) Genarray.t
Return the generic big array corresponding to the given three-dimensional big array.
val array1_of_genarray : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array1.t
Return the one-dimensional big array corresponding to the given
generic big array. Raise Invalid_arg
if the generic big array
does not have exactly one dimension.
val array2_of_genarray : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array2.t
Return the two-dimensional big array corresponding to the given
generic big array. Raise Invalid_arg
if the generic big array
does not have exactly two dimensions.
val array3_of_genarray : ('a, 'b, 'c) Genarray.t -> ('a, 'b, 'c) Array3.t
Return the three-dimensional big array corresponding to the given
generic big array. Raise Invalid_arg
if the generic big array
does not have exactly three dimensions.
val reshape : ('a, 'b, 'c) Genarray.t ->
int array -> ('a, 'b, 'c) Genarray.t
reshape b [|d1;...;dN|]
converts the big array 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 big array must have exactly the same number of
elements as the original big array b
. That is, the product
of the dimensions of b
must be equal to i1 * ... * iN
.
Otherwise, Invalid_arg
is raised.
val reshape_1 : ('a, 'b, 'c) Genarray.t -> int -> ('a, 'b, 'c) Array1.t
Specialized version of
Bigarray.reshape
for reshaping to one-dimensional arrays.
val reshape_2 : ('a, 'b, 'c) Genarray.t ->
int -> int -> ('a, 'b, 'c) Array2.t
Specialized version of
Bigarray.reshape
for reshaping to two-dimensional arrays.
val reshape_3 : ('a, 'b, 'c) Genarray.t ->
int -> int -> int -> ('a, 'b, 'c) Array3.t
Specialized version of
Bigarray.reshape
for reshaping to three-dimensional arrays.