forked from sysprog21/compute-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_clock_gettime.c
147 lines (129 loc) · 4.29 KB
/
benchmark_clock_gettime.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "computepi.h"
#define CLOCK_ID CLOCK_MONOTONIC_RAW
#define ONE_SEC 1000000000.0
#define compute_error(pi) ( (pi > M_PI) ? pi - M_PI : M_PI - pi)
int main(int argc, char const *argv[])
{
struct timespec start = {0, 0};
struct timespec end = {0, 0};
double time_record;
double min, max;
FILE *fp_time = fopen("result_clock_gettime.csv", "a");
FILE *fp_error = fopen("error_rate.txt", "a");
FILE *fp_ci = fopen("ci.txt", "r");
if (argc < 2) return -1;
int N = atoi(argv[1]);
//SAMPLE_SIZE define in "compute.h"
int i, loop = SAMPLE_SIZE;
double mean = 0.0;
// Baseline
mean = 0.0;
fscanf(fp_ci, "%lf %lf", &min, &max);
for(i = 0; i < loop; i++) {
clock_gettime(CLOCK_ID, &start);
compute_pi_baseline(N);
clock_gettime(CLOCK_ID, &end);
time_record = (double)(end.tv_sec - start.tv_sec) +
(end.tv_nsec - start.tv_nsec)/ONE_SEC;
if(time_record < min && time_record > max)
i--;
else
mean += time_record;
}
fprintf(fp_error, "%d %lf ", N, compute_error(compute_pi_baseline(N))/M_PI);
mean /= (double)SAMPLE_SIZE;
fprintf(fp_time, "%d, %lf, ", N, mean);
// OpenMP with 2 threads
mean = 0.0;
fscanf(fp_ci, "%lf %lf", &min, &max);
for(i = 0; i < loop; i++) {
clock_gettime(CLOCK_ID, &start);
compute_pi_openmp(N, 2);
clock_gettime(CLOCK_ID, &end);
time_record = (double)(end.tv_sec - start.tv_sec) +
(end.tv_nsec - start.tv_nsec)/ONE_SEC;
if(time_record < min && time_record > max)
i--;
else
mean += time_record;
}
fprintf(fp_error, "%lf ", compute_error(compute_pi_openmp(N, 2))/M_PI);
mean /= (double)SAMPLE_SIZE;
fprintf(fp_time, "%lf, ", mean);
// OpenMP with 4 threads
mean = 0.0;
fscanf(fp_ci, "%lf %lf", &min, &max);
for(i = 0; i < loop; i++) {
clock_gettime(CLOCK_ID, &start);
compute_pi_openmp(N, 4);
clock_gettime(CLOCK_ID, &end);
time_record = (double)(end.tv_sec - start.tv_sec) +
(end.tv_nsec - start.tv_nsec)/ONE_SEC;
if(time_record < min && time_record > max)
i--;
else
mean += time_record;
}
fprintf(fp_error, "%lf ", compute_error(compute_pi_openmp(N, 4))/M_PI);
mean /= (double)SAMPLE_SIZE;
fprintf(fp_time, "%lf, ", mean);
// AVX SIMD
mean = 0.0;
fscanf(fp_ci, "%lf %lf", &min, &max);
for(i = 0; i < loop; i++) {
clock_gettime(CLOCK_ID, &start);
compute_pi_avx(N);
clock_gettime(CLOCK_ID, &end);
time_record = (double)(end.tv_sec - start.tv_sec) +
(end.tv_nsec - start.tv_nsec)/ONE_SEC;
if(time_record < min && time_record > max)
i--;
else
mean += time_record;
}
fprintf(fp_error, "%lf ", compute_error(compute_pi_avx(N))/M_PI);
mean /= (double)SAMPLE_SIZE;
fprintf(fp_time, "%lf, ", mean);
// AVX SIMD + Loop unrolling
mean = 0.0;
fscanf(fp_ci, "%lf %lf", &min, &max);
for(i = 0; i < loop; i++) {
clock_gettime(CLOCK_ID, &start);
compute_pi_avx_unroll(N);
clock_gettime(CLOCK_ID, &end);
time_record = (double)(end.tv_sec - start.tv_sec) +
(end.tv_nsec - start.tv_nsec)/ONE_SEC;
if(time_record < min && time_record > max)
i--;
else
mean += time_record;
}
fprintf(fp_error, "%lf ", compute_error(compute_pi_avx_unroll(N))/M_PI);
mean /= (double)SAMPLE_SIZE;
fprintf(fp_time, "%lf, ", mean);
// LEIBNIZ
mean = 0.0;
fscanf(fp_ci, "%lf %lf", &min, &max);
for(i = 0; i < loop; i++) {
clock_gettime(CLOCK_ID, &start);
compute_pi_leibniz(N);
clock_gettime(CLOCK_ID, &end);
time_record = (double)(end.tv_sec - start.tv_sec) +
(end.tv_nsec - start.tv_nsec)/ONE_SEC;
if(time_record < min && time_record > max)
i--;
else
mean += time_record;
}
fprintf(fp_error, "%lf\n", compute_error(compute_pi_leibniz(N))/M_PI);
mean /= (double)SAMPLE_SIZE;
fprintf(fp_time, "%lf\n", mean);
fclose(fp_time);
fclose(fp_error);
fclose(fp_ci);
return 0;
}