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.