On Sat, Jan 9, 2016 at 10:59 AM, Yichao Yu <[email protected]> wrote:
> On Sat, Jan 9, 2016 at 4:45 AM, Shamika <[email protected]> wrote:
>> I'm using jl_call for more than one input/output argument. The code is
>>
>> int main(int argc, char *argv[])
>> {
>>     /* required: setup the julia context */
>>
>>     jl_init_with_image("/usr/lib/x86_64-linux-gnu/julia","sys.so");
>>
>>     /* run julia commands */
>>     jl_value_t** args;
>>     jl_value_t** ret;
>>     jl_array_t* yDataTemp;
>>     double* yData     = NULL;
>>     int32_t nargs=2;
>>
>>     jl_value_t* arg1 = (jl_box_float64(2));
>>     jl_value_t* arg2 = (jl_box_float64(3));

FYI, arg1 is not rooted here.

>>     args[0]=arg1;
>>     args[1]=arg2;

And arg2 also needs to be rooted.

>>
>>     jl_function_t *func  = jl_get_function(jl_base_module, "rand");
>>
>>     ret = (jl_value_t**) jl_call(func,args,nargs);
>> //    yDataTemp = (jl_array_t*) ret[0];
>> //    yData=(double*)jl_array_data (yDataTemp);
>>
>>     /* strongly recommended: notify julia that the
>>          program is about to terminate. this allows
>>          julia time to cleanup pending write requests
>>          and run all finalizers
>>     */
>>     jl_atexit_hook(0);
>>     return 0;
>> }
>>
>> I'm trying to call rand(2,3). Once this works I want to use this code for
>> more than one output argument. At the moment, I get the error-
>>
>> fatal: error thrown and no exception handler available.
>> ReadOnlyMemoryError()
>> rec_backtrace at /usr/lib/x86_64-linux-gnu/julia/libjulia.so (unknown line)
>> jl_throw at /usr/lib/x86_64-linux-gnu/julia/libjulia.so (unknown line)
>> unknown function (ip: 0x7fc415c0ff19)
>> unknown function (ip: 0x7fc41542fd40)
>> unknown function (ip: 0x400992)
>> __libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
>> unknown function (ip: 0x400829)
>> unknown function (ip: (nil))
>>
>> 1. What am I doing wrong?
>> 2. If there is more than one output argument, is the following code correct?
>> I'm using ret[index value] to access the output.
>
> There's always only one return value ("output argument"), when you
> write `return (a, b)`, it returns a tuple.
> A tuple is just a anonymous struct and you can access like any other types.
>
> If you know it is a Tuple{Array,Float64}, just cast the pointer to a
> `struct {jl_array_t *ary; double data};` and access the field.
>
> If you are not so sure about the return types, check the type and/or
> use `jl_get_field` etc.
>
>> yDataTemp = (jl_array_t*) ret[0];
>> yData=(double*)jl_array_data (yDataTemp);
>>
>> Shamika
>>
>> On Thursday, January 7, 2016 at 8:06:24 PM UTC+5:30, Isaiah wrote:
>>>>
>>>> Can the output, jl_value_t* ret, point to more than one output argument?
>>>
>>>
>>> Only as a tuple or array,
>>>
>>> On Thu, Jan 7, 2016 at 12:26 AM, Shamika <[email protected]> wrote:
>>>>
>>>> Can the output, jl_value_t* ret, point to more than one output argument?
>>>>
>>>> On Wednesday, January 6, 2016 at 7:45:33 PM UTC+5:30, Stefan Karpinski
>>>> wrote:
>>>>>
>>>>> On Wed, Jan 6, 2016 at 12:15 AM, Shamika <[email protected]> wrote:
>>>>>>
>>>>>>
>>>>>> I'm using Julia in c++ code. I have a few doubts regarding the jl_call
>>>>>> function.The code is
>>>>>>
>>>>>> jl_array_t *ret = (jl_array_t*)jl_call(func,args,nargs);
>>>>>>
>>>>>> 1. Can args contain both scalar/array values?
>>>>>
>>>>>
>>>>> Yes, both scalar and array values are represented on the C side as
>>>>> jl_value_t*. Args will be a pointer to an array of jl_value_t* values – 
>>>>> i.e.
>>>>> an array of pointers (of length nargs). Some of those pointers can point 
>>>>> to
>>>>> scalar values on the heap, some can point to array values. These can be
>>>>> distinguished by their type tags, which there are various macros to 
>>>>> access.
>>>>>
>>>>>>
>>>>>> Does it use zero based or one based indexing?
>>>>>
>>>>>
>>>>> Zero-based: everything on the C side is zero-based.
>>>>>
>>>>>>
>>>>>> 2. Is there any data type that can hold both scalar/array output that
>>>>>> is returned by jl_call? Right now, I have to define the output as 
>>>>>> jl_value_t
>>>>>> or jl_array_t. Is there something more generic?
>>>>>
>>>>>
>>>>> As I explained above, jl_value_t* is strictly more generic than
>>>>> jl_array_t*. You can think of these as corresponding to Any and Array in
>>>>> Julia – Array is a subtype of Any.
>>>
>>>
>>

Reply via email to