TechBuzzPro

How to Use GPU on MATLAB: A Practical Guide

· 6 min read · Windows & PC

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

MATLAB can use your computer's graphics card (GPU) to speed up certain calculations, often by a factor of ten or more for large arrays. To do this, you need a supported NVIDIA GPU, the Parallel Computing Toolbox, and a few lines of code that move data to the GPU and run GPU-enabled functions. The steps below walk you through checking your hardware, enabling GPU support, writing GPU-aware code, and fixing common problems.

What You'll Need

  • A 64-bit Windows, Linux, or macOS computer with an NVIDIA GPU that supports CUDA. MATLAB's GPU support is built on CUDA, so AMD and Intel GPUs are not supported for GPU computing in MATLAB.
  • MATLAB installed (R2016b or later recommended for the current gpuArray workflow).
  • The Parallel Computing Toolbox, which provides the gpuArray and GPU-enabled functions.
  • A current NVIDIA display driver. You do not need to install the CUDA Toolkit separately; MATLAB ships with the CUDA libraries it needs, but the driver must be recent enough for your MATLAB version.

Menu labels and exact wording can differ slightly by MATLAB version and operating system. The steps below focus on the desktop version of MATLAB; the same commands work in the MATLAB Online environment only if your account includes GPU access, which most standard accounts do not.

Step-by-Step

  1. Check that MATLAB sees a supported GPU. In the Command Window, run gpuDevice. If a compatible NVIDIA GPU is present and the driver is working, MATLAB prints a table with the device name, compute capability, total memory, and available memory. If you see an error saying no supported GPU was found, stop here and see Troubleshooting.

  2. Confirm the Parallel Computing Toolbox is installed. Run ver and look for "Parallel Computing Toolbox" in the list. If it is missing, install it through the Add-Ons explorer or your license administrator; GPU computing in MATLAB will not work without it.

  3. Move your data to the GPU. Use gpuArray to copy an array from CPU memory to GPU memory. For example, A = gpuArray(rand(1000)); creates a 1000-by-1000 random matrix directly on the GPU. You can also convert an existing variable: G = gpuArray(A);. Only numeric arrays (and a few other types) can be moved; cell arrays and most objects cannot.

  4. Run GPU-enabled functions on the gpuArray. Many core MATLAB functions, such as mtimes (matrix multiply), fft, conv2, sum, max, and most element-wise operators, automatically execute on the GPU when their inputs are gpuArrays. For example, C = A * A; runs on the GPU if A is a gpuArray. No special syntax is needed beyond keeping data on the GPU.

  5. Bring results back to the CPU. Use gather to copy a gpuArray back to normal CPU memory: C_cpu = gather(C);. Do this once at the end of a chain of GPU operations, not after every step, because each transfer has overhead.

  6. Write GPU-aware code with gpuArray and gather. A typical pattern is: create or load data on the CPU, convert to gpuArray, run a loop or series of GPU-enabled operations, then gather the final result. Avoid mixing CPU and GPU arrays in the same expression; MATLAB will either error or silently transfer data, which slows things down.

  7. Measure the benefit. Use tic and toc, or gpuDevice before and after to see memory use. Compare the same calculation on the CPU and GPU with realistic data sizes. Small arrays often run faster on the CPU because of transfer overhead; the GPU wins on large, parallel-friendly operations.

  8. Check for GPU support in toolboxes you use. Some functions in Image Processing Toolbox, Deep Learning Toolbox, and Signal Processing Toolbox have GPU-enabled versions. Look for "GPU support" in the function's documentation page. If a function is not GPU-enabled, it will either run on the CPU or return an error when given a gpuArray.

Common Mistakes

  • Assuming any graphics card works. MATLAB GPU computing requires an NVIDIA GPU with CUDA support. Integrated Intel graphics and AMD cards are not supported for gpuArray work.
  • Forgetting the Parallel Computing Toolbox. Without it, gpuArray and gpuDevice are unavailable, even if the hardware is present.
  • Transferring data back and forth inside a loop. Each gather call moves data over the PCIe bus. Keep data on the GPU for the whole calculation and gather only the final result.
  • Using tiny arrays. GPU overhead dominates for small data. A 100-by-100 matrix multiplication is usually faster on the CPU; a 5000-by-5000 one is usually much faster on the GPU.
  • Mixing CPU and GPU arrays. Expressions like gpuArray(A) + B where B is a normal array can cause unexpected transfers or errors. Convert all operands first.
  • Ignoring memory limits. GPU memory is separate from system RAM and is often smaller. A gpuArray that fits in RAM may not fit on the GPU; check gpuDevice for available memory.

Tips

  • Use gpuDevice to see how much GPU memory is free before allocating large arrays.
  • Preallocate gpuArrays with zeros(n, 'gpuArray') or gpuArray.zeros to avoid repeated allocation inside loops.
  • Prefer vectorized operations over loops; GPU speedup comes from parallelism, and MATLAB's GPU-enabled functions are already optimized.
  • Use arrayfun with a gpuArray input to run an element-wise function on the GPU when no built-in GPU function exists, but note that arrayfun on the GPU has restrictions on the function you can pass.
  • If you have multiple NVIDIA GPUs, select one with gpuDevice(index) before running GPU code.
  • Keep your NVIDIA driver updated; an old driver is a common cause of "no supported GPU" errors after a MATLAB upgrade.

Troubleshooting

If gpuDevice returns an error, work through these checks in order:

  1. Restart MATLAB. A fresh session clears stale GPU state.
  2. Update the NVIDIA driver from the manufacturer's official site, then restart the computer.
  3. Check the GPU model against MATLAB's list of supported GPUs for your release. Very old GPUs may no longer be supported by newer MATLAB versions.
  4. Confirm the Parallel Computing Toolbox license is active with license('test','Distrib_Computing_Toolbox').
  5. Look for conflicting processes. Other applications using the GPU heavily (games, rendering, other CUDA programs) can reduce available memory or cause errors.
  6. If a function errors on a gpuArray, check its documentation for GPU support. If it is not GPU-enabled, gather the input to the CPU, run the function, and optionally convert the result back.

If none of that helps, run gpuDevice again and note the exact error message; it usually points to driver, license, or hardware support issues.

Frequently Asked Questions

Can I use an AMD or Intel GPU with MATLAB?

No. MATLAB's GPU computing support is built on NVIDIA CUDA, so only NVIDIA GPUs are supported for gpuArray and GPU-enabled functions.

Do I need to install CUDA separately?

No. MATLAB includes the CUDA libraries it needs. You only need a compatible NVIDIA driver. Installing the full CUDA Toolkit is optional and mainly useful if you compile custom GPU code.

Will all MATLAB functions run faster on the GPU?

No. Only functions with GPU support run on the GPU, and only large, parallel-friendly operations see a speedup. Small arrays and functions without GPU support run on the CPU or error.

How do I know if my GPU has enough memory?

Run gpuDevice and check the "AvailableMemory" value. Keep your largest gpuArray well below that figure, since intermediate results also use GPU memory.

Can I use GPU computing in MATLAB Online?

Only if your MATLAB Online license includes GPU access, which most standard accounts do not. Check your account's features or use a desktop installation with a supported NVIDIA GPU.

What is the difference between gpuArray and gather?

gpuArray copies data from CPU memory to GPU memory so calculations run on the GPU. gather copies data back from GPU memory to the CPU so you can use it with non-GPU functions or display it.