Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 additions & 5 deletions docs/gpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@ across launches, and a `GpuArray` lives there for as long as the program keeps i

- It is a function marked `@kernel`, whose first parameter is the thread's index (`int`). It returns nothing,
or, for `gpuReduce`, the number to add up.
- Its other parameters are arrays of numbers (`number[]`, `float[]`, `int[]`), typed arrays (below) or plain
numbers (`number`, `float`, `int`).
- Its other parameters are arrays of numbers (`number[]`, `float[]`, `int[]`, `boolean[]`), arrays of tuples and
object types of numbers (below), typed arrays (below) or plain numbers (`number`, `float`, `int`).
- An array of a tuple or an object type, `[number, int][]` or `{ x: number, y: number }[]`, holds its elements by
value, one after another, and the GPU has them so too: a kernel reads and writes their fields (`ps[i].x`,
`ts[i][0]`), a nested object's (`bodies[i].pos.x`), or a whole element (`ps[i] = { x: 0, y: 1 }`). Their fields
are numbers, integers, floats, booleans and such tuples. A launch, `gpuReduce`, `gpuBatch` and a `GpuArray`
take them as they take arrays of numbers. A field that is a string, an array or a class's object is a pointer
into the CPU's memory, so such an element is an error (`a @kernel parameter is a number, or an array of
numbers or of tuples and object types of numbers`). A class's objects are not taken either: an array of them
is of pointers. `tslang/test/gpu/gpu_structs.ts` steps 10000 particles on the GPU and on the CPU alike, and
checks fields of every size with the padding between them.
- A typed array (`Float32Array`, `Float64Array`, `Int8Array` to `Int32Array`, `Uint8Array` to `Uint32Array`,
`BigInt64Array`, `BigUint64Array`) is given where the kernel takes it, `a: Float32Array`, or takes the array of
its elements, `a: float[]` (a `Float64Array` for a `number[]` too). In the kernel it is that array: `a[i]`,
Expand Down Expand Up @@ -247,6 +256,46 @@ gpuLaunch(matmul, [p, m], a, b, c, m, k, p);
- `tslang/test/gpu/gpu_blocks.ts` checks a block sum in blocks of 128, a 70 x 90 tiled matrix product (not a
multiple of 16), a 4 x 4 x 4 block, and blocks of 64 and 512 sharing a function, against the CPU.

## Threads that run together: warps

The GPU runs a block's threads in warps, groups whose lanes run each step together and can read each other's
values directly, without shared memory or a barrier: a warp's sum, a vote, a prefix sum.

- `gpuWarpSize()`: the warp's lanes. 32 on NVIDIA's GPUs and on AMD's from gfx10 on (RDNA; 64 on the ones before,
as gfx906); on Intel's the driver picks 8, 16 or 32 for each kernel. Use it rather than a number.
- `gpuLaneIndex()`: the thread's lane, 0 to `gpuWarpSize() - 1`. In a block of one dimension the warps are the
block's threads in order: lane `gpuThreadIndex() % gpuWarpSize()`.
- `gpuShuffle(v, lane)`: `v` of the warp's lane `lane` (modulo the warp's size). `gpuShuffleDown(v, d)`: of the lane
`d` above, `gpuShuffleUp(v, d)` of the lane `d` below, `gpuShuffleXor(v, m)` of lane `^ m`; a thread whose lane
would be outside the warp gets its own `v`. `v` is a number of 4 or 8 bytes (`number`, `float`, `int`, ...),
as CUDA's `__shfl_sync`, `__shfl_down_sync`, `__shfl_up_sync`, `__shfl_xor_sync`.
- `gpuBallot(p)`: an `int` with a bit for each lane `p` is true for (lane 0 the lowest). `gpuAll(p)`, `gpuAny(p)`:
whether `p` is true for all, or any, of the warp's lanes.

```ts
// each warp's sum, which its lane 0 writes
@kernel
function warpSums(i: int, n: int, a: number[], sums: number[]) {
let v = i < n ? a[i] : 0;
for (let d = gpuWarpSize() >> 1; d > 0; d = d >> 1) v += gpuShuffleDown(v, d);
if (gpuLaneIndex() == 0) sums[i] = v;
}
```

- The lanes have to run together, so a kernel that uses these runs as one that waits at `gpuBarrier` does (above):
on every thread of its blocks, past `n` too, with its array accesses guarded. A thread past `n` is in no vote
(it is no bit of a ballot, no `gpuAny`'s true, no `gpuAll`'s false), but what it gives a shuffle is its own
`v`: so `warpSums` gives 0 past `n` itself (`i < n ? a[i] : 0`).
- A shuffle reads the lanes that run with it: in an `if` only some lanes take, a lane that does not is not there
to read (on NVIDIA its value is undefined). `gpuShuffleXor(v, 2)` among the even lanes is fine; reading an odd
lane from there is not.
- Its block is of whole warps, a multiple of 32 threads (`@kernel({ block: 100 })` is an error); `gpuReduce` does
not take it, and a call of it on the CPU is an error, as for a barrier kernel. On AMD's GPUs before gfx10
`gpuBallot` is an error: their 64 lanes do not fit in an `int`.
- `tslang/test/gpu/gpu_warp.ts` checks each of them on 1000 threads (the last warp partly past `n`), with arrays
longer than that; `gpu_scan.ts` is a prefix sum of 100000 values: each warp's of `gpuShuffleUp`, each block's of
its warps' totals in shared memory, then the blocks' (on the RTX 5080: warps of 32).

## Combining what the threads find: `Atomics`

Threads that write the same element would overwrite each other. `Atomics.add(a, i, v)` and the rest, given a
Expand Down Expand Up @@ -497,13 +546,12 @@ Arithmetic on `number`s brings 64-bit floats back: `Math.sqrt(v) + 1` is a `numb
## What has been run

The NVIDIA path runs on an RTX 5080, as executables and under the JIT, on Windows and on Linux (Ubuntu under WSL2,
whose CUDA driver is the Windows one's: the running tests below up to `gpu_call.ts` pass there too; those after it
have been run on Windows only so far). The AMD and Intel ones are checked as
whose CUDA driver is the Windows one's: the running tests below pass there too). The AMD and Intel ones are checked as
far as their compiled code goes (the AMD code object's kernel table, the SPIR-V by `spirv-val`): neither has
been run on its GPU yet. The test suite compiles a kernel for every GPU there is a backend for, and checks the
errors (`tslang/test/gpu`). Running ones, which need a GPU: `tslang/test/gpu/gpu_add_arrays.ts`,
`gpu_saxpy_table.ts`, `gpu_add_ints.ts`, `gpu_math.ts`, `gpu_copy_back.ts`, `gpu_batch.ts`, `gpu_float.ts`,
`gpu_atomics.ts`, `gpu_reduce.ts`, `gpu_batch_temporaries.ts`, `gpu_math_library.ts`, `gpu_shared.ts`, `gpu_grid.ts`, `gpu_call.ts`, `gpu_min_max.ts`, `gpu_array.ts`, `gpu_array_collect.ts`, `gpu_async.ts`, `gpu_barrier_guard.ts`, `gpu_blocks.ts`, `gpu_typed_arrays.ts`.
`gpu_atomics.ts`, `gpu_reduce.ts`, `gpu_batch_temporaries.ts`, `gpu_math_library.ts`, `gpu_shared.ts`, `gpu_grid.ts`, `gpu_call.ts`, `gpu_min_max.ts`, `gpu_array.ts`, `gpu_array_collect.ts`, `gpu_async.ts`, `gpu_barrier_guard.ts`, `gpu_blocks.ts`, `gpu_typed_arrays.ts`, `gpu_warp.ts`, `gpu_scan.ts`, `gpu_structs.ts`.

## Not yet

Expand Down
4 changes: 2 additions & 2 deletions tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ class MLIRCustomMethods
static std::map<std::string, bool> m {
{"print", true}, {"convertf", true}, {"assert", true}, {"parseInt", true}, {"parseFloat", true}, {"isNaN", true}, {"sizeof", true}, {GENERATOR_SWITCHSTATE, true},
{"LoadLibraryPermanently", true}, { "SearchForAddressOfSymbol", true }, { "LoadReference", true }, { "ReferenceOf", true }, { "Deref", true }, { "Ref", true },
{"atomicrmw", true}, {"cmpxchg", true}, {"atomicload", true}, {"atomicstore", true}, {"fence", true}, {"inline_asm", true}, {"call_intrinsic", true}, {"linker_options", true}, {"gpuLaunch", true}, {"gpuReduce", true}, {"gpuBatch", true}, {"gpuThreadIndex", true}, {"gpuBlockIndex", true}, {"gpuBlockSize", true}, {"gpuBarrier", true}, {"gpuShared", true}, {"gpuArray", true}, {"gpuRead", true}, {"gpuWrite", true}, {"gpuFree", true}
{"atomicrmw", true}, {"cmpxchg", true}, {"atomicload", true}, {"atomicstore", true}, {"fence", true}, {"inline_asm", true}, {"call_intrinsic", true}, {"linker_options", true}, {"gpuLaunch", true}, {"gpuReduce", true}, {"gpuBatch", true}, {"gpuThreadIndex", true}, {"gpuBlockIndex", true}, {"gpuBlockSize", true}, {"gpuBarrier", true}, {"gpuShared", true}, {"gpuArray", true}, {"gpuRead", true}, {"gpuWrite", true}, {"gpuFree", true}, {"gpuLaneIndex", true}, {"gpuWarpSize", true}, {"gpuShuffle", true}, {"gpuShuffleDown", true}, {"gpuShuffleUp", true}, {"gpuShuffleXor", true}, {"gpuBallot", true}, {"gpuAll", true}, {"gpuAny", true}
};
return m[functionName.str()];
}
Expand All @@ -430,7 +430,7 @@ class MLIRCustomMethods
static std::map<std::string, bool> m {
{"print", true}, {"convertf", true}, {"assert", true}, {"sizeof", true}, {GENERATOR_SWITCHSTATE, true},
{"LoadLibraryPermanently", true}, { "SearchForAddressOfSymbol", true }, { "LoadReference", true }, { "ReferenceOf", true }, { "Deref", true }, { "Ref", true },
{"atomicrmw", true}, {"cmpxchg", true}, {"atomicload", true}, {"atomicstore", true}, {"fence", true}, {"inline_asm", true}, {"call_intrinsic", true}, {"linker_options", true}, {"gpuLaunch", true}, {"gpuReduce", true}, {"gpuBatch", true}, {"gpuThreadIndex", true}, {"gpuBlockIndex", true}, {"gpuBlockSize", true}, {"gpuBarrier", true}, {"gpuShared", true}, {"gpuArray", true}, {"gpuRead", true}, {"gpuWrite", true}, {"gpuFree", true}
{"atomicrmw", true}, {"cmpxchg", true}, {"atomicload", true}, {"atomicstore", true}, {"fence", true}, {"inline_asm", true}, {"call_intrinsic", true}, {"linker_options", true}, {"gpuLaunch", true}, {"gpuReduce", true}, {"gpuBatch", true}, {"gpuThreadIndex", true}, {"gpuBlockIndex", true}, {"gpuBlockSize", true}, {"gpuBarrier", true}, {"gpuShared", true}, {"gpuArray", true}, {"gpuRead", true}, {"gpuWrite", true}, {"gpuFree", true}, {"gpuLaneIndex", true}, {"gpuWarpSize", true}, {"gpuShuffle", true}, {"gpuShuffleDown", true}, {"gpuShuffleUp", true}, {"gpuShuffleXor", true}, {"gpuBallot", true}, {"gpuAll", true}, {"gpuAny", true}
};
return m[functionName.str()];
}
Expand Down
Loading
Loading