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: 6
            ToolkitVersion: 5.5000
        MaxThreadsPerBlock: 1024
          MaxShmemPerBlock: 49152
        MaxThreadBlockSize: [1024 1024 64]
               MaxGridSize: [2.1475e+09 65535 65535]
                 SIMDWidth: 32
               TotalMemory: 5.0330e+09
           AvailableMemory: 4.9185e+09
       MultiprocessorCount: 13
              ClockRateKHz: 705500
               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: 6
            ToolkitVersion: 5.5000
        MaxThreadsPerBlock: 1024
          MaxShmemPerBlock: 49152
        MaxThreadBlockSize: [1024 1024 64]
               MaxGridSize: [2.1475e+09 65535 65535]
                 SIMDWidth: 32
               TotalMemory: 5.0330e+09
           AvailableMemory: 4.9185e+09
       MultiprocessorCount: 13
              ClockRateKHz: 705500
               ComputeMode: 'Default'
      GPUOverlapsTransfers: 1
    KernelExecutionTimeout: 0
          CanMapHostMemory: 1
           DeviceSupported: 1
            DeviceSelected: 1


device = 

  CUDADevice with properties:

                      Name: 'Quadro 600'
                     Index: 2
         ComputeCapability: '2.1'
            SupportsDouble: 1
             DriverVersion: 6
            ToolkitVersion: 5.5000
        MaxThreadsPerBlock: 1024
          MaxShmemPerBlock: 49152
        MaxThreadBlockSize: [1024 1024 64]
               MaxGridSize: [65535 65535 65535]
                 SIMDWidth: 32
               TotalMemory: 1.0737e+09
           AvailableMemory: 771207168
       MultiprocessorCount: 2
              ClockRateKHz: 1280000
               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?