KernelShift / solutions /simd-loop /baseline-simd-loop-sve /loop_124 /baseline-simd-loop-sve_loop_124.json
Download solutions/simd-loop/baseline-simd-loop-sve/loop_124/baseline-simd-loop-sve_loop_124.json from l3lab/KernelShift: direct link, hf CLI and curl.
- Browser
- Download file 18.8 kB
-
https://huggingface.co/datasets/l3lab/KernelShift/resolve/main/solutions/simd-loop/baseline-simd-loop-sve/loop_124/baseline-simd-loop-sve_loop_124.json
- Command line
-
hf download hf://datasets/l3lab/KernelShift/solutions/simd-loop/baseline-simd-loop-sve/loop_124/baseline-simd-loop-sve_loop_124.json
-
curl -L -o baseline-simd-loop-sve_loop_124.json https://huggingface.co/datasets/l3lab/KernelShift/resolve/main/solutions/simd-loop/baseline-simd-loop-sve/loop_124/baseline-simd-loop-sve_loop_124.json
18.8 kB
| { | |
| "name": "baseline-simd-loop-sve_loop_124", | |
| "definition": "loop_124", | |
| "dataset": "simd-loop", | |
| "author": "baseline-simd-loop-sve", | |
| "spec": { | |
| "language": "cpp", | |
| "target_hardware": [ | |
| "aarch64" | |
| ], | |
| "entry_point": "kernel.cpp::inner_loop_124", | |
| "dependencies": [], | |
| "isa_features": [ | |
| "sve" | |
| ], | |
| "compile_flags": [ | |
| "-O3", | |
| "-std=c++14", | |
| "-march=armv8.2-a+sve", | |
| "-Wno-c++11-narrowing" | |
| ], | |
| "link_flags": [] | |
| }, | |
| "sources": [ | |
| { | |
| "path": "loop_124.h", | |
| "content": "// Auto-generated by scripts/gen_simd_loop_harness.py \u2014 do not hand-edit.\n#pragma once\n#include <stdint.h>\n\nstruct loop_124_data {\n int32_t *data;\n int32_t *temp;\n uint32_t *hist;\n uint32_t *prfx;\n uint32_t n;\n};\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nint kernelshift_entry_loop_124(void *data, void *temp, void *hist, void *prfx, int64_t n, void *unused);\n#ifdef __cplusplus\n}\n#endif\n" | |
| }, | |
| { | |
| "path": "loop_124.cpp", | |
| "content": "// Auto-generated by scripts/gen_simd_loop_harness.py \u2014 do not hand-edit.\n// Expert-baseline binding: pads edge-size workloads to the sizes Arm's kernel\n// supports (see _SVE_PAD_BINDINGS in the generator). Plain call otherwise.\n#include \"loop_124.h\"\n#include <stdint.h>\n#include <stdlib.h>\n#include <string.h>\n// Vector-length units for the padding rules below. Arm's kernel is built for\n// 128-bit SVE (Graviton3/4), so a build without SVE (the neon arm of an ISA\n// ablation lifts this binding too) pads to the same sizes with constants.\n#if defined(__ARM_FEATURE_SVE)\n#include <arm_sve.h>\n#define _VLW() ((uint64_t)svcntw())\n#define _VLH() ((uint64_t)svcnth())\n#else\n#define _VLW() ((uint64_t)4)\n#define _VLH() ((uint64_t)8)\n#endif\n\nextern \"C\" void inner_loop_124(struct loop_124_data *data);\n\nstatic inline uint64_t _round_up(uint64_t v, uint64_t m) { return (v + m - 1) / m * m; }\nstatic inline void *_zeroed(uint64_t bytes) { void *p = calloc(bytes ? bytes : 1, 1); if (!p) abort(); return p; }\n\nextern \"C\" int kernelshift_entry_loop_124(void *data, void *temp, void *hist, void *prfx, int64_t n_in, void *unused) {\n const uint64_t n = (uint64_t)n_in, mvl = _VLW();\n struct loop_124_data d;\n if (n >= mvl) {\n d.n = (uint32_t)n; d.data = static_cast<int32_t *>(data); d.temp = static_cast<int32_t *>(temp);\n d.hist = static_cast<uint32_t *>(hist); d.prfx = static_cast<uint32_t *>(prfx);\n inner_loop_124(&d);\n return 0;\n }\n const uint64_t np = mvl;\n int32_t *pd = (int32_t *)_zeroed(np * sizeof(int32_t)), *pt = (int32_t *)_zeroed(np * sizeof(int32_t));\n uint32_t *ph = (uint32_t *)_zeroed(np * 16 * sizeof(uint32_t)), *pp = (uint32_t *)_zeroed(np * sizeof(uint32_t));\n for (uint64_t i = 0; i < np; ++i) pd[i] = INT32_MAX;\n memcpy(pd, data, n * sizeof(int32_t));\n d.n = (uint32_t)np; d.data = pd; d.temp = pt; d.hist = ph; d.prfx = pp;\n inner_loop_124(&d);\n memcpy(data, pd, n * sizeof(int32_t));\n free(pd); free(pt); free(ph); free(pp);\n return 0;\n}\n" | |
| }, | |
| { | |
| "path": "kernel.cpp", | |
| "content": "#include \"loop_124.h\"\n#include <stdint.h>\n#include <string.h>\n#include <stdlib.h>\n#include <stdio.h>\n#include <limits.h>\n#include <assert.h>\n#include <math.h>\n#include <arm_sve.h>\n#define restrict __restrict\n#define SC_SVE_ATTR\n#define SC_SVE_LOOP_ATTR\n#define NS_SVE_LOOP_ATTR\n#define NOINLINE __attribute__((noinline))\n#define FOR_COND(P, S, I, N) svptest_first(svptrue_b##S(), P = svwhilelt_b##S(I, N))\n#define FOR_LOOP(T, I, M, N, P, S, W) for (T I = M; FOR_COND(P, S, I, N); I += svcnt##W())\n#define FOR_LOOP_8(T, I, M, N, P) FOR_LOOP(T, I, M, N, P, 8, b)\n#define FOR_LOOP_16(T, I, M, N, P) FOR_LOOP(T, I, M, N, P, 16, h)\n#define FOR_LOOP_32(T, I, M, N, P) FOR_LOOP(T, I, M, N, P, 32, w)\n#define FOR_LOOP_64(T, I, M, N, P) FOR_LOOP(T, I, M, N, P, 64, d)\nstatic inline uint32_t get_sve_vl(void) {\n uint64_t vl = 0; asm volatile(\"incb %[vl], all, mul #8\" : [vl] \"+r\"(vl) : :);\n return (uint32_t)vl;\n}\nstatic inline uint32_t get_vl(void) { return get_sve_vl(); }\n// common/helpers.h: 64-byte-aligned scratch. Only reached when the harness\n// hands a kernel a NULL scratch buffer (it never does \u2014 every scratch field is\n// bound), so a plain aligned malloc stands in for Arm's arena allocator.\nstatic inline void *alloc_64b(uint64_t size, const char *name) {\n void *p = NULL; (void)name;\n if (posix_memalign(&p, 64, size ? size : 64) != 0) abort();\n return p;\n}\n#define ALLOC_64B(P, S, N) P = (__typeof__(P))alloc_64b((S) * sizeof((P)[0]), N)\n\n#define SORT_H_ \n#define IS_NOT_POWER_OF_2(v) (((v) & ((v)-1)) && (v))\ninline uint32_t mylog2_32(uint32_t val) {\n uint32_t ret = -1;\n while (val != 0) {\n val >>= 1;\n ret++;\n }\n return ret;\n}\ninline uint32_t min2_32u(uint32_t a, uint32_t b) {\n uint32_t result;\n asm(\"cmp %w1, %w2 ; csel %w0, %w1, %w2, lo\" : \"=r\"(result) : \"r\"(a), \"r\"(b));\n return result;\n}\ninline uint32_t max2_32u(uint32_t a, uint32_t b) {\n uint32_t result;\n asm(\"cmp %w1, %w2 ; csel %w0, %w1, %w2, hi\" : \"=r\"(result) : \"r\"(a), \"r\"(b));\n return result;\n}\ninline uint32_t upper_power_of_two_32u(uint32_t v) {\n v--;\n v |= v >> 1;\n v |= v >> 2;\n v |= v >> 4;\n v |= v >> 8;\n v |= v >> 16;\n v++;\n return v;\n}\n#define SWAP_DECL(NAME,TYPE) void NAME(TYPE *a, TYPE *b) { TYPE c; c = *a; *a = *b; *b = c; }\ninline SWAP_DECL(swap_32, int32_t);\ninline int check_sorted(uint32_t n, int32_t *v) {\n int r = 0;\n for (uint32_t i = 1; i < n; i++) r += (int)(v[i] < v[i - 1]);\n return r;\n}\nvoid com_sort_oet(uint32_t, int32_t *, uint32_t);\nvoid com_sort_insertion(uint32_t, int32_t *);\nvoid com_sort_radix(uint32_t, int32_t *, int32_t *);\ntypedef struct {\n uint32_t bits;\n int32_t offset;\n} radix_t;\nradix_t find_radix_sub_offset(uint32_t, int32_t *);\nvoid post_add_offset(uint32_t, int32_t *, int32_t);\nvoid com_sort_oet(uint32_t n, int32_t * data, uint32_t partition) {\n uint32_t mvl2 = 0;\n asm volatile(\n \"addvl %x0, %x0, #1 \\n\"\n \"asr %x0, %x0, #1 \\n\"\n : \"+r\"(mvl2));\n for (uint32_t i = 0; i < partition; i++) {\n uint32_t j = i % 2;\n uint32_t remaining = n - j;\n while (remaining >= 2) {\n uint32_t vals_per_iteration = min2_32u(remaining, mvl2);\n uint32_t vals_per_register = vals_per_iteration / 2;\n asm volatile(\n \"whilelt p1.s, wzr, %w[vpr] \\n\"\n \"ld2w {z0.s, z1.s}, p1/z, [%x[data], %x[j], lsl #2] \\n\"\n \"cmplt p2.s, p1/z, z0.s, z1.s \\n\"\n \"sel z2.s, p2, z0.s, z1.s \\n\"\n \"sel z3.s, p2, z1.s, z0.s \\n\"\n \"st2w {z2.s, z3.s}, p1 , [%x[data], %x[j], lsl #2] \\n\"\n :\n : [data] \"r\"(data), [j] \"r\"(j), [vpr] \"r\"(vals_per_register)\n : \"v0\", \"v1\", \"v2\", \"v3\", \"p1\", \"p2\", \"cc\", \"memory\");\n j += vals_per_iteration;\n remaining -= vals_per_iteration;\n }\n }\n}\nvoid com_sort_insertion(uint32_t n, int32_t * data) {\n uint32_t i, j;\n for (i = 1; i < n; i++) {\n int32_t tmp = data[i];\n for (j = i; j > 0 && data[j - 1] > tmp; j--) {\n data[j] = data[j - 1];\n }\n data[j] = tmp;\n }\n}\nvoid com_sort_radix(uint32_t n, int32_t * data,\n int32_t * temp) {\n int32_t min = data[0];\n int32_t max = data[0];\n for (uint32_t i = 1; i < n; i++) {\n min = (min < data[i]) ? min : data[i];\n max = (max > data[i]) ? max : data[i];\n }\n for (uint32_t i = 0; i < n; i++) data[i] -= min;\n uint32_t lim = max - min;\n#define BIT_STRIDE 8\n#define BIN_INDEX(i,o) (((uint32_t)data[i] >> o) % (1 << BIT_STRIDE))\n for (int off = 0; lim != 0; off += BIT_STRIDE, lim >>= BIT_STRIDE) {\n uint32_t count[1 << BIT_STRIDE] = {0};\n for (uint32_t i = 0; i < n; i++) count[BIN_INDEX(i, off)]++;\n uint32_t count_tmp = count[0];\n for (int j = 1; j < (1 << BIT_STRIDE); j++) {\n count[j] += count_tmp;\n count_tmp = count[j];\n }\n for (uint32_t i = n; i > 0; i--)\n temp[--count[BIN_INDEX(i - 1, off)]] = data[i - 1];\n memcpy(data, temp, sizeof(data[0]) * n);\n }\n for (uint32_t i = 0; i < n; i++) data[i] += min;\n}\nradix_t find_radix_sub_offset(uint32_t n, int32_t * data) {\n uint32_t min, max, rdx, i;\n i = 0;\n asm volatile(\n \" whilelt p0.s, %w[i], %w[n] \\n\"\n \" ld1w z1.s, p0/z, [%[data], %x[i], lsl #2] \\n\"\n \" mov z2.d, z1.d \\n\"\n \" incw %x[i] \\n\"\n \" whilelt p1.s, %w[i], %w[n] \\n\"\n \" b.none 2f \\n\"\n \"1: ld1w z0.s, p1/z, [%[data], %x[i], lsl #2] \\n\"\n \" smax z1.s, p1/m, z1.s, z0.s \\n\"\n \" smin z2.s, p1/m, z2.s, z0.s \\n\"\n \" incw %x[i] \\n\"\n \" whilelt p1.s, %w[i], %w[n] \\n\"\n \" b.first 1b \\n\"\n \"2: smaxv s1, p0, z1.s \\n\"\n \" sminv s2, p0, z2.s \\n\"\n \" fmov %w[max], s1 \\n\"\n \" fmov %w[min], s2 \\n\"\n \" sub %w[rdx], %w[max], %w[min] \\n\"\n \" clz %w[rdx], %w[rdx] \\n\"\n : [i] \"+r\"(i), [min] \"=&r\"(min), [max] \"=&r\"(max), [rdx] \"=&r\"(rdx)\n : [n] \"r\"(n), [data] \"r\"(data)\n : \"z0\", \"z1\", \"z2\", \"p0\", \"p1\", \"cc\", \"memory\");\n radix_t result = {\n .bits = sizeof(int32_t) * 8 - rdx,\n .offset = min,\n };\n i = 0;\n asm volatile(\n \" dup z1.s, %w[off] \\n\"\n \" whilelt p0.s, %w[i], %w[n] \\n\"\n \" b.none 4f \\n\"\n \"3: \\n\"\n \" ld1w z0.s, p0/z, [%[data], %x[i], lsl #2]\\n\"\n \" sub z0.s, z0.s, z1.s \\n\"\n \" st1w z0.s, p0 , [%[data], %x[i], lsl #2]\\n\"\n \" incw %x[i] \\n\"\n \" whilelt p0.s, %w[i], %w[n] \\n\"\n \" b.first 3b \\n\"\n \"4: \\n\"\n : [i] \"+r\"(i)\n : [n] \"r\"(n), [data] \"r\"(data), [off] \"r\"(result.offset)\n : \"z0\", \"z1\", \"p0\", \"cc\", \"memory\");\n return result;\n}\nvoid post_add_offset(uint32_t n, int32_t * data, int32_t off) {\n uint32_t i = 0;\n asm volatile(\n \" dup z1.s, %w[off] \\n\"\n \" whilelt p0.s, %w[i], %w[n] \\n\"\n \" b.none 2f \\n\"\n \"1: \\n\"\n \" ld1w z0.s, p0/z, [%[data], %x[i], lsl #2]\\n\"\n \" add z0.s, z0.s, z1.s \\n\"\n \" st1w z0.s, p0 , [%[data], %x[i], lsl #2]\\n\"\n \" incw %x[i] \\n\"\n \" whilelt p0.s, %w[i], %w[n] \\n\"\n \" b.first 1b \\n\"\n \"2: \\n\"\n : [i] \"+r\"(i)\n : [data] \"r\"(data), [off] \"r\"(off), [n] \"r\"(n)\n : \"z0\", \"z1\", \"p0\", \"cc\", \"memory\");\n}\n#define MAX_BIN 16\ninline SWAP_DECL(swap_u32p, uint32_t *);\nvoid NOINLINE do_sort(struct loop_124_data * input) {\n uint32_t n = input->n;\n uint32_t mvl;\n asm volatile(\"cntw %x0\" : \"=r\"(mvl));\n uint32_t num_bins = MAX_BIN;\n if (IS_NOT_POWER_OF_2(num_bins)) {\n printf(\" - Error: number of bins must be a power-of-2\\n\");\n return;\n }\n if (n < mvl) {\n printf(\" - Error: buffer size must be greater than VL\\n\");\n return;\n }\n if (input->hist == NULL)\n ALLOC_64B(input->hist, mvl * num_bins, \"histogram\");\n if (input->prfx == NULL)\n ALLOC_64B(input->prfx, mvl, \"prefix sum table\");\n int32_t *data = input->data;\n int32_t *temp = input->temp;\n uint32_t *histogram = input->hist;\n uint32_t *prefixsum = input->prfx;\n radix_t radix = find_radix_sub_offset(n, data);\n uint32_t num_bins_log2 = mylog2_32(num_bins);\n uint32_t num_passes = (radix.bits + num_bins_log2 - 1) / num_bins_log2;\n uint32_t stride = (n + mvl - 1) / mvl;\n uint32_t std_vl = (n + stride - 1) / stride;\n stride = (n + std_vl - 1) / std_vl;\n uint32_t short_iterations = (std_vl - (n % std_vl)) % std_vl;\n uint32_t switching_i = stride - short_iterations;\n uint32_t *data_inp = (uint32_t *)data;\n uint32_t *data_out = (uint32_t *)temp;\n uint32_t *data_ptr, *hist_ptr, i;\n asm volatile(\n \"ptrue p0.s \\n\"\n \"dup z0.s, #0 \\n\"\n \"dup z1.s, %w[and_mask] \\n\"\n \"dup z2.s, %w[mvl] \\n\"\n \"index z3.s, #0, #1 \\n\"\n \"index z4.s, #0, %w[stride] \\n\"\n ::[stride] \"r\"(stride),\n [mvl] \"r\"(mvl), [and_mask] \"r\"(num_bins - 1)\n : \"z0\", \"z1\", \"z2\", \"z3\", \"z4\", \"p0\");\n uint32_t radix_shift = 0;\n for (uint32_t pass = 0; pass < num_passes; pass++) {\n for (i = 0, hist_ptr = histogram; i < num_bins; i++) {\n asm volatile(\n \"st1w z0.s, p0, [%[hist]] \\n\"\n \"addvl %[hist], %[hist], #1 \\n\"\n : [hist] \"+&r\"(hist_ptr)\n :\n : \"z0\", \"p0\", \"memory\");\n }\n asm volatile(\"dup z5.s, %w[rs]\" : : [rs] \"r\"(radix_shift));\n for (i = 0, data_ptr = data_inp; i < stride; i++, data_ptr++) {\n uint32_t m = (i < switching_i) - 1;\n uint32_t this_vl = ((std_vl - 1) & m) | (std_vl & ~m);\n asm volatile(\n \"whilelt p1.s, wzr, %w[this_vl] \\n\"\n \"ld1w z6.s, p1/z, [%[data], z4.s, uxtw #2] \\n\"\n \"lsr z6.s, p1/m, z6.s, z5.s \\n\"\n \"and z6.s, p1/m, z6.s, z1.s \\n\"\n \"mul z6.s, p1/m, z6.s, z2.s \\n\"\n \"add z6.s, p1/m, z6.s, z3.s \\n\"\n \"ld1w z7.s, p1/z, [%[hist], z6.s, uxtw #2] \\n\"\n \"add z7.s, z7.s, #1 \\n\"\n \"st1w z7.s, p1 , [%[hist], z6.s, uxtw #2] \\n\" ::[data] \"r\"(\n data_ptr),\n [hist] \"r\"(histogram), [this_vl] \"r\"(this_vl)\n : \"z1\", \"z2\", \"z3\", \"z4\", \"z5\", \"z6\", \"z7\", \"p1\", \"cc\", \"memory\");\n }\n asm volatile(\n \"index z6.s, #0, %w[num_bins] \\n\"\n \"dup z7.s, #0 \\n\" ::[num_bins] \"r\"(num_bins)\n : \"z6\", \"z7\");\n for (i = 0, hist_ptr = histogram; i < num_bins; i++, hist_ptr++) {\n asm volatile(\n \"ld1w z8.s, p0/z, [%[hist], z6.s, uxtw #2] \\n\"\n \"add z7.s, p0/m, z7.s, z8.s \\n\" ::[hist] \"r\"(\n hist_ptr)\n : \"z6\", \"z7\", \"z8\", \"p0\", \"memory\");\n }\n asm volatile(\"st1w z7.s, p0, [%[pfs]]\" ::[pfs] \"r\"(prefixsum)\n : \"z7\", \"p0\", \"memory\");\n for (uint32_t temp_sum = 0, i = 0; i < mvl; i++) {\n uint32_t temp_elm = prefixsum[i];\n prefixsum[i] = temp_sum;\n temp_sum += temp_elm;\n }\n asm volatile(\"ld1w z7.s, p0/z, [%[pfs]]\" ::[pfs] \"r\"(prefixsum)\n : \"z7\", \"p0\", \"memory\");\n for (i = 0, hist_ptr = histogram; i < num_bins; i++, hist_ptr++) {\n asm volatile(\n \"ld1w z8.s, p0/z, [%[hist], z6.s, uxtw #2] \\n\"\n \"st1w z7.s, p0 , [%[hist], z6.s, uxtw #2] \\n\"\n \"add z7.s, p0/m, z7.s, z8.s \\n\" ::[hist] \"r\"(\n hist_ptr)\n : \"z6\", \"z7\", \"z8\", \"p0\", \"memory\");\n }\n for (i = 0, data_ptr = data_inp; i < stride; i++, data_ptr++) {\n uint32_t m = (i < switching_i) - 1;\n uint32_t this_vl = ((std_vl - 1) & m) | (std_vl & ~m);\n asm volatile(\n \"whilelt p1.s, wzr, %w[this_vl] \\n\"\n \"ld1w z6.s, p1/z, [%[data], z4.s, uxtw #2] \\n\"\n \"mov z8.d, z6.d \\n\"\n \"lsr z6.s, p1/m, z6.s, z5.s \\n\"\n \"and z6.s, p1/m, z6.s, z1.s \\n\"\n \"mul z6.s, p1/m, z6.s, z2.s \\n\"\n \"add z6.s, p1/m, z6.s, z3.s \\n\"\n \"ld1w z7.s, p1/z, [%[hist], z6.s, uxtw #2] \\n\"\n \"st1w z8.s, p1 , [%[dest], z7.s, uxtw #2] \\n\"\n \"add z7.s, z7.s, #1 \\n\"\n \"st1w z7.s, p1 , [%[hist], z6.s, uxtw #2] \\n\" ::[data] \"r\"(\n data_ptr),\n [dest] \"r\"(data_out), [hist] \"r\"(histogram), [this_vl] \"r\"(this_vl)\n : \"z1\", \"z2\", \"z3\", \"z4\", \"z5\", \"z6\", \"z7\", \"p1\", \"cc\", \"memory\");\n }\n swap_u32p(&data_inp, &data_out);\n radix_shift += num_bins_log2;\n }\n if ((void *)data_inp != (void *)data) {\n asm volatile(\n \" mov %w[i], wzr \\n\"\n \" whilelt p1.s, %w[i], %w[n] \\n\"\n \" b.none 4f \\n\"\n \"3: \\n\"\n \" ld1w z2.s, p1/z, [%[src], %x[i], lsl #2] \\n\"\n \" st1w z2.s, p1 , [%[dst], %x[i], lsl #2] \\n\"\n \" incw %x[i] \\n\"\n \" whilelt p1.s, %w[i], %w[n] \\n\"\n \" b.first 3b \\n\"\n \"4: \\n\"\n : [i] \"+&r\"(i)\n : [src] \"r\"(data_inp), [dst] \"r\"(data_out), [n] \"r\"(n)\n : \"z2\", \"p1\", \"cc\", \"memory\");\n }\n post_add_offset(n, data, radix.offset);\n}\nextern \"C\" void inner_loop_124(struct loop_124_data * input) {\n do_sort(input);\n}\n" | |
| } | |
| ], | |
| "description": "Arm hand-written SVE kernel for loop_124 (sve tier expert ceiling)." | |
| } | |