Matlab nvmex compiling and problems

Hi,

I’m a newbie to cuda and trying to learn. I’m on Macbook pro with 8600Gs Nvidia and with CUDA 2.2 working (the examples are working including simpleCUBLAS). So I want to write a function in C and compile it to use it in Matlab as a .mexmaci. So I found some interesting guesses here ( http://forums.nvidia.com/index.php?showtop…amp;hl=mex+code ) and the unix file here ( http://developer.nvidia.com/object/matlab_cuda.html ), modified the makefile as follows:

[codebox]# Define installation location for CUDA and compilation flags compatible

with the CUDA include files.

CUDAHOME = /usr/local/cuda

INCLUDEDIR = -I$(CUDAHOME)/include

INCLUDELIB = -L$(CUDAHOME)/lib -lcuda -lcudart -lcufft -Wl,-rpath,$(CUDAHOME)/lib

CFLAGS = -Xcompiler -fPIC -D_GNU_SOURCE -pthread -fexceptions #-Xcompiler -pthread -fexceptions

COPTIMFLAGS = -O3 #-funroll-loops -msse2

Define installation location for MATLAB.

export MATLAB = /Applications/MATLAB_R2007b/

#export MATLAB = /Applications/MATLAB_R2007b

MEX = $(MATLAB)/bin/mex

MEXEXT = .$(shell $(MATLAB)/bin/mexext)

nvmex is a modified mex script that knows how to handle CUDA .cu files.

NVMEX = ./nvmex

List the mex files to be built. The .mex extension will be replaced with the

appropriate extension for this installation of MATLAB, e.g. .mexglx or

.mexa64.

MEXFILES = sgemm_cu.mex

       #fft2_cuda_sp_dp.mex \

       #ifft2_cuda.mex      \

       #Szeta.mex

all: $(MEXFILES:.mex=$(MEXEXT))

clean:

rm -f $(MEXFILES:.mex=$(MEXEXT))

.SUFFIXES: .cu .cu_o .mexglx .mexa64 .mexmaci

.c.mexglx:

$(MEX) CFLAGS='$(CFLAGS)' COPTIMFLAGS='$(COPTIMFLAGS)' $< \

    $(INCLUDEDIR) $(INCLUDELIB)

.cu.mexglx:

$(NVMEX) -f nvopts.sh $< $(INCLUDEDIR) $(INCLUDELIB)

.c.mexa64:

$(MEX) CFLAGS='$(CFLAGS)' COPTIMFLAGS='$(COPTIMFLAGS)' $< \

    $(INCLUDEDIR) $(INCLUDELIB)

.cu.mexa64:

$(NVMEX) -f nvopts.sh $< $(INCLUDEDIR) $(INCLUDELIB)

.c.mexmaci:

$(MEX) CFLAGS='$(CFLAGS)' COPTIMFLAGS='$(COPTIMFLAGS)' $< \

    $(INCLUDEDIR) $(INCLUDELIB)

.cu.mexmaci:

$(NVMEX) -f nvopts.sh $< $(INCLUDEDIR) $(INCLUDELIB)

[/codebox]

then saved the code in this thread (http://forums.nvidia.com/index.php?showtopic=69566&hl=mex+code) as sgemm_cu.c and tried compiling in matlab 7.5.0 but I get the error:

[codebox]>> unix ‘Make’

/Applications/MATLAB_R2007b//bin/mex CFLAGS=‘-Xcompiler -fPIC -D_GNU_SOURCE -pthread -fexceptions ’ COPTIMFLAGS=’-O3 ’ sgemm_cu.c \

    -I/usr/local/cuda/include -L/usr/local/cuda/lib -lcuda -lcudart -lcufft -Wl,-rpath,/usr/local/cuda/lib

nvcc fatal : Unknown option ‘pthread’

mex: compile of ‘sgemm_cu.c’ failed.

Make: *** [sgemm_cu.mexmaci] Error 1

[/codebox]

So what did I do, I tried commenting out (yes I don’t know exactly what I’m doing) the part -pthread -fexceptions in the makefile and the file got compiled but then I tried using the compiled function and Matlab just crashes with no advice. So I decided to check the report to see what it was referring to and there’s some error regarding _cublasInit .

I checked if I had put cuda folder in the Set Path of matlab and it’s ok, so can somebody please help me out giving me a clue on where the error is?

thanks in advance

Francesco

You are missing the link to CUBLAS.

INCLUDELIB = -L$(CUDAHOME)/lib -lcuda -lcudart -lcufft -lcublas -Wl,-rpath,$(CUDAHOME)/lib

should fix your problem

Hi mfatica,

thanks for your help! I tried changing the INCLUDELIB in the makefile as you suggested and checked again if the cuda folder is under /usr/local/cuda and it’s there but still I receive the error.

You are absolutely right, it’s like I’m missing the link to CUBLAS and actually to the CUDA folder, even though I checked the path and it’s correct. What am I doing wrong?

shall I comment out the “-pthread -fexceptions” from CFLAGS?

thanks for your help!

this is how the makefile looks like now:

[codebox]# Define installation location for CUDA and compilation flags compatible

with the CUDA include files.

CUDAHOME = /Users/heon/Desktop/PALM_GPU/cuda

INCLUDEDIR = -I$(CUDAHOME)/include

INCLUDELIB = -L$(CUDAHOME)/lib -lcuda -lcudart -lcufft -lcublas -Wl,-rpath,$(CUDAHOME)/lib

CFLAGS = -Xcompiler -fPIC -D_GNU_SOURCE -pthread -fexceptions

COPTIMFLAGS = -O3 -funroll-loops

Define installation location for MATLAB.

export MATLAB = /Applications/MATLAB_R2007b

#export MATLAB = /Applications/MATLAB_R2007b

MEX = $(MATLAB)/bin/mex

MEXEXT = .$(shell $(MATLAB)/bin/mexext)

nvmex is a modified mex script that knows how to handle CUDA .cu files.

NVMEX = ./nvmex

List the mex files to be built. The .mex extension will be replaced with the

appropriate extension for this installation of MATLAB, e.g. .mexglx or

.mexa64.

MEXFILES = fft2_cuda.mex \

       fft2_cuda_sp_dp.mex \

       ifft2_cuda.mex      \

       Szeta.mex           \

       sgemm_cu.mex        

all: $(MEXFILES:.mex=$(MEXEXT))

clean:

rm -f $(MEXFILES:.mex=$(MEXEXT))

.SUFFIXES: .cu .cu_o .mexglx .mexa64 .mexmaci

.c.mexglx:

$(MEX) CFLAGS='$(CFLAGS)' COPTIMFLAGS='$(COPTIMFLAGS)' $< \

    $(INCLUDEDIR) $(INCLUDELIB)

.cu.mexglx:

$(NVMEX) -f nvopts.sh $< $(INCLUDEDIR) $(INCLUDELIB)

.c.mexa64:

$(MEX) CFLAGS='$(CFLAGS)' COPTIMFLAGS='$(COPTIMFLAGS)' $< \

    $(INCLUDEDIR) $(INCLUDELIB)

.cu.mexa64:

$(NVMEX) -f nvopts.sh $< $(INCLUDEDIR) $(INCLUDELIB)

.c.mexmaci:

$(MEX) CFLAGS='$(CFLAGS)' COPTIMFLAGS='$(COPTIMFLAGS)' $< \

    $(INCLUDEDIR) $(INCLUDELIB)

.cu.mexmaci:

$(NVMEX) -f nvopts.sh $< $(INCLUDEDIR) $(INCLUDELIB)

[/codebox]

and this is the error I still get… funny thing is that I still get the same error even if I put a random folder instead of CUDAHOME…

[codebox]>> unix ‘Make’

/Applications/MATLAB_R2007b//bin/mex CFLAGS=‘-Xcompiler -fPIC -D_GNU_SOURCE -pthread -fexceptions ’ COPTIMFLAGS=’-O3 -funroll-loops -msse2’ sgemm_cu.c \

    -I/usr/local/cuda/include -L/usr/local/cuda/lib -lcuda -lcudart -lcufft -lcublas -Wl,-rpath,/usr/local/cuda/lib

nvcc fatal : Unknown option ‘pthread’

mex: compile of ‘sgemm_cu.c’ failed.

Make: *** [sgemm_cu.mexmaci] Error 1

ans =

2

[/codebox]

Ok now the example from here http://forums.nvidia.com/index.php?showtop…amp;hl=mex+code that includes CUBLAS is running, I changed the makefile as follow and I am using Mac OS X 10.5.7 with 2.4 GHz Intel Core 2 Duo on GeForce 8600M GT and Matlab 7.5.0.338 R2007b . :mellow:

The makefile I used is the following:

[codebox]# Define installation location for CUDA and compilation flags compatible

with the CUDA include files.

CUDAHOME = /Users/heon/Desktop/PALM_GPU/cuda

INCLUDEDIR = -I$(CUDAHOME)/include

INCLUDELIB = -L$(CUDAHOME)/lib -lcuda -lcudart -lcufft -lcublas -Wl,-rpath,$(CUDAHOME)/lib

CFLAGS = -Xcompiler -fPIC -D_GNU_SOURCE #-pthread -fexceptions

COPTIMFLAGS = -O3 #-funroll-loops

Define installation location for MATLAB.

export MATLAB = /Applications/MATLAB_R2007b

#export MATLAB = /Applications/MATLAB_R2007b

MEX = $(MATLAB)/bin/mex

MEXEXT = .$(shell $(MATLAB)/bin/mexext)

nvmex is a modified mex script that knows how to handle CUDA .cu files.

NVMEX = ./nvmex

List the mex files to be built. The .mex extension will be replaced with the

appropriate extension for this installation of MATLAB, e.g. .mexglx or

.mexa64.

MEXFILES = fft2_cuda.mex \

       fft2_cuda_sp_dp.mex \

       ifft2_cuda.mex      \

       Szeta.mex           \

       sgemm_cu.mex        

all: $(MEXFILES:.mex=$(MEXEXT))

clean:

rm -f $(MEXFILES:.mex=$(MEXEXT))

.SUFFIXES: .cu .cu_o .mexglx .mexa64 .mexmaci

.c.mexglx:

$(MEX) CFLAGS='$(CFLAGS)' COPTIMFLAGS='$(COPTIMFLAGS)' $< \

    $(INCLUDEDIR) $(INCLUDELIB)

.cu.mexglx:

$(NVMEX) -f nvopts.sh $< $(INCLUDEDIR) $(INCLUDELIB)

.c.mexa64:

$(MEX) CFLAGS='$(CFLAGS)' COPTIMFLAGS='$(COPTIMFLAGS)' $< \

    $(INCLUDEDIR) $(INCLUDELIB)

.cu.mexa64:

$(NVMEX) -f nvopts.sh $< $(INCLUDEDIR) $(INCLUDELIB)

.c.mexmaci:

$(MEX) CFLAGS='$(CFLAGS)' COPTIMFLAGS='$(COPTIMFLAGS)' $< \

    $(INCLUDEDIR) $(INCLUDELIB)

.cu.mexmaci:

$(NVMEX) -f nvopts.sh $< $(INCLUDEDIR) $(INCLUDELIB)

[/codebox]

basically commenting #-pthread -fexceptions in CFLAGS and #-funroll-loops in COPTIMFLAGS. But still I am wondering why I have to comment these out in order for it to run and also what exactly they do. :mellow:

Anyway I will really soon have other questions…