Main Content

gpuDeviceCount

Number of GPU devices present

    Description

    example

    n = gpuDeviceCount 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.

    example

    n = gpuDeviceCount(countMode) returns the number of GPU devices in your machine, counted according to countMode. Use this syntax to count only supported GPU devices, or count only devices that are available for use in this MATLAB session.

    example

    [n,indx] = gpuDeviceCount(___) also returns the indices of the counted GPU devices for any of the previous syntaxes. Use this syntax when you want to select or examine the counted GPU devices.

    Examples

    collapse all

    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 gpuDeviceTable.

    gpuDeviceTable
    ans = 
        Index        Name              ComputeCapability    DeviceAvailable    DeviceSelected
        _____    __________________    _________________    _______________    ______________
    
          1      "TITAN RTX"                 "7.5"               true              false     
          2      "GeForce GTX 1080"          "5.0"               true              true      
    
    

    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.

    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.

    parpool('local',availableGPUs);
    Starting parallel pool (parpool) using the 'local' 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 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.

    delete(gcp('nocreate'));

    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.

    [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 labindex function identifies the index of each worker.

    parpool('local',numel(useGPUs));
    Starting parallel pool (parpool) using the 'local' profile ...
    Connected to the parallel pool (number of workers: 2).
    
    spmd
        gpuDevice(useGPUs(labindex));
    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.

    delete(gcp('nocreate'));

    Input Arguments

    collapse all

    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.

    Example: "available"

    Data Types: char | string

    Output Arguments

    collapse all

    Number of GPU devices, returned as a positive scalar.

    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 gpuDevice function.

    Introduced in R2010b