File Exchange

image thumbnail

NVCC cuda compiler wraper

version 1.1 (3.55 KB) by

Compile cuda kernels in Matlab, like nvcc('example.cu')

6 Downloads

Updated

View License

This function NVCC is a wraper for the NVIDIA Cuda compiler NVCC.exe
in combination with a Visual Studio compiler. After this Cuda
files can be compiled into kernels

If you call the code the first time, or with "nvcc -config":
1) It will try to locate the "The NVIDIA GPU Computing Toolkit", which
can be downloaded from www.nvidia.com/object/cuda_get.html
Typical Location :
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\bin
2) It will try to locate the visual studio compiler
Typical Location :
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\
3) It creates a file nvccbat.bat with the compiler information.

After this configuration procedure, you can compile files with:

nvcc(filename);

or

nvcc(options,filename)

filename : A string with the filename, for example 'example.cu'
options : NVCC Compiler options for example,
nvcc(option1, option2, option3,filename)

For help on NVCC config options type, "nvcc --help"

Note!
If the NVCC fails to locate the compiler you can try to
write your own "nvccbat.bat" file in a text-editor, for example:
echo off
set PATH=C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\;PATH
set PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\bin;PATH
call vcvars32.bat
nvcc 1 2 3 4 5 6 7 8 9

1 Example, Configuration
Locate Cuda and VS compiler
nvcc -config

Show the NVCC compiler options
nvcc --help

Test some input options
nvcc --dryrun -ptx example.cu

2 Example,

Locate Cuda and VS compiler
nvcc -config
Compile the code
nvcc('example.cu');
It the same as :
nvcc -ptx example.cu

Make the kernel
Kernel = parallel.gpu.CUDAKernel('example.ptx', 'example.cu');

We want to execute this kernel 100 times
Kernel.ThreadBlockSize=100;

We make an array with 100 random files
Data=rand(100, 1, 'single');
DataCuda= gpuArray(Data);

We will add the value 1
OneCuda = parallel.gpu.GPUArray.ones(1,1);

Execute the kernel
DataOneCuda = feval(Kernel, DataCuda, OneCuda);

Get the data back
DataOne=gather(DataOneCuda);

Show the result
figure, hold on;
plot(Data,'b');
plot(DataOne,'r');

Comments and Ratings (10)

UPDATE: the error "Cannot open include file: "gpu/mxGPUArray.h" was resolved when I copied the header file in: MATLABROOT\toolbox\distcomp\gpu\extern\include\gpu\mxGPUArray.h to the current folder, make a subfolder named "gpu" then put the header in it. But it's still weird

Hi, I'm using Windows 10, matlab R2016b, visual studio c++ 2015 professional as mex compiler, and you example worked fine. But when I tried to compile mexGPUExample.cu (which is an example in MATLAB's directories), I got this error:
>> nvcc('mexGPUExample.cu')

C:\Users\LENOVO\Downloads\nvcc_version1>echo off
mexGPUExample.cu(10): fatal error C1083: Cannot open include file: 'gpu/mxGPUArray.h': No such file or directory
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
mexGPUExample.cu
Please help me, thank you very much

It was able to locate the Cuda toolkit and compiler, but when I tried nvcc('mexGPUExample.cu') it gave me the following:
mexGPUExample.cu(9): fatal error C1083: Cannot open include file: 'mex.h': No such file or directory
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
mexGPUExample.cu
What should i do?

Tamer Oraby

It asks me
"Select the Cuda Toolkit you want to use :"
when I do nvcc -config
what should I do?

ahmed ismail

IT'S A GREAT SCRIPT,IT SOLVE THE PROBLEM TO RUN THIS FROM CMD , DO U HAVE EXPERIENCE TO RUN THIS IN CMD SCREEN

A minor possible improvement: I needed to pass several defines (-D) to the compiler, but currently the bat file only supports 9 arguments. You can replace the %1 %2 ... with a single %* to support more arguments (at least in Windows 7).

Michael Knap

Good work, however it seems to be platform specific. I am working on a similar implementation for linux.

Leon Su

Nice and helpful post. For those of you using Jacket, you'll recognize this concept as GLAUNCH, documentation here: http://wiki.accelereyes.com/wiki/index.php/GLAUNCH

Enjoy!

Updates

1.1

Now allows NVCC.EXE extra configuration options. And will store the main .bat file with the configuration always in the same folder.

MATLAB Release
MATLAB 7.11 (R2010b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video