How to Monitor GPU Usage in MATLAB
In this article
Monitoring GPU usage in MATLAB is essential when running deep learning, parallel computing, or any GPU-accelerated code. It helps you confirm that your GPU is actually being utilized, identify bottlenecks, and optimize performance. This guide shows you how to check GPU usage both from within MATLAB and using system tools, and how to interpret the results.
What You'll Need
- A MATLAB installation with the Parallel Computing Toolbox (required for GPU support).
- A supported NVIDIA GPU with appropriate drivers installed.
- Optional: NVIDIA System Management Interface (nvidia-smi) – usually installed with the driver.
Step-by-Step
- Verify GPU availability in MATLAB. Open MATLAB and run
gpuDevicein the Command Window. This displays the GPU name, compute capability, total memory, and current memory used. If it returns an error, your GPU may not be supported or the Parallel Computing Toolbox is missing. - Check GPU utilization from within MATLAB. MATLAB itself does not provide a built-in function for real-time GPU utilization percentage. However, you can use the
gpuDeviceoutput to see memory usage. For actual utilization, use external tools likenvidia-smi(see next step). - Use nvidia-smi (Windows/Linux). Open a terminal (Command Prompt on Windows) and run
nvidia-smi. This shows GPU utilization, memory usage, temperature, and running processes. To refresh continuously, runnvidia-smi -l 1(updates every second). - Use Task Manager (Windows). Press
Ctrl+Shift+Escto open Task Manager, go to the “Performance” tab, and select your GPU on the left. You’ll see utilization graphs, memory, and more. Note: On older Windows versions, you may need to enable GPU monitoring in Task Manager settings. - Use Activity Monitor (macOS). For macOS, open Activity Monitor, click the “Window” menu, then “GPU History”. This shows GPU usage over time. However, MATLAB GPU support on macOS is limited to certain versions, so this may not be applicable.
- Monitor during MATLAB execution. To see real-time usage while your code runs, keep the nvidia-smi terminal or Task Manager visible. Alternatively, you can programmatically capture utilization by calling
system('nvidia-smi --query-gpu=utilization.gpu --format=csv')from MATLAB to get a snapshot. - Use MATLAB's wait function to sync. When using GPU operations, MATLAB queues them asynchronously. To get accurate timing or memory readings, use
wait(gpuDevice)to synchronize before checking usage.
Common Mistakes
- Not having the Parallel Computing Toolbox. Without it,
gpuDevicewill error. Ensure the toolbox is installed. - Using an unsupported GPU. MATLAB only supports NVIDIA GPUs with compute capability 3.0 or higher. Check your GPU’s capability with
gpuDeviceor the NVIDIA website. - Misinterpreting 0% utilization. If your code is not using GPU operations (e.g., you forgot to convert arrays to
gpuArray), the GPU will remain idle. UsegpuArrayto move data to the GPU. - Forgetting to wait for GPU operations. MATLAB often queues GPU tasks, so memory may appear higher than expected. Use
wait(gpuDevice)before checking. - Relying solely on MATLAB's memory info.
gpuDeviceshows memory used by MATLAB, but not total GPU utilization. Use system tools for a full picture.
Tips
- Use
gpuDevicebefore and after your code. Compare memory usage to see if your GPU is being used. - For deep learning, use
trainingOptionswith 'Plots' set to 'training-progress'. This shows GPU usage in the training plot. - Use
nvidia-smiwith the query flag for specific metrics. For example,nvidia-smi --query-gpu=utilization.gpu,memory.used --format=csvgives a clean output. - Set environment variable
NVIDIA_VISIBLE_DEVICESif using containers. This ensures MATLAB sees the correct GPU. - If you have multiple GPUs, use
gpuDevice(2)to select the second one. Monitor each separately.
Troubleshooting
Issue: gpuDevice returns an error.
Check that the Parallel Computing Toolbox is installed (run ver to see toolboxes). Also ensure your GPU is NVIDIA and supports compute capability 3.0+. Update drivers.
Issue: GPU utilization shows 0% even though you think it should be used.
Verify that your code actually uses GPU operations. For example, if you do A = rand(1000); that’s on the CPU. Use A = gpuArray(rand(1000)); to move it to the GPU. Also check that you’re not using a CPU-only function.
Issue: nvidia-smi shows high memory but low utilization.
This is normal when data is transferred to the GPU but computation is light. It may indicate a bottleneck in data transfer or that your kernel is not compute-intensive.
Issue: Task Manager shows GPU usage but MATLAB is not listed.
In Task Manager, go to the “Details” tab, right-click the column headers, and add “GPU” columns. This shows GPU usage per process.
Issue: nvidia-smi is not found.
On Windows, nvidia-smi is usually in C:\Windows\System32. If not, reinstall the NVIDIA driver. On Linux, it’s in /usr/bin or /usr/local/bin.
Frequently Asked Questions
Can I get GPU utilization directly in MATLAB without external tools?
No, MATLAB does not have a built-in function for utilization percentage. You can use gpuDevice for memory, but for utilization, you need external tools like nvidia-smi or Task Manager.
Why does my GPU memory show as used even when my code is idle?
MATLAB may keep GPU memory allocated after operations for reuse. Use reset(gpuDevice) to clear it, but be aware this resets all GPU variables.
Does MATLAB support AMD or Intel GPUs?
No, MATLAB GPU support is limited to NVIDIA GPUs with compute capability 3.0 or higher. AMD and Intel GPUs are not officially supported.
How do I monitor GPU usage during deep learning training?
Use the training progress plot in MATLAB, which shows GPU usage if you set 'Plots' to 'training-progress' in trainingOptions. Alternatively, use nvidia-smi in another terminal.
Is there a way to log GPU usage over time?
Yes, you can use system('nvidia-smi --query-gpu=utilization.gpu --format=csv -l 1') in a loop to log data. Or use MATLAB's parfeval to run nvidia-smi periodically while your main code runs.
What does 'compute capability' mean and why does it matter?
Compute capability is a NVIDIA GPU architecture version. MATLAB requires 3.0 or higher. You can check it with gpuDevice. Higher is generally better for performance.