-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest_C_CPU_Performance.c
66 lines (54 loc) · 2.6 KB
/
Test_C_CPU_Performance.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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <omp.h>
const int n_trials = 1000000000; // Enough to keep cores busy for a while and observe a steady state
const int flops_per_calc = 2; // Multiply + add = 2 instructions
const int n_chained_fmas = 20; // Must be tuned for architectures here and in blocks (R) and in (E)
const int VECTOR_WIDTH = 8;
double time_difference=0.0;
struct timespec start, end; //C11 only, new method to measure time.
const int align_length = VECTOR_WIDTH*sizeof(double);
//const int align_length = sizeof(double);
int main() {
#pragma omp parallel
{
register double *fa = aligned_alloc(align_length, VECTOR_WIDTH*n_chained_fmas*sizeof(double));
register double *fb = aligned_alloc(align_length, VECTOR_WIDTH*sizeof(double));
register double *fc = aligned_alloc(align_length, VECTOR_WIDTH*sizeof(double));
#pragma omp simd simdlen(VECTOR_WIDTH) aligned(fa:align_length)
for(int i = 0; i < VECTOR_WIDTH*n_chained_fmas; i++){
fa[i] = 0.0;
}
#pragma omp simd simdlen(VECTOR_WIDTH) aligned(fb, fc:align_length)
for(int i = 0; i < VECTOR_WIDTH; i++){
fb[i] = 0.5;
fc[i] = 1.0;
}
#pragma omp master
{
timespec_get(&start, 1/*TIME_UTC*/);
}
#pragma nounroll // Prevents automatic unrolling by compiler to avoid skewed benchmarks
for(int i = 0; i < n_trials; i++){
#pragma omp simd simdlen(VECTOR_WIDTH) aligned(fa, fb, fc:align_length)
for(int j = 0; j < VECTOR_WIDTH; j++){
#pragma unroll(n_chained_fmas)
for(int k = 0; k < VECTOR_WIDTH*n_chained_fmas; k+=VECTOR_WIDTH){
//fa[i*VECTOR_WIDTH*n_chained_fmas + k+j] = fa[i*VECTOR_WIDTH*n_chained_fmas + k+j]*fb[j] + fc[j];
fa[k+j] = fa[k+j]*fb[j] + fc[j];
}
}
}
#pragma omp master
{
timespec_get(&end, 1/*TIME_UTC*/);
time_difference = (double)(end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1.0e9;
//printf("The time difference is %.9f\n", time_difference);
printf("The number of threads is %i\n", omp_get_max_threads());
}
}
const double gflops = 1.0e-9*(double)VECTOR_WIDTH*(double)n_trials*(double)flops_per_calc*(double)omp_get_max_threads()*(double)n_chained_fmas;
printf("Chained FMAs=%d, vector width=%d, GFLOPs=%.1f, time=%.6f s, performance=%.1f GFLOP/s\n", n_chained_fmas, VECTOR_WIDTH, gflops, time_difference, gflops/time_difference);
return 0;
}