Source: ViewEdit

GPU compute

GPU compute uses a graphics card’s thousands of parallel execution lanes to transform arrays, multiply matrices, run physics steps, or evaluate neural networks — with no window, no triangles, and no pixels involved. If you have tried to figure out why one project requires NVIDIA’s CUDA toolkit, another mentions AMD’s ROCm and HIP, and a browser demo runs parallel math in WebGPU or Vulkan compute, the confusion comes from mixing up the job with the platform. Every one of those doors runs the same kind of parallel program on device memory; they differ in hardware reach, setup ceremony, and the math libraries shipped beside them.

For how compute sits beside frame rendering and hardware video decode on the same card, start at GPU programming. For drawing pixels and render passes, read Graphics programming.

One execution model under four names

Regardless of which API you choose, a GPU compute program follows the same four-step pattern:

  1. Allocate GPU memory. Reserve a buffer through the GPU API (cudaMalloc, hipMalloc, device.createBuffer, or vkCreateBuffer plus vkAllocateMemory). That buffer is a GPU-managed allocation, not CPU process mmap, even on unified-memory machines where synchronization rules still separate CPU and GPU access.

  2. Move inputs in. Copy your data across the bus to the device buffer (or write into a mapped host-visible buffer before syncing).

  3. Dispatch a grid of lanes. Instead of writing a CPU for loop from 0 to n - 1, you write the body of one iteration — called a kernel (in CUDA/HIP) or a compute shader (in WebGPU/Vulkan) — and launch enough parallel workgroups (called blocks in CUDA/HIP) to cover n elements. Each lane asks for its global index i, exits immediately if i >= n, and operates on data[i].

  4. Synchronize and read results back. Wait for the GPU queue to finish and copy or map the output buffer back to CPU memory.

Your program often calls a translator, which calls a target API, which calls a vendor driver, which drives the GPU

One add-one job across four doors

To make the shared model concrete, consider the smallest useful GPU compute task: take an array of 128 unsigned 32-bit integers (0, 1, …​, 127), launch 2 workgroups of 64 lanes each, and add 1 to every element in place.

CUDA (NVIDIA)

__global__ void add_one(unsigned* data, int n) {
  int i = blockIdx.x * blockDim.x + threadIdx.x;
  if (i < n) data[i] += 1u;
}

// Launch 2 blocks of 64 threads:
add_one<<<(n + 63) / 64, 64>>>(device, n);

HIP (AMD ROCm, or NVIDIA via HIP)

__global__ void add_one(unsigned* data, int n) {
  int i = blockIdx.x * blockDim.x + threadIdx.x;
  if (i < n) data[i] += 1u;
}

// Identical kernel and launch syntax; runtime calls use hipMalloc / hipMemcpy:
add_one<<<(n + 63) / 64, 64>>>(device, n);

WebGPU compute (WGSL)

@group(0) @binding(0) var<storage, read_write> data: array<u32>;

@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) id: vec3<u32>) {
  let i = id.x;
  if (i >= arrayLength(&data)) { return; }
  data[i] = data[i] + 1u;
}

Host dispatch in JavaScript (or native Rust wgpu):

pass.dispatchWorkgroups(Math.ceil(n / 64));

Vulkan compute (GLSL compiled to SPIR-V)

#version 450
layout(local_size_x = 64) in;
layout(std430, set = 0, binding = 0) buffer Data {
  uint values[];
};

void main() {
  uint i = gl_GlobalInvocationID.x;
  if (i >= values.length()) return;
  values[i] = values[i] + 1u;
}

Host dispatch in C:

vkCmdDispatch(cmd, (n + 63) / 64, 1, 1);

The vocabulary shifts slightly from door to door, while the arithmetic and indexing stay identical:

Concept CUDA / HIP WebGPU (WGSL) Vulkan (GLSL)

Program unit

global kernel

@compute entry point

Compute shader (.comp → SPIR-V)

Group of lanes

Thread block (blockDim.x)

Workgroup (@workgroup_size(64))

Workgroup (layout(local_size_x = 64))

Global index

blockIdx.x * blockDim.x + threadIdx.x

@builtin(global_invocation_id).x

gl_GlobalInvocationID.x

Launch call

kernel<threads>(…​)

pass.dispatchWorkgroups(groups)

vkCmdDispatch(cmd, groups, 1, 1)

Dedicated compute platforms: CUDA, ROCm/HIP, OpenCL, and SYCL

Dedicated compute platforms exist for workloads where math throughput and tuned numerical libraries matter more than browser reach or rendering integration.

CUDA

CUDA is NVIDIA’s compute platform — not merely a language extension, but a full vertical stack comprising a C++ kernel dialect, the nvcc compiler, a runtime and driver interface, profilers, and heavily optimized libraries such as cuBLAS and cuDNN. CUDA runs exclusively on NVIDIA GPUs, including embedded NVIDIA Jetson boards. An AMD or Intel GPU cannot execute a compiled CUDA binary.

One common source of setup confusion is the split between the driver and the toolkit: nvidia-smi succeeding means the NVIDIA driver is installed and can run prebuilt CUDA binaries, whereas compiling .cu files requires the separate CUDA toolkit (nvcc) at a version compatible with that driver. CUDA can also interoperate with OpenGL, Vulkan, or Direct3D to share buffers with a graphics renderer, though the compute and graphics contexts still synchronize and take turns on the card.

ROCm and HIP

ROCm is AMD’s compute platform: kernel/driver components, runtime, and vendor math libraries (such as rocBLAS). HIP is the C++ programming model that sits on top of ROCm and mirrors CUDA’s syntax and runtime API names on purpose. Moving a CUDA file to HIP is a source-level port — often accelerated by hipify tools — that you compile with hipcc.

hipcc builds native AMD binaries on ROCm, and it can also target NVIDIA hardware when pointed at a CUDA installation. Even then, portability happens at the source level: you compile HIP source for each target rather than running a prebuilt NVIDIA .cubin on an AMD card. ROCm is a normal, first-class setup on Linux. On Windows, treat ROCm/HIP as available on a machine only after rocminfo confirms a GPU agent there; otherwise reach for Vulkan compute or WebGPU, both of which talk to AMD cards on Windows without ROCm.

OpenCL and SYCL

OpenCL is Khronos’s older cross-vendor compute API, still present in some drivers and scientific tools, though many modern cross-vendor stacks now prefer Vulkan compute or WebGPU. SYCL (with Intel’s oneAPI implementation) offers a single-source C++ kernel model targeting CPUs, GPUs, and accelerators across several backends. Neither OpenCL nor SYCL is a triangle-drawing API.

Compute shaders: WebGPU compute and Vulkan compute

When you want GPU math across multiple GPU vendors without requiring users to install CUDA or ROCm — or when your workload already lives inside a browser or graphics engine — you reach for compute shaders in an explicit GPU API. Direct3D 12 and Metal also provide compute shaders on Windows and Apple respectively, while WebGPU and Vulkan are the two cross-vendor entry points.

WebGPU compute

WebGPU makes general GPU compute a first-class capability inside modern web browsers and in native applications built on wgpu or Dawn. Unlike WebGL 2, which has no compute stage at all, WebGPU provides dedicated compute pipelines (createComputePipeline), read-write storage buffers (GPUBufferUsage.STORAGE), explicit bind groups, and workgroup dispatches written in WGSL.

In a browser, Chrome and Edge implement WebGPU with Dawn, while Firefox and native Rust applications use wgpu. Both translate your WGSL compute shaders and command encoders into Direct3D 12 on Windows, Metal on Apple devices, or Vulkan on Linux, Android, and Windows. A user opens a page and runs GPU compute without installing a single vendor SDK.

WebGPU enforces explicit buffer roles. A storage buffer that your compute shader writes to cannot simply be mapped directly into JavaScript memory while the GPU uses it; to read results back on the CPU, you copy from the storage buffer into a separate staging buffer created with COPY_DST | MAP_READ, submit the command buffer, and await readback.mapAsync(GPUMapMode.READ). That extra copy reflects real discrete-GPU memory separation rather than an arbitrary browser quirk.

Keep the runtime boundary in mind: navigator.gpu exists in a browser page, not in stock Node. Node has no built-in WebGPU (nor WebGL or WebCodecs); running WebGPU compute outside a browser means using a native host such as Rust’s wgpu, C++ Dawn, or native bindings rather than expecting browser globals in Node.

Vulkan compute

Vulkan compute gives native C, C++, Rust, or D applications direct, cross-vendor access to compute queues on Windows, Linux, and Android (and on Apple platforms via MoltenVK). Because Vulkan splits queues by capability, a pure compute program does not need a window, a swapchain, or a fragment shader — it picks a physical device with a VK_QUEUE_COMPUTE_BIT queue family and dispatches directly to it.

Unlike WebGPU, where the implementation compiles WGSL text at runtime, Vulkan consumes SPIR-V binary modules compiled ahead of time from GLSL or HLSL (for example with glslangValidator -V). The trade-off is explicitness: even for the eight-line add_one shader, the host program must create an instance, select a device and queue family, pick a memory type, bind descriptors, build a compute pipeline, record a pipeline barrier (VK_ACCESS_SHADER_WRITE_BIT to VK_ACCESS_HOST_READ_BIT), and submit. You pay that setup cost once and gain predictable, cross-vendor native execution without a vendor-locked compute SDK.

Choosing between a compute platform and a compute shader

The practical split is libraries versus reach:

  • Choose CUDA (or HIP on ROCm) when you control the target hardware and depend on mature, vendor-tuned ecosystems — dense linear algebra, deep-learning kernels, distributed multi-GPU collectives, and profiling suites like cuBLAS, cuDNN, or rocBLAS.

  • Choose WebGPU compute when your code runs in a browser, needs zero-install cross-platform portability across Windows, macOS, and Linux, or shares buffers directly with a WebGPU renderer (including native wgpu apps such as compute-first 2D renderers).

  • Choose Vulkan compute when you are shipping native cross-vendor software that already uses Vulkan or wants explicit native compute across NVIDIA, AMD, and Intel without requiring CUDA or ROCm toolkits.

Compute shaders give you the parallel dispatch everywhere; they do not automatically include cuBLAS or rocBLAS unless you write the kernels or bring a portable library built on top of them.

Try the add-one kernel yourself

Every door in this article has a working companion page running the same 128-element add-one job: