forked from savamarc/MRGspeedtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harness.c
118 lines (98 loc) · 3.16 KB
/
harness.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
/*
* Copyright (C) 2014 Sebastiano Vigna
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <sys/time.h>
#include <sys/resource.h>
#ifdef PAPI
#include <papi.h>
#endif
// This statement is executed, if necessary, to initialize the generator
#ifndef INIT
#define INIT
#endif
// This statement is executed at the end of the test
#ifndef TAIL
#define TAIL ;
#endif
// We measure this function
#ifndef NEXT
#define NEXT next()
#endif
uint64_t get_user_time(void) {
struct rusage rusage;
getrusage(0, &rusage);
return rusage.ru_utime.tv_sec * 1000000000ULL + rusage.ru_utime.tv_usec * 1000ULL;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "USAGE: %s ITERATIONS\n", argv[0]);
exit(1);
}
const long long int n = strtoll(argv[1], NULL, 0);
INIT // Here you can optionally inject initialization code
#ifdef BLOCK
HEAD
const uint64_t start_time = get_user_time();
for(int64_t i = n; i-- != 0;) {
NEXT; // Measurement + XOR
}
const uint64_t time_delta = (get_user_time() - start_time);
printf("%s: %.03f s, %.03f GB/s, %.03f words/ns, %.03f ns/word\n", argv[0], time_delta / 1E9, (n * BLOCK * 8.) / time_delta, (n * BLOCK) / (double)time_delta, time_delta / (double)(n * BLOCK));
#else
#ifdef PAPI
const uint64_t start_cycle = PAPI_get_real_cyc();
#else
const uint64_t start_time = get_user_time();
#endif
uint64_t e = -1;
#ifdef HARNESS_DOUBLE
//union { double d; uint64_t i; } uu;
double sum=0;
// next() returns a double
for(int64_t i = n; i-- != 0;) {
sum += NEXT; // Measurement
//printf("%f\n", uu.d);
//e ^= uu.i;
}
printf("Sum: %f\n", sum);
#else
// next() returns a 64-bit integer
for(int64_t i = n; i-- != 0;)
#ifdef HARNESS_ADD
e += NEXT; // Measurement
#else
e ^= NEXT; // Measurement
#endif
#endif
#ifdef PAPI
const uint64_t cycles = (PAPI_get_real_cyc() - start_cycle);
printf("%s: %" PRId64 " cycles, %.03f B/cycle, %.03f cycles/B, %.03f words/cycle, %.03f cycles/word\n", argv[0], cycles, (n * 8.) / cycles, cycles / (n * 8.), n / (double)cycles, cycles / (double)n);
#else
const uint64_t time_delta = (get_user_time() - start_time);
printf("%s: %.03f s, %.03f GB/s, %.03f words/ns, %.03f ns/word\n", argv[0], time_delta / 1E9, (n * 8.) / time_delta, n / (double)time_delta, time_delta / (double)n);
#endif
const volatile uint64_t unused = e;
#endif
TAIL // Here you can add instructions that must be executed at the end, usually to avoid dead-code elimination
return 0;
}