home/ atoms/ webgpu-compute-workgroups

WebGPU compute threads are grouped into workgroups and addressed by a global invocation id

A WebGPU compute shader runs as dispatched workgroups — blocks of threads that execute in parallel. Each thread gets built-in ids: local_invocation_id (its position within its workgroup), workgroup_id (which workgroup, shared by all its threads), global_invocation_id (a unique id per thread, equal to workgroup_id * workgroup_size + local_invocation_id), and local_invocation_index (that id linearized). The general advice is a workgroup size of 64, and the product of the workgroup dimensions may not exceed maxComputeInvocationsPerWorkgroup (256). Because threads run concurrently and can touch shared data, race conditions are inherent and synchronization must be designed explicitly.

Examples

dispatchWorkgroups(512, 1, 1) with @workgroup_size(64) launches 32,768 threads; global_invocation_id.x gives each thread a unique index 0-32767 to address one array element.

Assessment

Given @workgroup_size(64), compute global_invocation_id.x for local_invocation_id.x = 5 in workgroup 3. Explain why a race condition can occur when two threads write the same array position.

“global_invocation_id = workgroup_id * workgroup_size + local_invocation_id”