-
Notifications
You must be signed in to change notification settings - Fork 3
/
triad-cpu.h
61 lines (51 loc) · 1.48 KB
/
triad-cpu.h
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
#pragma once
#include <omp.h>
//#include <immintrin.h>
#ifdef __INTEL_COMPILER
#define DECLARE_ALIGNED(p, a) __assume_aligned(p, a)
#elif defined __GNUC__
#define DECLARE_ALIGNED(p, a) p = __builtin_assume_aligned(p, a)
#else
// Ignore if we're using an unsupported compiler
#define DECLARE_ALIGNED(p, a)
#endif
#ifndef __INTEL_COMPILER
#if __STDC_VERSION__ == 201112L
void * _mm_malloc(size_t s, size_t n) { return aligned_alloc(n, s); }
void _mm_free(void* p) { free(p); }
#endif
#endif
double cache_triad(size_t n, size_t nreps)
{
const double scalar = 2.0f;
double tot_mem_bw = 0.0;
#pragma omp parallel reduction(+ : tot_mem_bw)
{
double* restrict a = (double*)_mm_malloc(sizeof(double) * n, 64);
double* restrict b = (double*)_mm_malloc(sizeof(double) * n, 64);
double* restrict c = (double*)_mm_malloc(sizeof(double) * n, 64);
DECLARE_ALIGNED(a, 64);
DECLARE_ALIGNED(b, 64);
DECLARE_ALIGNED(c, 64);
// This should place a,b,c in cache
for (int i = 0; i < n; ++i) {
a[i] = 0.0;
b[i] = 3.0;
c[i] = 2.0;
}
double t0 = omp_get_wtime();
for (int t = 0; t < nreps; ++t) {
#pragma omp simd aligned(a : 64) aligned(b : 64) aligned(c : 64)
for (int i = 0; i < n; ++i) {
a[i] += b[i] + scalar * c[i];
}
}
double t1 = omp_get_wtime();
double mem_bw = (4.0 * sizeof(double) * n) / ((t1 - t0) / nreps) / 1e9;
tot_mem_bw += mem_bw;
_mm_free(a);
_mm_free(b);
_mm_free(c);
}
return tot_mem_bw;
}