-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cc
More file actions
371 lines (321 loc) · 11.1 KB
/
Copy pathmain.cc
File metadata and controls
371 lines (321 loc) · 11.1 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <vector>
#include <cuda_runtime.h>
#include "PseudoJet.h"
#include "cluster.h"
#include "cudaCheck.h"
void initialise() {
cudaSetDevice(0);
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
std::cout << "Running on CUDA device " << prop.name << std::endl;
int value;
cudaDeviceGetAttribute(&value, cudaDevAttrMaxSharedMemoryPerBlock, 0);
std::cout << " - maximum shared memory per block: " << value / 1024 << " kB" << std::endl;
cudaCheck(cudaDeviceSetLimit(cudaLimitPrintfFifoSize, 10 * 1024 * 1024));
size_t size;
cudaCheck(cudaDeviceGetLimit(&size, cudaLimitPrintfFifoSize));
std::cout << " - kernel printf buffer size: " << size / 1024 << " kB" << std::endl;
}
bool read_next_event(std::istream& input, std::vector<PseudoJet>& particles) {
// clear the input status flags
input.clear();
// skip comments and empty lines
while (input.peek() == '#' or input.peek() == '\n') {
input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// read the input one line at a time
int i = particles.size();
bool found = false;
std::string buffer;
while (std::getline(input, buffer).good()) {
std::istringstream line(buffer);
// read the four elements
double px, py, pz, E;
line >> px >> py >> pz >> E;
//std::cout << "reading: " << px << ", " << py << ", " << pz << ", " << E << std::endl;
if (line.fail()) {
// check for a comment or empty line
if (not buffer.empty() and buffer[0] != '#') {
throw std::runtime_error("Error while parsing particles:\n" + buffer);
}
break;
}
//std::cout << "found a particle" << std::endl;
particles.push_back({i++, false, px, py, pz, E});
found = true;
}
// return false if there was no event to read
return (found);
}
/* Read the next N events from the input stream, and returns the number of events actually read.
*
* Pass 0 to read all events in the stream.
*/
int read_n_events(std::istream& input, std::vector<PseudoJet>& particles, int n) {
// clear the output buffer
particles.clear();
int events = 0;
while ((n == 0 or events < n) and read_next_event(input, particles))
++events;
return events;
}
void print_jets(std::vector<PseudoJet> const& jets, bool cartesian = false) {
std::cout << std::fixed << std::setprecision(8);
int i = 0;
for (auto const& jet : jets) {
if (cartesian) {
// print px, py, pz, E
std::cout << std::setw(5) << i++ << std::setw(16) << jet.px << std::setw(16) << jet.py << std::setw(16) << jet.pz
<< std::setw(16) << jet.E << std::endl;
} else {
// print eta, phi, pT
double pT = std::hypot(jet.px, jet.py);
double phi = atan2(jet.py, jet.px);
while (phi > 2 * M_PI) {
phi -= 2 * M_PI;
}
while (phi < 0.) {
phi += 2 * M_PI;
}
double effective_m2 = std::max(0.0, (jet.E + jet.pz) * (jet.E - jet.pz) - pT * pT);
double E_plus_pz = jet.E + std::abs(jet.pz);
double eta = 0.5 * std::log((pT * pT + effective_m2) / (E_plus_pz * E_plus_pz));
if (jet.pz > 0) {
eta = -eta;
}
std::cout << std::setw(5) << i++ << std::setw(16) << eta << std::setw(16) << phi << std::setw(16) << pT
<< std::endl;
}
}
std::cout << std::endl;
}
int main(int argc, const char* argv[]) {
double ptmin = 0.0; // GeV
double r = 1.0; // clustering radius
Algorithm algo = Algorithm::Kt; // clustering algorithm
bool sort = true;
bool cartesian = false;
int repetitions = 1;
int combine = 1;
std::string filename; // read data from file instead of standard input
bool output_csv = false;
for (unsigned int i = 1; i < argc; ++i) {
// --ptmin, -p
if (std::strcmp(argv[i], "--ptmin") == 0 or std::strcmp(argv[i], "-p") == 0) {
++i;
if (i >= argc) {
// error
std::cerr << "Missing argument to option " << argv[i - 1] << std::endl;
return 1;
}
char* stop;
auto arg = std::strtod(argv[i], &stop);
if (stop != argv[i] and arg >= 0.) {
ptmin = arg;
} else {
// error
std::cerr << "Error while parsing argument to option " << argv[i - 1] << std::endl;
return 1;
}
} else
// -r, -R
if (std::strcmp(argv[i], "-r") == 0 or std::strcmp(argv[i], "-R") == 0) {
++i;
if (i >= argc) {
// error
std::cerr << "Missing argument to option " << argv[i - 1] << std::endl;
return 1;
}
char* stop;
auto arg = std::strtod(argv[i], &stop);
if (stop != argv[i] and arg >= 0) {
r = arg;
} else {
// error
std::cerr << "Error while parsing argument to option " << argv[i - 1] << std::endl;
return 1;
}
} else
// --repeat, -repeat
if (std::strcmp(argv[i], "--repeat") == 0 or std::strcmp(argv[i], "-repeat") == 0) {
++i;
if (i >= argc) {
// error
std::cerr << "Missing argument to option " << argv[i - 1] << std::endl;
return 1;
}
char* stop;
auto arg = std::strtol(argv[i], &stop, 0);
if (stop != argv[i] and arg >= 0) {
repetitions = arg;
} else {
// error
std::cerr << "Error while parsing argument to option " << argv[i - 1] << std::endl;
return 1;
}
} else
// --sort, -s
if (std::strcmp(argv[i], "--sort") == 0 or std::strcmp(argv[i], "-s") == 0) {
sort = true;
} else
// --cartesian
if (std::strcmp(argv[i], "--cartesian") == 0) {
cartesian = true;
} else
// --polar
if (std::strcmp(argv[i], "--polar") == 0) {
cartesian = false;
} else
// --kt, -kt
if (std::strcmp(argv[i], "--kt") == 0 or std::strcmp(argv[i], "-kt") == 0) {
algo = Algorithm::Kt;
} else
// --anti-kt, -antikt
if (std::strcmp(argv[i], "--anti-kt") == 0 or std::strcmp(argv[i], "-antikt") == 0) {
algo = Algorithm::AntiKt;
} else
// --cambridge-aachen, -cam
if (std::strcmp(argv[i], "--cambridge-aachen") == 0 or std::strcmp(argv[i], "-cam") == 0) {
algo = Algorithm::CambridgeAachen;
} else
// --file, -f
if (std::strcmp(argv[i], "--file") == 0 or std::strcmp(argv[i], "-f") == 0) {
++i;
if (i >= argc) {
// error
std::cerr << "Missing argument to option " << argv[i - 1] << std::endl;
return 1;
}
filename = argv[i];
} else
// --combine, -combine
if (std::strcmp(argv[i], "--combine") == 0 or std::strcmp(argv[i], "-combine") == 0) {
++i;
if (i >= argc) {
// error
std::cerr << "Missing argument to option " << argv[i - 1] << std::endl;
return 1;
}
char* stop;
auto arg = std::strtol(argv[i], &stop, 0);
if (stop != argv[i] and arg >= 0) {
combine = arg;
} else {
// error
std::cerr << "Error while parsing argument to option " << argv[i - 1] << std::endl;
return 1;
}
} else
// --csv, -csv
if (std::strcmp(argv[i], "--csv") == 0 or std::strcmp(argv[i], "-csv") == 0) {
output_csv = true;
} else
// unknown option
{
std::cerr << "Unrecognized option " << argv[i] << std::endl;
return 1;
}
}
// initialise the GPU
initialise();
// open an input file
std::ifstream input(filename, std::ios_base::in);
std::vector<PseudoJet> particles;
std::vector<PseudoJet> jets;
int events;
while (events = read_n_events(filename.empty() ? std::cin : input, particles, combine)) {
if (not output_csv) {
std::cout << "found " << particles.size() << " particles";
if (combine != 1)
std::cout << " in " << events << (events == 1 ? " event" : " events");
std::cout << std::endl;
}
// allocate GPU memory for the input particles
PseudoJet* particles_d;
cudaCheck(cudaMalloc(&particles_d, sizeof(PseudoJet) * particles.size()));
cudaEvent_t start, stop;
cudaCheck(cudaEventCreate(&start));
cudaCheck(cudaEventCreate(&stop));
double sum = 0.;
double sum2 = 0.;
for (int step = 0; repetitions == 0 or step < repetitions; ++step) {
cudaCheck(cudaEventRecord(start));
// copy the input to the GPU
cudaCheck(cudaMemcpy(particles_d, particles.data(), sizeof(PseudoJet) * particles.size(), cudaMemcpyDefault));
// run the clustering algorithm and measure its running time
cluster(particles_d, particles.size(), algo, r);
// copy the clustered jets back to the CPU
jets.resize(particles.size());
cudaCheck(cudaMemcpy(jets.data(), particles_d, sizeof(PseudoJet) * jets.size(), cudaMemcpyDefault));
cudaCheck(cudaEventRecord(stop));
cudaCheck(cudaEventSynchronize(stop));
float milliseconds;
cudaCheck(cudaEventElapsedTime(&milliseconds, start, stop));
sum += milliseconds;
sum2 += milliseconds * milliseconds;
// remove the unused elements and the jets with pT < pTmin
auto last = std::remove_if(jets.begin(), jets.end(), [ptmin](auto const& jet) {
return (not jet.isJet) or (jet.px * jet.px + jet.py * jet.py < ptmin * ptmin);
});
jets.erase(last, jets.end());
if (not output_csv) {
if (ptmin > 0.) {
std::cout << "found " << jets.size() << " jets above " << ptmin << " GeV in " << milliseconds << " ms"
<< std::endl;
} else {
std::cout << "found " << jets.size() << " jets in " << milliseconds << " ms" << std::endl;
}
// optionally, sort the jets by decreasing pT
if (sort) {
std::sort(jets.begin(), jets.end(), [](auto const& a, auto const& b) {
return (a.px * a.px + a.py * a.py > b.px * b.px + b.py * b.py);
});
}
}
}
// free GPU memory
cudaCheck(cudaFree(particles_d));
if (not output_csv)
print_jets(jets, cartesian);
std::cout << std::defaultfloat;
if (not output_csv) {
std::cout << "clustered " << particles.size() << " particles into " << jets.size() << " jets above " << ptmin
<< " GeV";
} else {
std::cout << particles.size() << ',' << jets.size() << ',';
}
std::cout << std::fixed;
double mean = sum / repetitions;
int precision;
if (repetitions > 1) {
double sigma = std::sqrt((sum2 - sum * sum / repetitions) / (repetitions - 1));
precision = std::max((int)-std::log10(sigma / 2.) + 1, 0);
precision = std::cout.precision(precision);
if (not output_csv) {
std::cout << " in " << mean << " +/- " << sigma << " ms" << std::endl;
} else {
std::cout << mean << ',' << sigma << std::endl;
}
} else {
precision = std::cout.precision(1);
if (not output_csv) {
std::cout << " in " << mean << " ms" << std::endl;
} else {
std::cout << mean << std::endl;
}
}
std::cout.precision(precision);
std::cout << std::defaultfloat;
}
return 0;
}