0
votes

I have a CUDA program for Matlab, but the mex version is much slower than the Visual Studio version, though the code are identical except the brief mexFunction for in/out arguments. The mex version takes 3 seconds while the pure C takes 0.5 second.

I am using a Quadro K2000M card, CUDA capability 3.0, CUDA Driver 5.5, runtime 5.0, programming with Visual Studio 2010. I followed steps for the mexGPUExample.cu by MATLAB, only changing the setting to -gencode=arch=compute_30,code=\"sm_30,compute_30\" (deleting the lower version flags).

In details,

Pure C code (created in Nsight 3.1 for Visual Sutdio 2010 project, changed code generation to compute_30,sm_30)

int main(int argc, char *argv[]){
clock_t begin, end;
double elapsed_time;

// some codes that prepare parameters from argc and argv

begin = clock();
a_function_that_calls_a_cuda_kernel(parameters);
end = clock();
elapsed_time = (double)(end - begin) / CLOCKS_PER_SEC;
printf("elapsed time: %f seconds\n", elapsed_time);

return 0;
}

Matlab mex code (follow the mexGPUExample.cu by MATLAB, details in http://www.mathworks.se/help/distcomp/create-and-run-mex-files-containing-cuda-code.html, slightly modified the setting to -gencode=arch=compute_30,code=\"sm_30,compute_30\")

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
clock_t begin, end;
double elapsed_time;

// some codes that prepare parameters from prhs

begin = clock();
a_function_that_calls_a_cuda_kernel(parameters);
end = clock();
elapsed_time = (double)(end - begin) / CLOCKS_PER_SEC;
mexPrintf("elapsed time: %f seconds\n", elapsed_time);      
}

The mex version takes 3 seconds while the pure C takes 0.5 second, why? Very appreciated for any hints.

1
I guess you're new to Stack Overflow. We would normally expect questions like this to include some code. If your code is truly identical, it's not surprising it runs slowly on the GPU. You need to take advantage of the GPU by running many threads. - Robert Crovella
What if you call the mex function twice in a row in your matlab code, does it still take 3 seconds each time? - Robert Crovella

1 Answers

1
votes

Your question is unclear. I'm assuming the following terms of comparison:

You have a CUDA code that, when compiled as a standalone program under Visual Studio, is faster than when interfaced by the mexFunction and compiled to be invoked under Matlab.

You should be aware that the first call to the mexFunction is "slow" since the CUDA context is setup, the kernel is processed by the driver, and the code is uploaded to the GPU.

Accordingly, to have a meaningful estimate of the execution time, one should first "warm up" the kernel by calling it once, and then time the execution of subsequent calls. The timing should be calculated as the average time of many calls if the code is very quick.