Re: [julia-users] Segfault when passing large arrays to Julia from C

2014-11-21 Thread David Smith
Thanks for your reply. I think that fixed it, but I don't understand why.

I played around with the code and used what I saw in julia.h to figure out 
that waiting until after the JL_GC_PUSH2 call to assign to the pointers 
worked. But I don't know why it worked.

(Disclaimer: I know almost nothing about gc.)



On Friday, November 21, 2014 12:08:44 PM UTC-7, Jameson wrote:
>
> You are missing a gc root for x when you allocate y. I can't say if that 
> is the only issue, since you seem to be unboxing a jl_array_t* using 
> jl_unbox_voidpointer
> On Fri, Nov 21, 2014 at 1:51 PM David Smith  > wrote:
>
>> The following code works as posted, but when I increase N to 1000, I 
>> get a segfault. I don't see why that would break anything, as the size_t 
>> type should be large enough to handle that.
>>
>> Can someone point me in the right direction?
>>
>> Thanks!
>>
>>
>> #include  
>> #include  
>>
>> int main() 
>> { 
>> size_t N = 100; 
>> jl_init(NULL); 
>> JL_SET_STACK_BASE; 
>> jl_value_t* array_type = jl_apply_array_type( jl_float64_type, 1 ); 
>> jl_array_t* x = jl_alloc_array_1d(array_type, N); 
>> jl_array_t* y = jl_alloc_array_1d(array_type, N); 
>> JL_GC_PUSH2(&x, &y); 
>> double* xData = jl_array_data(x); 
>> double* yData = jl_array_data(y); 
>> for (size_t i=0; i> xData[i] = i; 
>> yData[i] = i; 
>> } 
>> jl_eval_string("myfft(x,y) = fft(complex(x,y))"); 
>> jl_function_t *func = jl_get_function(jl_current_module, "myfft"); 
>> jl_value_t* jlret = jl_call2(func, (jl_value_t*) x, (jl_value_t*)y); 
>> double *ret = jl_unbox_voidpointer(jlret); 
>> for(size_t i=0; i<10; i++) 
>> printf("(%f,%f) = %f + %fi\n", xData[i], yData[i], ret[2*i], 
>> ret[2*i+1]); 
>> JL_GC_POP(); 
>> return 0; 
>> }
>>
>

Re: [julia-users] Segfault when passing large arrays to Julia from C

2014-11-21 Thread Jameson Nash
You are missing a gc root for x when you allocate y. I can't say if that is
the only issue, since you seem to be unboxing a jl_array_t* using
jl_unbox_voidpointer
On Fri, Nov 21, 2014 at 1:51 PM David Smith  wrote:

> The following code works as posted, but when I increase N to 1000, I
> get a segfault. I don't see why that would break anything, as the size_t
> type should be large enough to handle that.
>
> Can someone point me in the right direction?
>
> Thanks!
>
>
> #include 
> #include 
>
> int main()
> {
> size_t N = 100;
> jl_init(NULL);
> JL_SET_STACK_BASE;
> jl_value_t* array_type = jl_apply_array_type( jl_float64_type, 1 );
> jl_array_t* x = jl_alloc_array_1d(array_type, N);
> jl_array_t* y = jl_alloc_array_1d(array_type, N);
> JL_GC_PUSH2(&x, &y);
> double* xData = jl_array_data(x);
> double* yData = jl_array_data(y);
> for (size_t i=0; i xData[i] = i;
> yData[i] = i;
> }
> jl_eval_string("myfft(x,y) = fft(complex(x,y))");
> jl_function_t *func = jl_get_function(jl_current_module, "myfft");
> jl_value_t* jlret = jl_call2(func, (jl_value_t*) x, (jl_value_t*)y);
> double *ret = jl_unbox_voidpointer(jlret);
> for(size_t i=0; i<10; i++)
> printf("(%f,%f) = %f + %fi\n", xData[i], yData[i], ret[2*i],
> ret[2*i+1]);
> JL_GC_POP();
> return 0;
> }
>


[julia-users] Segfault when passing large arrays to Julia from C

2014-11-21 Thread David Smith
The following code works as posted, but when I increase N to 1000, I 
get a segfault. I don't see why that would break anything, as the size_t 
type should be large enough to handle that.

Can someone point me in the right direction?

Thanks!


#include  
#include  

int main() 
{ 
size_t N = 100; 
jl_init(NULL); 
JL_SET_STACK_BASE; 
jl_value_t* array_type = jl_apply_array_type( jl_float64_type, 1 ); 
jl_array_t* x = jl_alloc_array_1d(array_type, N); 
jl_array_t* y = jl_alloc_array_1d(array_type, N); 
JL_GC_PUSH2(&x, &y); 
double* xData = jl_array_data(x); 
double* yData = jl_array_data(y); 
for (size_t i=0; i