From: Odalric-Ambrym Maillard <odalricambrym.maillard@gmail.com>
To: Paolo Donadeo <p.donadeo@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] [Ocaml and C] Passing Arrays and Matrices of floats from Ocaml to C and back
Date: Thu, 4 Mar 2010 18:02:50 +0100 [thread overview]
Message-ID: <406c33611003040902o897243cled850c2e8184334e@mail.gmail.com> (raw)
In-Reply-To: <406c33611003040604y25517b4bmd34f137708ce5b37@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2405 bytes --]
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
>
> 2010/3/4 Paolo Donadeo <p.donadeo@gmail.com>
>
> 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
>>
>
>
>
[-- Attachment #2: Type: text/html, Size: 3562 bytes --]
next prev parent reply other threads:[~2010-03-04 17:02 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-03-04 11:07 Odalric-Ambrym Maillard
2010-03-04 13:08 ` [Caml-list] " Paolo Donadeo
2010-03-04 14:04 ` Odalric-Ambrym Maillard
2010-03-04 17:02 ` Odalric-Ambrym Maillard [this message]
2010-03-04 13:52 ` Christophe TROESTLER
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=406c33611003040902o897243cled850c2e8184334e@mail.gmail.com \
--to=odalricambrym.maillard@gmail.com \
--cc=caml-list@inria.fr \
--cc=odalric.maillard@inria.fr \
--cc=p.donadeo@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox