Code covered by the BSD License  

Highlights from
CUDA MEX

5.0

5.0 | 6 ratings Rate this file 23 Downloads (last 30 days) File Size: 2.64 KB File ID: #25314

CUDA MEX

by

 

Compile CUDA from within MATLAB! Just write cuda_mex filename.cu and it is ready.

| Watch this File

File Information
Description

This small script modifies the files used by MATLAB to compile mex-functions within MATLAB so that CUDA is supported.

Once the script setup_cuda_mex.m is run you can just compile CUDA mex files with the function cuda_mex.m.

Only Win32 with Visual Studio is supported in the current version.
CUDA Toolkit and SDK required.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (27)
10 Apr 2012 Lahdan alfahian

where can I find this fiel it is not in my computer cuda_mex.p ??

where can I found this one too
vcvarsamd64.bat I have "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\VCExpress.exe"
so it is not in bin where can I find it ?

10 Apr 2012 Lahdan alfahian

hello all

I can't get this right .
I triad ti fix what I found but some files wasn't in my Malab folder don't know what I should to do

this is the error

===================================
EDU>> setup_cuda_mex
??? Undefined function or variable 'setup_cuda_mex'.

Note that only Visual Studio compilers are supported.
Creating cudamexopts.bat based on mexopts.bat...
Creating cuda_mex.m based on mex.m...
Creating cuda_mex_helper.m based on mex_helper.m...
Adding current directory to path...
Warning: Unable to save path to file 'C:\Program Files (x86)\MATLAB\R2011a
Student\toolbox\local\pathdef.m'. You can save your path to a different
location by calling SAVEPATH with an input argument that specifies the
full path. For MATLAB to use that path in future sessions, save the path
to 'pathdef.m' in your MATLAB startup folder.
> In savepath at 162
In setup_cuda_mex at 118
Testing:
Compiling example.cu...
Can't open perl script "C:\Program Files (x86)\MATLAB\R2011a Student\bin\cuda_mex.pl": No such file or directory
??? Error using ==> cuda_mex at 208
Unable to complete successfully.

Error in ==> setup_cuda_mex at 123
cuda_mex example.cu

Can't open perl script "C:\Program Files (x86)\MATLAB\R2011a Student\bin\cuda_mex.pl": No such file or directory
??? Error using ==> cuda_mex at 208
Unable to complete successfully.

Can't open perl script "C:\Program Files (x86)\MATLAB\R2011a Student\bin\cuda_mex.pl": No such file or directory
??? Error using ==> cuda_mex at 208
Unable to complete successfully.

EDU>> cuda_mex simple.cu
Can't open perl script "C:\Program Files (x86)\MATLAB\R2011a Student\bin\cuda_mex.pl": No such file or directory
??? Error using ==> cuda_mex at 208
Unable to complete successfully.

EDU>>
===============
is it becase I'm using student version??why they don't just do the stepes clearly for the bigenear!!
========
this is whay I have

MATLAB Version (R2011a)
MATLAB License Number: STUDENT

OS Name Microsoft Windows 7 Home Premium
Version 6.1.7601 Service Pack 1 Build 7601
System Type x64-based PC

Thanks

28 Nov 2011 Takashi

Hi,
I had some troubles when I used it on windows7(x64), Matlab R2011a(x64) and Visual C++ 2010 express with WinSDK7.1, but I could get over it. I had to add a slight modification to Eddie's advice. Thanks Haim and Eddie!!

Modify cuda_mex.pl modification as written above.
Add
$ARCH = "win64";
before the command (line 184)
$ENV{'ARCH'} = $ARCH;

Also, you need to comment out the entire block in the setup_cuda_mex.m titled as
%% Process mex.pl
to prevent further renewal of mex_cuda.pl

vcvarsamd64.bat should be created as written above. You have to slightly change it for use with VS2010Ex and SDK7.1.

Finally, you have to move the cudamexopts.bat to the C:\Users\(USER\AppData\Roaming\MathWorks\MATLAB.
Funny thing is that Matlab R2011a create an additional folder, named R2011a, in above folder and put cudamexopts.bat to the directory, which is not recognized by cuda_mex. So this caused the error message saying 'option file not found'. You just move it to the upper directory and things will go perfectly well.

17 Aug 2011 Kevin Brown

San, this can happen when you don't have the 64-bit part of Visual Studio installed - check your installation of Visual Studio(Control Panel -> Modify Program) to see if the x64 part is installed (mine did not by default , so I had to update it).

16 Aug 2011 San Baz

VISUAL STUDIO 2010 anybody?
Thanks Hakim for a nearly automatic solution. I'm still having some trouble. I'm running win64, Cuda 4.0 and Visual Studio 2010.
I tried the original solution and performed Eddie Kotowski's hack but to no avail.
I get the error:

Visual Studio configuration file '(null)' could not be found for installation at 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64/../../..

Thanks!
SB

21 May 2011 Stanimir Mollov

Many thanks, successfully compiled from the first time with 32bit Window 7, MATLAB 2008a, VS 2005 and CUDA 4.0, on a Dell XPS M1730 & GeForce 8800GXT :)

21 Oct 2010 Loren

The script reads {prefdir}/mexopts.bat and I do not have such a file on this computer.

r2010b, x64, Windows 7

I have permission, btw.

07 Sep 2010 Haim Avron

@bochra

It looks like it failed to create a file.
Check if you have write permission to the directory.

06 Sep 2010 bochra

Hi,

I have downloaded these files. I am using Matlab 2010b, Windows 7 64 bit, and VS 2008 .

I run into the following problem when trying to compile the example through setup_cuda_mex.m :
Note that only Visual Studio compilers are supported.
Creating cudamexopts.bat based on mexopts.bat...
Creating cuda_mex.pl based on mex.pl...
??? Error using ==> fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in ==> setup_cuda_mex at 73
fprintf(fid_cuda_mex, '%s', line);

Has anyone run into a similar problem?
Thanks for any help you can provide.

10 Aug 2010 Jason Riley

Hi,
Ok so I have it working, great!
One question I have 2 CUDA compattible cards, how do i know which one it is using and how do i chose which one it uses?
:)
I know other cuda interfaces give you the option, although i have found them less 'useful' for other reasons..

Thanks,
Jason

03 Aug 2010 Petter

With the above hack in cuda_mex.pl I got it working the Windows 64-bit. The author should update the script to do this automatically.

27 Jul 2010 Drew

Eddie,

I was able to get it working! I have also compiled and run some of the other examples available in the Matlab/CUDA tutorial.

Thank you so much!

Drew

17 Jul 2010 Eddie Kotowski

@Drew,
To get 64-bit working you need to open the cuda_mex.pl file and around line 203 just hard code the $ARCH to "win64" so it would look like this.

$ARCH = "win64";

Also ensure that you have vcvarsamd64.bat and ensure that this information is in it and in this location (C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64\) Here is what vcvarsamd64.bat should have in it:

@SET VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio 9.0
@SET VCINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC
@SET WIN64SDKDIR=C:\Program Files\Microsoft SDKs\Windows\v6.1

@SET FrameworkDir=C:\WINDOWS\Microsoft.NET\Framework64
@SET FrameworkVersion=v3.5
@if "%VSINSTALLDIR%"=="" goto error_no_VSINSTALLDIR
@if "%VCINSTALLDIR%"=="" goto error_no_VCINSTALLDIR

@echo Setting environment for using Microsoft Visual Studio 2008 x64 tools.

@set PATH=%VCINSTALLDIR%\BIN\amd64;%FrameworkDir%\%FrameworkVersion%;%VCINSTALLDIR%\VCPackages;%VSINSTALLDIR%\Common7\IDE;%VSINSTALLDIR%\Common7\Tools;%WIN64SDKDIR%\Bin\x64;%WIN64SDKDIR%\Bin\x64\vsstools;c:\CUDA\bin;%PATH%
@set INCLUDE=%VCINSTALLDIR%\INCLUDE;%WIN64SDKDIR%\INCLUDE;%INCLUDE%
@set LIB=%VCINSTALLDIR%\LIB\amd64;%WIN64SDKDIR%\Lib\x64;%LIB%

@set LIBPATH=%FrameworkDir%\%Framework35Version%;%VCINSTALLDIR%\ATLMFC\LIB\amd64;%VCINSTALLDIR%\LIB\amd64;%LIBPATH%

08 Jun 2010 Drew

Hello,

I have downloaded these files. I am using Matlab 2009b, Windows 7 64 bit, and VS 2008 Express addition.

I run into the following problem when trying to compile the example through setup_cuda_mex.m

WARNING: $ARCH (UNKNOWN) not set correctly [win64]

C:\PROGRA~1\MATLAB\R2009B\BIN\CUDA_MEX.PL:
Error: Using options file:
C:\Users\dcreal\AppData\Roaming\MathWorks\MATLAB\R2009b\cudamexopts.bat
You cannot use this file with the UNKNOWN architecture because it enables
a compiler for a different architecture.
Running mex -setup may resolve this problem.

Has anyone run into a similar problem? Thanks for any help you can provide.

05 Jun 2010 Shu Heng Shu Heng  
01 Feb 2010 R. Scott

Haim,

nice file, but I have a question. Currently I use XPx64-bit and I was unable to get it to run. I am aware of the x32 is only supported from your notes. But how could I get it to run in x64? Any thoughts.

Oh BTW you may already know this but if you have a CUDA enabled Vid card (non-Tesla) and you are using it as for your monitor as well. There is a max allowed time use of the GPU at 2 sec to any code. If you want more(non-Tesla) it cannot be used as a monitor support vid-card. just FYI that's all

23 Oct 2009 Sara

Ok, hopefully I'm not overstaying my welcome on this forum, but I've found out more answers to my cutCreateTimer issue and I wanted to put them out there incase someone else falls into this issue.
It appears that somehow the cuda_mex compilation couldn't find my cutil libraries. This is not necessarily it's fault, I don't know much about make files, linking, etc. so it's fairly probable that I screwed something up somewhere. There is likely a short, easy, and correct way to fix this, but since I don't know it, here's my work around. If you find your way into the CUDA SDK folder you will find a Visual Studio solution for compiling everything cutil. If you change the output of these libraries to the cuda standard library ( likely something along the lines of CUDA/bin) and then rebuild the solution the cutil32 libraries will then be found by the compiler/linker. Will keep posting if I find out a more elegant solution.

22 Oct 2009 Anthony

What would need to be done to port this code to a 64-bit platform?

22 Oct 2009 Sara

Still having trouble with the cutCreateTimer. Here is an example. I have modified the example.cu that comes with this package to create this timer. In addition, the cutil32.lib and cutil32D.lib are linked in the setup_cuda_mex.m file. If anyone has a solution, I would be very excited to hear it.

#include <stdio.h>
#include <cuda.h>
#include <cutil.h>

#include "mex.h"

__global__ void square_array(float *a, int N)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx<N) a[idx] = a[idx] * a[idx];
}

void mexFunction(int nargout, mxArray *argout[], int nargin, const mxArray *argin[])
{
float *input, *cuda_mem;
int N;
size_t size;
unsigned int hTimer;

cutCreateTimer(&hTimer);

argout[0] = mxDuplicateArray(argin[0]);
input = (float *)mxGetPr(argout[0]);
N = mxGetN(argout[0]) * mxGetM(argout[0]);
size = N * sizeof(float);
cudaMalloc((void **) &cuda_mem, size);
cudaMemcpy(cuda_mem, input, size, cudaMemcpyHostToDevice);
int block_size = 4;
int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
square_array <<< n_blocks, block_size >>> (cuda_mem, N);
cudaMemcpy(input, cuda_mem, size, cudaMemcpyDeviceToHost);
cudaFree(cuda_mem);
}

22 Oct 2009 Sara

I'm working on Windows XP...has anyone had problems with cutCreateTimer not working? When it is called by either the CUT_SAFE_CALL or directly it crashes the program.

20 Oct 2009 Sara

If you add cutil32.lib to

if (strcmp(name, 'LINKFLAGS'))
value = [value ' cudart.lib cutil32.lib'];
end

in setup_cuda_mex.m, the linker will properly access cutil based functions. I spent a lot of time trying to figure out why I would get errors in the linking involving various cuda based functions even though the compiler knew the address to the header files...

29 Sep 2009 Haim Avron

Hi Mklcst,

Do you have permissions to write to MATLAB's directories?

29 Sep 2009 mklcst mklcst

Hi Haim Avron, whe i run I get this error:

> Note that only Visual Studio compilers are supported.
> Creating cudamexopts.bat based on mexopts.bat...
> Creating cuda_mex.pl based on mex.pl...
> ??? Error using ==> fprintf
> Invalid file identifier. Use fopen to generate a valid file identifier.
>
> Error in ==> setup_cuda_mex at 73
> fprintf(fid_cuda_mex, '%s', line);

29 Sep 2009 Oliver Woodford

Thanks, Haim. I've found a solution now. I use nvcc to convert.cu files to .cpp files, then call mex as normal. This avoids copying/changing any of the MATLAB scripts.

26 Sep 2009 Haim Avron

To do the same on a Linux the one running the script should have root privileges. I currently do not have MATLAB on a Linux for which I have root privileges...

25 Sep 2009 Oliver Woodford

Sounds good, but currently no use to me - I'm desperate for an equivalent function for a linux system.

15 Sep 2009 Sara

Works!!! Thank you so much! I've spent the last week arguing with Matlab 2008a, Visual Studio 2008, and CUDA trying to get them to talk to each other.

Contact us