calling a .cu function from a .c MEX function

I want to call a .cu file from a .c mex file. I haven’t inserted the kernel calls into the .cu file yet, but I have defined some texture references in a header file which I am including in my .cu file. When I go to compile the code, I get the following error:

make

make: Warning: File `DMAtoFileVarBlock_mex.c’ has modification time 2.1e+02 s in the future

/opt/matlab2009b//bin/mex CFLAGS=’-fPIC -D_GNU_SOURCE -pthread -fexceptions’ COPTIMFLAGS=’-O3 -funroll-loops -msse2’ DMAtoFileVarBlock_mex.c \

    -I/home/derby/a/copeland/cuda/tk2.3/include -L/home/derby/a/copeland/cuda/tk2.3/lib64 -lcufft -lcudart -lcuda -Wl,-rpath,/home/derby/a/copeland/cuda/tk2.3/lib64

In file included from measure_noise.cu:3,

             from DMAtoFileVarBlock_mex.c:11:

measure_noise.h:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token

measure_noise.h:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token

In file included from measure_noise.cu:3,

             from DMAtoFileVarBlock_mex.c:11:

measure_noise.h:14:7: warning: no newline at end of file

DMAtoFileVarBlock_mex.c: In function ‘mexFunction’:

DMAtoFileVarBlock_mex.c:21: warning: passing argument 1 of ‘measure_noise’ from incompatible pointer type

mex: compile of ’ “DMAtoFileVarBlock_mex.c”’ failed.

The .c mex file(DMAtoFileVarBlock.c) is calling measure_noise.cu and it seems like for some reason it doesn’t recognize the cuda functions that I’m calling from within measure_noise.cu. I’m compiling using a Makefile, on Linux. All code along with my Makefile are included below:

Can anyone please tell me what I’m doing wrong? Particularly, why doesn’t it recognize my texture reference definitions. I’ve tried placing calls to other cuda runtime api functions in the measure_noise.cu file and it doesn’t seem to recognize those either…

Thank you in advance,

cudaprog1691

DMAtoFileVarBlock.c

[codebox]

#include “measure_noise.cu”

void measure_noise(char filename[50], int nnoisesamps, int nchans);

void mexFunction(int nlhs, mxArray *plhs,

int nrhs, const mxArray *prhs)

{

unsigned char filename[50] = {"/dev/shm/demonoisefile_chx.bin","/dev/shm/demonoisefile_chxx.bin"};

measure_noise(&filename,10,2);

printf("Done!\n");

}

[/codebox]

measure_noise.cu

[codebox]

#include<stdio.h>

#include<math.h>

#include “measure_noise.h”

void measure_noise(char filename[MAX_FILE_NAME], int nnoisesamps, int nchans){

/* declare local vars */

FILE *fp;

unsigned char *noisesamps;

int chanidx;

size_t noisesamps_offset = nnoisesamps;

/* make room */

noisesamps = (unsigned char*) malloc(nchans*nnoisesamps*sizeof(unsigned char));

/* open noise files and read in samples */

for(chanidx=0;chanidx<nchans;chanidx++){

fp = fopen(filename[chanidx], “r”);

/* check for file opening problems */

    if(fp == NULL){

        printf("Error opening file for chan# :%d\n",chanidx);

        return;}

/* read in file constents */

    fread(&noisesamps[nnoisesamps*chanidx], sizeof(unsigned char), nnoisesamps, fp);

    /* close file and check for errors */      

    if(fclose(fp)==EOF){

        printf("Error closing file for chan# :%d\n",chanidx);

        return;}

}

int k;

for(k=0;k<nnoisesamps*nchans;k++){

    printf("values: %d\n", noisesamps[k]);

}

return;

}[/codebox]

measure_noise.h

[codebox]

#ifndef _CHANCAPHEADER

#define CHANCAPHEADER

#include “cuda.h”

#include “cuda_runtime.h”

#define MAX_FILE_NAME 50

/* Input data texture reference */

texture<unsigned char, 1, cudaReadModeNormalizedFloat>texRef;

texture<float, 1, cudaReadModeElementType>texRefLowpass;

void measure_noise(char filename[MAX_FILE_NAME], int nnoisesamps, int nchans);

#endif[/codebox]

Makefile

[codebox]

Define installation location for CUDA and compilation flags compatible

with the CUDA include files.

CUDAHOME = /home/derby/a/copeland/cuda/tk2.3

INCLUDEDIR = -I$(CUDAHOME)/include

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

CFLAGS = -fPIC -D_GNU_SOURCE -pthread -fexceptions

COPTIMFLAGS = -O3 -funroll-loops -msse2

Define installation location for MATLAB.

export MATLAB = /opt/matlab2009b/

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 = peekreg_mex.mex pokereg_mex.mex getDMAdata.mex DMAtoFile_mex.mex DMAtoFileVarBlock_mex.mex getChanCap.mex

SRCS = getChanCap.cu $(MEXFILES)

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

master: $(SRCS)

touch nvopts.sh; chmod 755 nvopts.sh

$(NVMEX) -f nvopts.sh $(SRCS) $(INCLUDELIB)

    #rm nvopts.sh

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]

If you include a .cu file into a .c file, it is treated as pure C (without CUDA extensions). You have to compile the .cu file separately with nvcc and link against it.

If you include a .cu file into a .c file, it is treated as pure C (without CUDA extensions). You have to compile the .cu file separately with nvcc and link against it.

Thanks for the reply. How do I do that with nvcc? A detailed explanation would be appreciated.

Thanks for the reply. How do I do that with nvcc? A detailed explanation would be appreciated.