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:
Function | Description |
---|---|
gpuDeviceCount | The number of GPU devices in your computer |
gpuDevice | Select 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
:
Class | Description |
---|---|
GPUDeviceManager | Manager 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.
This example shows how to identify and select a GPU for your computations.
Determine how many GPU devices are in your computer:
gpuDeviceCount
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.
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):
gpuDevice(2)
If you select a device that does not have sufficient compute capability, you are warned that you cannot use that device.
This example shows how to print out messages when the device is being selected or deselected:
First, ensure that you have no gpuDevice selected before you start:
gpuDevice([])
Deselecting: 1
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
Print out messages when the device is being deselected:
reset(mgr.SelectedDevice)
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.
Establish Arrays on a GPU | GPU Capabilities and Performance | Measure and Improve GPU Performance | Run Built-In Functions on a GPU