Number of GPU devices present
returns the number of GPU
devices present in your local machine, as reported by the GPU device driver. All devices
reported by the driver are counted, including devices that are not supported in MATLAB® and devices that are not available for use in the current MATLAB session.
n = gpuDeviceCount
Count and Query GPU Devices
Determine the number of GPU devices available in your computer and their indices.
[n,indx] = gpuDeviceCount
n = 2 indx = 1 2
Query the properties of the GPUs using
ans = Index Name ComputeCapability DeviceAvailable DeviceSelected _____ __________________ _________________ _______________ ______________ 1 "TITAN RTX" "7.5" true false 2 "GeForce GTX 1080" "5.0" true true
Use Multiple GPUs in Parallel Pool
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
availableGPUs = gpuDeviceCount("available")
availableGPUs = 3
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.
spmd gpuDevice end
Use parallel language features, such as
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
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.
[availableGPUs,gpuIndx] = gpuDeviceCount("available")
availableGPUs = 3
gpuIndx = 1×3 1 2 3
Define the indices of the devices you want to use.
useGPUs = [1 3];
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).
spmd gpuDevice(useGPUs(spmdIndex)); end
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.
countMode — Device count mode
"all" (default) |
Device count mode, specified as one of the following:
"all"— Count all GPU devices reported by the GPU device driver. The count includes devices that are not supported in MATLAB and devices that are not available for use in the current MATLAB session.
"supported"— Count only GPU devices that are supported by the current version of MATLAB.
"available"— Count only GPU devices that are available for use in the current MATLAB session.
n — Number of GPU devices
Number of GPU devices, returned as a positive scalar.
indx — Indices of GPU devices
Indices of GPU devices, returned as a numeric vector. Each element of
indx is the device index of a counted GPU device. Use the device
index to select or query the GPU device using the
Introduced in R2010b