Support for Fortran-layout bigarrays #523
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for Fortran-layout bigarrays.
Background: existing bigarray support in ctypes
Bigarrays are already closely integrated into ctypes. For example, it is possible to
All these features are available via a small number of operations in the interface:
bigarray
,genarray
,array1
,array2
,array3
construct bigarray type descriptionsbigarray_start
andbigarray_of_ptr
convert between bigarrays and pointersbigarray_of_array
andarray_of_bigarray
convert between bigarrays and ctypes arraysThese operations construct values that can be used with the generic ctypes functions for operating on C types and values.
Background: bigarray type descriptions
OCaml bigarrays are divided into several classes with distinct types:
Array1.t
,Array2.t
,Array3.t
andGenarray.t
. In ctypes these different types of array are all described by a single type,bigarray_class
, which is indexed by information about the type it describes. For example, here is the type of the description ofArray2.t
:The various fields of the object --
element
,ba_repr
,dims
, etc. -- describe the characteristics of theArray2.t
type. For example,dims: int * int
indicates that the dimensions are represented as a pair of integers (sinceArray2.t
has rank 2), andcarray:
a carray carrayindicates that an
Array2.t` value can be converted to a ctypes array of arrays.These fields are used to constrain the types of the generic bigarray operations in ctypes. For example, here is the type of
array_of_bigarray
:Since
array_of_bigarray
is parameterised by abigarray_class
value it can operate on any kind of bigarray. The return type of the function,'c
is constrained to match the value of thecarray
field in the bigarray description, and so it varies with the type of the first argument. For example,array_of_bigarray
can convert a rank-1 bigarray to a ctypes array:or convert a rank-2 bigarray to a ctypes array of arrays:
Interface changes
OCaml provides support for both C-layout (0-indexed, row-major) and Fortran-layout (1-indexed, column-major) bigarrays. However, at present all of the ctypes bigarray operations only support C layout.
This PR generalizes the interface to add support for Fortran layout. The changes fall into two classes. First, the types of most of the bigarray operations are generalized to operate on either C-layout or Fortran-layout bigarrays. This involves the addition of an extra field,
layout
, to thebigarray_class
index. For example, here is the new type ofarray2
:The operations
genarray
,array1
,array2
,array3
andbigarray_start
are generalized in this way. Second, two functions,bigarray
andbigarray_of_ptr
, which could not be generalized without breaking backwards compatibility now have new Fortran-layout counterparts. These functions could not be generalized because they construct bigarray type representations or bigarray values, which requires supplying them with information about layout. It would have been possible to add an additionallayout
parameter to the functions, but to avoid breaking existing code this PR instead adds new functionsfortran_bigarray
andfortran_bigarray_of_ptr
.Finally, the operations
bigarray_of_array
andarray_of_bigarray
have not been generalized, since they convert between bigarrays and ctypes arrays, which are defined as having C layout. If necessary, it is possible to convert between Fortran-layout bigarrays and ctypes arrays in two steps, by using one of theBigarray
change_layout
functions, or by a combination ofbigarray_start
andfortran_bigarray_of_ptr
.@nilsbecker, @berke: comments on the interface would be very welcome here, if you have time to take a look.
This closes #509.