mexcuda

Compile MEX-function for GPU computation

Syntax

  • mexcuda filenames
    example
  • mexcuda option1 ... optionN filenames
    example

Description

example

mexcuda filenames compiles and links source files into a shared library called a MEX-file, executable from within MATLAB. mexcuda is an extension of the MATLAB mex function. It compiles MEX-files written using the CUDA C++ framework with NVIDIA's nvcc compiler, allowing the files to define and launch GPU kernels. In addition, it exposes the GPU MEX API to allow the MEX-file to read and write gpuArrays.

example

mexcuda option1 ... optionN filenames builds with the specified build options. The option1 ... optionN arguments supplement or override the default mexcuda build configuration.

Examples

collapse all

Compile Simple MEX-Function

Compile a simple MEX-function to create the function myMexFunction from a CUDA C++ source file.

mexcuda myMexFunction.cu

An example source file is available at matlabroot/toolbox/distcomp/gpu/extern/src/mex/mexGPUExample.cu.

Display Detailed Build and Troubleshooting Information

Use verbose mode to display the compile and link commands and other information useful for troubleshooting.

mexcuda -v myMexFunction.cu

Compile and Link Multiple Source Files

Compile and link multiple source files with one command.

mexcuda myMexFunction.cu otherSource1.cpp otherSource2.cpp

Compile and Link in Two Stages

First compile, then link to create a function.

mexcuda -c myMexFunction.cu
mexcuda myMexFunction.obj

The first line compiles to myMexFunction.obj (Windows®) or myMexFunction.o (UNIX®), and the second links to create the function myMexFunction.

Compile with Dynamic Parallelism

Compile code that uses dynamic parallelism, defining kernels that launch other kernels.

mexcuda -dynamic myMexFunction.cu

Link to Third-Party Library

Compile a MEX-function that makes use of the CUDA image primitives library, npp, which is installed at C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.0\lib\x64\nppi.lib.

mexcuda '-LC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.0\lib\x64' -lnppi myMexFunction.cu 

Related Examples

Input Arguments

collapse all

filenames — One or more file namesstring

One or more file names, including name and file extension, specified as a string. If the file is not in the current folder, specify the full path to the file. File names can be any combination of:

  • C or C++ language source files

  • object files

  • library files

The first source code file listed in filenames is the name of the binary MEX-file. To override this naming convention, use the '-output' option.

Data Types: char

option1 ... optionN — One or more build optionsstrings corresponding to valid option flags

One or more build options, specified as one of these values. Options can appear in any order on any platform, except where indicated.

In addition to most options available for the mex function, these options are supported:

OptionDescription

-dynamic

Dynamic parallelism: compiles MEX-files that define kernels that launch other kernels.

-G

Generate debug information for device code. This makes it possible to step through kernel code line by line in one of NVIDIA's debugging applications (NSight or cuda-gdb). To enable debugging of host code use -g.

All other mex function options are supported, except for the following:

OptionReason

-compatibleArrayDims

Use of -largeArrayDims is implicit, and cannot be overridden.

See Also

Introduced in R2015b

Was this topic helpful?