GPU computing

Move an array to the GPU, and your expressions run on the device.

Nelson’s gpu_engine module runs array computations on the GPU through WebGPU (Dawn). The same code runs on Windows, Linux, and macOS, over Vulkan, Metal, or Direct3D 12, with no proprietary CUDA dependency and no vendor lock-in.

gpuArray and gather

Move data to the device with gpuArray, compute, and bring the result back with gather:

A = single(rand(1000));
G = gpuArray(A);                 % copy the array to the device
R = gather(G .* G + sqrt(G));    % compute on the GPU, copy the result back
class(G)                         % gpuArray

Operators and many functions are overloaded, so expressions on a gpuArray run on the device and keep their result device-resident until you gather it.

Use the GPU when it is available

canUseGPU lets you write code that uses the GPU when present and falls back to the CPU otherwise:

if canUseGPU()
  A = gpuArray(single(rand(1000)));
else
  A = single(rand(1000));
end

It returns false on a machine without a supported Vulkan, Metal, or Direct3D 12 backend.

Inspect the device

gpuDevice returns a description of the selected device, and gpuDeviceCount reports how many compatible devices are present:

>> d = gpuDevice()

d =

  struct with fields:

            Index: 1
             Name: 'Intel(R) UHD Graphics 770'
           Vendor: 'intel'
     Architecture: 'gen-12lp'
          Backend: 'D3D12'
    MaxBufferSize: 2.1475e+09

What runs on the device

Call gpuArrayFunctions for the exact, always-current list.

Precision and supported types


Read the reference for gpuArray, gather, canUseGPU, gpuDevice, and gpuArrayFunctions.

Back to home