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

[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: 2010-03-04 (17:02) From: Odalric-Ambrym Maillard 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
Bigarray.c_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){
CAMLparam2(bpsi,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);
print_matrix(rows,cols,mat_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 ?

Thanks,
Odalric

2010/3/4 Odalric-Ambrym Maillard <odalricambrym.maillard@gmail.com>

> 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
>
>
> Did you consider using the bigarray library?
>>
>> http://caml.inria.fr/pub/docs/manual-ocaml/manual043.html
>>
>>
>> --
>> Paolo
>>
>> _______________________________________________
>> Caml-list mailing list. Subscription management:
>> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
>> Archives: http://caml.inria.fr
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>
>
>
>

```