From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 54AA8BC57 for ; Thu, 4 Mar 2010 18:02:53 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AuEBAFd2j0vRVdrhkGdsb2JhbACPLAeBbYoXCBUBAQEBCQkMBxMDH6hSgV6FLC2ISQEBAwWEdwSLSg X-IronPort-AV: E=Sophos;i="4.49,582,1262559600"; d="scan'208";a="54035597" Received: from mail-bw0-f225.google.com ([209.85.218.225]) by mail1-smtp-roc.national.inria.fr with ESMTP; 04 Mar 2010 18:02:53 +0100 Received: by bwz25 with SMTP id 25so793886bwz.17 for ; Thu, 04 Mar 2010 09:02:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:reply-to:in-reply-to :references:date:message-id:subject:from:to:cc:content-type; bh=JiLCQj033Pj2lKAyWmDGX54BX9uRyXMrclJYTZ3hvNk=; b=C1pCOGqIJQRqPeeOp2w0L0I/bzuF4Z/X4/yUN+tQ9PnJK2c2I40tMKWv7EfA8jFP6S Yf4R+ldaQUnCsquUH/DSXr412zVjlgfiZjLz2iaMGX894Uq11FzVyIpqeb/dpumlnj4s Yi6uT+8fIUnowBCccnRzFJjcwdU8jEbNr0nB0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; b=vfxc36pXT4RJTfavGO+MPvngHcbepNc9E1OAnw9FQsRMEx3gpg/olElhirY0hXClmS Uk8S/O3IuwfzRgnqkX0ZFpr8zFmEg2mGhWxjOtGVLeNNZsfz0kcM2YdBwTve9Hhf4x7z g9aoNUXb1zSn6OINBNJI05HZHxbbiYAp6XaYQ= MIME-Version: 1.0 Received: by 10.239.164.80 with SMTP id s16mr1136389hbd.54.1267722172354; Thu, 04 Mar 2010 09:02:52 -0800 (PST) Reply-To: odalric.maillard@inria.fr In-Reply-To: <406c33611003040604y25517b4bmd34f137708ce5b37@mail.gmail.com> References: <406c33611003040307v2195e006qfe273bf85c7f2c95@mail.gmail.com> <4b5157c31003040508u98111adkc485f89c319f129c@mail.gmail.com> <406c33611003040604y25517b4bmd34f137708ce5b37@mail.gmail.com> Date: Thu, 4 Mar 2010 18:02:50 +0100 Message-ID: <406c33611003040902o897243cled850c2e8184334e@mail.gmail.com> Subject: Re: [Caml-list] [Ocaml and C] Passing Arrays and Matrices of floats from Ocaml to C and back From: Odalric-Ambrym Maillard To: Paolo Donadeo Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=001485f1ec2ac1d40e0480fc91a5 X-Spam: no; 0.00; ocaml:01 arrays:01 matrices:01 ocaml:01 bigarrays:01 bigarray:01 bigarray:01 stub:01 camlparam:01 val:01 val:01 bigarrays:01 beginner's:01 bug:01 stub:01 --001485f1ec2ac1d40e0480fc91a5 Content-Type: text/plain; charset=ISO-8859-1 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 > 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? >> >> 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 >> > > > --001485f1ec2ac1d40e0480fc91a5 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Using Bigarrays, most of the problem seems to be solved.
However, I stil= l have an important problem:

Now I define in the Ocaml code a matrix= (Bigarray.Array2)=A0 "bpsi" and=A0 a vector (Bigarray.Array1) &q= uot;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 exte= rnal ...etc)
let beta =3D solve_olsr bpsi by

In C, I have:

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


There are two problems : =
=A0- 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 outpu= ts 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 t= hat does the job, i.e. maps a Bigarray.Array2 to double ** with the right a= llocation ?
Now, when I print the matrix, the programs crashes (Segmentation fault), wh= ich seems coherent with the previous interpretation.
=A0- When I print t= he vector vec_data, it has the right number of elements, but unfortu= nately all values are 0. Thus I guess there is some problem with the Bigarr= ay.float32 thing. Is it that ?

Thanks,
Odalric


2010/3/4 Odalr= ic-Ambrym Maillard <odalricambrym.maillard@gmail.com>
Thanks, I was not aware of this relation between Bigarrays and C.
I wil= l try to use this and tell you of it works as I want.

Odalric
2010/3/4 Paolo Donadeo <p.donadeo@gmail.com<= /a>>

Did you consider = using the bigarray library?

http://caml.inria.fr/pub/docs/manual-ocaml/manual043.html<= br>

--
Paolo

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.in= ria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


--001485f1ec2ac1d40e0480fc91a5--