Problem with cuda and mexfile

HI *,
i wrote a makefile for cuda+matlab and create a -mexglx file…
But how to use this file in matlab with the file main.m?
Maybe I have not explained it well, I have the following files:
arraySum.c (contains mex function)
arraySum.cu (contains cuda kernel)
arraySum.h
main.m (main matlab)
makeOperator_Plus.m (class defined in matlab)

This is my makefle:

# 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 -lcufft -Wl,-rpath,$(CUDAHOME)/lib
INCLUDELIB  = -L$(CUDAHOME)/lib -lcudart -lcufft -lcudadevrt -Wl,-rpath,$(CUDAHOME)/lib
CFLAGS      = -fPIC -D_GNU_SOURCE -pthread -fexceptions
COPTIMFLAGS = -O3 -funroll-loops -msse2

# Define installation location for MATLAB.
export MATLAB = /usr/local/MATLAB/R2012a
#export MATLAB = /Applications/MATLAB_R2012a
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

# matlab-cuda install location
matcuda = /usr/local/matcuda

NVMEX = $(matcuda)/nvmex -f $(matcuda)/nvopts.sh

# 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 = test.mexglx

#all: $(MEXFILES:.mexglx=$(MEXEXT))

all: 	mx.mexglx

mx.mexglx:	mx.o
	$(MEX) -o mx.mexglx mx.o $(INCLUDELIB)

mx.o:	arraySum.o
	nvcc -arch=sm_35 -Xcompiler -fPIC -o mx.o -dlink arraySum.o -lcudadevrt

arraySum.o:	arraySum.c
	$(MEX) -c -o arraySum.o arraySum.c 

clean:
	rm -fv $(MEXFILES:.mexglx=$(MEXEXT))

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

#.cu.mexglx:
	#$(NVMEX) $< $(INCLUDEDIR) $(INCLUDELIB)

This makefile creates a. mexglx file…
my question is: how to use this. mexglx in matlba? how do I use with the file main.m?
Maybe I have not explained it well so I attach all the files …
Thanks for the help
Davide

zip.tar.gz (160 KB)

I did something similar lately with Octave and found that I had to add the -Shared compiler switch to nvcc for the linkage stage to work correctly.

You call your *.mexglx function the same way as you would call any other ordinary MATLAB function. So for example, if your compiled function name is foo.mexglx (assuming 1 input, and 1 output), you’d call it as:

out1 = foo(in1);

Depending on the (lack of) error checking on the MEX code, you want to make sure you pass the correct input and output arguments to your MEX function, otherwise you will segfault and crash matlab.