-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.cc
199 lines (175 loc) · 4.78 KB
/
benchmark.cc
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
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <getopt.h>
#include <omp.h>
#include "Timer.hh"
#include "ForceBarnesHut.hh"
using namespace std;
void makeCluster(Body *bodies, int N, double cx, double cy, double range){
for(int i = 0; i < N; i++){
double x;
double y;
do{
x = -1.0 + 2.0 * rand()/(RAND_MAX+1.0);
y = -1.0 + 2.0 * rand()/(RAND_MAX+1.0);
}while(x * x + y * y > 1.0);
x = cx + range * x;
y = cy + range * y;
double vx = 0.0;
double vy = 0.0;
double m = 1.0e-5;
bodies[i] = Body(x,y,vx,vy,m);
}
}
class ForceTest{
public:
ForceTest(int nbodies, int nqueries, double theta, unsigned int seed, int max_nthreads) :
nbodies_(nbodies),
nqueries_(nqueries),
theta_(theta),
seed_(seed),
max_nthreads_(max_nthreads)
{
double range = 2.0e-5;
srand(seed);
omp_set_num_threads(max_nthreads);
Body *bodies = new Body[nbodies];
makeCluster(bodies, nbodies, 0.0, 0.0, range);
queries_ = new Body[nqueries];
makeCluster(queries_, nqueries, 0.0, 0.0, range);
//Setting up the reference computation
Timer timer_setup;
timer_setup.start();
ForceBarnesHut f(bodies, nbodies, theta);
time_setup_ = timer_setup.stop();
//Running the reference computation
Timer timer_queries;
timer_queries.start();
#pragma omp parallel for
for(int i = 0; i < nqueries; i++)
f(&queries_[i]);
time_queries_ = timer_queries.stop();
delete[] bodies;
}
std::ostream& printFullJSON(std::ostream &os){
os<<"{\"nbodies\": "<<nbodies_<<",\n";
os<<"\"nqueries\": "<<nqueries_<<",\n";
os<<"\"theta\": "<<theta_<<",\n";
os<<"\"seed\": "<<seed_<<",\n";
if(max_nthreads_)
os<<"\"max_nthreads\": "<<max_nthreads_<<",\n";
else
os<<"\"max_nthreads\": null,\n";
os<<"\"time_setup\": "<<time_setup_<<",\n";
os<<"\"time_queries\": "<<time_queries_<<",\n";
os<<"\"fx\": [";
for(int i = 0; i < nqueries_; i++){
os<<queries_[i].fx();
if (i+1 < nqueries_)
os<<",\n";
}
os<<"],\n";
os<<"\"fy\": [";
for(int i = 0; i < nqueries_; i++){
os<<queries_[i].fy();
if (i+1 < nqueries_)
os<<",\n";
}
os<<"]}\n";
return os;
};
std::ostream& printSummaryJSON(std::ostream &os){
os<<"{\"nbodies\": "<<nbodies_<<",\n";
os<<"\"nqueries\": "<<nqueries_<<",\n";
os<<"\"theta\": "<<theta_<<",\n";
os<<"\"seed\": "<<seed_<<",\n";
if(max_nthreads_)
os<<"\"max_nthreads\": "<<max_nthreads_<<",\n";
else
os<<"\"max_nthreads\": null,\n";
os<<"\"time_setup\": "<<time_setup_<<",\n";
os<<"\"time_queries\": "<<time_queries_<<"}\n";
return os;
};
~ForceTest(){
delete[] queries_;
}
private:
int nbodies_;
int nqueries_;
double theta_;
unsigned int seed_;
int max_nthreads_;
double time_setup_;
double time_queries_;
Body *queries_;
};
#define USAGE \
"usage:\n" \
" driver [options]\n" \
"options:\n" \
" -o [filename] Output file (Default: stdout)\n" \
" -n [nbodies] Number of bodies (Default: 0.1)\n" \
" -s [seed] Seed for randomization (Default: 5)\n" \
" -t [theta] Theta value for BarnesHut (Default: 0.1)\n" \
" -q [nqueries] Number of queries\n" \
" -u [max_nthreads] Upper limit on number of threads\n" \
" -h Show this help message\n"
int main(int argc, char **argv){
int nbodies = 10000;
double theta = 0.1;
char *filename = NULL;
int nqueries = 10000;
unsigned int seed = 5;
bool verbose = false;
int max_nthreads = 0;
int option_char;
while ((option_char = getopt(argc, argv, "o:n:s:t:q:u:vh")) != -1) {
switch (option_char) {
case 'o':
filename = optarg;
break;
case 'n':
nbodies = atoi(optarg);
break;
case 's':
seed = atoi(optarg);
break;
case 't':
theta = atof(optarg);
break;
case 'q':
nqueries = atoi(optarg);
break;
case 'u':
max_nthreads = atoi(optarg);
break;
case 'v':
verbose = true;
break;
case 'h':
fprintf(stderr, "%s\n", USAGE);
exit(EXIT_SUCCESS);
break;
default:
fprintf(stderr, "%s\n", USAGE);
exit(EXIT_FAILURE);
}
}
ForceTest test(nbodies, nqueries, theta, seed, max_nthreads);
if(filename == NULL)
if(verbose)
test.printFullJSON(cout);
else
test.printSummaryJSON(cout);
else{
ofstream os(filename);
if(verbose)
test.printFullJSON(os);
else
test.printSummaryJSON(os);
}
return EXIT_SUCCESS;
}