GPU Computing in MATLAB
To speed up your code, first try profiling and vectorizing it. For information, see Performance and Memory. After profiling and vectorizing, you can also try using your computer’s GPU to speed up your calculations. If all the functions that you want to use are supported on the GPU, you can simply use
gpuArray to transfer input data to the GPU, and call
gather to retrieve the output data from the GPU. To get started with GPU computing, see Run MATLAB Functions on a GPU.
For deep learning, MATLAB® provides automatic parallel support for multiple GPUs. See Deep Learning with MATLAB on Multiple GPUs (Deep Learning Toolbox).
Key Functions and Classes
|Number of GPU devices present|
|Table of properties of GPU devices (Since R2021a)|
|Wait for GPU calculation to complete|
|Time required to run function on GPU|
|Determine if gpuArray or CUDAKernel is available on GPU|
|Determine whether input is |
|Apply function to each element of array on GPU|
|Apply function to each page of distributed or GPU array|
|Control random number generation on the GPU|
|Reset GPU device and clear its memory|
|Random number stream on a GPU|
|Query and set forward compatibility for GPU devices (Since R2020b)|
Run MATLAB Code on GPU
- Run MATLAB Functions on a GPU
gpuArrayargument to automatically run functions on a GPU.
- Identify and Select a GPU Device
This example shows how to use
gpuDeviceto identify and select which device you want to use.
- GPU Computing Requirements
Support for NVIDIA® GPU architectures.
- Establish Arrays on a GPU
gpuArrayobjects to store data on the GPU and perform calculation on the GPU.
- Run MATLAB Functions on Multiple GPUs
This example shows how to run MATLAB® code on multiple GPUs in parallel, first on your local machine, then scaling up to a cluster.
- Run MATLAB using GPUs in the Cloud
Take advantage of powerful GPUs in the cloud to accelerate your code.
- Train Network Using Automatic Multi-GPU Support (Deep Learning Toolbox)
This example shows how to use multiple GPUs on your local machine for deep learning training using automatic parallel support.
- Work with Sparse Arrays on a GPU
Learn about the conditions for working with sparse arrays on a GPU.
- Work with Complex Numbers on a GPU
Learn about the conditions for applying functions that might return complex values on a GPU.
Improve Performance on GPU
- Measure and Improve GPU Performance
Time code running on your GPU and explore options for improving performance.
- Improve Performance Using a GPU and Vectorized Calculations
This example shows you how to improve performance by running a function on the GPU instead of the CPU, and by vectorizing the calculations.
- Improve Performance of Element-Wise MATLAB Functions on the GPU Using arrayfun
This example shows how to improve the performance of your code by running MATLAB® functions on the GPU using
- Improve Performance of Small Matrix Problems on the GPU Using pagefun
This example shows how to use
pagefunto improve the performance of independent operations applied to multiple matrices arranged in a multidimensional array.
- Benchmarking A\b on the GPU
This example looks at how we can benchmark the solving of a linear system on the GPU.
- Profile Your Code to Improve Performance
Use the Profiler to measure the time it takes to run your code and identify which lines of code consume the most time or which lines do not run.
Revise loop-based, scalar-oriented code to use MATLAB matrix and vector operations.
- Random Number Streams on a GPU
Control the random number streams on a GPU to generate the same sequences of random numbers as on the CPU.
- Generating Random Numbers on a GPU
This example shows how to switch between the different random number generators that are supported on the GPU.
- Stencil Operations on a GPU
This example uses Conway's "Game of Life" to demonstrate how stencil operations can be performed using a GPU.
- Compute the Mandelbrot Set Using GPU-Enabled Functions
This example shows how to use GPU-enabled MATLAB® functions to compute a well-known mathematical construction: the Mandelbrot set.