image thumbnail

NVCC cuda compiler wraper

version (3.55 KB) by Dirk-Jan Kroon
Compile cuda kernels in Matlab, like nvcc('')


Updated 06 Dec 2010

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
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:




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

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

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

2 Example,

Locate Cuda and VS compiler
nvcc -config
Compile the code
It the same as :
nvcc -ptx

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

We want to execute this kernel 100 times

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

Show the result
figure, hold on;

Cite As

Dirk-Jan Kroon (2021). NVCC cuda compiler wraper (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!