Main Content

Run MATLAB Functions on a GPU

You can speed up your code by running MATLAB® functions on a GPU. GPU computing in MATLAB requires Parallel Computing Toolbox™.

MATLAB Functions with gpuArray Arguments

Many functions in MATLAB and other toolboxes run automatically on a GPU if you supply a gpuArray data argument. A gpuArray in MATLAB represents an array that is stored on the GPU.

A = gpuArray([1 0 1; -1 -2 0; 0 1 -1]);
e = eig(A);

Whenever you call any of these functions with at least one gpuArray as a data input argument, the function executes on the GPU. The function generates a gpuArray as the result, unless returning numeric data to the local workspace is more appropriate (for example, size). You can mix inputs using both gpuArray data and arrays stored in host memory in the same function call. gpuArray-enabled functions include the discrete Fourier transform (fft), matrix multiplication (mtimes), left matrix division (mldivide), and hundreds of others.

Conditions for gpuArray inputs

GPU-enabled functions run on the GPU only when the input data is on the GPU. The data type of parameter arguments such as dimensions or indices do not affect where the function is run. For example, the sum function in this code runs on the GPU because the data, the first input, is on the GPU.

A = rand(10);
d = 2;
sum(gpuArray(A),d);
However, the sum function in this code does not run on GPU because the data, the first input, is not on the GPU.
A = rand(10);
d = 2;
sum(A,gpuArray(d));

Work with Complex Numbers on a GPU

If the output of a function running on a GPU could potentially be complex, you must explicitly specify its input arguments as complex. For more information, see Work with Complex Numbers on a GPU.

Work with Sparse Arrays on a GPU

The sparse function can be used to create sparse gpuArray objects. Many MATLAB functions support sparse gpuArray objects. For more information, see Work with Sparse Arrays on a GPU.

Check gpuArray-Supported Functions

Several MATLAB toolboxes include functions with gpuArray support. To view lists of all functions in these toolboxes that support gpuArray objects, use the links in the following table. Functions in the lists with information indicators have limitations or usage notes specific to running the function on a GPU. You can check the usage notes and limitations in the Extended Capabilities section of the function reference page. For information about updates to individual gpuArray-enabled functions, see the release notes.

Toolbox NameList of Functions with gpuArray SupportGPU-Specific Documentation
MATLABFunctions with gpuArray support 
Statistics and Machine Learning Toolbox™Functions with gpuArray support (Statistics and Machine Learning Toolbox)Analyze and Model Data on GPU (Statistics and Machine Learning Toolbox)
Image Processing Toolbox™Functions with gpuArray support (Image Processing Toolbox)GPU Computing (Image Processing Toolbox)
Deep Learning Toolbox™

Functions with gpuArray support (Deep Learning Toolbox)

*(see also Deep Learning with GPUs)

Scale Up Deep Learning in Parallel, on GPUs, and in the Cloud (Deep Learning Toolbox)

Deep Learning with MATLAB on Multiple GPUs (Deep Learning Toolbox)

Computer Vision Toolbox™Functions with gpuArray support (Computer Vision Toolbox)GPU Code Generation and Acceleration (Computer Vision Toolbox)
Communications Toolbox™Functions with gpuArray support (Communications Toolbox)C/C++ Code Generation (Communications Toolbox)
5G Toolbox™Functions with gpuArray support (5G Toolbox) 
Signal Processing Toolbox™Functions with gpuArray support (Signal Processing Toolbox)Code Generation and GPU Support (Signal Processing Toolbox)
Audio Toolbox™Functions with gpuArray support (Audio Toolbox)Code Generation and GPU Support (Audio Toolbox)
Wavelet Toolbox™Functions with gpuArray support (Wavelet Toolbox)Code Generation and GPU Support (Wavelet Toolbox)
Curve Fitting Toolbox™Functions with gpuArray support (Curve Fitting Toolbox) 

For a list of functions with gpuArray support in all MathWorks® products, see gpuArray-supported functions. Alternatively, you can filter by product. On the Help bar, click Functions. In the function list, browse the left pane to select a product, for example, MATLAB. At the bottom of the left pane, select GPU Arrays. If you select a product that does not have gpuArray-enabled functions, then the GPU Arrays filter is not available.

Deep Learning with GPUs

For many functions in Deep Learning Toolbox, GPU support is automatic if you have a supported GPU and Parallel Computing Toolbox. You do not need to convert your data to gpuArray. The following is a non-exhaustive list of functions that, by default, run on the GPU if available.

For more information about automatic GPU support in Deep Learning Toolbox, see Scale Up Deep Learning in Parallel, on GPUs, and in the Cloud (Deep Learning Toolbox).

For custom training loops, convert your data to gpuArray and use functions with gpuArray support (Deep Learning Toolbox).

Check or Select a GPU

If you have a supported GPU, then MATLAB automatically uses it for GPU computation. If you have multiple GPUs, then you can use gpuDeviceTable to examine the properties of all GPUs detected in your system. You can use gpuDevice to select one of them, or use multiple GPUs with a parallel pool. For more information, see Identify and Select a GPU Device and Run MATLAB Functions on Multiple GPUs. To check if your GPU is supported, see GPU Computing Requirements.

gpuDeviceTable
    Index           Name           ComputeCapability    DeviceAvailable    DeviceSelected
    _____    __________________    _________________    _______________    ______________

      1      "NVIDIA RTX A5000"          "8.6"               true              true      
      2      "Quadro P620"               "6.1"               true              false     

Alternatively, you can determine how many GPU devices are available, inspect some of their properties, and select a device to use from the MATLAB® desktop. On the Home tab, in the Environment area, select Parallel > Select GPU Environment.

The Parallel menu, including the Select GPU Environment pane showing two GPU devices. A tick next to the first device indicates that it is the selected device.

Use MATLAB Functions with the GPU

This example shows how to use gpuArray-enabled MATLAB functions to operate with gpuArray objects. You can check the properties of your GPU using the gpuDevice function.

gpuDevice
ans = 
  CUDADevice with properties:

                 Name: 'NVIDIA RTX A5000'
                Index: 1 (of 2)
    ComputeCapability: '8.6'
          DriverModel: 'TCC'
          TotalMemory: 25544294400 (25.54 GB)
      AvailableMemory: 24734105600 (24.73 GB)
      DeviceAvailable: true
       DeviceSelected: true

  Show all properties.

Create a row vector that repeats values from -15 to 15. To transfer it to the GPU and create a gpuArray object, use the gpuArray function.

X = [-15:15 0 -15:15 0 -15:15];
gpuX = gpuArray(X);
whos gpuX
  Name      Size            Bytes  Class       Attributes

  gpuX      1x95              760  gpuArray              

To operate with gpuArray objects, use any gpuArray-enabled MATLAB function. MATLAB automatically runs calculations on the GPU. For more information, see Run MATLAB Functions on a GPU. For example, use diag, expm, mod, round, abs, and fliplr together.

gpuE = expm(diag(gpuX,-1)) * expm(diag(gpuX,1));
gpuM = mod(round(abs(gpuE)),2);
gpuF = gpuM + fliplr(gpuM);

Plot the results.

imagesc(gpuF);
colormap(flip(gray));

If you need to transfer the data back from the GPU, use gather. Transferring data back to the CPU can be costly, and is generally not necessary unless you need to use your result with functions that do not support gpuArray.

result = gather(gpuF);
whos result
  Name         Size            Bytes  Class     Attributes

  result      96x96            73728  double              

In general, running code on the CPU and the GPU can produce different results due to numerical precision and algorithmic differences between the GPU and CPU. Answers from the CPU and GPU are both equally valid floating point approximations to the true analytical result, having been subjected to different roundoff behavior during computation. In this example, the results are integers and round eliminates the roundoff errors.

Examples Using GPUs

Examples Running MATLAB Functions on GPUs

The following examples pass gpuArray objects to supported MATLAB functions, causing those functions to run on the GPU.

Toolbox NameExamples
MATLAB
Image Processing Toolbox
Deep Learning Toolbox
Statistics and Machine Learning Toolbox
Signal Processing Toolbox
Audio Toolbox
Communications Toolbox
Wavelet Toolbox

Other Examples Using GPUs

The following examples make use of other automatic GPU support.

Toolbox NameExamples
Deep Learning Toolbox
Communications Toolbox

Acknowledgments

MAGMA is a library of linear algebra routines that take advantage of GPU acceleration. Linear algebra functions implemented for gpuArray objects in Parallel Computing Toolbox leverage MAGMA to achieve high performance and accuracy.

See Also

| | | |

Related Examples

More About