If you have access to several GPUs, you can perform your calculations on multiple GPUs in parallel using a parallel pool.
To determine the number of GPUs that are available for use in MATLAB, use the gpuDeviceCount
function.
Start a parallel pool with as many workers as available GPUs. For best performance, MATLAB assigns a different GPU to each worker by default.
Starting parallel pool (parpool) using the 'Processes' profile ...
Connected to the parallel pool (number of workers: 3).
To identify which GPU each worker is using, call gpuDevice
inside an spmd
block. The spmd
block runs gpuDevice
on every worker.
Use parallel language features, such as parfor
or parfeval
, to distribute your computations to workers in the parallel pool. If you use gpuArray
enabled functions in your computations, these functions run on the GPU of the worker. For more information, see Run MATLAB Functions on a GPU. For an example, see Run MATLAB Functions on Multiple GPUs.
When you are done with your computations, shut down the parallel pool. You can use the gcp
function to obtain the current parallel pool.
If you want to use a different choice of GPUs, then you can use gpuDevice
to select a particular GPU on each worker, using the GPU device index. You can obtain the index of each GPU device in your system using the gpuDeviceCount
function.
Suppose you have three GPUs available in your system, but you want to use only two for a computation. Obtain the indices of the devices.
Define the indices of the devices you want to use.
Start your parallel pool. Use an spmd
block and gpuDevice
to associate each worker with one of the GPUs you want to use, using the device index. The spmdIndex
function identifies the index of each worker.
Starting parallel pool (parpool) using the 'Processes' profile ...
Connected to the parallel pool (number of workers: 2).
As a best practice, and for best performance, assign a different GPU to each worker.
When you are done with your computations, shut down the parallel pool.