caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* camlidl and 2D arrays with the [out] attribute
@ 2008-02-15 19:02 Hezekiah M. Carty
  2008-02-15 20:15 ` [Caml-list] " Brian Hurt
  0 siblings, 1 reply; 3+ messages in thread
From: Hezekiah M. Carty @ 2008-02-15 19:02 UTC (permalink / raw)
  To: caml-list

I am attempting to use camlidl to wrap a function with the following C
prototype portion (see [1] for the full prototype):

... void c_plriddata( ... int nptsx, ... int nptsy, [out,
size_is(nptsx,nptsy)] double ** zg ...);

There is a lot more in the full prototype [1], but these are the
important bits for this question.  In this form, the 2D zg array is
not allocated as I would have expected.  Rather than allocating a
rectangular nptsx by nptsy array, the generated C is:

zg = camlidl_malloc(nptsx * sizeof(double *), _ctx);

This is then passed in to the c_plgriddata function as-is, which in
turn causes a segmentation fault as c_plgriddata expects that zg has
already been fully allocated as a two dimensional double array.

If I add the "in" attribute to the zg argument in the annotated C
prototype, then camlidl fully allocates the required space.  I do not
want to have to do this, as the zg array is only needed as an output
from this function, not an input.

If there is no fix/solution to my problem with camlidl then I can
write the binding for this particular function by hand.  I would
prefer to keep the amount of hand-written C bindings I need to
maintain to a minimum.  camlidl has made wrapping almost all of the
rest of this library (PLplot) very straightforward.

Sincerely,
Hez


[1] - Full function prototype:
[mlname(plgriddata)] void
c_plgriddata (
[in, size_is(npts)] double * x, [in, size_is(npts)] double * y,
[in, size_is(npts)] double * z, int npts,
[in, size_is(nptsx)] double * xg, int nptsx,
[in, size_is(nptsy)] double * yg, int nptsy,
[out, size_is(nptsx,nptsy)] double ** zg,
int type, double data);

-- 
Hezekiah M. Carty
Graduate Research Assistant
University of Maryland
Department of Atmospheric and Oceanic Science


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

* Re: [Caml-list] camlidl and 2D arrays with the [out] attribute
  2008-02-15 19:02 camlidl and 2D arrays with the [out] attribute Hezekiah M. Carty
@ 2008-02-15 20:15 ` Brian Hurt
  2008-02-15 20:46   ` Hezekiah M. Carty
  0 siblings, 1 reply; 3+ messages in thread
From: Brian Hurt @ 2008-02-15 20:15 UTC (permalink / raw)
  To: Hezekiah M. Carty; +Cc: caml-list

Hezekiah M. Carty wrote:

>I am attempting to use camlidl to wrap a function with the following C
>prototype portion (see [1] for the full prototype):
>
>... void c_plriddata( ... int nptsx, ... int nptsy, [out,
>size_is(nptsx,nptsy)] double ** zg ...);
>
>There is a lot more in the full prototype [1], but these are the
>important bits for this question.  In this form, the 2D zg array is
>not allocated as I would have expected.  Rather than allocating a
>rectangular nptsx by nptsy array, the generated C is:
>
>zg = camlidl_malloc(nptsx * sizeof(double *), _ctx);
>
>  
>
I think the problem is with your C code.  zg is defined as a pointer to 
a pointer- not a pointer to an array!  And yes, Virginia, there is a 
difference.  If c_plriddata is accessing this pointer as a pointer to an 
array, you probably have a bug in your C code.

Brian


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

* Re: [Caml-list] camlidl and 2D arrays with the [out] attribute
  2008-02-15 20:15 ` [Caml-list] " Brian Hurt
@ 2008-02-15 20:46   ` Hezekiah M. Carty
  0 siblings, 0 replies; 3+ messages in thread
From: Hezekiah M. Carty @ 2008-02-15 20:46 UTC (permalink / raw)
  To: Brian Hurt; +Cc: caml-list

On Fri, Feb 15, 2008 at 3:15 PM, Brian Hurt <bhurt@janestcapital.com> wrote:
> Hezekiah M. Carty wrote:
>
>  >I am attempting to use camlidl to wrap a function with the following C
>  >prototype portion (see [1] for the full prototype):
>  >
>  >... void c_plriddata( ... int nptsx, ... int nptsy, [out,
>  >size_is(nptsx,nptsy)] double ** zg ...);
>  >
>  >There is a lot more in the full prototype [1], but these are the
>  >important bits for this question.  In this form, the 2D zg array is
>  >not allocated as I would have expected.  Rather than allocating a
>  >rectangular nptsx by nptsy array, the generated C is:
>  >
>  >zg = camlidl_malloc(nptsx * sizeof(double *), _ctx);
>  >
>  >
>  >
>  I think the problem is with your C code.  zg is defined as a pointer to
>  a pointer- not a pointer to an array!  And yes, Virginia, there is a
>  difference.  If c_plriddata is accessing this pointer as a pointer to an
>  array, you probably have a bug in your C code.
>
>  Brian
>
>

The C code in question is from the PLplot library[1].  From what I
understand, zg is treated as a two dimensional array in the
c_plgriddata code - zg[i][j] to access an element.  There are a set of
functions plAlloc2dGrid(zg, xdim, ydim) and plFree2dGrid(zg, xdim,
ydim) use in the C API which respectively allocate and free a 2D array
using a (double** zg).  I have written the interface for this
c_plgriddata function by hand using these plAlloc2dGrid and
plFree2dGrid functions, but I would like to be able to use camlidl to
do the work instead.  As I mentioned, camlidl works as I expected for
other functions which take a (double**) argument with an [in]
attribute and treat them as 2D double arrays.  While it may not be
possible, I would ideally like camlidl to do the same memory
allocation for an [out] double** as it does for an [in] double **.

As I mentioned in my post, camlidl currently generates this line to
allocate zg when it has just an [out] attribute:

zg = camlidl_malloc(nptsx * sizeof(double *), _ctx);

Something along the lines of a set of camlidl_malloc(nptsy *
sizeof(double), _ctx) calls for each of the nptsx double* pointers
allocated in the above line should do the trick of allocating the
appropriate memory.  This is what camlidl does for parameters with an
[in] attribute, since it needs to copy the OCaml float array array to
a C 2D array.  I do not know how to tell camlidl to do this for
parameters with an [out] attribute, or if there is a way to tell it to
do this.

The c_plgriddata function does work properly in C applications and
with my hand-written wrapper [2], so I don't think that the problem is
in the PLplot library.

Hez

[1] - http://plplot.sf.net/
[2] - The working hand-written wrapper code: http://ocaml.pastewith.us/33

-- 
Hezekiah M. Carty
Graduate Research Assistant
University of Maryland
Department of Atmospheric and Oceanic Science


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

end of thread, other threads:[~2008-02-15 20:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-15 19:02 camlidl and 2D arrays with the [out] attribute Hezekiah M. Carty
2008-02-15 20:15 ` [Caml-list] " Brian Hurt
2008-02-15 20:46   ` Hezekiah M. Carty

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