I am having an C++ library environment where I am supposed to call Julia kernel (example SAXPY) with threading.
A minimum work example to illustrate the issue is given below.
The Julia main() function calls ‘call_directly’ function written in ‘C’ to callback ‘saxpy_openmp’ function written in Julia. It works good. However, when the Julia main() functions calls ‘call_on_thread’ function written in ‘C’ which creates a pthread and callback ‘saxpy_openmp’ function written in Julia, it hangs during execution.
c_mwe.c:
#include <julia.h>
#include <pthread.h>
typedef void (*julia_callback)();
void call_directly(julia_callback callback) {
printf("Calling Julia directly\n");
callback();
}
void *thread_function(void* callback) {
printf("Calling Julia from thread\n");
((julia_callback)callback)();
return NULL;
}
void call_on_thread(julia_callback callback) {
jl_init();
printf("Creating thread\n");
pthread_t thread;
pthread_create(&thread, NULL, thread_function, callback);
pthread_join(thread, NULL);
}
julia_mwe.jl:
export callback
using Base.Threads
function saxpy_openmp(Z, A, X, Y)
n = length(X)
@assert length(Y) == n "Vectors X and Y must have the same length"
@assert length(Z) == n "Vectors X and Z must have the same length"
Threads.@threads for i in 1:n
Z[i] = A * X[i] + Y[i]
end
end
function callback()::Cvoid
println(Core.stdout, "Calling the GC")
GC.gc()
size = 10
X = ones(size)
Y = ones(size)
Z = ones(size)
saxpy_openmp(Z, 2.0, X, Y)
println(Core.stdout, "Z: $Z")
println(Core.stdout, "GC call done")
end
function main()
callback_ptr = @cfunction(callback, Cvoid, ())
ccall((:call_directly, "./wip.so"), Cvoid, (Ptr{Cvoid},), callback_ptr)
println()
gc_state = @ccall(jl_gc_safe_enter()::Int8)
ccall((:call_on_thread, "./wip.so"), Cvoid, (Ptr{Cvoid},), callback_ptr)
@ccall(jl_gc_safe_leave(gc_state::Int8)::Cvoid)
println("Done")
end
main()
Instructions to compile:
Makefile:
build:
gcc -g -O0 -fPIC -shared -o wip.so c_mwe.c -I$(JULIA)/include/julia -L$(JULIA)/lib -ljulia -lpthread
run:
julia julia_mwe.jl
Please help in resolving this issue.