-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathnvbandwidth.cpp
316 lines (275 loc) · 10.6 KB
/
nvbandwidth.cpp
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
/*
* SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <boost/program_options.hpp>
#include <cuda.h>
#include <cuda_runtime_api.h>
#include <nvml.h>
#include <iostream>
#ifdef MULTINODE
#include <mpi.h>
#endif
#include "json_output.h"
#include "kernels.cuh"
#include "output.h"
#include "testcase.h"
#include "version.h"
#include "inline_common.h"
namespace opt = boost::program_options;
int deviceCount;
unsigned int averageLoopCount;
unsigned long long bufferSize;
unsigned long long loopCount;
bool verbose;
bool shouldOutput = true;
bool disableAffinity;
bool skipVerification;
bool useMean;
bool perfFormatter;
Verbosity VERBOSE(verbose);
Verbosity OUTPUT(shouldOutput);
#ifdef MULTINODE
// Device ordinal of the GPU owned by the process
int localRank;
// Process rank within one OS
int localDevice;
int worldRank;
int worldSize;
char localHostname[STRING_LENGTH];
#endif
bool jsonOutput;
Output *output;
// Define testcases here
std::vector<Testcase*> createTestcases() {
return {
new HostToDeviceCE(),
new DeviceToHostCE(),
new HostToDeviceBidirCE(),
new DeviceToHostBidirCE(),
new DeviceToDeviceReadCE(),
new DeviceToDeviceWriteCE(),
new DeviceToDeviceBidirReadCE(),
new DeviceToDeviceBidirWriteCE(),
new AllToHostCE(),
new AllToHostBidirCE(),
new HostToAllCE(),
new HostToAllBidirCE(),
new AllToOneWriteCE(),
new AllToOneReadCE(),
new OneToAllWriteCE(),
new OneToAllReadCE(),
new HostToDeviceSM(),
new DeviceToHostSM(),
new HostToDeviceBidirSM(),
new DeviceToHostBidirSM(),
new DeviceToDeviceReadSM(),
new DeviceToDeviceWriteSM(),
new DeviceToDeviceBidirReadSM(),
new DeviceToDeviceBidirWriteSM(),
new AllToHostSM(),
new AllToHostBidirSM(),
new HostToAllSM(),
new HostToAllBidirSM(),
new AllToOneWriteSM(),
new AllToOneReadSM(),
new OneToAllWriteSM(),
new OneToAllReadSM(),
new HostDeviceLatencySM(),
new DeviceToDeviceLatencySM(),
new DeviceLocalCopy(),
#ifdef MULTINODE
new MultinodeDeviceToDeviceReadCE(),
new MultinodeDeviceToDeviceWriteCE(),
new MultinodeDeviceToDeviceBidirReadCE(),
new MultinodeDeviceToDeviceBidirWriteCE(),
new MultinodeDeviceToDeviceReadSM(),
new MultinodeDeviceToDeviceWriteSM(),
new MultinodeDeviceToDeviceBidirReadSM(),
new MultinodeDeviceToDeviceBidirWriteSM(),
new MultinodeAllToOneWriteSM(),
new MultinodeAllFromOneReadSM(),
new MultinodeBroadcastOneToAllSM(),
new MultinodeBroadcastAllToAllSM(),
new MultinodeBisectWriteCE(),
#endif
};
}
Testcase* findTestcase(std::vector<Testcase*> &testcases, std::string id) {
// Check if testcase ID is index
char* p;
long index = strtol(id.c_str(), &p, 10);
if (*p) {
// Conversion failed so key is ID
auto it = find_if(testcases.begin(), testcases.end(), [&id](Testcase* test) {return test->testKey() == id;});
if (it != testcases.end()) {
return testcases.at(std::distance(testcases.begin(), it));
} else {
throw "Testcase " + id + " not found!";
}
} else {
// ID is index
if (index < 0 || index >= static_cast<long>(testcases.size())) throw "Testcase index " + id + " out of bound!";
return testcases.at(index);
}
}
std::vector<std::string> expandTestcases(std::vector<Testcase*> &testcases, std::vector<std::string> prefixes) {
std::vector<std::string> testcasesToRun;
for (auto testcase : testcases) {
auto it = find_if(prefixes.begin(), prefixes.end(), [&testcase](std::string prefix) {return testcase->testKey().compare(0, prefix.size(), prefix) == 0;});
if (it != prefixes.end()) {
testcasesToRun.push_back(testcase->testKey());
}
}
return testcasesToRun;
}
void runTestcase(std::vector<Testcase*> &testcases, const std::string &testcaseID) {
Testcase* test{nullptr};
try {
test = findTestcase(testcases, testcaseID);
} catch (std::string &s) {
output->addTestcase(testcaseID, "ERROR", s);
return;
}
try {
if (!test->filter()) {
output->addTestcase(test->testKey(), NVB_WAIVED);
return;
}
output->addTestcase(test->testKey(), NVB_RUNNING);
// Run the testcase
if (test->testKey() == "host_device_latency_sm" || test->testKey() == "device_to_device_latency_sm") {
// use fixd-size buffer for latency tests
test->run(2 * _MiB, loopCount);
} else {
test->run(bufferSize * _MiB, loopCount);
}
} catch (std::string &s) {
output->setTestcaseStatusAndAddIfNeeded(test->testKey(), NVB_ERROR_STATUS, s);
}
}
int main(int argc, char **argv) {
std::vector<Testcase*> testcases = createTestcases();
std::vector<std::string> testcasesToRun;
std::vector<std::string> testcasePrefixes;
output = new Output();
#ifdef MULTINODE
ASSERT(0 == gethostname(localHostname, STRING_LENGTH - 1));
// Set up MPI
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &worldSize);
MPI_Comm_rank(MPI_COMM_WORLD, &worldRank);
// Avoid excessive output by limit output to rank 0
shouldOutput = (worldRank == 0);
#endif
// Args parsing
opt::options_description visible_opts("nvbandwidth CLI");
visible_opts.add_options()
("help,h", "Produce help message")
("bufferSize,b", opt::value<unsigned long long int>(&bufferSize)->default_value(defaultBufferSize), "Memcpy buffer size in MiB")
("list,l", "List available testcases")
("testcase,t", opt::value<std::vector<std::string>>(&testcasesToRun)->multitoken(), "Testcase(s) to run (by name or index)")
("testcasePrefixes,p", opt::value<std::vector<std::string>>(&testcasePrefixes)->multitoken(), "Testcase(s) to run (by prefix))")
("verbose,v", opt::bool_switch(&verbose)->default_value(false), "Verbose output")
("skipVerification,s", opt::bool_switch(&skipVerification)->default_value(false), "Skips data verification after copy")
("disableAffinity,d", opt::bool_switch(&disableAffinity)->default_value(false), "Disable automatic CPU affinity control")
("testSamples,i", opt::value<unsigned int>(&averageLoopCount)->default_value(defaultAverageLoopCount), "Iterations of the benchmark")
("useMean,m", opt::bool_switch(&useMean)->default_value(false), "Use mean instead of median for results")
("json,j", opt::bool_switch(&jsonOutput)->default_value(false), "Print output in json format instead of plain text.");
opt::options_description all_opts("");
all_opts.add(visible_opts);
all_opts.add_options()
("loopCount", opt::value<unsigned long long int>(&loopCount)->default_value(defaultLoopCount), "Iterations of memcpy to be performed within a test sample")
("perfFormatter", opt::bool_switch(&perfFormatter)->default_value(false), "Use perf formatter prefix (&&&& PERF) in output");
opt::variables_map vm;
try {
opt::store(opt::parse_command_line(argc, argv, all_opts), vm);
opt::notify(vm);
} catch (...) {
output->addVersionInfo();
std::stringstream errmsg;
errmsg << "ERROR: Invalid Arguments " << std::endl;
for (int i = 0; i < argc; i++) {
errmsg << argv[i] << " ";
}
std::vector<std::string> messageParts;
std::stringstream buf;
buf << visible_opts;
messageParts.emplace_back(errmsg.str());
messageParts.emplace_back(buf.str());
output->recordError(messageParts);
return 1;
}
if (jsonOutput) {
delete output;
output = new JsonOutput(shouldOutput);
}
output->addVersionInfo();
if (vm.count("help")) {
OUTPUT << visible_opts << "\n";
return 0;
}
if (vm.count("list")) {
output->listTestcases(testcases);
return 0;
}
if (testcasePrefixes.size() != 0 && testcasesToRun.size() != 0) {
output->recordError("You cannot specify both testcase and testcasePrefix options at the same time");
return 1;
}
CU_ASSERT(cuInit(0));
NVML_ASSERT(nvmlInit());
CU_ASSERT(cuDeviceGetCount(&deviceCount));
if (bufferSize < defaultBufferSize) {
output->recordWarning("NOTE: You have chosen a buffer size that is smaller than the default buffer size. It is suggested to use the default buffer size (64MB) to achieve maximal peak bandwidth.");
}
int cudaVersion;
cudaRuntimeGetVersion(&cudaVersion);
CU_ASSERT(cuDriverGetVersion(&cudaVersion));
char driverVersion[NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE];
NVML_ASSERT(nvmlSystemGetDriverVersion(driverVersion, NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE));
output->addCudaAndDriverInfo(cudaVersion, driverVersion);
output->recordDevices(deviceCount);
if (testcasePrefixes.size() > 0) {
testcasesToRun = expandTestcases(testcases, testcasePrefixes);
if (testcasesToRun.size() == 0) {
output->recordError("Specified list of testcase prefixes did not match any testcases");
return 1;
}
}
// This triggers the loading of all kernels on all devices, even with lazy loading enabled.
// Some tests can create complex dependencies between devices and function loading requires a
// device synchronization, so loading in the middle of a test can deadlock.
preloadKernels(deviceCount);
if (testcasesToRun.size() == 0) {
// run all testcases
for (auto testcase : testcases) {
runTestcase(testcases, testcase->testKey());
}
} else {
for (const auto& testcaseIndex : testcasesToRun) {
runTestcase(testcases, testcaseIndex);
}
}
output->print();
for (auto testcase : testcases) {
delete testcase;
}
#ifdef MULTINODE
MPI_Finalize();
#endif
output->printInfo();
return 0;
}