forked from google/XNNPACK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cc
458 lines (409 loc) · 13.5 KB
/
utils.cc
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <mutex>
#ifdef __linux__
#include <sched.h>
#endif
#if defined(__ANDROID__) || defined(_WIN32) || defined(__CYGWIN__)
#include <malloc.h>
#endif
#if defined(__SSE__) || defined(__x86_64__)
#include <xmmintrin.h>
#endif
#include <cpuinfo.h>
#include <xnnpack.h>
#include <xnnpack/common.h>
#include <xnnpack/allocator.h>
#include <xnnpack/config.h>
#include "bench/utils.h"
static void* wipe_buffer = nullptr;
static size_t wipe_buffer_size = 0;
static std::once_flag wipe_buffer_guard;
static void InitWipeBuffer() {
// Default: the largest know cache size (128 MB Intel Crystalwell L4 cache).
wipe_buffer_size = 128 * 1024 * 1024;
if (cpuinfo_initialize()) {
wipe_buffer_size = benchmark::utils::GetMaxCacheSize();
}
#if defined(_WIN32)
wipe_buffer = _aligned_malloc(wipe_buffer_size, 128);
#elif defined(__ANDROID__) || defined(__CYGWIN__)
// memalign is obsolete, but it is the only option on Android until API level 17.
wipe_buffer = memalign(128, wipe_buffer_size);
#else
(void) posix_memalign((void**) &wipe_buffer, 128, wipe_buffer_size);
#endif
if (wipe_buffer != nullptr) {
memset(wipe_buffer, 0xA5, wipe_buffer_size);
}
}
namespace benchmark {
namespace utils {
uint32_t PrefetchToL1(const void* ptr, size_t size) {
uint32_t step = 16;
if (cpuinfo_initialize()) {
step = cpuinfo_get_l1d_cache(0)->line_size;
}
const uint8_t* u8_ptr = static_cast<const uint8_t*>(ptr);
// Compute and return sum of data to prevent compiler from removing data reads.
uint32_t sum = 0;
while (size >= step) {
sum += uint32_t(*u8_ptr);
u8_ptr += step;
size -= step;
}
return sum;
}
uint32_t WipeCache() {
std::call_once(wipe_buffer_guard, InitWipeBuffer);
return PrefetchToL1(wipe_buffer, wipe_buffer_size);
}
void DisableDenormals() {
#if defined(__SSE__) || defined(__x86_64__)
_mm_setcsr(_mm_getcsr() | 0x8040);
#elif defined(__arm__) && defined(__ARM_FP) && (__ARM_FP != 0)
uint32_t fpscr;
#if defined(__thumb__) && !defined(__thumb2__)
__asm__ __volatile__(
"VMRS %[fpscr], fpscr\n"
"ORRS %[fpscr], %[bitmask]\n"
"VMSR fpscr, %[fpscr]\n"
: [fpscr] "=l" (fpscr)
: [bitmask] "l" (0x1000000)
: "cc");
#else
__asm__ __volatile__(
"VMRS %[fpscr], fpscr\n"
"ORR %[fpscr], #0x1000000\n"
"VMSR fpscr, %[fpscr]\n"
: [fpscr] "=r" (fpscr));
#endif
#elif defined(__aarch64__)
uint64_t fpcr;
__asm__ __volatile__(
"MRS %[fpcr], fpcr\n"
"ORR %w[fpcr], %w[fpcr], 0x1000000\n"
"ORR %w[fpcr], %w[fpcr], 0x80000\n"
"MSR fpcr, %[fpcr]\n"
: [fpcr] "=r" (fpcr));
#endif
}
// Return clockrate in Hz
uint64_t GetCurrentCpuFrequency() {
#ifdef __linux__
int freq = 0;
char cpuinfo_name[512];
int cpu = sched_getcpu();
snprintf(cpuinfo_name, sizeof(cpuinfo_name),
"/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", cpu);
FILE* f = fopen(cpuinfo_name, "r");
if (f) {
if (fscanf(f, "%d", &freq)) {
fclose(f);
return uint64_t(freq) * 1000;
}
fclose(f);
}
#endif // __linux__
return 0;
}
size_t GetMaxCacheSize() {
if (!cpuinfo_initialize()) {
#if CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64
// DynamIQ max: 4 MB
return 4 * 1024 * 1024;
#else
// Intel eDRAM max: 128 MB
return 128 * 1024 * 1024;
#endif
}
return cpuinfo_get_max_cache_size();
}
void MultiThreadingParameters(benchmark::internal::Benchmark* benchmark) {
benchmark->ArgName("T");
// Disabled thread pool (execution on the caller thread only).
benchmark->Arg(1);
#if !XNN_PLATFORM_WINDOWS || !(XNN_ARCH_ARM || XNN_ARCH_ARM64)
if (cpuinfo_initialize()) {
// All cores except the little ones.
uint32_t max_cores = cpuinfo_get_cores_count();
if (cpuinfo_get_clusters_count() > 1) {
max_cores -= cpuinfo_get_cluster(cpuinfo_get_clusters_count() - 1)->core_count;
}
for (uint32_t t = 2; t <= max_cores; t++) {
benchmark->Arg(t);
}
// All cores (if more than one cluster).
if (cpuinfo_get_cores_count() > max_cores) {
benchmark->Arg(cpuinfo_get_cores_count());
}
// All cores + hyperthreads (only if hyperthreading supported).
if (cpuinfo_get_processors_count() > cpuinfo_get_cores_count()) {
benchmark->Arg(cpuinfo_get_processors_count());
}
}
#endif // !XNN_PLATFORM_WINDOWS || !(XNN_ARCH_ARM || XNN_ARCH_ARM64)
}
#if XNN_ARCH_ARM
bool CheckVFP(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !(hardware_config->use_arm_vfpv2 || hardware_config->use_arm_vfpv3)) {
state.SkipWithError("no VFP extension");
return false;
}
return true;
}
#endif // XNN_ARCH_ARM
#if XNN_ARCH_ARM
bool CheckARMV6(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_arm_v6) {
state.SkipWithError("no ARMv6 extension");
return false;
}
return true;
}
#endif // XNN_ARCH_ARM
#if XNN_ARCH_ARM || XNN_ARCH_ARM64
bool CheckFP16ARITH(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_arm_fp16_arith) {
state.SkipWithError("no FP16-ARITH extension");
return false;
}
return true;
}
#endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
#if XNN_ARCH_ARM || XNN_ARCH_ARM64
bool CheckNEON(benchmark::State& state) {
#if XNN_ARCH_ARM64
return true;
#else
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_arm_neon) {
state.SkipWithError("no NEON extension");
return false;
}
return true;
#endif
}
#endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
#if XNN_ARCH_ARM || XNN_ARCH_ARM64
bool CheckNEONFP16(benchmark::State& state) {
#if XNN_ARCH_ARM64
return true;
#else
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_arm_neon_fp16) {
state.SkipWithError("no NEON-FP16 extension");
return false;
}
return true;
#endif
}
#endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
#if XNN_ARCH_ARM || XNN_ARCH_ARM64
bool CheckNEONFMA(benchmark::State& state) {
#if XNN_ARCH_ARM64
return true;
#else
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_arm_neon_fma) {
state.SkipWithError("no NEON-FMA extension");
return false;
}
return true;
#endif
}
#endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
#if XNN_ARCH_ARM || XNN_ARCH_ARM64
bool CheckNEONV8(benchmark::State& state) {
#if XNN_ARCH_ARM64
return true;
#else
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_arm_neon_v8) {
state.SkipWithError("no NEON-V8 extension");
return false;
}
return true;
#endif
}
#endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
#if XNN_ARCH_ARM || XNN_ARCH_ARM64
bool CheckNEONFP16ARITH(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_arm_neon_fp16_arith) {
state.SkipWithError("no NEON-FP16-ARITH extension");
return false;
}
return true;
}
#endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
#if XNN_ARCH_ARM || XNN_ARCH_ARM64
bool CheckNEONBF16(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_arm_neon_bf16) {
state.SkipWithError("no NEON-BF16 extension");
return false;
}
return true;
}
#endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
#if XNN_ARCH_ARM || XNN_ARCH_ARM64
bool CheckNEONDOT(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_arm_neon_dot) {
state.SkipWithError("no NEON-DOT extension");
return false;
}
return true;
}
#endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
#if XNN_ARCH_RISCV
bool CheckRVV(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_riscv_vector) {
state.SkipWithError("no V extension");
return false;
}
return true;
}
#endif // XNN_ARCH_RISCV
#if XNN_ARCH_X86 || XNN_ARCH_X86_64
bool CheckSSSE3(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_x86_ssse3) {
state.SkipWithError("no SSSE3 extension");
return false;
}
return true;
}
#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
#if XNN_ARCH_X86 || XNN_ARCH_X86_64
bool CheckSSE41(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_x86_sse4_1) {
state.SkipWithError("no SSE4.1 extension");
return false;
}
return true;
}
#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
#if XNN_ARCH_X86 || XNN_ARCH_X86_64
bool CheckAVX(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_x86_avx) {
state.SkipWithError("no AVX extension");
return false;
}
return true;
}
#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
#if XNN_ARCH_X86 || XNN_ARCH_X86_64
bool CheckF16C(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_x86_f16c) {
state.SkipWithError("no F16C extension");
return false;
}
return true;
}
#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
#if XNN_ARCH_X86 || XNN_ARCH_X86_64
bool CheckXOP(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_x86_xop) {
state.SkipWithError("no XOP extension");
return false;
}
return true;
}
#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
#if XNN_ARCH_X86 || XNN_ARCH_X86_64
bool CheckFMA3(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_x86_fma3) {
state.SkipWithError("no FMA3 extension");
return false;
}
return true;
}
#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
#if XNN_ARCH_X86 || XNN_ARCH_X86_64
bool CheckAVX2(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_x86_avx2) {
state.SkipWithError("no AVX2 extension");
return false;
}
return true;
}
#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
#if XNN_ARCH_X86 || XNN_ARCH_X86_64
bool CheckAVX512F(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_x86_avx512f) {
state.SkipWithError("no AVX512F extension");
return false;
}
return true;
}
#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
#if XNN_ARCH_X86 || XNN_ARCH_X86_64
bool CheckAVX512SKX(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_x86_avx512skx) {
state.SkipWithError("no AVX512 SKX extensions");
return false;
}
return true;
}
#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
#if XNN_ARCH_X86 || XNN_ARCH_X86_64
bool CheckAVX512VBMI(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_x86_avx512vbmi) {
state.SkipWithError("no AVX512 VBMI extension");
return false;
}
return true;
}
#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
#if XNN_ARCH_WASMRELAXEDSIMD
bool CheckWAsmPSHUFB(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_wasm_pshufb) {
state.SkipWithError("no WAsm PSHUFB support");
return false;
}
return true;
}
#endif // XNN_ARCH_WASMRELAXEDSIMD
#if XNN_ARCH_WASMRELAXEDSIMD
bool CheckWAsmSDOT(benchmark::State& state) {
const xnn_hardware_config* hardware_config = xnn_init_hardware_config();
if (hardware_config == nullptr || !hardware_config->use_wasm_sdot) {
state.SkipWithError("no WAsm SDOT support");
return false;
}
return true;
}
#endif // XNN_ARCH_WASMRELAXEDSIMD
#if XNN_PLATFORM_JIT
CodeMemoryHelper::CodeMemoryHelper() {
status = xnn_allocate_code_memory(&buffer, XNN_DEFAULT_CODE_BUFFER_SIZE);
}
CodeMemoryHelper::~CodeMemoryHelper() {
if (status == xnn_status_success) {
xnn_release_code_memory(&buffer);
}
}
#endif // XNN_PLATFORM_JIT
} // namespace utils
} // namespace benchmark