Hi,
I’m trying to use a shared library in a pmap call from Distributed.jl, but can’t seem to make it work.
MWE:
A file mylib.c with this:
#include <stdio.h>
int say_y(int y)
{
printf("Hello from C: got y = %d.\n", y);
return y*2;
}
and the julia code:
using Distributed, Libdl
addprocs()
run(`gcc -fPIC -shared -o mylib.so mylib.c`)
@everywhere function get_dlhandler()
lib = Libdl.dlopen(joinpath(@__DIR__, "mylib.so"))
symbol = Libdl.dlsym(lib, :say_y)
return (lib, symbol)
end
@everywhere lib, symbol = get_dlhandler()
map(id->fetch(@spawnat id lib), 1:nprocs())
#=
All pointers but the first are undefined:
Ptr{Nothing}(0x000000003ceccb50)
Ptr{Nothing}(0x0000000000000000)
Ptr{Nothing}(0x0000000000000000)
...
=#
I also tried having a different copy of the shared library mylib.so for each process, without any change.
There must be something I don’t grasp about distributed computing, shared libraries and filesystem interaction, but I can’t seem to find an answer. Any idea as to what I’m doing wrong?
PS: using the library with @ccall works, but I want to keep the library reference in order to avoid opening it again and again:
pmap(y->(@ccall "./mylib.so".say_y(y::Cint)::Cint), 1:10)
# returns [2, 4, 6,..., 20]