This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Identify and Select a GPU Device

If you have only one GPU in your computer, that GPU is the default. If you have more than one GPU device in your computer, you can use the following functions to identify and select which device you want to use:

gpuDeviceCountThe number of GPU devices in your computer
gpuDeviceSelect which device to use, or see which device is selected and view its properties

You can also find out whenever a GPU device is selected, deselected or reset, using GPUDeviceManager:

GPUDeviceManagerManager for GPU Devices

After you have identified and selected your GPU, you can use it, for example, to Run Built-In Functions on a GPU.

Example: Select a GPU

This example shows how to identify and select a GPU for your computations.

  1. Determine how many GPU devices are in your computer:

  2. With two devices, the first is the default. You can examine its properties to determine if that is the one you want to use:

    d = gpuDevice
    d =
      CUDADevice with properties:
                          Name: 'Tesla K20c'
                         Index: 1
             ComputeCapability: '3.5'
                SupportsDouble: 1
                 DriverVersion: 7.5000
                ToolkitVersion: 7.5000
            MaxThreadsPerBlock: 1024
              MaxShmemPerBlock: 49152
            MaxThreadBlockSize: [1024 1024 64]
                   MaxGridSize: [2.1475e+09 65535 65535]
                     SIMDWidth: 32
                   TotalMemory: 5.0330e+09
               AvailableMemory: 4.7858e+09
           MultiprocessorCount: 13
                  ClockRateKHz: 705500
                   ComputeMode: 'Default'
          GPUOverlapsTransfers: 1
        KernelExecutionTimeout: 0
              CanMapHostMemory: 1
               DeviceSupported: 1
                DeviceSelected: 1

    If this is the device you want to use, you can proceed.

  3. To use another device, call gpuDevice with the index of the other device, and view its properties to verify that it is the one you want. For example, this step chooses and views the second device (indexing is 1-based):



If you select a device that does not have sufficient compute capability, you are warned that you cannot use that device.

Example: Print message when a GPU is selected

This example shows how to print out messages when the device is being selected or deselected:

  1. First, ensure that you have no gpuDevice selected before you start:

        Deselecting: 1

  2. Print out messages when the device is being selected:

    mgr = parallel.gpu.GPUDeviceManager.instance();
    l1 = event.listener(mgr, 'DeviceSelected', @(src, data) fprintf('Selected: %d\n', data.DeviceIndex));
    l2 = event.listener(mgr, 'DeviceDeselecting', @(src, data) fprintf('Deselecting: %d\n', data.DeviceIndex));
    a1 = gpuArray(1:10);
        Selected: 1

  3. Print out messages when the device is being deselected:

        Deselecting: 1 
        Selected: 1


If you select a device that does not have sufficient compute capability, you are warned that you cannot use that device.

See Also

| | |

Was this topic helpful?