This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

mxInitGPU (C)

Initialize MATLAB GPU library on currently selected device

C Syntax

#include "gpu/mxGPUArray.h"
 int mxInitGPU()


int type with one of the following values:

  • MX_GPU_SUCCESS if the MATLAB GPU library is successfully initialized.

  • MX_GPU_FAILURE if not successfully initialized.


Before using any CUDA code in your MEX file, initialize the MATLAB GPU library if you intend to use any mxGPUArray functionality in MEX or any GPU calls in MATLAB. There are many ways to initialize the MATLAB GPU API, including:

  • Call mxInitGPU at the beginning of your MEX file before any CUDA code.

  • Call gpuDevice(deviceIndex) in MATLAB before running any MEX code.

  • Create a gpuArray in MATLAB before running any MEX code.

You should call mxInitGPU at the beginning of your MEX file, unless you have an alternate way of guaranteeing that the MATLAB GPU library is initialized at the start of your MEX file.

If the library is initialized, this function returns without doing any work. If the library is not initialized, the function initializes the default device. Note: At present, a MATLAB MEX file can work with only one GPU device at a time.

Introduced in R2013a