识别并选择 GPU 设备
此示例显示如何使用 gpuDevice
来识别和选择您想要使用的设备。
要确定您的计算机中有多少个可用的 GPU 设备,请使用 gpuDeviceCount
函数。
gpuDeviceCount("available")
ans = 2
当有多个设备时,第一个是默认的。您可以使用 gpuDeviceTable
函数检查其属性,以确定它是否是您想要使用的函数。
gpuDeviceTable
ans=2×5 table
Index Name ComputeCapability DeviceAvailable DeviceSelected
_____ __________________ _________________ _______________ ______________
1 "NVIDIA RTX A5000" "8.6" true false
2 "Quadro P620" "6.1" true false
如果第一个设备是您想要使用的设备,那么您可以继续。要在 GPU 上运行计算,请使用启用 gpuArray
的函数。有关详细信息,请参阅在 GPU 上运行 MATLAB 函数。
要验证 MATLAB® 是否可以使用您的 GPU,请使用 canUseGPU
函数。如果有可用于计算的 GPU,则该函数返回 1
(true
),否则返回 0
(false
)。
canUseGPU
ans = logical
1
要诊断 GPU 设置的问题,例如如果 canUseGPU
返回 0
(false
),请使用 validateGPU
函数。验证您的 GPU 是可选的。
validateGPU
# Beginning GPU validation # Performing system validation # CUDA-supported platform .................................................PASSED # CUDA-enabled graphics driver exists .....................................PASSED # Version: 537.70 # CUDA-enabled graphics driver load .......................................PASSED # CUDA environment variables ..............................................PASSED # CUDA device count .......................................................PASSED # Found 2 devices. # GPU libraries load ......................................................PASSED # # Performing device validation for device index 1 # Device exists ...........................................................PASSED # NVIDIA RTX A5000 # Device supported ........................................................PASSED # Device available ........................................................PASSED # Device is in 'Default' compute mode. # Device selectable .......................................................PASSED # Device memory allocation ................................................PASSED # Device kernel launch ....................................................PASSED # # Finished GPU validation with no failures.
要使用另一个设备,请使用另一个设备的索引调用gpuDevice
。
gpuDevice(2)
ans = CUDADevice with properties: Name: 'Quadro P620' Index: 2 (of 2) ComputeCapability: '6.1' DriverModel: 'WDDM' TotalMemory: 2147352576 (2.15 GB) AvailableMemory: 1596066816 (1.60 GB) DeviceAvailable: true DeviceSelected: true Show all properties.
或者,您可以确定有多少个可用的 GPU 设备,检查它们的一些属性,然后从 MATLAB® 桌面中选择要使用的设备。在主页选项卡上的环境区域中,选择并行 > 选择 GPU 环境。
另请参阅
gpuDevice
| gpuDeviceCount
| gpuArray
| gpuDeviceTable
| canUseGPU
| validateGPU