|
| 1 | +// Copyright 2025 ETH Zurich and University of Bologna. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#include <benchmark.h> |
| 6 | +#include <snrt.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <stdio.h> |
| 9 | + |
| 10 | +#include DATAHEADER |
| 11 | +#include "kernel/spmv.c" |
| 12 | + |
| 13 | +#if (PREC != 64) |
| 14 | +#error "spmv currently supports double precision only" |
| 15 | +#endif |
| 16 | + |
| 17 | +#define T double |
| 18 | + |
| 19 | +// Use all cluster cores by default. Set SPMV_NUM_CORES to a positive value to |
| 20 | +// limit the kernel to a smaller number of worker cores. |
| 21 | +#ifndef SPMV_NUM_CORES |
| 22 | +#define SPMV_NUM_CORES 0 |
| 23 | +#endif |
| 24 | + |
| 25 | +static T *row_val; |
| 26 | +static T *x_vec; |
| 27 | +static T *result; |
| 28 | +static uint32_t *row_ptr; |
| 29 | +static uint32_t *col_idx; |
| 30 | +static uint32_t *x_off; |
| 31 | + |
| 32 | +static inline void *l1alloc_aligned(size_t size, size_t alignment) { |
| 33 | + uintptr_t raw = (uintptr_t)snrt_l1alloc(size + alignment - 1); |
| 34 | + uintptr_t aligned = (raw + alignment - 1) & ~(uintptr_t)(alignment - 1); |
| 35 | + return (void *)aligned; |
| 36 | +} |
| 37 | + |
| 38 | +static inline double abs_diff(double a, double b) { |
| 39 | + double d = a - b; |
| 40 | + return d < 0.0 ? -d : d; |
| 41 | +} |
| 42 | + |
| 43 | +static inline int fp_check(const T *a, const T *b) { |
| 44 | + return abs_diff(*a, *b) > 0.001; |
| 45 | +} |
| 46 | + |
| 47 | +static inline void build_offsets(uint32_t *dst, const uint32_t *src, |
| 48 | + uint32_t nnz) { |
| 49 | + for (uint32_t i = 0; i < nnz; ++i) dst[i] = src[i] * sizeof(T); |
| 50 | +} |
| 51 | + |
| 52 | +int main() { |
| 53 | + const uint32_t num_cores_hw = snrt_cluster_core_num(); |
| 54 | + const uint32_t cid = snrt_cluster_core_idx(); |
| 55 | + const uint32_t num_cores = |
| 56 | + (SPMV_NUM_CORES > 0 && SPMV_NUM_CORES < num_cores_hw) ? SPMV_NUM_CORES |
| 57 | + : num_cores_hw; |
| 58 | + |
| 59 | +#if USE_CACHE == 1 |
| 60 | + uint32_t spm_size = 16; |
| 61 | +#else |
| 62 | + uint32_t spm_size = 120; |
| 63 | +#endif |
| 64 | + |
| 65 | + const uint32_t num_fpu = sizeof(T) / 2; |
| 66 | + |
| 67 | + if (cid == 0) { |
| 68 | + l1d_init(spm_size); |
| 69 | + } |
| 70 | + |
| 71 | +#if MEAS_1ITER == 1 |
| 72 | + const int measure_iter = 1; |
| 73 | +#else |
| 74 | + const int measure_iter = 2; |
| 75 | +#endif |
| 76 | + |
| 77 | + unsigned int timer = (unsigned int)-1; |
| 78 | + unsigned int timer_best = (unsigned int)-1; |
| 79 | + unsigned int timer_1iter = (unsigned int)-1; |
| 80 | + int ret = 0; |
| 81 | + |
| 82 | + const uint32_t row_start = (cid < num_cores) ? (spmv_l.M * cid) / num_cores : 0; |
| 83 | + const uint32_t row_end = |
| 84 | + (cid < num_cores) ? (spmv_l.M * (cid + 1)) / num_cores : 0; |
| 85 | + |
| 86 | +#if USE_CACHE == 1 |
| 87 | + if (cid == 0) { |
| 88 | + x_off = (uint32_t *)l1alloc_aligned(spmv_l.K * sizeof(uint32_t), 8); |
| 89 | + result = (T *)l1alloc_aligned(spmv_l.M * sizeof(T), 8); |
| 90 | + build_offsets(x_off, spmv_col_idx_dram, spmv_l.K); |
| 91 | + } |
| 92 | + |
| 93 | + row_ptr = spmv_row_ptr_dram; |
| 94 | + col_idx = spmv_col_idx_dram; |
| 95 | + row_val = spmv_val_dram; |
| 96 | + x_vec = spmv_x_dram; |
| 97 | +#else |
| 98 | + if (cid == 0) { |
| 99 | + row_ptr = (uint32_t *)l1alloc_aligned((spmv_l.M + 1) * sizeof(uint32_t), 8); |
| 100 | + col_idx = (uint32_t *)l1alloc_aligned(spmv_l.K * sizeof(uint32_t), 8); |
| 101 | + x_off = (uint32_t *)l1alloc_aligned(spmv_l.K * sizeof(uint32_t), 8); |
| 102 | + row_val = (T *)l1alloc_aligned(spmv_l.K * sizeof(T), 8); |
| 103 | + x_vec = (T *)l1alloc_aligned(spmv_l.N * sizeof(T), 8); |
| 104 | + result = (T *)l1alloc_aligned(spmv_l.M * sizeof(T), 8); |
| 105 | + |
| 106 | + snrt_dma_start_1d(row_ptr, spmv_row_ptr_dram, |
| 107 | + (spmv_l.M + 1) * sizeof(uint32_t)); |
| 108 | + snrt_dma_start_1d(col_idx, spmv_col_idx_dram, spmv_l.K * sizeof(uint32_t)); |
| 109 | + snrt_dma_start_1d(row_val, spmv_val_dram, spmv_l.K * sizeof(T)); |
| 110 | + snrt_dma_start_1d(x_vec, spmv_x_dram, spmv_l.N * sizeof(T)); |
| 111 | + snrt_dma_wait_all(); |
| 112 | + build_offsets(x_off, col_idx, spmv_l.K); |
| 113 | + } |
| 114 | +#endif |
| 115 | + |
| 116 | + snrt_cluster_hw_barrier(); |
| 117 | + |
| 118 | + for (int iter = 0; iter < measure_iter; ++iter) { |
| 119 | + if (cid == 0) { |
| 120 | + start_kernel(); |
| 121 | + timer = benchmark_get_cycle(); |
| 122 | + } |
| 123 | + |
| 124 | + spmv_v64b(row_ptr, x_off, row_val, x_vec, result, row_start, row_end); |
| 125 | + |
| 126 | + snrt_cluster_hw_barrier(); |
| 127 | + |
| 128 | + if (cid == 0) { |
| 129 | + stop_kernel(); |
| 130 | + timer = benchmark_get_cycle() - timer; |
| 131 | + if (iter == 0) { |
| 132 | + timer_1iter = timer; |
| 133 | + } else { |
| 134 | + timer_best = (timer_best > timer) ? timer : timer_best; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + snrt_cluster_hw_barrier(); |
| 139 | + } |
| 140 | + |
| 141 | + if (measure_iter == 1) timer_best = timer_1iter; |
| 142 | + |
| 143 | + if (cid == 0) { |
| 144 | + double checksum = 0.0; |
| 145 | + int errors = 0; |
| 146 | + |
| 147 | + for (uint32_t i = 0; i < spmv_l.M; ++i) { |
| 148 | + checksum += result[i]; |
| 149 | + if (fp_check(&result[i], &spmv_result[i])) { |
| 150 | + ++errors; |
| 151 | + printf("Error: row %u result=%f golden=%f\n", i, result[i], |
| 152 | + spmv_result[i]); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if (abs_diff(checksum, spmv_checksum) > 0.001) { |
| 157 | + ++errors; |
| 158 | + printf("Error: checksum=%f golden=%f\n", checksum, spmv_checksum); |
| 159 | + } |
| 160 | + |
| 161 | + write_cyc(timer_best); |
| 162 | + |
| 163 | + { |
| 164 | + const unsigned long performance = 1000UL * 2UL * spmv_l.K / timer_best; |
| 165 | + const unsigned long utilization = |
| 166 | + performance / (2 * num_cores * num_fpu * 8 / sizeof(T)); |
| 167 | + |
| 168 | + printf("\n----- (%u x %u, nnz=%u) spmv -----\n", spmv_l.M, spmv_l.N, |
| 169 | + spmv_l.K); |
| 170 | + printf("Active cores: %u / %u\n", num_cores, num_cores_hw); |
| 171 | + printf("The first iter takes %u cycles.\n", timer_1iter); |
| 172 | + printf("The best execution took %u cycles.\n", timer_best); |
| 173 | + printf("Checksum: %f\n", checksum); |
| 174 | + printf("The performance is %lu OP/1000cycle (%lu%%o utilization).\n", |
| 175 | + performance, utilization); |
| 176 | + } |
| 177 | + |
| 178 | + if (errors) ret = -1; |
| 179 | + } |
| 180 | + |
| 181 | + snrt_cluster_hw_barrier(); |
| 182 | + set_eoc(); |
| 183 | + return ret; |
| 184 | +} |
0 commit comments