How to generate mex file for .cu file? (I want to create a mex file for my CUDA code so that I can run it in MATLAB).
Thanks----- Anik
How to generate mex file for .cu file? (I want to create a mex file for my CUDA code so that I can run it in MATLAB).
Thanks----- Anik
Here’s a quick tutorial from one of our forum members:
[url]http://www.orangeowlsolutions.com/archives/498[/url]
For newer MATLAB gpuArray syntax here’s an article direct from the vendor:
[url]http://www.mathworks.com/help/distcomp/run-mex-functions-containing-cuda-code.html[/url]
Simple example:
[url]http://www.computersdontsee.net/index.php/post/2012/08/29/A-simple-CUDA-kernel-mex-file-(part-1)[/url]
Google is your friend ;)
Thanks a lot for your reply. In the tutorial I saw that it is saying to choose from Visual Studio C++ 2010
File → New Project; then to Choose MFC DLL as well as the Name of the project and the Location; Choose OK; Choose Next; Choose Finish (do not change any selection); Solution Explorer → Right click on the project’s name → Build Customizations → CUDA 5.0 → Ok;
But the problem is in my visual studio c++ 2010, MFC DLL option is not showing and in Build customization CUDA 5.0 or any other version option is also not showing. I searched for the solution in the internet. But didnot get satisfactory answer. Can anyone help in this case please?
Thanks-----anik
You could do it the other way around… choose a CUDA project and change the project type to DLL after the fact. If the CUDA project is not showing you’ll need to uninstall and reinstall the CUDA Toolkit for it to show up as an option…
thanks again for your reply. I tried that, i think it is working fine but again difficulty occured at the 10th step which is
where to find this .def file and the TestMex?
can anyone please help me to create mex file from my cuda code? I m really in need of this.
Hello anik, here is a solution to your problem:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
// The following lines must be located BEFORE '#include <mex.h>'
#ifdef _MSC_VER
#define DLL_EXPORT_SYM __declspec(dllexport)
#else
#define DLL_EXPORT_SYM
#endif
#include <mex.h>
DLL_EXPORT_SYM
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
// Computations goes in here
mexPrintf("Hello matlab world\n");
}
Also, make sure that your build is correctly setup for 32-bit/64-bit matlab.
Type ‘computer’ in matlab to find out if you are running 64 or 32-bit.
>> MyProject
Hello matlab world
Good luck!
Thanks nutti for your reply. :) :)
But I have two queries.
Do I have to create this regular CUDA project using visual studio C++ 2010 version?
And the trick which u used to make sure the mexfunction() is exported correctly, will I have to write this trick in my CUDA code itself or in any other separate file?
Please reply. Thank you
Also if possible please tell me which CUDA version you are referring?
Thanks!
Error 1 error C1083: Cannot open include file: ‘mex.h’: No such file or directory H:\My project\My project\kernel.cu 10 1 My project
Error 2 error MSB3721: The command ““G:\bin\nvcc.exe” -gencode=arch=compute_20,code="sm_20,compute_20" --use-local-env --cl-version 2013 -ccbin “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin” -I"C:\Program Files\MATLAB\R2011a\extern\lib\win64\microsoft\libmex.lib” -IG:\include -IG:\include -G --keep-dir Debug -maxrregcount=0 --machine 32 --compile -cudart static -g -DWIN32 -D_DEBUG -D_CONSOLE -D_WINDLL -D_MBCS -Xcompiler “/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd " -o Debug\kernel.cu.obj “H:\My project\My project\kernel.cu”” exited with code 2. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\BuildCustomizations\CUDA 6.5.targets 593 9 My project
Hi friends…Thanks for your kind replies.
@nutti—> I tried the way u have mentioned . It is working but above two errors are coming.
@vacaloca----> thanks… but I am finding above two errors. What to do now?? pls suggest. I am using CUDA 6.5 and visual studio 12.0
I am very close to my work. Pls help guys… Thanks
Is there any way to create mex file for CUDA code in ubuntu?
Other than using visual studio can we create mex file using ubuntu?
Please reply.Thanks!!
Is there no one to help me with the above mentioned error? Please guys help
Looks like you did something wrong setting up the include path for the mex.h file. Re-do the solution nutti gave you, I think you did step 5 wrongly. Check the other steps as well!
Thanks alot guys for your suggestions. I have sorted out the problems. But only following issues I could not solve. It says “cblas.h”, “matrix_funcs.h” and “mex.h” source files cannot be opened. I have searched in the Program Files\MATLAB\R2014a\extern\include directory. mex.h file is available but I cant get it why it cannot be opned. Other two files are not available in the directory. Even I have not used other two header files in my code as well but still dont know why it is asking for “cblas.h” and “matrix_funcs.h”
any good suggestion will really be appreciated. Thanks…