Repository navigation
Expand file tree
/
Copy pathfractional_gpu_testing.hpp
More file actions
252 lines (200 loc) · 5.89 KB
/
Copy pathfractional_gpu_testing.hpp
File metadata and controls
252 lines (200 loc) · 5.89 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* Contains helper functions for testing/benchmarking */
#ifndef __FRACTIONAL_GPU_TESTING_HPP__
#define __FRACTIONAL_GPU_TESTING_HPP__
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <algorithm>
#include <vector>
#define DEFAULT_NUM_ITERATION INT_MAX
#if defined(NDEBUG)
#define dprintf(...)
#else
#define dprintf(...) printf(__VA_ARGS__)
#endif
#define USECPSEC 1000000ULL
inline double dtime_usec(unsigned long long start)
{
timeval tv;
gettimeofday(&tv, 0);
return (double)(((tv.tv_sec*USECPSEC)+tv.tv_usec)-start);
}
/* For benchmarking applications */
typedef struct pstats {
double sum;
size_t count;
std::vector<double> vals;
} pstats_t;
inline void pstats_init(pstats_t *stats)
{
stats->count = stats->sum = 0;
stats->vals.clear();
}
inline void pstats_add_observation(pstats_t *stats, double time)
{
stats->count++;
stats->sum += time;
stats->vals.push_back(time);
}
inline void pstats_print(pstats_t *stats)
{
double median;
size_t count = stats->count;
size_t count_95p = ((double)(count) * 95.0) / 100.0;
size_t count_5p = ((double)(count) * 5.0) / 100.0;
if (count == 0) {
printf("STATS:No values\n");
return;
}
std::sort(stats->vals.begin(), stats->vals.begin() + stats->vals.size());
/* Count odd or even? */
if (count & 0x1) {
median = stats->vals[count / 2];
} else {
median = (stats->vals[count / 2 - 1] + stats->vals[count / 2]) / 2;
}
printf("STATS: Avg:%f, Median:%f, Min:%f, Max:%f, 5 percentile:%f, 95 percentile:%f, Count:%zu\n",
stats->sum / stats->count, median,
stats->vals[0], stats->vals[count - 1], stats->vals[count_5p], stats->vals[count_95p],
stats->count);
}
/*
* Do we just execute kernel in a loop? Helpful during benchmarking and creating
* background interference tasks.
*/
static bool execute_just_kernel = false;
/* To be called after initialization */
bool test_execute_just_kernel(void)
{
return execute_just_kernel;
}
#if defined(USE_FGPU)
#include <fractional_gpu.hpp>
void print_usage(char **argv)
{
fprintf(stderr, "Usage: %s -c <color> -m <memory size> -i <number of iterations> [OPTIONS]\n"
"-k Execute only kernel (Default: Memcpy and kernels both executed)\n",
argv[0]);
fprintf(stderr, "Exiting\n");
exit(-1);
}
/* Parse arguments to find color, memory size and number of iterations */
static inline void test_initialize(int argc, char **argv, int *out_num_iterations,
bool do_init = true)
{
int opt, ret;
int color, num_iterations;
size_t mem_size;
/* Set default values */
color = fgpu_get_env_color();
mem_size = fgpu_get_env_color_mem_size();
num_iterations = DEFAULT_NUM_ITERATION;
while ((opt = getopt(argc, argv, "c:i:m:k")) != -1) {
switch (opt) {
case 'c':
color = atoi(optarg);
break;
case 'm':
mem_size = atoll(optarg);
break;
case 'i':
num_iterations = atoi(optarg);
break;
case 'k':
execute_just_kernel = true;
break;
default: /* '?' */
fprintf(stderr, "Invalid arguments found\n");
print_usage(argv);
fprintf(stderr, "Exiting\n");
exit(EXIT_FAILURE);
}
}
printf("Configuration:\n");
printf("Computational Coloring:\t");
#if defined(FGPU_COMP_COLORING_ENABLE)
printf("Enabled\n");
#else
printf("Disabled\n");
#endif
printf("Memory Coloring:\t");
#if defined(FGPU_MEM_COLORING_ENABLED)
printf("Enabled\n");
#else
printf("Disabled\n");
#endif
printf("User memory coloring:\t");
#if defined(FGPU_USER_MEM_COLORING_ENABLED)
printf("Enabled\n");
#else
printf("Disabled\n");
#endif
printf("Test memory coloring:\t");
#if defined(FGPU_TEST_MEM_COLORING_ENABLED)
printf("Enabled\n");
#else
printf("Disabled\n");
#endif
if (do_init) {
ret = fgpu_init();
if (ret < 0) {
fprintf(stderr, "Exiting as can't initialize fgpu\n");
exit(EXIT_FAILURE);
}
ret = fgpu_set_color_prop(color, mem_size);
if (ret < 0) {
fprintf(stderr, "Exiting as unable to set color property\n");
exit(EXIT_FAILURE);
}
}
printf("Color:\t%d\n", color);
printf("Memory:\t%zd\n", mem_size);
printf("Iterations:\t%d\n", num_iterations);
printf("Color Initialized Delayed:%s\n", do_init ? "FALSE" : "TRUE");
*out_num_iterations = num_iterations;
}
static inline void test_deinitialize()
{
fgpu_deinit();
}
#else /* USE_FGPU */
void print_usage(char **argv)
{
fprintf(stderr, "Usage: %s -i <number of iterations> [OPTIONS]\n"
"-k Execute only kernel (Default: Memcpy and kernels both executed)\n",
argv[0]);
fprintf(stderr, "Exiting\n");
exit(-1);
}
/* Parse arguments to find color, memory size and number of iterations */
static inline void test_initialize(int argc, char **argv, int *out_num_iterations,
bool do_init = false)
{
int opt;
int num_iterations;
num_iterations = DEFAULT_NUM_ITERATION;
while ((opt = getopt(argc, argv, "i:k")) != -1) {
switch (opt) {
case 'i':
num_iterations = atoi(optarg);
break;
case 'k':
execute_just_kernel = true;
default: /* '?' */
fprintf(stderr, "Invalid arguments found\n");
print_usage(argv);
fprintf(stderr, "Exiting\n");
exit(EXIT_FAILURE);
}
}
printf("Configuration:\n");
printf("Overall Coloring: Disabled\n");
printf("Iterations:\t%d\n", num_iterations);
*out_num_iterations = num_iterations;
}
static inline void test_deinitialize()
{
}
#endif /* USE_FGPU */
#endif /* __FRACTIONAL_GPU_TESTING_HPP__ */