The global thread index (Launch.index_ops, Launch.index_oneDim, Launch.index_inj, and on 2-D launches Launch.thread_inj, Launch.xy_inj, Launch.cell_inj, Launch.cell_apart; onto: Launch.index_onto, Launch.cell_onto, Launch.index_lt_mul)
Lean: proved, for every launch, thread number, dimension, width, flags, stride, base, cell width and predicate as stated below, in MojoProof/Sem/Index.lean. Instances and twins: MojoProofTest/Index.lean.
What the kernel computes
vector_addition's entry block computes its element index as
%12 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
%13 = zext i32 %12 to i64
%14 = call i32 @llvm.nvvm.read.ptx.sreg.ctaid.x()
%15 = zext i32 %14 to i64
%16 = call i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
%17 = sext i32 %16 to i64
%18 = mul i64 %15, %17
%19 = add i64 %18, %13
and, in block %21 after icmp slt i64 %19, %11 with %11 = sext i32 %3 to i64,
%22 = trunc i64 %19 to i32 and %23 = sext i32 %22 to i64. A call to a special register gives
the i32 word BitVec.ofNat 32 v of its value v = (L.env n).sreg r d (intrinsic,
MojoProof/Sem/Thread.lean),
and zext, sext, trunc, mul and add are intCast and intBin (MojoProof/Sem/Op.lean).
Statements
Write t, c, k for tid d, ctaid d, ntid d of thread n ((L.env n).sreg r d), and
[x]_w for BitVec.ofNat w x.
- Words of natural numbers, for every width and all flags
fl:truncandzexttow'of.bits w [k]_wwithk < 2^wis.ok (.bits w' [k]_w')(intCast_ofNat).sexttow'of.bits w [k]_wwith2k < 2^wis.ok (.bits w' [k]_w')(intCast_sext_ofNat).addof[a]_wand[b]_wwith2(a + b) < 2^wis.ok (.bits w [a + b]_w)(intBin_add_ofNat);mulwith2a,2b,2abbelow2^wis.ok (.bits w [ab]_w)(intBin_mul_ofNat).- For
w' ≤ wand2k < 2^w',trunctow'andsextback towof[k]_wgive[k]_w'and then[k]_w(intCast_trunc_sext). Withw = 64,w' = 32:%22,%23are the identity on indices below2^31, which the branch%19 < %11 < 2^31gives.
Launch.index_ops: on a valid launch, for everyn,d,fl,fl', the five operations%13,%15,%17,%18,%19give[t]_64,[c]_64,[k]_64,[ck]_64and[ck + t]_64.Launch.index_lt:ck + t < 2^42.Launch.index_oneDim: if the grid and the block have size 1 inyandz, then for everyn < volume L.grid * volume L.block(the threads ofGrid.start),ck + t = nin dimensionx.Launch.index_inj: on a valid launch, if[c₁k + t₁]_64 = [c₂k + t₂]_64for threadsn₁,n₂in dimensiond, thenc₁ = c₂andt₁ = t₂.
Proofs
- A word
[x]_wwithx < 2^whastoNat = x.setWidth w'keepsx mod 2^w', as does[x]_w', sotruncandzextagree with[x]_w'. If2x < 2^wthe top bit of[x]_wis clear (x < 2^(w-1), and forw = 0,x = 0), sosignExtendissetWidthandtoInt = x(BitVec.toInt_ofNat_of_lt). Foradd,[a]_w + [b]_w = [a + b]_w; thenuwtesta + b ≥ 2^wfails, and thenswtest asks whether the signed suma + blies outside[-2^(w-1), 2^(w-1)), which2(a + b) < 2^wexcludes (outS_of_lt).mulis the same with the product; itstoInts need2a, 2b < 2^w. The facts at2x < 2^ware the core'sGPUProof.WordFit(GPUProof/MixedRadix.lean, mixed-radix.md), proved once atx < 2^(w-1)(GPUProof.WordFit.of_lt, withGPUProof.WordFit.two_mul_lt_ifffor the premise):BitVec.toInt_ofNat_of_ltis its fieldtoInt,intCast_sext_ofNatits fieldsignExtend, and the two tests ofintBin_add_ofNatits fieldadd(no unsigned and no signed overflow);outS_of_ltisx < 2^(w-1).truncandzext(atx < 2^w, whichWordFitdoes not cover) andmulare proved here. - On a valid launch
Launch.sreg_boundgivest < maxBlock d ≤ 1024,c < maxGrid d ≤ 2^31 - 1andk < maxBlock d + 1 ≤ 1025, sock ≤ 2^31 · 1024 = 2^41andck + t < 2^42. Every premise of 1 holds withw = 64(andw = 32for the operands of the casts). Launch.envgivest = coord L.block (n mod B) x = (n mod B) mod B = n mod BwithB = L.block x = volume L.block,k = B, andc = coord L.grid (n / B) x = (n / B) mod GwithG = L.grid x = volume L.grid. Asn < G B,n / B < G, soc = n / B, andck + t = (n / B) B + n mod B = n. (B > 0becausen < G B.)- Both indices are below
2^42 < 2^64, so the words' equality is that of the numbers.k = Bfor both threads andt₁, t₂ < B(coord_lt), so dividingc₁B + t₁ = c₂B + t₂byBgivesc₁ = c₂, and thent₁ = t₂.
Instance and twin
- Instance: the launch of grid (2, 1, 1) and block (4, 1, 1), valid. For its 8 threads the
operations give
[n]_64by the theorems (index24) and by evaluation (eval24), and equal index words are one thread (inj24).truncandsextback are the identity on these indices (roundtrip24) and not at2^31(roundtrip_twin). - Twin: grid (1, 1, 1) and block (2, 2, 1), valid, 4 threads. Thread 2 has
tid = (0, 1), so itsck + tinxis 0, as is thread 0's (twin22): without the premise on the block, 3 fails, and the index is not injective on thread numbers (4 still holds:candt.xagree).
Two-dimensional launches
The naive matmul kernel computes a row and a column, each a global index:
%24 = ctaid.x * ntid.x + tid.x and %35 = ctaid.y * ntid.y + tid.y (each by the five
operations of 2, in x and in y). It reads row row of A and row col of B and writes
C[row, col] at element row * 50257 + col, under row < m and col < n. Its race freedom
needs: two in-bounds threads with one cell are one thread. Write Launch.index L n d for
ck + t of thread n in dimension d (the number whose i64 word 2 gives), row n for
Launch.index L n x and col n for Launch.index L n y.
Statements
coord_linear,coord_inj: for every boxdanda < volume d,a = coord d a x + d x * (coord d a y + d y * coord d a z); so two numbers below the volume with the same three coordinates are equal.Launch.env_inj: for every launch and threadsn,n'below the thread countvolume L.grid * volume L.block, equalctaidand equaltidin all three dimensions given = n'.Launch.thread_inj: on every valid launch, threads below the thread count whose index words[row]_64,[col]_64and[Launch.index L n z]_64are equal are one thread.Launch.xy_inj: if moreover the grid and the block have side 1 inz, equal words inxandysuffice.Launch.cell_inj: on every valid launch of side 1 inz, for every stridesand threadsn,n'below the thread count withcol n < sandcol n' < s,row n * s + col n = row n' * s + col n'givesn = n'.Launch.cell_apart: on the same launches, for every strides, baseb, cell widthwand predicatepon thread numbers such thatp ngivesnbelow the thread count andcol n < s: for distinctn,n'withp nandp n', the cellsb + (row n * s + col n) * wandb + (row n' * s + col n') * warewbytes apart (one ends before the other starts). Withw = 4these are the 4-byte cells of afloatmatrix with row strides, andp nis "threadnwrites":row n < Mandcol n < Nfor the kernel's sizesMandN ≤ s.coord_onto: for every boxdand every pointpwithp k < d kin each dimension, the numbera = p x + d x * (p y + d y * p z)is belowvolume dandcoord d a = p(the converse of 5).Launch.index_onto: for every launch and every pointrwithr d < L.grid d * L.block din each dimension, some threadnbelow the thread count hasLaunch.index L n d = r din every dimension.Launch.index_lt_mul: for every launch with positive sides ind, every thread'sLaunch.index L n dis belowL.grid d * L.block d, so the bounds of 11 are sharp.Launch.cell_onto: on every valid launch, for everyr < L.grid x * L.block xandc < L.grid y * L.block y, some threadnbelow the thread count hasrow n = randcol n = c(11 at the point(r, c, 0)). With side 1 inzthat thread is unique (7), so every cell(r, c)withr < Mandc < Nof a launch withM ≤ L.grid x * L.block xandN ≤ L.grid y * L.block yis written by exactly one thread. No premise onzis needed for existence: on a valid launchL.grid z * L.block z > 0.
Proofs
coord z = a / (d x · d y) mod d z = a / (d x · d y)asa < d x · d y · d z; anda / (d x · d y) = a / d x / d y, socoord y + d y · coord z = a / d xand thencoord x + d x · (a / d x) = a(Nat.mod_add_divtwice).- With
B = volume L.block > 0(asn < G B),tidiscoord L.block (n mod B)andctaidiscoord L.grid (n / B), withn mod B < Bandn / B < volume L.grid: by 5,n mod Bandn / Bare those ofn', andn = B (n / B) + n mod B. - 4 in each dimension gives equal
ctaid dandtid d; then 6. With side 1 inz,ctaid z = tid z = 0for every thread (a coordinate mod 1), so thezwords are equal. - If
row n < row n'thenrow n * s + s ≤ row n' * s(stride_lt), sorow n * s + col n < row n' * s ≤ row n' * s + col n'ascol n < s; symmetrically, so the rows are equal, then the columns, and 7 (Launch.xy_inj) givesn = n'. apart_stride b(w ≤ w) at the two cell numbers, which differ by 8.- With
Y = p y + d y * p z:a = p x + d x Yis the mixed-radix value of the digitsp x,p y,p zwith radicesd x,d y,d z, soa < d x d y d zis the core'sGPUProof.MixedRadix.lt_prodat those three digits.a mod d x = p x(asp x < d x),a / d x = YandY mod d y = p y, anda / (d x d y) = Y / d y = p zwithp z mod d z = p z. - Every side is positive, as
r d < L.grid d * L.block d. Take the block pointb d = r d / L.block d < L.grid dand the thread pointt d = r d mod L.block d < L.block d, their numbersB < volume L.gridandT < volume L.blockby 10, andn = B · volume L.block + T, which is belowvolume L.grid * volume L.block. Thenn / volume L.block = Bandn mod volume L.block = T, soctaid d = b dandtid d = t dby 10, andctaid d * ntid d + tid d = (r d / L.block d) L.block d + r d mod L.block d = r d. ForLaunch.index_lt_mul:ctaid d < L.grid dandtid d < L.block d(coord_lt), soctaid d * L.block d + tid d < (ctaid d + 1) L.block d ≤ L.grid d * L.block d. - 11 at
r x = r,r y = c,r z = 0 < L.grid z * L.block z(Launch.Valid).
Instances and twins
- Instance: grid (2, 2, 1) and block (2, 2, 1) (
launch2 2 2 2 2), valid, 16 threads. The cellsrow * 4 + colof its 16 threads are0, ..., 15each once (cells2222, by evaluation), andLaunch.cell_apartgives the 4-byte cells at stride 4 apart on all of them (apart2222); equalxandyindex words are one thread (inj2222,Launch.xy_inj). - Twin of the
zpremise: grid (1, 1, 1) and block (1, 1, 2) (L112), valid, 2 threads withrow = col = 0and differenttid z(twin112):Launch.xy_injneeds side 1 inz. - Twin of
col < s: onlaunch2 2 2 2 2with stride 1, thread 1 (row 1, column 0) and thread 2 (row 0, column 1) have cell 1 (twin_stride):Launch.cell_injneeds the columns below the stride. - Instance of 12: on
launch2 2 2 2 2, every cell(r, c)withr, c < 4is some thread's below 16 (onto2222,Launch.cell_onto); the thread of cell(3, 2)is the proof'sB · volume L.block + Twith block point(1, 1, 0),B = 3, and thread point(1, 0, 0),T = 1: thread 13 (cell32, by evaluation). - Twin of the bound: no thread of
launch2 2 2 2 2, below 16 or not, has row 4 or column 4 (twin_onto,Launch.index_lt_mul), soLaunch.cell_ontoneedsr < L.grid x * L.block xandc < L.grid y * L.block y.
Use
own of T2′ for vector_addition is the global index. On a launch whose grid and block have
size 1 in y and z, the index of each thread below volume L.grid * volume L.block is its
thread number (3, Launch.index_oneDim), so two such threads with one index are one thread. 4
(Launch.index_inj) does not give this: it gives equal ctaid d and tid d in the one
dimension d only, and on the 2-D block of twin22 threads 0 and 2 have one index. The
ownership argument therefore needs the premises of 3 (a one-dimensional grid and block, and
threads in range) and goes through Launch.index_oneDim. On a 2-D launch of side 1 in z (the
naive matmul's), the pair of words of 7 is the thread (Launch.xy_inj), and the output cells of
the threads that write are apart (Launch.cell_apart, 9). Conversely, every cell (r, c) of an
M × N output with M ≤ L.grid x * L.block x and N ≤ L.grid y * L.block y is some thread's
(Launch.cell_onto, 12), so a kernel whose thread writes its cell when row < M and col < N
writes every cell of the output, each once. The block checker's
SNode.eval of the .cast and .bin nodes is lanes of intCast and intBin, so its terms
meet 1 and 2 by rewriting.