forked from UoB-HPC/cachebw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriad-cpu.h
More file actions
53 lines (44 loc) · 1.28 KB
/
triad-cpu.h
File metadata and controls
53 lines (44 loc) · 1.28 KB
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
#pragma once
#include <omp.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
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*)aligned_alloc(64, sizeof(double) * n);
double* restrict b = (double*)aligned_alloc(64, sizeof(double) * n);
double* restrict c = (double*)aligned_alloc(64, sizeof(double) * n);
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;
free(a);
free(b);
free(c);
}
return tot_mem_bw;
}