Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Ocaml and C] Passing Arrays and Matrices of floats from Ocaml to C and back
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Odalric-Ambrym Maillard <odalricambrym.maillard@g...>
Subject: Re: [Caml-list] [Ocaml and C] Passing Arrays and Matrices of floats from Ocaml to C and back
Using Bigarrays, most of the problem seems to be solved.
However, I still have an important problem:

Now I define in the Ocaml code a matrix (Bigarray.Array2)  "bpsi" and  a
vector (Bigarray.Array1) "by".
Both are defined using the kind Bigarray.float32 and the layout
If I print them in Ocaml, they contains the good values.

Now I call my stub function from Ocaml, written in C: (definef with *
external* ...etc)
*let beta = solve_olsr bpsi by*

In C, I have:

*value solve_olsr(value bpsi, value by){
int rows = Bigarray_val(bpsi)->dim[0];
int cols = Bigarray_val(bpsi)->dim[1];
double * vec_data = Data_bigarray_val(by);
double ** mat_data = Data_bigarray_val(bpsi);
print_vector(rows, vec_data);

There are two problems :
 - First, I need to have a matrix defined with type double **, due to the
function I call next, which takes as input a matrix of type double ** and an
array of type double *. I guess that *Data_bigarray_val only* outputs a flat
array corresponding to an Array of any dimension. If this is the case, this
is not what I want (it seems more difficult to allocate a such a contiguous
block in memory when it is big). So is there another function that does the
job, i.e. maps a Bigarray.Array2 to double ** with the right allocation ?
Now, when I print the matrix, the programs crashes (Segmentation fault),
which seems coherent with the previous interpretation.
 - When I print the vector *vec_data*, it has the right number of elements,
but unfortunately all values are 0. Thus I guess there is some problem with
the Bigarray.float32 thing. Is it that ?


2010/3/4 Odalric-Ambrym Maillard <>

> Thanks, I was not aware of this relation between Bigarrays and C.
> I will try to use this and tell you of it works as I want.
> Odalric
> 2010/3/4 Paolo Donadeo <>
> Did you consider using the bigarray library?
>> --
>> Paolo
>> _______________________________________________
>> Caml-list mailing list. Subscription management:
>> Archives:
>> Beginner's list:
>> Bug reports: