~/bend-docscommunity

test/cuda_test.bend source

test/cuda_test.bend on the hub · documented module

# Cu.module and Cu.run against the same computations in plain Bend: an elementwise kernel with an F32 parameter# (exact: written with __fmul_rn / __fadd_rn, since NVRTC, like nvcc, fuses a * b + c into one rounding by default), a per-row sum with dynamic shared memory (to 1e-5), a transpose on a 2D grid (exact), and the array sizes# a kernel sees (.n, exactly each array's capacity). Prints each check; exits 1 if one fails.# Native CUDA build: bend cuda_test.bend -o cuda_test && ./cuda_testimport Baseimport ../cuda.bend as CU@unsafedef fv(u: U32) -> F32:  match u:    case U32{w}:      F32{w}@unsafedef uv(x: F32) -> U32:  match x:    case F32{w}:      U32{w}def source() -> String:  "BEND_KERNEL axpb(BEND_ARGS) {\n" ++  "  unsigned i = blockIdx.x * blockDim.x + threadIdx.x;\n" ++  "  if (i < p.v[0] && i < a0.n && i < a1.n && i < a2.n) a2.c[i] = bu(__fadd_rn(__fmul_rn(bf(a0.c[i]), bf(p.v[1])), bf(a1.c[i])));\n" ++  "}\n" ++  "BEND_KERNEL rowsum(BEND_ARGS) {\n" ++  "  extern __shared__ float s[];\n" ++  "  unsigned r = blockIdx.x, k = p.v[0];\n" ++  "  float acc = 0.f;\n" ++  "  for (unsigned j = threadIdx.x; j < k; j += blockDim.x) if ((unsigned long long)r * k + j < a0.n) acc += bf(a0.c[r * k + j]);\n" ++  "  s[threadIdx.x] = acc;\n" ++  "  __syncthreads();\n" ++  "  for (unsigned h = blockDim.x / 2; h > 0; h /= 2) { if (threadIdx.x < h) s[threadIdx.x] += s[threadIdx.x + h]; __syncthreads(); }\n" ++  "  if (threadIdx.x == 0 && r < a1.n) a1.c[r] = bu(s[0]);\n" ++  "}\n" ++  "BEND_KERNEL transpose(BEND_ARGS) {\n" ++  "  unsigned c = blockIdx.x * blockDim.x + threadIdx.x, r = blockIdx.y * blockDim.y + threadIdx.y, rows = p.v[0], cols = p.v[1];\n" ++  "  if (r < rows && c < cols && r * cols + c < a0.n && c * rows + r < a1.n) a1.c[c * rows + r] = a0.c[r * cols + c];\n" ++  "}\n" ++  "BEND_KERNEL sizes(BEND_ARGS) {\n" ++  "  if (threadIdx.x == 0 && a5.n >= 5) { a5.c[0] = (unsigned)a0.n; a5.c[1] = (unsigned)a1.n; a5.c[2] = (unsigned)a2.n; a5.c[3] = (unsigned)a3.n; a5.c[4] = (unsigned)a4.n; }\n" ++  "}\n"def mix(+x: U32) -> U32:  +a = (U32.xor(x, (x / 65536 : U32)) * 2246822519 : U32)  +b = (U32.xor(a, (a / 8192 : U32)) * 3266489917 : U32)  U32.xor(b, (b / 65536 : U32))def rnd(+i: U32, +seed: U32) -> F32:  (((U32.to_f32((mix(((i * 2654435761 : U32) + (seed * 40503 : U32) : U32)) / 256 : U32)) / 16777216.0 : F32) * 2.0 : F32) - 1.0 : F32)@unsafedef fill(done: Bool, +i: U32, +n: U32, +seed: U32, a: Array<U32>) -> Array<U32>:  match done:    case True{}:      a    case False{}:      fill(U32.is_eq((i + 1 : U32), n), (i + 1 : U32), n, seed, Array.set(U32, a, i, uv(rnd(i, seed))))def one(u: Unit) -> Array<U32>:  Array.new(U32, 0n, 0)# ---- checks, each a count of mismatching cells ----# c[i] == a[i] * s + b[i], i < n@unsafedef chk_axpb(done: Bool, +i: U32, +n: U32, +s: F32, +bad: U32, a: Array<U32>, b: Array<U32>, c: Array<U32>) -> U32:  match done:    case True{}:      bad    case False{}:      chk_axpb_a(i, n, s, bad, b, c, Array.get(U32, a, i))@unsafedef chk_axpb_a(+i: U32, +n: U32, +s: F32, +bad: U32, b: Array<U32>, c: Array<U32>, g: Array<U32> & U32) -> U32:  match g:    case (a, +av):      chk_axpb_b(i, n, s, bad, a, c, fv(av), Array.get(U32, b, i))@unsafedef chk_axpb_b(+i: U32, +n: U32, +s: F32, +bad: U32, a: Array<U32>, c: Array<U32>, +av: F32, g: Array<U32> & U32) -> U32:  match g:    case (b, +bv):      chk_axpb_c(i, n, s, bad, a, b, uv(((av * s : F32) + fv(bv) : F32)), Array.get(U32, c, i))@unsafedef chk_axpb_c(+i: U32, +n: U32, +s: F32, +bad: U32, a: Array<U32>, b: Array<U32>, +want: U32, g: Array<U32> & U32) -> U32:  match g:    case (c, +cv):      chk_axpb(U32.is_eq((i + 1 : U32), n), (i + 1 : U32), n, s, Bool.pick(U32, U32.is_eq(cv, want), bad, (bad + 1 : U32)), a, b, c)# the sum of a's k cells from base@unsafedef sum_row(done: Bool, +j: U32, +k: U32, +base: U32, +acc: F32, a: Array<U32>) -> Array<U32> & F32:  match done:    case True{}:      (a, acc)    case False{}:      sum_row_g(j, k, base, acc, Array.get(U32, a, (base + j : U32)))@unsafedef sum_row_g(+j: U32, +k: U32, +base: U32, +acc: F32, g: Array<U32> & U32) -> Array<U32> & F32:  match g:    case (a, +v):      sum_row(U32.is_eq((j + 1 : U32), k), (j + 1 : U32), k, base, (acc + fv(v) : F32), a)# |s[r] - sum of row r| <= 1e-5 * (1 + |sum|), r < rows@unsafedef chk_rows(done: Bool, +r: U32, +rows: U32, +k: U32, +bad: U32, a: Array<U32>, s: Array<U32>) -> U32:  match done:    case True{}:      bad    case False{}:      chk_rows_s(r, rows, k, bad, s, sum_row(False{}, 0, k, (r * k : U32), 0.0, a))@unsafedef chk_rows_s(+r: U32, +rows: U32, +k: U32, +bad: U32, s: Array<U32>, g: Array<U32> & F32) -> U32:  match g:    case (a, +want):      chk_rows_v(r, rows, k, bad, a, want, Array.get(U32, s, r))@unsafedef chk_rows_v(+r: U32, +rows: U32, +k: U32, +bad: U32, a: Array<U32>, +want: F32, g: Array<U32> & U32) -> U32:  match g:    case (s, +got):      +ok = F32.is_le(F32.abs((fv(got) - want : F32)), (0.00001 * (1.0 + F32.abs(want) : F32) : F32))      chk_rows(U32.is_eq((r + 1 : U32), rows), (r + 1 : U32), rows, k, Bool.pick(U32, ok, bad, (bad + 1 : U32)), a, s)# t[c * rows + r] == m[r * cols + c]@unsafedef chk_tr(done: Bool, +i: U32, +rows: U32, +cols: U32, +bad: U32, m: Array<U32>, t: Array<U32>) -> U32:  match done:    case True{}:      bad    case False{}:      chk_tr_m(i, rows, cols, bad, t, Array.get(U32, m, i))@unsafedef chk_tr_m(+i: U32, +rows: U32, +cols: U32, +bad: U32, t: Array<U32>, g: Array<U32> & U32) -> U32:  match g:    case (m, +mv):      chk_tr_t(i, rows, cols, bad, m, mv, Array.get(U32, t, ((U32.mod(i, cols) * rows : U32) + (i / cols : U32) : U32)))@unsafedef chk_tr_t(+i: U32, +rows: U32, +cols: U32, +bad: U32, m: Array<U32>, +mv: U32, g: Array<U32> & U32) -> U32:  match g:    case (t, +tv):      chk_tr(U32.is_eq((i + 1 : U32), (rows * cols : U32)), (i + 1 : U32), rows, cols, Bool.pick(U32, U32.is_eq(tv, mv), bad, (bad + 1 : U32)), m, t)# z[0 .. 4] == the five capacities@unsafedef chk_sizes(+w: List<U32>, z: Array<U32>, +i: U32, +bad: U32) -> U32:  match w:    case []:      bad    case +h <> t:      chk_sizes_g(t, i, bad, h, Array.get(U32, z, i))@unsafedef chk_sizes_g(+t: List<U32>, +i: U32, +bad: U32, +h: U32, g: Array<U32> & U32) -> U32:  match g:    case (z, +v):      chk_sizes(t, z, (i + 1 : U32), Bool.pick(U32, U32.is_eq(v, h), bad, (bad + 1 : U32)))def report(+name: String, +bad: U32) -> IO(Bool):  do IO<Bool>:    IO.print(name ++ ": " ++ U32.show(bad) ++ " mismatches")    return U32.is_eq(bad, 0)# ---- the runs ----@unsafedef t_axpb(+m: U32) -> IO(Bool):  do IO<Bool>:    r : Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> <- CU.Cu.run(m, "axpb", 4, 1, 1, 256, 1, 1, 0, fill(False{}, 0, 1000, 1, Array.new(U32, 10n, 0)), fill(False{}, 0, 1000, 2, Array.new(U32, 10n, 0)), Array.new(U32, 10n, 0), one(Unit{}), one(Unit{}), one(Unit{}), 1000, uv(1.5), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)    axpb_got(r)@unsafedef axpb_got(r: Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32>) -> IO(Bool):  match r:    case (a, (b, (c, z))):      report("axpb (1,000 cells, a * 1.5 + b)", chk_axpb(False{}, 0, 1000, 1.5, 0, a, b, c))@unsafedef t_rows(+m: U32) -> IO(Bool):  do IO<Bool>:    r : Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> <- CU.Cu.run(m, "rowsum", 50, 1, 1, 128, 1, 1, 512, fill(False{}, 0, 15000, 3, Array.new(U32, 14n, 0)), Array.new(U32, 6n, 0), one(Unit{}), one(Unit{}), one(Unit{}), one(Unit{}), 300, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)    rows_got(r)@unsafedef rows_got(r: Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32>) -> IO(Bool):  match r:    case (a, (s, z)):      report("rowsum (50 rows of 300, shared memory)", chk_rows(False{}, 0, 50, 300, 0, a, s))@unsafedef t_tr(+m: U32) -> IO(Bool):  do IO<Bool>:    r : Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> <- CU.Cu.run(m, "transpose", 3, 3, 1, 16, 16, 1, 0, fill(False{}, 0, 1330, 4, Array.new(U32, 11n, 0)), Array.new(U32, 11n, 0), one(Unit{}), one(Unit{}), one(Unit{}), one(Unit{}), 35, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)    tr_got(r)@unsafedef tr_got(r: Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32>) -> IO(Bool):  match r:    case (m, (t, z)):      report("transpose (35 x 38, 2D grid)", chk_tr(False{}, 0, 35, 38, 0, m, t))@unsafedef t_sizes(+m: U32) -> IO(Bool):  do IO<Bool>:    r : Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> <- CU.Cu.run(m, "sizes", 1, 1, 1, 32, 1, 1, 0, Array.new(U32, 3n, 0), Array.new(U32, 10n, 0), one(Unit{}), Array.new(U32, 7n, 0), Array.new(U32, 16n, 0), Array.new(U32, 3n, 0), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)    sizes_got(r)@unsafedef sizes_got(r: Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32> & Array<U32>) -> IO(Bool):  match r:    case (a, (b, (c, (d, (e, z))))):      report("sizes (.n of arrays of 8, 1024, 1, 128, 65536 cells)", chk_sizes([8, 1024, 1, 128, 65536], z, 0, 0))@unsafedef all(+m: U32) -> IO(Unit):  do IO<Unit>:    a : Bool <- t_axpb(m)    b : Bool <- t_rows(m)    c : Bool <- t_tr(m)    d : Bool <- t_sizes(m)    Bool.pick(IO(Unit), Bool.and(Bool.and(a, b), Bool.and(c, d)), IO.print("Cu: all checks match plain Bend"), IO.exit(1))def ping(+x: U32) -> U32:  (x + 1 : U32)@unsafedef main() -> IO(Unit):  do IO<Unit>:    IO.print("GPU check: " ++ U32.show(ping!(1)))    m : U32 <- CU.Cu.module(source())    all(m)