How can I use multiple GPUs concurrently from MATLAB?
5 views (last 30 days)
Show older comments
MathWorks Support Team
on 22 Nov 2016
Edited: MathWorks Support Team
on 19 Nov 2025 at 16:48
How can I use multiple GPUs concurrently, or at the same time, from MATLAB?
Accepted Answer
MathWorks Support Team
on 19 Nov 2025 at 0:00
Edited: MathWorks Support Team
on 19 Nov 2025 at 16:48
Simple Approach: 'gpuDevice(IDX)'
To use a specific Graphics Processing Unit (GPU) in MATLAB, use the 'gpuDevice(IDX)' form of the 'gpuDevice' function to explicitly select a GPU Device. This allows for using different GPUs for different MATLAB sessions. For example:
MATLAB Session A:
>> g = gpuDevice(1);
>> x = rand(100, 'gpuArray');
MATLAB Session B:
>> g = gpuDevice(2);
>> x = rand(100, 'gpuArray');
It is important to keep in mind that 'gpuDevice', called without an index, chooses the default GPU, and does not automatically select the GPU with the most available memory or the lowest computation load. For more information about 'gpuDevice', refer to the following link:
Parallel Approach: 'parpool'
Another alternative is to use a parallel pool of workers to execute the GPU code across multiple devices. When workers share a single machine with multiple GPUs, MATLAB automatically assigns each worker in a parallel pool to use a different GPU by default.
Using 'parfor':
parfor ix = 1:nIter
data = gpuArray.ones(1000, 1)
results(ix) = sum(data,"all");
end
Using 'spmd':
spmd
gd = gpuDevice;
idx = gd.Index;
disp(['Using GPU ',num2str(idx)]);
end
Refer to the following link for more information: Run MATLAB functions on multiple GPUs
Out-of-memory Errors
Out-of-memory errors can occur when multiple MATLAB programs are using the same GPU to perform calculations, unaware that they do not have exclusive access to the GPU's total amount of memory. The error might look like this:
Out of memory on device. To view more detail about available memory on the GPU, use 'gpuDevice()'. If the problem persists, reset the GPU by calling 'gpuDevice(2)'.
0 Comments
More Answers (0)
See Also
Categories
Find more on Parallel Computing Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!