-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests-256.c
236 lines (208 loc) · 8.11 KB
/
tests-256.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "assert.h"
#include "stdatomic.h"
#include "stdint.h"
#include "stdio.h"
#include "sys/sysinfo.h"
#include "immintrin.h"
#include "cblas.h"
#include "SIMDxorshift/include/simdxorshift128plus.h"
static uint64_t CPUS;
static _Atomic(uint64_t)* ERRORS;
static avx_xorshift128plus_key_t rng;
#define FOR_EACH_BLOCK_UP \
_Pragma("omp parallel for schedule(static)") \
for(ssize_t i = 0; i < CPUS; i++) \
for(ssize_t j = 0; j < (size / CPUS); j += 32)
#define FOR_EACH_BLOCK_DOWN \
_Pragma("omp parallel for schedule(static)") \
for(ssize_t i = CPUS - 1; i >= 0; i--) \
for(ssize_t j = (size / CPUS) - 32; j >= 0; j -= 32)
#define BLOCK_IDX (j + i * (size / CPUS))
void avx2_tests_init(size_t cpus, _Atomic(uint64_t)* errors) {
CPUS = cpus;
ERRORS = errors;
unsigned long long r1 = 0, r2 = 0;
while(r1 == 0 && r2 == 0) {
assert(_rdrand64_step(&r1));
assert(_rdrand64_step(&r2));
}
avx_xorshift128plus_init(r1, r2, &rng);
}
static inline void get(const char* const restrict mem, const size_t idx, const __m256i expected) {
const __m256i actual = _mm256_load_si256((__m256i*)&mem[idx]);
const __m256i cmp = _mm256_cmpeq_epi8(expected, actual);
const int result = _mm256_testz_si256(cmp, cmp);
if(__builtin_expect(result != 0, 0)) {
const uint64_t error_total = _mm_popcnt_u64(result);
fprintf(stderr, "errors detected at offset 0x%016lx\n", idx);
atomic_fetch_add(ERRORS, error_total);
}
}
static inline void get_all_up(const char* const restrict mem, const size_t size, const __m256i expected) {
FOR_EACH_BLOCK_UP {
get(mem, BLOCK_IDX, expected);
}
}
static inline void get_all_down(const char* const restrict mem, const size_t size, const __m256i expected) {
FOR_EACH_BLOCK_DOWN {
get(mem, BLOCK_IDX, expected);
}
}
static inline void set(char* const restrict mem, const size_t idx, const __m256i val) {
_mm256_stream_si256((__m256i*)&mem[idx], val);
}
static inline void set_all_up(char* const restrict mem, const size_t size, const __m256i val) {
FOR_EACH_BLOCK_UP {
set(mem, BLOCK_IDX, val);
}
}
static inline void set_all_down(char* const restrict mem, const size_t size, const __m256i val) {
FOR_EACH_BLOCK_DOWN {
set(mem, BLOCK_IDX, val);
}
}
void avx2_basic_tests(void* const restrict mem, const size_t size) {
const uint8_t patterns[] = { 0x00, 0xFF, 0x0F, 0xF0, 0x55, 0xAA, };
for(size_t i = 0; i < sizeof(patterns) / sizeof(uint8_t); i++) {
const __m256i pattern = _mm256_set1_epi8(patterns[i]);
set_all_up(mem, size, pattern);
get_all_up(mem, size, pattern);
set_all_down(mem, size, pattern);
get_all_down(mem, size, pattern);
}
}
void avx2_march(void* const restrict mem, const size_t size) {
for(size_t _ = 0; _ < 2; _++) {
const __m256i ones = _mm256_set1_epi8(0xFF);
const __m256i zeroes = _mm256_set1_epi8(0x00);
FOR_EACH_BLOCK_DOWN {
set(mem, j + i * (size / CPUS), zeroes);
}
FOR_EACH_BLOCK_UP {
get(mem, BLOCK_IDX, zeroes);
set(mem, BLOCK_IDX, ones);
get(mem, BLOCK_IDX, ones);
set(mem, BLOCK_IDX, zeroes);
get(mem, BLOCK_IDX, zeroes);
set(mem, BLOCK_IDX, ones);
}
FOR_EACH_BLOCK_UP {
get(mem, BLOCK_IDX, ones);
set(mem, BLOCK_IDX, zeroes);
set(mem, BLOCK_IDX, ones);
}
FOR_EACH_BLOCK_DOWN {
get(mem, BLOCK_IDX, ones);
set(mem, BLOCK_IDX, zeroes);
set(mem, BLOCK_IDX, ones);
set(mem, BLOCK_IDX, zeroes);
}
FOR_EACH_BLOCK_DOWN {
get(mem, BLOCK_IDX, zeroes);
set(mem, BLOCK_IDX, ones);
set(mem, BLOCK_IDX, zeroes);
}
}
}
void avx2_random_inversions(void* const restrict mem, const size_t size) {
for(size_t i = 0; i < 16; i++) {
const __m256i pattern = avx_xorshift128plus(&rng);
set_all_up(mem, size, pattern);
get_all_up(mem, size, pattern);
const __m256i not_pattern = _mm256_xor_si256(pattern, _mm256_set1_epi8(0xFF));
set_all_up(mem, size, not_pattern);
get_all_up(mem, size, not_pattern);
}
}
static void moving_inversions_template(void* const restrict mem, const size_t size, const size_t iters, __m256i (*shift)(__m256i, int), const __m256i initial) {
for(size_t i = 0; i < iters; i++) {
const __m256i pattern = shift(initial, i);
set_all_up(mem, size, pattern);
get_all_up(mem, size, pattern);
const __m256i not_pattern = _mm256_xor_si256(pattern, _mm256_set1_epi8(0xFF));
set_all_up(mem, size, not_pattern);
get_all_up(mem, size, not_pattern);
}
}
void avx2_moving_inversions_left_64(void* const restrict mem, const size_t size) {
return moving_inversions_template(mem, size, 64, _mm256_slli_epi64, _mm256_set1_epi64x(0x0000000000000001));
}
void avx2_moving_inversions_right_32(void* const restrict mem, const size_t size) {
return moving_inversions_template(mem, size, 32, _mm256_srli_epi64, _mm256_set1_epi32(0x80000000));
}
void avx2_moving_inversions_left_16(void* const restrict mem, const size_t size) {
return moving_inversions_template(mem, size, 16, _mm256_slli_epi64, _mm256_set1_epi16(0x0001));
}
void avx2_moving_inversions_right_8(void* const restrict mem, const size_t size) {
return moving_inversions_template(mem, size, 8, _mm256_srli_epi64, _mm256_set1_epi8(0x80));
}
void avx2_moving_inversions_left_4(void* const restrict mem, const size_t size) {
return moving_inversions_template(mem, size, 4, _mm256_slli_epi64, _mm256_set1_epi8(0x11));
}
void avx2_moving_saturations_right_16(void* const restrict mem, const size_t size) {
for(size_t i = 0; i < 16; i++) {
const __m256i pattern = _mm256_srli_epi16(_mm256_set1_epi16(0x8000), i);
set_all_up(mem, size, pattern);
get_all_up(mem, size, pattern);
const __m256i zeroes = _mm256_set1_epi8(0x00);
set_all_up(mem, size, zeroes);
get_all_up(mem, size, zeroes);
set_all_up(mem, size, pattern);
get_all_up(mem, size, pattern);
const __m256i ones = _mm256_set1_epi8(0xFF);
set_all_up(mem, size, ones);
get_all_up(mem, size, ones);
}
}
void avx2_moving_saturations_left_8(void* const restrict mem, const size_t size) {
for(size_t i = 0; i < 8; i++) {
const __m256i pattern = _mm256_srli_epi16(_mm256_set1_epi16(0x01), i);
set_all_up(mem, size, pattern);
get_all_up(mem, size, pattern);
const __m256i zeroes = _mm256_set1_epi8(0x00);
set_all_up(mem, size, zeroes);
get_all_up(mem, size, zeroes);
set_all_up(mem, size, pattern);
get_all_up(mem, size, pattern);
const __m256i ones = _mm256_set1_epi8(0xFF);
set_all_up(mem, size, ones);
get_all_up(mem, size, ones);
}
}
void avx2_addressing(void* const restrict mem, const size_t size) {
for(size_t _ = 0; _ < 16; _++) {
__m256i increasing = _mm256_set_epi64x(24, 16, 8, 0);
FOR_EACH_BLOCK_UP {
set(mem, BLOCK_IDX, _mm256_add_epi64(_mm256_set1_epi64x(BLOCK_IDX), increasing));
}
FOR_EACH_BLOCK_UP {
get(mem, BLOCK_IDX, _mm256_add_epi64(_mm256_set1_epi64x(BLOCK_IDX), increasing));
}
FOR_EACH_BLOCK_DOWN {
set(mem, BLOCK_IDX, _mm256_add_epi64(_mm256_set1_epi64x(BLOCK_IDX), increasing));
}
FOR_EACH_BLOCK_DOWN {
get(mem, BLOCK_IDX, _mm256_add_epi64(_mm256_set1_epi64x(BLOCK_IDX), increasing));
}
}
}
void avx2_sgemm(char* const restrict mem, const size_t size) {
const __m256 zeroes = _mm256_set1_ps(0.0f);
set_all_down(mem, size, (__m256i) zeroes);
for(ssize_t _ = 0; _ < 32; _++) {
_Pragma("omp parallel for schedule(static)")
for(ssize_t i = 0; i < CPUS; i++) {
for(ssize_t j = 64 * 64 * 4 * 2; j < (size / CPUS); j += 64 * 64 * 4) {
float* const a = (float*) &mem[BLOCK_IDX - 64 * 64 * 4 * 2];
float* const b = (float*) &mem[BLOCK_IDX - 64 * 64 * 4 * 1];
float* const c = (float*) &mem[BLOCK_IDX - 64 * 64 * 4 * 0];
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, 64, 64, 64, 1.0, a, 64, b, 64, 0.0, c, 64);
for(ssize_t k = 0; k < 64 * 64 * 4; k += 64) {
_mm_clflushopt(&mem[BLOCK_IDX + k]);
}
_mm_sfence();
}
}
}
get_all_up(mem, size, (__m256i) zeroes);
}