-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.cc
241 lines (228 loc) · 8.97 KB
/
main.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
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
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <tbb/global_control.h>
#include <tbb/info.h>
#include <tbb/task_arena.h>
#include <cuda_runtime.h>
#include "CUDACore/getCachingDeviceAllocator.h"
#include "EventProcessor.h"
#include "PosixClockGettime.h"
namespace {
void print_help(std::string const& name) {
std::cout
<< "Usage: " << name
<< " [--numberOfThreads NT] [--numberOfStreams NS] [--warmupEvents WE] [--maxEvents ME] [--runForMinutes RM]"
<< " [--data PATH] [--transfer] [--validation] [--histogram] [--empty]\n";
std::cout << R"(
Options:
--numberOfThreads Number of threads to use (default 1, use 0 to use all CPU cores).
--numberOfStreams Number of concurrent events (default 0 = numberOfThreads).
--warmupEvents Number of events to process before starting the benchmark (default 0).
--maxEvents Number of events to process (default -1 for all events in the input file).
--runForMinutes Continue processing the set of 1000 events until this many minutes have passed
(default -1 for disabled; conflicts with --maxEvents).
--data Path to the 'data' directory (default 'data' in the directory of the executable).
--transfer Transfer results from GPU to CPU (default is to leave them on GPU).
--validation Run (rudimentary) validation at the end (implies --transfer).
--histogram Produce histograms at the end (implies --transfer).
--empty Ignore all producers (for testing only).
)";
}
} // namespace
int main(int argc, char** argv) {
// Parse command line arguments
std::vector<std::string> args(argv, argv + argc);
int numberOfThreads = 1;
int numberOfStreams = 0;
int warmupEvents = 0;
int maxEvents = -1;
int runForMinutes = -1;
std::filesystem::path datadir;
bool transfer = false;
bool validation = false;
bool histogram = false;
bool empty = false;
for (auto i = args.begin() + 1, e = args.end(); i != e; ++i) {
if (*i == "-h" or *i == "--help") {
print_help(args.front());
return EXIT_SUCCESS;
} else if (*i == "--numberOfThreads") {
++i;
numberOfThreads = std::stoi(*i);
} else if (*i == "--numberOfStreams") {
++i;
numberOfStreams = std::stoi(*i);
} else if (*i == "--warmupEvents") {
++i;
warmupEvents = std::stoi(*i);
} else if (*i == "--maxEvents") {
++i;
maxEvents = std::stoi(*i);
} else if (*i == "--runForMinutes") {
++i;
runForMinutes = std::stoi(*i);
} else if (*i == "--data") {
++i;
datadir = *i;
} else if (*i == "--transfer") {
transfer = true;
} else if (*i == "--validation") {
transfer = true;
validation = true;
} else if (*i == "--histogram") {
transfer = true;
histogram = true;
} else if (*i == "--empty") {
empty = true;
} else {
std::cout << "Invalid parameter " << *i << std::endl << std::endl;
print_help(args.front());
return EXIT_FAILURE;
}
}
if (maxEvents >= 0 and runForMinutes >= 0) {
std::cout << "Got both --maxEvents and --runForMinutes, please give only one of them" << std::endl;
return EXIT_FAILURE;
}
if (numberOfThreads == 0) {
numberOfThreads = tbb::info::default_concurrency();
}
if (numberOfStreams == 0) {
numberOfStreams = numberOfThreads;
}
if (datadir.empty()) {
datadir = std::filesystem::path(args[0]).parent_path() / "data";
}
if (not std::filesystem::exists(datadir)) {
std::cout << "Data directory '" << datadir << "' does not exist" << std::endl;
return EXIT_FAILURE;
}
int numberOfDevices;
auto status = cudaGetDeviceCount(&numberOfDevices);
if (cudaSuccess != status) {
std::cout << "Failed to initialize the CUDA runtime";
return EXIT_FAILURE;
}
std::cout << "Found " << numberOfDevices << " devices" << std::endl;
#if CUDA_VERSION >= 11020
// Initialize the CUDA memory pool
uint64_t threshold = cms::cuda::allocator::minCachedBytes();
for (int device = 0; device < numberOfDevices; ++device) {
cudaMemPool_t pool;
cudaDeviceGetDefaultMemPool(&pool, device);
cudaMemPoolSetAttribute(pool, cudaMemPoolAttrReleaseThreshold, &threshold);
}
#endif
// Initialize EventProcessor
std::vector<std::string> edmodules;
std::vector<std::string> esmodules;
if (not empty) {
edmodules = {
"BeamSpotToCUDA", "SiPixelRawToClusterCUDA", "SiPixelRecHitCUDA", "CAHitNtupletCUDA", "PixelVertexProducerCUDA"};
esmodules = {"BeamSpotESProducer",
"SiPixelFedCablingMapGPUWrapperESProducer",
"SiPixelGainCalibrationForHLTGPUESProducer",
"PixelCPEFastESProducer"};
if (transfer) {
auto capos = std::find(edmodules.begin(), edmodules.end(), "CAHitNtupletCUDA");
assert(capos != edmodules.end());
edmodules.insert(capos + 1, "PixelTrackSoAFromCUDA");
auto vertpos = std::find(edmodules.begin(), edmodules.end(), "PixelVertexProducerCUDA");
assert(vertpos != edmodules.end());
edmodules.insert(vertpos + 1, "PixelVertexSoAFromCUDA");
}
if (validation) {
edmodules.emplace_back("CountValidator");
}
if (histogram) {
edmodules.emplace_back("HistoValidator");
}
}
edm::EventProcessor processor(warmupEvents,
maxEvents,
runForMinutes,
numberOfStreams,
std::move(edmodules),
std::move(esmodules),
datadir,
validation);
if (runForMinutes < 0) {
std::cout << "Processing " << processor.maxEvents() << " events,";
} else {
std::cout << "Processing for about " << runForMinutes << " minutes,";
}
if (warmupEvents > 0) {
std::cout << " after " << warmupEvents << " events of warm up,";
}
std::cout << " with " << numberOfStreams << " concurrent events and " << numberOfThreads << " threads." << std::endl;
// Initialize the TBB thread pool
tbb::global_control tbb_max_threads{tbb::global_control::max_allowed_parallelism,
static_cast<std::size_t>(numberOfThreads)};
// Warm up
try {
tbb::task_arena arena(numberOfThreads);
arena.execute([&] { processor.warmUp(); });
} catch (std::runtime_error& e) {
std::cout << "\n----------\nCaught std::runtime_error" << std::endl;
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
} catch (std::exception& e) {
std::cout << "\n----------\nCaught std::exception" << std::endl;
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
} catch (...) {
std::cout << "\n----------\nCaught exception of unknown type" << std::endl;
return EXIT_FAILURE;
}
// Run work
auto cpu_start = PosixClockGettime<CLOCK_PROCESS_CPUTIME_ID>::now();
auto start = std::chrono::high_resolution_clock::now();
try {
tbb::task_arena arena(numberOfThreads);
arena.execute([&] { processor.runToCompletion(); });
} catch (std::runtime_error& e) {
std::cout << "\n----------\nCaught std::runtime_error" << std::endl;
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
} catch (std::exception& e) {
std::cout << "\n----------\nCaught std::exception" << std::endl;
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
} catch (...) {
std::cout << "\n----------\nCaught exception of unknown type" << std::endl;
return EXIT_FAILURE;
}
auto cpu_stop = PosixClockGettime<CLOCK_PROCESS_CPUTIME_ID>::now();
auto stop = std::chrono::high_resolution_clock::now();
// Run endJob
try {
processor.endJob();
} catch (std::runtime_error& e) {
std::cout << "\n----------\nCaught std::runtime_error" << std::endl;
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
} catch (std::exception& e) {
std::cout << "\n----------\nCaught std::exception" << std::endl;
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
} catch (...) {
std::cout << "\n----------\nCaught exception of unknown type" << std::endl;
return EXIT_FAILURE;
}
// Work done, report timing
auto diff = stop - start;
auto time = static_cast<double>(std::chrono::duration_cast<std::chrono::microseconds>(diff).count()) / 1e6;
auto cpu_diff = cpu_stop - cpu_start;
auto cpu = static_cast<double>(std::chrono::duration_cast<std::chrono::microseconds>(cpu_diff).count()) / 1e6;
maxEvents = processor.processedEvents();
std::cout << "Processed " << maxEvents << " events in " << std::scientific << time << " seconds, throughput "
<< std::defaultfloat << (maxEvents / time) << " events/s, CPU usage per thread: " << std::fixed
<< std::setprecision(1) << (cpu / time / numberOfThreads * 100) << "%" << std::endl;
return EXIT_SUCCESS;
}