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 / runtime / scanref.om

270 lines · 6169 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note gpu/runtime/scanref.om: the host scan references of CUDApp src/omega_gpu_scan_ref.c (84b524ca) in Omega,
3note the frozen tile order on the CPU, additions only. Program mode (OM-1, run-time pools). Composed after
4note gpu/nvidia/om/apiviews/scan.om, whose av_scan is the one 32-wide tile, as the C called it. The cross-tile
5note carry is the order of gpu/nvidia/om/apiviews/scancarry.om (ar_carry), tot[e / 32 - 1] + out[e]; that unit is
6note OM-2 and scan.om is OM-1 (its parameter `out` is an OM-2 word), so the two do not compose into one unit and
7note the carry is written here on the pools.
8note The caller fills the input pools (sr_x, sr_u, sr_k, sr_v) to at least n elements; the temporaries are
9note sized here from n. Every function gives 0, 1 for bad arguments (the C's -1), or 4 when the host refuses
10note memory (the C's malloc failure, also -1 there).
11global sr_x f32 *
12global sr_y f32 *
13global sr_t f32 *
14global sr_r f32 1
15global sr_u uint *
16global sr_uy uint *
17global sr_k uint *
18global sr_v uint *
19global sr_tk uint *
20global sr_tv uint *
21note Tile count of n values: ceil(n / 32).
22fn sr_tiles n ulong to ulong
23ret div add n 31 32
24end
25note Length of prefix level l: level 0 is the n inputs, level l + 1 the tile totals of level l.
26fn sr_len n ulong l ulong to ulong
27m = n
28for i ulong l
29m = sr_tiles m
30end
31ret m
32end
33note Offset in sr_t of the totals of level l; level l + 1 is scanned in place there.
34fn sr_off n ulong l ulong to ulong
35o = ulong 0
36m = n
37for i ulong l
38k = sr_tiles m
39o = add o k
40m = k
41end
42ret o
43end
44note Elements of sr_t the prefix needs: the totals of every level the C's recursion allocated.
45fn sr_ptmp n ulong to ulong
46k = sr_tiles n
47t = k
48while gt k 1 bound 64
49k = sr_tiles k
50t = add t k
51end
52ret t
53end
54fn sr_grow n ulong to ulong
55r = enlarge sr_t n
56if ne r 0
57ret 4
58end
59ret 0
60end
61note Load the live values of a tile into w: from sr_x (src 0) or sr_t (src 1) at offset o.
62fn sr_ld src ulong o ulong live ulong w edit f32 32 to void
63for i ulong live
64if eq src 0
65put w i get sr_x add o i
66else
67put w i get sr_t add o i
68end
69end
70end
71fn sr_live m ulong b ulong to ulong
72live = sub m b
73if gt live 32
74ret 32
75end
76ret live
77end
78note omega_gpu_reduce_reference: op 0 sum, 1 max, 2 min over sr_x[0, n); the result in sr_r[0]. Each level
79note is written over sr_t from 0 (tile j is read before sr_t[j] is written, j <= 32j).
80fn sr_reduce op ulong n ulong to ulong
81if or gt op 2 eq n 0
82ret 1
83end
84if lt len sr_x n
85ret 1
86end
87if ne sr_grow sr_tiles n 0
88ret 4
89end
90w = new f32 32 0.0f32
91v = new f32 32 0.0f32
92src = ulong 0
93m = n
94while or eq src 0 gt m 1 bound 64
95nt = sr_tiles m
96for j ulong nt bound 9223372036854775807
97b = mul j 32
98live = sr_live m b
99sr_ld src b live w
100if ne av_scan op live w v 0
101ret 1
102end
103put sr_t j get v 0
104end
105src = ulong 1
106m = nt
107end
108put sr_r 0 get sr_t 0
109ret 0
110end
111note One prefix tile: live values at data offset o + b of level l (l 0: sr_x into sr_y; later: sr_t in place),
112note scanned through av_scan op 3; its total (v[31]) to sr_t[ti].
113fn sr_ltile l ulong o ulong b ulong live ulong ti ulong to ulong
114w = new f32 32 0.0f32
115v = new f32 32 0.0f32
116src = ulong 0
117if ne l 0
118src = ulong 1
119end
120sr_ld src add o b live w
121if ne av_scan 3 live w v 0
122ret 1
123end
124for i ulong live
125if eq l 0
126put sr_y add b i get v i
127else
128put sr_t add o add b i get v i
129end
130end
131put sr_t ti get v 31
132ret 0
133end
134note Scan prefix level l of n inputs; the tile totals to sr_t from sr_off n l.
135fn sr_lvl n ulong l ulong to ulong
136m = sr_len n l
137o = ulong 0
138if gt l 0
139o = sr_off n sub l 1
140end
141tt = sr_off n l
142for j ulong sr_tiles m bound 9223372036854775807
143b = mul j 32
144if ne sr_ltile l o b sr_live m b add tt j 0
145ret 1
146end
147end
148ret 0
149end
150note Carry of prefix level l from its prefixed totals: data[e] = tot[e / 32 - 1] + data[e], e from 32.
151fn sr_carry n ulong l ulong to void
152m = sr_len n l
153tt = sr_off n l
154e = ulong 32
155if eq l 0
156while lt e m bound 9223372036854775807
157c = get sr_t add tt sub div e 32 1
158put sr_y e add c get sr_y e
159e = add e 1
160end
161else
162o = sr_off n sub l 1
163while lt e m bound 9223372036854775807
164c = get sr_t add tt sub div e 32 1
165d = add o e
166put sr_t d add c get sr_t d
167e = add e 1
168end
169end
170end
171note omega_gpu_prefix_sum_reference: inclusive prefix sum of sr_x[0, n) into sr_y. The C recursed (scan a
172note level, prefix its totals, carry); this scans down the levels, then carries back up, deepest first.
173fn sr_prefix n ulong to ulong
174if or eq n 0 lt len sr_x n
175ret 1
176end
177r = enlarge sr_y n
178if ne r 0
179ret 4
180end
181if ne sr_grow sr_ptmp n 0
182ret 4
183end
184if ne sr_lvl n 0 0
185ret 1
186end
187l = ulong 1
188while gt sr_len n l 1 bound 64
189if ne sr_lvl n l 0
190ret 1
191end
192l = add l 1
193end
194note levels 0 .. l - 1 are scanned and level l - 1 is one tile; carry levels l - 2 down to 0
195k = sub l 1
196while gt k 0 bound 64
197k = sub k 1
198sr_carry n k
199end
200ret 0
201end
202note omega_gpu_prefix_sum_u32_reference: the running total of sr_u[0, n) into sr_uy, modulo 2^32.
203fn sr_pu32 n ulong to ulong
204if or eq n 0 lt len sr_u n
205ret 1
206end
207r = enlarge sr_uy n
208if ne r 0
209ret 4
210end
211s = uint 0
212for i ulong n bound 9223372036854775807
213s = add s get sr_u i
214put sr_uy i s
215end
216ret 0
217end
218note One stable counting pass of sr_radix on the 4-bit digit at shift sh.
219fn sr_rpass n ulong hv ulong sh ulong to void
220at = new ulong 16 0
221for i ulong n bound 9223372036854775807
222d = and shr ulong get sr_k i sh 15
223put at d add get at d 1
224end
225s = ulong 0
226for d ulong 16
227c = get at d
228put at d s
229s = add s c
230end
231for i ulong n bound 9223372036854775807
232d = and shr ulong get sr_k i sh 15
233w = get at d
234put at d add w 1
235put sr_tk w get sr_k i
236if ne hv 0
237put sr_tv w get sr_v i
238end
239end
240for i ulong n bound 9223372036854775807
241put sr_k i get sr_tk i
242if ne hv 0
243put sr_v i get sr_tv i
244end
245end
246end
247note omega_gpu_radix_sort_reference: sort sr_k[0, n), and sr_v[0, n) with it when hv is not 0 (the C's vals
248note not NULL): least significant digit first, 4-bit digits, a stable counting pass per digit.
249fn sr_radix n ulong hv ulong to ulong
250if or eq n 0 lt len sr_k n
251ret 1
252end
253if and ne hv 0 lt len sr_v n
254ret 1
255end
256r = enlarge sr_tk n
257if ne r 0
258ret 4
259end
260if ne hv 0
261r = enlarge sr_tv n
262if ne r 0
263ret 4
264end
265end
266for p ulong 8
267sr_rpass n hv mul p 4
268end
269ret 0
270end