caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] C -> CAML linking: loosing values in arrays??
@ 2003-01-11 19:07 Daniel Andor
  2003-01-11 23:20 ` Artem Prisyznuk
  2003-01-15 15:10 ` Damien Doligez
  0 siblings, 2 replies; 4+ messages in thread
From: Daniel Andor @ 2003-01-11 19:07 UTC (permalink / raw)
  To: Caml List

Hi All,

I'm trying to call a numerical routine I wrote in caml from c.  I'm using the 
-output-obj option of ocamlopt, and have stub functions in c that convert the 
parameters.  Since it's for numerics, I want to pass arrays of doubles.  So I 
use make_float_array from below to convert a 4dimensional doube array into a 
value ready to be passed to caml.

value make_float(double *d) 
{ 
  /* value of d intentionally ignored while we debug... */
  return copy_double(0.0);
}

#define NUM_DIMS 4
value make_float_array(double *ds)
{
  int i;
  double* p[NUM_DIMS+1];
  p[NUM_DIMS]=NULL;
  for (i=0; i<NUM_DIMS; i++)
    p[i] = &(ds[i]);
  return alloc_array(make_float,p);
}

The caml code gets called correctly, and from within it I can see that the 
length or the arrays and the content are bogus (they come out length 2, not 
4, with the first elements looking like this: 62.6141, not 0.0).

If I pass a non-array double using copy_double(), it works and I get the 
correct number on the other side.

Any ideas as to where I've gone wrong?

Thanks,
Daniel.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] C -> CAML linking: loosing values in arrays??
  2003-01-11 19:07 [Caml-list] C -> CAML linking: loosing values in arrays?? Daniel Andor
@ 2003-01-11 23:20 ` Artem Prisyznuk
  2003-01-12  1:02   ` Daniel Andor
  2003-01-15 15:10 ` Damien Doligez
  1 sibling, 1 reply; 4+ messages in thread
From: Artem Prisyznuk @ 2003-01-11 23:20 UTC (permalink / raw)
  To: Daniel.Andor; +Cc: Caml List

11.01.03 22:07:51, Daniel Andor <da209@cam.ac.uk> wrote:

>If I pass a non-array double using copy_double(), it works and I get the 
>correct number on the other side.
>
>Any ideas as to where I've gone wrong?


>From Caml manual:

....
18.3.3
Arrays

Arrays of integers and pointers are represented like tuples, that is, as pointers to blocks tagged 0. They are accessed with the Field macro for reading and the modify 
function for writing.

Arrays of floating-point numbers (type float array) have a _special, unboxed_, more efficient representation. These arrays are represented by pointers to blocks with 
tag Double_array_tag. They should be accessed with the Double_field and Store_double_field macros.
....


>
>Thanks,
>Daniel.
>-------------------
>To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
>Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] C -> CAML linking: loosing values in arrays??
  2003-01-11 23:20 ` Artem Prisyznuk
@ 2003-01-12  1:02   ` Daniel Andor
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Andor @ 2003-01-12  1:02 UTC (permalink / raw)
  To: Artem Prisyznuk; +Cc: Caml List

Thanks to you and Basile, who also replied.

I think I now understand that I need to use the more low-level alloc() 
function with the appropriate tag, since there doesn't seem to be a version 
of alloc_array() suitable for doubles.

At the moment my main worry is that I'm allocating new arrays to pass to the 
function faster than the gc can clean them up... I need to find out about 
hanging on to allocated values on the C side of things.

Thanks,
Daniel.

On Saturday 11 January 2003 11:20 pm, Artem Prisyznuk wrote:
> 11.01.03 22:07:51, Daniel Andor <da209@cam.ac.uk> wrote:
> >If I pass a non-array double using copy_double(), it works and I get the
> >correct number on the other side.
> >
> >Any ideas as to where I've gone wrong?
>
> From Caml manual:
>
> ....
> 18.3.3
> Arrays
>
> Arrays of integers and pointers are represented like tuples, that is, as
> pointers to blocks tagged 0. They are accessed with the Field macro for
> reading and the modify function for writing.
>
> Arrays of floating-point numbers (type float array) have a _special,
> unboxed_, more efficient representation. These arrays are represented by
> pointers to blocks with tag Double_array_tag. They should be accessed with
> the Double_field and Store_double_field macros. ....
>
> >Thanks,
> >Daniel.
> >-------------------
> >To unsubscribe, mail caml-list-request@inria.fr Archives:
> > http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ:
> > http://caml.inria.fr/FAQ/ Beginner's list:
> > http://groups.yahoo.com/group/ocaml_beginners
>
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives:
> http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ:
> http://caml.inria.fr/FAQ/ Beginner's list:
> http://groups.yahoo.com/group/ocaml_beginners

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] C -> CAML linking: loosing values in arrays??
  2003-01-11 19:07 [Caml-list] C -> CAML linking: loosing values in arrays?? Daniel Andor
  2003-01-11 23:20 ` Artem Prisyznuk
@ 2003-01-15 15:10 ` Damien Doligez
  1 sibling, 0 replies; 4+ messages in thread
From: Damien Doligez @ 2003-01-15 15:10 UTC (permalink / raw)
  To: Daniel.Andor; +Cc: Caml List

On Saturday, January 11, 2003, at 08:07 PM, Daniel Andor wrote:

> parameters.  Since it's for numerics, I want to pass arrays of doubles.

>   return alloc_array(make_float,p);

You cannot use alloc_array to return a value of type [float array].
This is because arrays of floating-point numbers are not represented
like the other array types in O'Caml: they are allocated in-line,
without the usual pointer indirection.

You will have to allocate the array with [alloc], then fill in the
elements with [Store_double_field]:

#define NUM_DIMS 4
value make_float_array(double *ds)
{
   int i;
   value res;

   res = alloc (NUM_DIMS*Double_wosize, Double_array_tag);
   for (i=0; i<NUM_DIMS; i++){
     Store_double_field (res, i, ds[i]);
   }
   return res;
}


-- Damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2003-01-15 15:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-11 19:07 [Caml-list] C -> CAML linking: loosing values in arrays?? Daniel Andor
2003-01-11 23:20 ` Artem Prisyznuk
2003-01-12  1:02   ` Daniel Andor
2003-01-15 15:10 ` Damien Doligez

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).