interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / gpu / nvidia / om / scan / scan_plan.om

167 lines · 4288 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note scan_plan.om: the launch plans of the CUDA++ scans and the radix sort (row C3 of docs/OMEGA.md). Ops 0..4
3note reproduce the C level drivers of src/omega_gpu_scan.c launch for launch (the C stays as the migration-pending
4note differential oracle, CUDAPP_MIGRATION_PENDING_C_SCAN). Each launch runs a kernel of om/scan/scan_ir.om.
5note op 0/1/2 reduce (sum/max/min) n f32 at inva, levels in scratch; 3 inclusive f32 prefix sum inva -> outva;
6note 4 the same over u32; 5 stable LSD radix sort of n u32 keys at inva (in place); 6 the same for key-value pairs
7note ([keys n | values n] at inva). 4-bit digits, 8 passes between inva and the scratch copy, ending in inva.
8note Plan words: 0 launches, 1 scratch words (u32) the plan uses from scrva, 2 the reduce result VA, 3 the shift of
9note the launch being written, 4 overflow; launch i at 16 + 12 i: kind, grid, then the 10 argument words
10note (in lo, in hi, out lo, out hi, tot lo, tot hi, len, tiles, warps, shift).
11note Returns 0 ok, 1 bad arguments, 2 n above 2^28 (or a plan overflow).
12
13const LO32 ulong 4294967295
14const MAXN ulong 268435456
15const MAXL ulong 169
16
17note ceil(len / 32): the 32-element tiles of len
18fn tiles nw ulong to ulong
19ret shr add nw 31 5
20end
21
22note CTAs of 256 threads (8 warps, one tile each per step), at most 1024
23fn grid nw ulong to ulong
24t = shr add tiles nw 7 3
25if eq t 0
26ret 1
27end
28if gt t 1024
29ret 1024
30end
31ret t
32end
33
34fn lau plan edit ulong 2048 k ulong in ulong ov ulong tot ulong nw ulong to void
35i = get plan 0
36if ge i MAXL
37put plan 4 1
38ret
39end
40b = add 16 mul i 12
41g = grid nw
42put plan b k
43put plan add b 1 g
44put plan add b 2 and in LO32
45put plan add b 3 shr in 32
46put plan add b 4 and ov LO32
47put plan add b 5 shr ov 32
48put plan add b 6 and tot LO32
49put plan add b 7 shr tot 32
50put plan add b 8 nw
51put plan add b 9 tiles nw
52put plan add b 10 mul g 8
53put plan add b 11 get plan 3
54put plan 0 add i 1
55end
56
57note one reduction level per launch, each writing its tile results after the last; plan 2 = the result VA
58fn pred plan edit ulong 2048 k ulong n ulong in ulong scr ulong to void
59ov = scr
60go = ulong 1
61w = ulong 0
62for i ulong 8
63if ne go 0
64lau plan k in ov 0 n
65n = tiles n
66in = ov
67ov = add ov mul n 4
68w = add w n
69if le n 1
70go = ulong 0
71end
72end
73end
74put plan 1 w
75put plan 2 in
76end
77
78note an inclusive scan of n words in -> out with the tile kernel tk and the carry kernel tk + 1; level totals from
79note sp; returns the scratch words used
80fn ppre plan edit ulong 2048 tk ulong n ulong in ulong ov ulong sp ulong to ulong
81buf = new ulong 9 0
82ln = new ulong 9 0
83put buf 0 ov
84put ln 0 n
85s0 = sp
86l = ulong 0
87for i ulong 8
88if eq get ln 8 0
89n = tiles get ln l
90lau plan tk in get buf l sp get ln l
91put buf add l 1 sp
92in = sp
93sp = add sp mul n 4
94if eq n 1
95put ln 8 1
96else
97put ln add l 1 n
98l = add l 1
99end
100end
101end
102for i ulong 8
103if gt l 0
104l = sub l 1
105lau plan add tk 1 get buf l get buf add l 1 0 get ln l
106end
107end
108ret shr sub sp s0 2
109end
110
111note radix sort: per pass the digit histogram (cnt[0] = 0, cnt[1 + dig * tiles + tile]), the in-place u32
112note scan of the 16 * tiles counts, then the stable scatter
113note one pass: ks = scatter kind | shift << 8; plan 1 = the sort's scratch words (the copy, cnt, the counts' scan)
114fn pas plan edit ulong 2048 ks ulong src ulong dst ulong cnt ulong n ulong to void
115t16 = mul tiles n 16
116put plan 3 shr ks 8
117lau plan 7 src 0 cnt n
118put plan 3 0
119w = ppre plan 5 t16 add cnt 4 add cnt 4 add cnt mul add t16 1 4
120put plan 3 shr ks 8
121lau plan and ks 255 src dst cnt n
122put plan 3 0
123w = add w add t16 1
124put plan 1 add w mul n sub and ks 255 7
125end
126
127fn psort plan edit ulong 2048 kv ulong n ulong a ulong scr ulong to void
128cnt = add scr mul mul n add kv 1 4
129for p ulong 8
130src = a
131dst = scr
132if ne and p 1 0
133src = scr
134dst = a
135end
136k = shl sub 28 mul p 4 8
137k = or k add 8 kv
138pas plan k src dst cnt n
139end
140end
141
142note ---- entry
143fn scan_plan op ulong n ulong inva ulong outva ulong scrva ulong plan edit ulong 2048 to ulong
144for i ulong 2048
145put plan i 0
146end
147if or gt op 6 eq n 0
148ret 1
149end
150if gt n MAXN
151ret 2
152end
153if le op 2
154pred plan op n inva scrva
155end
156if or eq op 3 eq op 4
157w = ppre plan add 3 mul sub op 3 2 n inva outva scrva
158put plan 1 w
159end
160if ge op 5
161psort plan sub op 5 n inva scrva
162end
163if ne get plan 4 0
164ret 2
165end
166ret 0
167end