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