Hi,
I need to call some julia function in C, but meet some problem. For example, I want to use hcat
to concatenate two arrays, but segmentation fault occurs. Below is my source code.
#include <stdlib.h>
#include <julia.h>
jl_value_t * checked_eval_string(const char* code)
{
jl_value_t * result = jl_eval_string(code);
if (jl_exception_occurred()) {
// none of these allocate, so a gc-root (JL_GC_PUSH) is not necessary
jl_call2(jl_get_function(jl_base_module, "showerror"),
jl_stderr_obj(),
jl_exception_occurred());
jl_printf(jl_stderr_stream(), "\n");
jl_atexit_hook(1);
exit(1);
}
assert(result && "Missing return value but no exception occurred!");
return result;
}
int main(void) {
jl_init();
// gc protect
jl_value_t * refs = jl_eval_string("refs = IdDict()");
jl_function_t * setindex = jl_get_function(jl_base_module, "setindex!");
double * existing_a = (double *)malloc(sizeof(double) * 4);
existing_a[0] = 1;
existing_a[1] = 2;
existing_a[2] = 3;
existing_a[3] = 4;
jl_value_t * array_type = jl_apply_array_type((jl_value_t *)jl_float64_type, 1);
jl_array_t * a = jl_ptr_to_array_1d(array_type, existing_a, 4, 0);
jl_call3(setindex, refs, (jl_value_t *)a, (jl_value_t *)a);
double * existing_b = (double *)malloc(sizeof(double) * 2);
existing_b[0] = 5;
existing_b[1] = 6;
jl_array_t * b = jl_ptr_to_array_1d(array_type, existing_b, 2, 0);
jl_call3(setindex, refs, (jl_value_t *)b, (jl_value_t *)b);
jl_function_t * print = jl_get_function(jl_base_module, "show");
jl_function_t * hcat = jl_get_function(jl_base_module, "hcat");
jl_call1(print, (jl_value_t *)a);
printf("\n");
jl_call1(print, (jl_value_t *)b);
printf("\n");
jl_value_t * c = jl_call2(hcat, (jl_value_t *)a, (jl_value_t *)b);
jl_call3(setindex, refs, c, c);
jl_call1(print, c);
printf("\n");
jl_atexit_hook(0);
free(existing_a);
existing_a = NULL;
free(existing_b);
existing_b = NULL;
return 0;
}
And the output is:
[1.0, 2.0, 3.0, 4.0]
[5.0, 6.0]
signal (11): Segmentation fault
in expression starting at none:0
sig_match_simple at /buildworker/worker/package_linux64/build/src/typemap.c:128 [inlined]
jl_typemap_entry_assoc_exact at /buildworker/worker/package_linux64/build/src/typemap.c:662
jl_typemap_assoc_exact at /buildworker/worker/package_linux64/build/src/julia_internal.h:1074 [inlined]
jl_typemap_level_assoc_exact at /buildworker/worker/package_linux64/build/src/typemap.c:715
jl_typemap_assoc_exact at /buildworker/worker/package_linux64/build/src/julia_internal.h:1078 [inlined]
jl_lookup_generic_ at /buildworker/worker/package_linux64/build/src/gf.c:2342 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2394
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1690 [inlined]
jl_call3 at /buildworker/worker/package_linux64/build/src/jlapi.c:269
main at /home/fanxp/vscode/julia_test/main2.cpp:50
__libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
_start at ./build/julia_test (unknown line)
Allocations: 2546 (Pool: 2536; Big: 10); GC: 0
Segmentation fault
The expected output is:
[1.0, 2.0, 3.0, 4.0]
[5.0, 6.0]
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
My environment is:
ubuntu 18.04
julia v1.5.3
gcc v7.5.0
The source code I provided uses hcat
function in julia to concatenate 1d array a
and b
. I have already allocated the array and initialized them with some data, so I usejl_ptr_to_array_1d
to wrap them. According to the output, array a
and b
are created successfully, but fail to call hcat
. After exhaustive online searching/reading on google, I find nothing. Anyone can help me?
Thanks!