caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Ocaml and C] Passing Arrays and Matrices of floats from Ocaml to C  and back
@ 2010-03-04 11:07 Odalric-Ambrym Maillard
  2010-03-04 13:08 ` [Caml-list] " Paolo Donadeo
  2010-03-04 13:52 ` Christophe TROESTLER
  0 siblings, 2 replies; 5+ messages in thread
From: Odalric-Ambrym Maillard @ 2010-03-04 11:07 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1154 bytes --]

<caml-list@inria.fr>,


Hello, I have the following problem : I wan to use a Least-squares
Regression solver written in C in a program written in OCaml.

I have something like this in the Ocaml program,
*
let beta = solve_olsr psi_matrix y_train in ...*

where psi_matrix is built using  Array.make_matrix and y_train by Array.make

The dimensions are KxN for psi_matrix and N for y_train, and the output beta
should be a vector of length K (built with Array.make etc).

I have a C function with the following signature :
*
void  olsr(int rows, int columns,
         double **matrix_of_data,
         double *vector_of_data,
         double ** vector_of_coefficients)*

Thus, I need to create a stub function, that will lok like this

*CAMLValue solve_olsr(value ...,value...) {
 CAMLparam...

 CAMLReturn ...
}*

My problem is the following : how do  proceed since the parameters I need to
transmt are vectors or matrices of float (for Caml, i.e. double for C), and
not just integers or floats ? I really do not know how to transfer arrays
and matrix of floats from Caml to C, and back.


It would be great to have an answer.

Thank you,
Odalric-Ambrym

[-- Attachment #2: Type: text/html, Size: 1334 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] [Ocaml and C] Passing Arrays and Matrices of floats  from Ocaml to C and back
  2010-03-04 11:07 [Ocaml and C] Passing Arrays and Matrices of floats from Ocaml to C and back Odalric-Ambrym Maillard
@ 2010-03-04 13:08 ` Paolo Donadeo
  2010-03-04 14:04   ` Odalric-Ambrym Maillard
  2010-03-04 13:52 ` Christophe TROESTLER
  1 sibling, 1 reply; 5+ messages in thread
From: Paolo Donadeo @ 2010-03-04 13:08 UTC (permalink / raw)
  To: caml-list

Did you consider using the bigarray library?

http://caml.inria.fr/pub/docs/manual-ocaml/manual043.html


-- 
Paolo


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] [Ocaml and C] Passing Arrays and Matrices of floats from Ocaml to C and back
  2010-03-04 11:07 [Ocaml and C] Passing Arrays and Matrices of floats from Ocaml to C and back Odalric-Ambrym Maillard
  2010-03-04 13:08 ` [Caml-list] " Paolo Donadeo
@ 2010-03-04 13:52 ` Christophe TROESTLER
  1 sibling, 0 replies; 5+ messages in thread
From: Christophe TROESTLER @ 2010-03-04 13:52 UTC (permalink / raw)
  To: odalric.maillard, odalricambrym.maillard; +Cc: caml-list

On Thu, 4 Mar 2010 12:07:57 +0100, Odalric-Ambrym Maillard wrote:
> 
> Hello, I have the following problem : I wan to use a Least-squares
> Regression solver written in C in a program written in OCaml.

Aren't the Lacaml/LAPACK least square enough ?
http://www.ocaml.info/home/ocaml_sources.html#lacaml

If you want to bind your very own solver, the recommended route is to
use bigarrays :
http://caml.inria.fr/pub/docs/manual-ocaml/manual043.html#toc143

My 0.02€,
C.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] [Ocaml and C] Passing Arrays and Matrices of floats  from Ocaml to C and back
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Odalric-Ambrym Maillard @ 2010-03-04 14:04 UTC (permalink / raw)
  To: Paolo Donadeo; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 954 bytes --]

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
>



-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Odalric-Ambrym Maillard, Phd student
INRIA Lille - Nord Europe,
- Sequential Learning Project
Institut de Mathématiques de Toulouse,
- Equipe de Statistique et Probabilités
Odalric.maillard@inria.fr,
+33 6.43.71.70.48 (cell)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[-- Attachment #2: Type: text/html, Size: 1699 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] [Ocaml and C] Passing Arrays and Matrices of floats  from Ocaml to C and back
  2010-03-04 14:04   ` Odalric-Ambrym Maillard
@ 2010-03-04 17:02     ` Odalric-Ambrym Maillard
  0 siblings, 0 replies; 5+ messages in thread
From: Odalric-Ambrym Maillard @ 2010-03-04 17:02 UTC (permalink / raw)
  To: Paolo Donadeo; +Cc: caml-list

[-- 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 --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-03-04 17:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-04 11:07 [Ocaml and C] Passing Arrays and Matrices of floats from Ocaml to C and back 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
2010-03-04 13:52 ` Christophe TROESTLER

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).