Linux CUDA-based Shared Library Crashes MATLAB with Segfault on Kernel Call
14 views (last 30 days)
Show older comments
Hey all,
I'm running into an issue with a CUDA-based shared library I've written to solve a system of PDEs that I load through loadlibrary. I've written this using pretty generic CUDA / no outside libraries, etc. When compiled to the shared library, executing it from MATLAB results in a crash (due to a segfault) once the execution arrives at the kernel calls (there are 5 distinct kernels, each called about twice). I've commented out the kernels one-by-one and they all lead to a segfault, leading me to believe there is some issue with the kernel calling mechanism, maybe?
I believe the kernels to be well functioning - I've written a c++ caller of the .so, and it works fine (passing cuda-memcheck as well). This code also works in Windows just fine, exactly as is (called from MATLAB). Therefore, I believe this to be a specific MATLAB issue or possibly compile flags issue. The odd thing is that writing a quick, trivial kernel appears to work within MATLAB (same flags as below) - the kernel does execute correctly.
So, I understand that you don't have my code...so I'm not asking for code debugging. My questions are moreso related to shared library compiling requirements by MATLAB. I use the following flags to compile through nvcc:
-std=c++14 -shared -x cu -cudart static -O2 -gencode=arch=compute_50,code=\"sm_50,compute_50\" -m64 -Xcompiler "-fPIC -Wno-narrowing" -w -Wno-deprecated-gpu-targets
and the following to link:
-shared -w
Do you see any issues with that? I mark the functions as extern "C" when compiling with g++ and have a clause for when MATLAB compiles the thunk library to simply use extern (due to using gcc).
#ifdef __linux__
#ifdef __cplusplus
#define EXTC extern "C"
#else
#define EXTC extern
#endif
...
Any issues there? I don't think there is - as mentioned, other funciton calls work fine.
I'm at a bit of a loss as to how to move forward here. Does anyone have any insight?
Thanks.
7 Comments
Joss Knight
on 16 Dec 2020
Try launching MATLAB with -softwareopengl and see whether there's some sort of graphics issue here.
Answers (0)
See Also
Categories
Find more on Get Started with GPU Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!