comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Can't pass data with CALL_EXTERNAL to C function
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Can't pass data with CALL_EXTERNAL to C function [message #60832] Fri, 20 June 2008 10:30
Dan[1] is currently offline  Dan[1]
Messages: 7
Registered: June 2008
Junior Member
Hi everyone.

I finally got it working. Thanks for all your suggestions. Sorry for
the truncated code, I didn't realize that happened until just
recently. I guess that's what I get for copying out of nano. It's
unfortunate they don't have emacs or vi on these computers. Like I
said, thanks again for the help.
Re: Can't pass data with CALL_EXTERNAL to C function [message #60835 is a reply to message #60832] Fri, 20 June 2008 07:56 Go to previous message
Trae is currently offline  Trae
Messages: 23
Registered: May 2007
Junior Member
Dan,

In your call to call_external I don't see that you set the VALUE
keyword. This is what tells call_external which parameters you are
passing via value and which via reference. This may be the cause of
your problems. Whenever I am forced to use call_external, I set this
keyword manually,

From the help manual:

VALUE
A byte array, with as many elements as there are optional parameters,
indicating the method of parameter passing. Arrays are always passed
by reference. If parameter Pi is a scalar, it is passed by reference
if VALUE[i] is 0; and by value if it is non-zero. There are some
restrictions on data types that should be considered when using this
keyword, as discussed below.

As Brian noted, using call_external can be a beast. We worked on a
project for a week, before I concluded that it was much faster (and
easier) to rewrite the offending C code in IDL. Many a frustration
beer was drunk that day. :)

Good Luck!
-Trae




On Jun 19, 3:32 pm, Dan <daniel.dex...@colorado.edu> wrote:
> Hi Brian,
>
> Thanks for the tutorial, but I couldn't figure out how to make it work
> for my C code. In particular, none of the pointers work, not just the
> ones for the multi-D arrays.
>
> Could you (or someone) please clarify how you got pass by reference to
> work.
>
> Thanks again,
> -Dan
Re: Can't pass data with CALL_EXTERNAL to C function [message #60842 is a reply to message #60835] Fri, 20 June 2008 04:18 Go to previous message
Allan Whiteford is currently offline  Allan Whiteford
Messages: 117
Registered: June 2006
Senior Member
Dan wrote:
> Hi everyone,
>
> I am having problems sending data to my C function through
> CALL_EXTERNAL. Specifically, I send the data, but it seems as if all
> the variables passed to the C function are suddenly 0 (maybe
> uninitialized). I have posted my test code below. Please take a
> look, any help would be greatly appreciated. It is also probably
> worth mentioning that when I return something from the function (like
> return 2;) or something like that, the IDL value will be 2 (i.e.
> values are returned correctly, so I know the function is at least
> running to completion).
>
> ----- CODE -----

<snip>

Dan,

It's hard to see/test exactly what you're doing as some of your lines
have been truncated (unfortunately the important line including
call_external).

However, the following is a short example of something which goes along
the lines of what you're doing and works ok for me:

passref.pro
-----------

pro passref
a=fltarr(100,100)
b=fltarr(200,200)

a[17,23]=3.14
b[52,79]=2.72

junk=call_external('passref.so','passref',a,b)

print,a[12,54]
end

passref.c
---------

#include <stdio.h>
#include "idl_export.h"

void do_work(float *a,float *b)
{
a[12+54*100]=1.62;
printf("%f\n",a[17+23*100]);
printf("%f\n",b[52+79*200]);
}

IDL_VPTR passref(int argc, IDL_VPTR argv[])
{
do_work((float *) argv[0],(float *) argv[1]);
}

Compilation
-----------

gcc -I/usr/local/rsi/idl/external/include \
-shared passref.c -o passref.so

I hope this helps. If you can repost your original example without the
lines being cut off then probably someone can point out the exact part
you'd need to change to get it working. In particular, the keywords
you're giving to call_external are probably important.

Thanks,

Allan
Re: Can't pass data with CALL_EXTERNAL to C function [message #60846 is a reply to message #60842] Thu, 19 June 2008 14:27 Go to previous message
Brian Larsen is currently offline  Brian Larsen
Messages: 270
Registered: June 2006
Senior Member
Dan,

in your idl distribution look for the "external/call_external/C"
folder. Those are the examples I used to figure out how to do the
call_external and get the passing working. For me the path is /
Applications/itt/idl64/external/call_external/C

Everything I did was on my macbook pro
idl 6.4.1 OSX 10.5.3


Cheers,

Brian

------------------------------------------------------------ --------------
Brian Larsen
Boston University
Center for Space Physics
http://people.bu.edu/balarsen/Home/IDL
Re: Can't pass data with CALL_EXTERNAL to C function [message #60847 is a reply to message #60846] Thu, 19 June 2008 12:32 Go to previous message
Dan[1] is currently offline  Dan[1]
Messages: 7
Registered: June 2008
Junior Member
Hi Brian,

Thanks for the tutorial, but I couldn't figure out how to make it work
for my C code. In particular, none of the pointers work, not just the
ones for the multi-D arrays.

Could you (or someone) please clarify how you got pass by reference to
work.

Thanks again,
-Dan
Re: Can't pass data with CALL_EXTERNAL to C function [message #60849 is a reply to message #60847] Thu, 19 June 2008 11:46 Go to previous message
Brian Larsen is currently offline  Brian Larsen
Messages: 270
Registered: June 2006
Senior Member
I have had these same issues and they are maddening. The issue is
with pass by reference, pass by value.

The key ends up being in the VALUE keyword to CALL_EXTERNAL. I am
trying to get ready for a meeting next week so I can't type more but I
will point you to a working example.

On this page I have a bit of explanation on how I did this as well as
the c and idl codes.
http://people.bu.edu/balarsen/Home/IDL/Entries/2008/4/15_Gen erating_Sobol_Sequence_with_GSL_in_IDL.html

What I did there was write a C wrapper for the C routine I wanted to
run to handle the array passing. Since the C routine needs to be
called N times and IDL-C calls are slow I reduced that number of calls
to 1 IDL-C call using the wrapper. Then there is an IDL code that
calls the wrapper and gives the answer. Seems to work well.

Let me know what you learn as this always drives me crazy and I have
to figure it our each time.


Cheers,

Brian

------------------------------------------------------------ --------------
Brian Larsen
Boston University
Center for Space Physics
http://people.bu.edu/balarsen/Home/IDL
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Implementation of G Statistic in IDL
Next Topic: Multidimensional arrays and CALL_EXTERNAL

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 13:42:30 PDT 2025

Total time taken to generate the page: 0.00566 seconds