Documentation Center

  • Trial Software
  • Product Updates

Selecting a GPU Device and Examining its Properties

This example shows how to find out the number of CUDA devices in your machine, how to choose which device MATLAB® uses, and how to query the properties of the currently selected device.

%#ok<*NOPTS>: disable code analyzer printing warning

Number of Devices

The function gpuDeviceCount returns the number of CUDA devices in your machine:

numDevices = gpuDeviceCount
origDevice = gpuDevice
numDevices =

     2


origDevice = 

  CUDADevice with properties:

                      Name: 'Tesla K20c'
                     Index: 1
         ComputeCapability: '3.5'
            SupportsDouble: 1
             DriverVersion: 5
            ToolkitVersion: 5
        MaxThreadsPerBlock: 1024
          MaxShmemPerBlock: 49152
        MaxThreadBlockSize: [1024 1024 64]
               MaxGridSize: [2.1475e+09 65535 65535]
                 SIMDWidth: 32
               TotalMemory: 5.0327e+09
                FreeMemory: 4.9026e+09
       MultiprocessorCount: 13
              ClockRateKHz: 614500
               ComputeMode: 'Default'
      GPUOverlapsTransfers: 1
    KernelExecutionTimeout: 0
          CanMapHostMemory: 1
           DeviceSupported: 1
            DeviceSelected: 1

Selecting and Querying Devices

Use the gpuDevice function with no inputs to return an object that represent the current device. Use the gpuDevice function with a single integer input to select a device with that device index. Note that device indices are one-based, which is different from the CUDA API. gpuDevice always returns an object representing the selected device. Not all devices are supported, in which case the DeviceSupported property is false, and the memory properties are not available.

%Ignore warnings about unsupported devices
warnState = warning( 'off', 'parallel:gpu:device:DeviceCapability' );

for idx = 1:numDevices
    device = gpuDevice( idx )
end
device = 

  CUDADevice with properties:

                      Name: 'Tesla K20c'
                     Index: 1
         ComputeCapability: '3.5'
            SupportsDouble: 1
             DriverVersion: 5
            ToolkitVersion: 5
        MaxThreadsPerBlock: 1024
          MaxShmemPerBlock: 49152
        MaxThreadBlockSize: [1024 1024 64]
               MaxGridSize: [2.1475e+09 65535 65535]
                 SIMDWidth: 32
               TotalMemory: 5.0327e+09
                FreeMemory: 4.9026e+09
       MultiprocessorCount: 13
              ClockRateKHz: 614500
               ComputeMode: 'Default'
      GPUOverlapsTransfers: 1
    KernelExecutionTimeout: 0
          CanMapHostMemory: 1
           DeviceSupported: 1
            DeviceSelected: 1


device = 

  CUDADevice with properties:

                      Name: 'Tesla C2070'
                     Index: 2
         ComputeCapability: '2.0'
            SupportsDouble: 1
             DriverVersion: 5
            ToolkitVersion: 5
        MaxThreadsPerBlock: 1024
          MaxShmemPerBlock: 49152
        MaxThreadBlockSize: [1024 1024 64]
               MaxGridSize: [65535 65535 65535]
                 SIMDWidth: 32
               TotalMemory: 5.6363e+09
                FreeMemory: 5.5202e+09
       MultiprocessorCount: 14
              ClockRateKHz: 1147000
               ComputeMode: 'Default'
      GPUOverlapsTransfers: 1
    KernelExecutionTimeout: 1
          CanMapHostMemory: 1
           DeviceSupported: 1
            DeviceSelected: 1

Reset to the Original Device

We use the original properties to revert to the original device.

gpuDevice( origDevice.Index );

revert warning state

warning( warnState );
Was this topic helpful?