KernelShift / solutions /simd-loop /baseline-simd-loop-sve /loop_122 /baseline-simd-loop-sve_loop_122.json
Download solutions/simd-loop/baseline-simd-loop-sve/loop_122/baseline-simd-loop-sve_loop_122.json from l3lab/KernelShift: direct link, hf CLI and curl.
- Browser
- Download file 7.52 kB
-
https://huggingface.co/datasets/l3lab/KernelShift/resolve/main/solutions/simd-loop/baseline-simd-loop-sve/loop_122/baseline-simd-loop-sve_loop_122.json
- Command line
-
hf download hf://datasets/l3lab/KernelShift/solutions/simd-loop/baseline-simd-loop-sve/loop_122/baseline-simd-loop-sve_loop_122.json
-
curl -L -o baseline-simd-loop-sve_loop_122.json https://huggingface.co/datasets/l3lab/KernelShift/resolve/main/solutions/simd-loop/baseline-simd-loop-sve/loop_122/baseline-simd-loop-sve_loop_122.json
7.52 kB
| { | |
| "name": "baseline-simd-loop-sve_loop_122", | |
| "definition": "loop_122", | |
| "dataset": "simd-loop", | |
| "author": "baseline-simd-loop-sve", | |
| "spec": { | |
| "language": "cpp", | |
| "target_hardware": [ | |
| "aarch64" | |
| ], | |
| "entry_point": "kernel.cpp::inner_loop_122", | |
| "dependencies": [], | |
| "isa_features": [ | |
| "sve" | |
| ], | |
| "compile_flags": [ | |
| "-O3", | |
| "-std=c++14", | |
| "-march=armv8.2-a+sve", | |
| "-Wno-c++11-narrowing" | |
| ], | |
| "link_flags": [] | |
| }, | |
| "sources": [ | |
| { | |
| "path": "loop_122.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_122_data {\n int32_t *data;\n uint32_t n;\n};\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nint kernelshift_entry_loop_122(void *data, int64_t n, void *unused);\n#ifdef __cplusplus\n}\n#endif\n" | |
| }, | |
| { | |
| "path": "loop_122.cpp", | |
| "content": "// Auto-generated by scripts/gen_simd_loop_harness.py \u2014 do not hand-edit.\n#include \"loop_122.h\"\n#include <stdint.h>\n\nextern \"C\" void inner_loop_122(struct loop_122_data *data);\n\nextern \"C\" int kernelshift_entry_loop_122(void *data, int64_t n, void *unused) {\n struct loop_122_data d;\n d.n = static_cast<uint32_t>(n);\n d.data = static_cast<int32_t *>(data);\n inner_loop_122(&d);\n return 0;\n}\n" | |
| }, | |
| { | |
| "path": "kernel.cpp", | |
| "content": "#include \"loop_122.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 k = n - 1;\n for (uint32_t i = 0; i < partition; i++) {\n for (uint32_t j = i % 2; j < k; j += 2 * svcntw()) {\n svbool_t p_loop = svwhilelt_b32(0u, (n - j) / 2);\n svint32x2_t a01 = svld2(p_loop, data + j);\n svint32_t a0 = svget2(a01, 0);\n svint32_t a1 = svget2(a01, 1);\n svbool_t p_comp = svcmplt(p_loop, a0, a1);\n svint32_t b0 = svsel(p_comp, a0, a1);\n svint32_t b1 = svsel(p_comp, a1, a0);\n svst2(p_loop, data + j, svcreate2(b0, b1));\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 svint32_t acc_min, acc_max;\n svint32_t vec_off, vec_val;\n svbool_t q = svwhilelt_b32(0u, n);\n acc_min = svld1(q, data);\n acc_max = acc_min;\n uint32_t i;\n svbool_t p;\n FOR_LOOP_32(, i, 0, n, p) {\n vec_val = svld1(p, data + i);\n acc_min = svmin_m(p, acc_min, vec_val);\n acc_max = svmax_m(p, acc_max, vec_val);\n }\n int32_t min = svminv(q, acc_min);\n int32_t max = svmaxv(q, acc_max);\n radix_t result = {\n .bits = sizeof(int32_t) * 8 - __builtin_clz(max - min),\n .offset = min,\n };\n vec_off = svdup_s32(result.offset);\n FOR_LOOP_32(, i, 0, n, p) {\n vec_val = svld1(p, data + i);\n vec_val = svsub_m(p, vec_val, vec_off);\n svst1(p, data + i, vec_val);\n }\n return result;\n}\nvoid post_add_offset(uint32_t n, int32_t * data, int32_t off) {\n svint32_t vec_val, vec_off = svdup_s32(off);\n svbool_t p;\n FOR_LOOP_32(uint32_t, i, 0, n, p) {\n vec_val = svld1(p, data + i);\n vec_val = svadd_m(p, vec_val, vec_off);\n svst1(p, data + i, vec_val);\n }\n}\nvoid NOINLINE do_sort(struct loop_122_data * input) {\n uint32_t n = input->n;\n int32_t *data = input->data;\n com_sort_oet(n, data, n);\n}\nextern \"C\" void inner_loop_122(struct loop_122_data * input) {\n do_sort(input);\n}\n" | |
| } | |
| ], | |
| "description": "Arm hand-written SVE kernel for loop_122 (sve tier expert ceiling)." | |
| } | |