CUDA and MEX Compilation

Hello,

I’m new here and have a question about compiling my project.
Originally, it was a package of .c files, where a mexfile for use in Matlab could be compiled with an script.

I now want to convert the corefunctions of the package to CUDA. I wrote a Caller-.cu (Memeryallocation, Grid/Block-Def., Kernelcall) and a Kernel-.cu file. The CUDA-function should now be called from one of the .c files.

“bool cbct_back(bunch of paramter)” is now defined in the Caller-.cu.
In the .c file with the call I defined “extern bool cbct_back(params);” (which is in the cu-file) at the beginning and the funtion should be called in the code of this file.

Now I modified the Makefile from the “MATLAB plug-in for CUDA” available on the Nvidia-Site. In detail,
I’m letting first the .cu files be compiled, that works out right:
(NVMEX) -f nvopts.sh < (INCLUDEDIR) (INCLUDELIB) *.cu

The the c-files are done by the mex-compiler co-using gcc, also ok:
/usr/local/matlab/extern/…/bin/mex CC=gcc -v CFLAGS=’$(CFLAGS)’ -output cbct_mex.mexa64 -I/usr/local/matlab/extern/include -DMATLAB_HOST -DMmex -UUse_plpa -v -DNeed_cbct_mex_gateway *.c

But at the end of this, the Makefile tries to put the files together but utilizes only the former c-files (now o-files) and I get an error:
-> gcc -O -pthread -shared -Wl,–version-script,/usr/local/matlab/extern/lib/glnxa64/mexFunction.map -Wl,–no-undefined -o “cbct_mex.mexa64” <all the .o-files> -Wl,-rpath-link,/usr/local/matlab/bin/glnxa64 -L/usr/local/matlab/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++

[i]cbct,pd1,back,t.o: In function cbct_pd1_back_init': cbct,pd1,back,t.c:(.text+0x5ed): undefined reference to cbct_back’
collect2: ld gab 1 als Ende-Status zurück

mex: link of ' "cbct_mex.mexa64"' failed.[/i]

How can I tell the Makefile to also link the formerly nvcc-compiled cu-files in this step? I’m sure that this is the reason the cbct_back function is not found. Any help would be ppreciated. Sadly, not much is found about compiling c+cu to mex on the web where I could have a look at an example.

Check that the symbol name is not mangled (nm the object file).
If this is the problem, use extern “C”

Thanks for the reply. I already tried using extern “C”, but since the code IS in C, the compiler says it cannot handle the string after the export. The problem is that the make does not includes the cu-files to form the package. Simply, I’m looking for a rule for a Makefile that does this:
first compile *.cu with nvcc or rather nvmex?!
the compile *.c with using the function bool cbct_back from the cu-files
and then putting everything together in a mex-file for use in Matlab.

I haven’t found any example for this. Hope anyone of you has an hint.

I am also trying to do something similar. I have a *.c function that contains mexFunction, which calls a function in a *.cu file I have. It compiles without complaining. Here is my modification to my makefile.

[codebox]mextest.mexa64: mextest.c compute.cu

$(NVMEX) -v -f ../nvopts.sh $^ $(INCLUDEDIR) $(INCLUDELIB)[/codebox]

however, matlab complains and I get this error:

I don’t want to take over your topic, so I suggest you try the -v flag, post the output of your compilation, and see if someone else can find what is going wrong. I know that with regular mex files it is possible to compile object files and link them all together into one mex file, but I do not know how to do it with cuda.

Do "nm mextest.mexa64 |grep -y compute " and see what it the symbol name.
If there is mangling, you need to use the extern C.

I don’t know how to tell if there is mangling, but surely there must have been. In my compute.cu I declare my function compute this way:

extern “C” float* compute()

and matlab runs the mex file properly. Now I can use multiple source files for a single mex file.

Thanks!