-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathexecution_plan.cc
More file actions
590 lines (539 loc) · 24.2 KB
/
execution_plan.cc
File metadata and controls
590 lines (539 loc) · 24.2 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "execution_plan.hpp"
#include <cassert>
#include <fstream>
#include <set>
namespace {
template <typename T, typename Predicate>
std::vector<T> filter(const std::vector<T>& vec, Predicate pred) {
std::vector<T> filtered;
std::copy_if(vec.begin(), vec.end(), std::back_inserter(filtered), pred);
return filtered;
}
auto getOpType = [](const std::string& str) {
if (str == "nop") {
return mscclpp::OperationType::NOP;
} else if (str == "barrier") {
return mscclpp::OperationType::BARRIER;
} else if (str == "put") {
return mscclpp::OperationType::PUT;
} else if (str == "pws") {
return mscclpp::OperationType::PUT_WITH_SIGNAL;
} else if (str == "pwsf") {
return mscclpp::OperationType::PUT_WITH_SIGNAL_AND_FLUSH;
} else if (str == "get") {
return mscclpp::OperationType::GET;
} else if (str == "copy") {
return mscclpp::OperationType::COPY;
} else if (str == "signal") {
return mscclpp::OperationType::SIGNAL;
} else if (str == "wait") {
return mscclpp::OperationType::WAIT;
} else if (str == "flush") {
return mscclpp::OperationType::FLUSH;
} else if (str == "re") {
return mscclpp::OperationType::REDUCE;
} else if (str == "rs") {
return mscclpp::OperationType::REDUCE_SEND;
} else if (str == "rrc") {
return mscclpp::OperationType::READ_REDUCE_COPY;
} else if (str == "rrcs") {
return mscclpp::OperationType::READ_REDUCE_COPY_SEND;
} else if (str == "ppkt") {
return mscclpp::OperationType::PUT_PACKET;
} else if (str == "rspkt") {
return mscclpp::OperationType::REDUCE_SEND_PACKET;
} else if (str == "cpkt") {
return mscclpp::OperationType::COPY_PACKET;
} else if (str == "tpkt") {
return mscclpp::OperationType::TRANSFORM_TO_PACKET;
} else if (str == "rpkt") {
return mscclpp::OperationType::REDUCE_PACKET;
} else if (str == "glres") {
return mscclpp::OperationType::MULTI_LOAD_REDUCE_STORE;
} else {
throw mscclpp::Error("Invalid operation type", mscclpp::ErrorCode::ExecutorError);
}
};
auto convertToBufferType = [](const std::string& str) {
if (str == "i") {
return mscclpp::BufferType::INPUT;
} else if (str == "o") {
return mscclpp::BufferType::OUTPUT;
} else if (str == "s") {
return mscclpp::BufferType::SCRATCH;
} else {
throw mscclpp::Error("Invalid buffer type", mscclpp::ErrorCode::ExecutorError);
}
};
auto convertToChannelType = [](const std::string& str) {
if (str == "sm") {
return mscclpp::ChannelType::SM;
} else if (str == "proxy") {
return mscclpp::ChannelType::PROXY;
} else if (str == "none") {
return mscclpp::ChannelType::NONE;
} else if (str == "nvls") {
return mscclpp::ChannelType::NVLS;
} else {
throw mscclpp::Error("Invalid channel type", mscclpp::ErrorCode::ExecutorError);
}
};
std::set groupChannelType{mscclpp::ChannelType::NVLS};
} // namespace
namespace mscclpp {
using json = nlohmann::json;
ExecutionPlan::Impl::Impl(const std::string planPath) : planPath(planPath), isUsingPacket(false) {
std::ifstream file(this->planPath);
json obj = json::parse(file);
this->name = obj["name"];
this->collective = obj["collective"];
this->isInPlace = obj["inplace"];
this->minMessageSize = obj.value("min_message_size", 0);
this->maxMessageSize = obj.value("max_message_size", std::numeric_limits<uint64_t>::max());
}
std::vector<ChannelInfo> ExecutionPlan::Impl::getChannelInfos(int rank, ChannelType channelType) const {
auto pred = [channelType](const ChannelInfo& info) { return info.channelType == channelType; };
return filter(this->channelInfos.at(rank), pred);
}
std::vector<ChannelInfo> ExecutionPlan::Impl::getChannelInfos(int rank, BufferType dstBufferType) const {
auto pred = [dstBufferType](const ChannelInfo& info) { return info.dstBufferType == dstBufferType; };
return filter(this->channelInfos.at(rank), pred);
}
std::vector<ChannelInfo> ExecutionPlan::Impl::getChannelInfosByDstRank(int rank, BufferType bufferType) const {
auto pred = [bufferType](const ChannelInfo& info) { return info.dstBufferType == bufferType; };
return filter(this->channelInfosByDstRank.at(rank), pred);
}
std::vector<ChannelInfo> ExecutionPlan::Impl::getUnpairedChannelInfos(int rank, int worldSize,
ChannelType channelType) {
std::vector<ChannelInfo> unpaired;
for (int peer = 0; peer < worldSize; peer++) {
if (peer == rank) {
continue;
}
if (this->channelCountMap[{rank, channelType}][peer] < this->channelCountMap[{peer, channelType}][rank]) {
int count = this->channelCountMap[{peer, channelType}][rank] - this->channelCountMap[{rank, channelType}][peer];
for (int i = 0; i < count; i++) {
ChannelInfo info;
info.srcBufferType = BufferType::NONE;
info.dstBufferType = BufferType::NONE;
info.channelType = channelType;
info.connectedPeers.push_back(peer);
unpaired.push_back(info);
}
}
}
return unpaired;
}
std::vector<NvlsInfo> ExecutionPlan::Impl::getNvlsInfos(int rank) const { return this->nvlsInfos.at(rank); }
std::vector<int> ExecutionPlan::Impl::getConnectedPeers(int rank) const {
std::set<int> peers;
for (const auto& info : this->channelInfos.at(rank)) {
for (int peer : info.connectedPeers) {
peers.insert(peer);
}
}
for (const auto& info : this->channelInfosByDstRank.at(rank)) {
for (int peer : info.connectedPeers) {
peers.insert(peer);
}
}
return std::vector<int>(peers.begin(), peers.end());
}
std::vector<BufferType> ExecutionPlan::Impl::getConnectedBufferTypes(int rank) const {
std::set<BufferType> bufferTypes;
for (const auto& info : this->channelInfos.at(rank)) {
bufferTypes.insert(info.dstBufferType);
}
return std::vector<BufferType>(bufferTypes.begin(), bufferTypes.end());
}
size_t ExecutionPlan::Impl::getScratchBufferSize(int rank, size_t inputSize) const {
size_t sizePerChunk = 0;
size_t inputChunks = this->inputChunks.at(rank);
if (inputChunks != 0)
sizePerChunk = (inputSize + inputChunks - 1) / this->inputChunks.at(rank);
else
throw mscclpp::Error("Input chunks must be greater than 0", mscclpp::ErrorCode::ExecutorError);
if (this->isUsingPacket) {
return sizePerChunk * this->scratchChunks.at(rank) * 2 /* data + flag*/ * 2 /*double buffer*/;
}
return sizePerChunk * this->scratchChunks.at(rank);
}
size_t ExecutionPlan::Impl::getMaxScratchBufferSize(int rank) const {
if (this->maxMessageSize == std::numeric_limits<uint64_t>::max()) {
return std::numeric_limits<size_t>::max();
}
size_t sizePerChunk = 0;
size_t inputChunks = this->inputChunks.at(rank);
if (inputChunks != 0)
sizePerChunk = (this->maxMessageSize + inputChunks - 1) / inputChunks;
else
throw mscclpp::Error("Input chunks must be greater than 0", mscclpp::ErrorCode::ExecutorError);
return this->getScratchBufferSize(rank, sizePerChunk * this->inputChunks.at(rank));
}
std::vector<Operation> ExecutionPlan::Impl::getOperations(int rank, int threadblock) const {
return this->operations.at(rank)[threadblock];
}
int ExecutionPlan::Impl::getThreadblockCount(int rank) const { return this->operations.at(rank).size(); }
int ExecutionPlan::Impl::getNThreadsPerBlock() const { return this->nThreadsPerBlock; }
void ExecutionPlan::Impl::loadExecutionPlan(size_t inputSize, size_t outputSize, size_t contsSrcOffset,
size_t constDstOffset) {
std::ifstream file(this->planPath);
json obj = json::parse(file);
if (this->name != obj["name"]) {
throw Error("Plan name does not match", ErrorCode::ExecutorError);
}
this->collective = obj["collective"];
std::string protocol = obj["protocol"];
if (protocol == "LL") {
this->isUsingPacket = true;
}
this->inputSize = inputSize;
this->outputSize = outputSize;
this->nThreadsPerBlock = obj.value("num_threads_per_block", 1024);
this->minMessageSize = obj.value("min_message_size", 0);
this->maxMessageSize = obj.value("max_message_size", std::numeric_limits<uint64_t>::max());
this->isInPlace = obj["inplace"];
const auto& gpus = obj["gpus"];
for (const auto& gpu : gpus) {
int rank = gpu["id"];
this->inputChunks[rank] = gpu["inputChunks"];
this->outputChunks[rank] = gpu["outputChunks"];
this->scratchChunks[rank] = gpu["scratchChunks"];
this->chunkGroups[rank] = gpu["chunkGroups"];
}
this->setupChannels(gpus);
this->setupOperations(gpus, contsSrcOffset, constDstOffset);
}
void ExecutionPlan::Impl::lightLoadExecutionPlan(size_t inputSize, size_t outputSize, size_t contsSrcOffset,
size_t constDstOffset) {
std::ifstream file(this->planPath);
json obj = json::parse(file);
if (this->name != obj["name"]) {
throw Error("Plan name does not match", ErrorCode::ExecutorError);
}
std::string protocol = obj["protocol"];
if (protocol == "LL") {
this->isUsingPacket = true;
}
const auto& gpus = obj["gpus"];
for (const auto& gpu : gpus) {
int rank = gpu["id"];
this->inputChunks[rank] = gpu["inputChunks"];
this->outputChunks[rank] = gpu["outputChunks"];
this->scratchChunks[rank] = gpu["scratchChunks"];
this->chunkGroups[rank] = gpu["chunkGroups"];
}
this->inputSize = inputSize;
this->outputSize = outputSize;
this->setupOperations(gpus, contsSrcOffset, constDstOffset);
}
void ExecutionPlan::Impl::parseChannels(
const json& gpu, std::vector<ChannelInfo>& channelInfos, std::vector<NvlsInfo>& nvlsInfos,
std::map<std::tuple<int, BufferType, BufferType, ChannelType>, std::vector<int>>& chanConnectedPeersMap, int rank) {
for (const auto& channel : gpu["channels"]) {
ChannelType chanType = convertToChannelType(channel["type"]);
if (chanType == ChannelType::NVLS) {
NvlsInfo info;
info.bufferType = convertToBufferType(channel["buff"]);
for (const auto& group : channel["rankGroups"]) {
info.bufferSize = (int)group["size"] * this->getUpperBoundChunkSize(rank, this->inputSize, this->outputSize);
info.ranks.clear();
for (int rank : group["ranks"]) {
info.ranks.push_back(rank);
}
nvlsInfos.push_back(info);
}
} else {
ChannelInfo info;
info.srcBufferType = convertToBufferType(channel["srcbuff"]);
info.dstBufferType = convertToBufferType(channel["dstbuff"]);
info.channelType = convertToChannelType(channel["type"]);
for (const auto& peer : channel["connectedTo"]) {
info.connectedPeers.push_back(peer);
chanConnectedPeersMap[{peer, info.srcBufferType, info.dstBufferType, info.channelType}].push_back(rank);
this->channelCountMap[{rank, info.channelType}][peer]++;
}
channelInfos.push_back(info);
}
}
}
// Construct the channel info. Step 1. Flatten SM and PROXY channels into separate vectors.
// Step 2. For each threadblock, construct a vector of channel indexes and keys.
void ExecutionPlan::Impl::setupChannels(const json& gpus) {
using mapKey = std::tuple<int, BufferType, BufferType, ChannelType>;
std::map<mapKey, std::vector<int>> chanConnectedPeersMap;
for (const auto& gpu : gpus) {
int rank = gpu["id"];
std::vector<ChannelInfo> channelInfos;
std::vector<NvlsInfo> nvlsInfos;
this->parseChannels(gpu, channelInfos, nvlsInfos, chanConnectedPeersMap, rank);
this->channelInfos[rank] = channelInfos;
this->nvlsInfos[rank] = nvlsInfos;
}
for (const auto& [key, connectedFrom] : chanConnectedPeersMap) {
auto [peer, srcBufferType, dstBufferType, channelType] = key;
ChannelInfo info;
info.srcBufferType = srcBufferType;
info.dstBufferType = dstBufferType;
info.channelType = channelType;
info.connectedPeers = connectedFrom;
this->channelInfosByDstRank[peer].push_back(info);
}
// setup threadblockChannelMap
for (const auto& gpu : gpus) {
int rank = gpu["id"];
auto channelTypes = {ChannelType::SM, ChannelType::PROXY, ChannelType::NVLS};
std::unordered_map<ChannelKey, std::vector<int>> channelMap;
for (auto channelType : channelTypes) {
const std::vector<ChannelInfo> channelInfos = this->getChannelInfos(rank, channelType);
int index = 0;
if (channelType == ChannelType::NVLS) {
const std::vector<NvlsInfo> nvlsInfos = this->getNvlsInfos(rank);
for (const auto& info : nvlsInfos) {
ChannelKey key = {info.bufferType, info.bufferType, ChannelType::NVLS};
channelMap[key].push_back(index++);
}
} else {
for (const auto& info : channelInfos) {
ChannelKey key = {info.srcBufferType, info.dstBufferType, info.channelType};
for (size_t i = 0; i < info.connectedPeers.size(); i++) {
channelMap[key].push_back(index++);
}
}
}
}
int nthreadblocks = gpu["threadblocks"].size();
this->threadblockSMChannelMap[rank].resize(nthreadblocks);
this->threadblockProxyChannelMap[rank].resize(nthreadblocks);
this->threadblockNvlsChannelMap[rank].resize(nthreadblocks);
for (const auto& threadblock : gpu["threadblocks"]) {
for (const auto& channel : threadblock["channels"]) {
ChannelType channelType = convertToChannelType(channel["ctype"]);
ChannelKey key = {convertToBufferType(channel["src"]), convertToBufferType(channel["dst"]), channelType};
for (int id : channel["cids"]) {
if (channelType == ChannelType::SM) {
this->threadblockSMChannelMap[rank][threadblock["id"]].emplace_back(channelMap[key][id], key);
} else if (channelType == ChannelType::PROXY) {
this->threadblockProxyChannelMap[rank][threadblock["id"]].emplace_back(channelMap[key][id], key);
} else if (channelType == ChannelType::NVLS) {
this->threadblockNvlsChannelMap[rank][threadblock["id"]].emplace_back(channelMap[key][id], key);
}
}
}
}
}
}
void ExecutionPlan::Impl::setupOperations(const json& gpus, size_t constSrcOffset, size_t constDstOffset) {
auto getConstOffset = [&](BufferType type) -> size_t {
switch (type) {
case BufferType::INPUT:
return constSrcOffset;
case BufferType::OUTPUT:
return constDstOffset;
case BufferType::SCRATCH:
return 0;
default:
throw Error("Invalid buffer type", ErrorCode::ExecutorError);
}
};
// setup threadblocks and operations
for (const auto& gpu : gpus) {
int rank = gpu["id"];
for (const auto& threadblock : gpu["threadblocks"]) {
std::unordered_map<ChannelKey, std::vector<int>> channelIndexes;
std::vector<Operation> ops;
int threadblockId = threadblock["id"];
const auto& smChannels = this->threadblockSMChannelMap[rank][threadblockId];
const auto& proxyChannels = this->threadblockProxyChannelMap[rank][threadblockId];
const auto& nvlsChannels = this->threadblockNvlsChannelMap[rank][threadblockId];
for (size_t i = 0; i < smChannels.size(); i++) {
const auto& [_, key] = smChannels[i];
channelIndexes[key].push_back(i);
}
for (size_t i = 0; i < proxyChannels.size(); i++) {
const auto& [_, key] = proxyChannels[i];
channelIndexes[key].push_back(i);
}
for (size_t i = 0; i < nvlsChannels.size(); i++) {
const auto& [_, key] = nvlsChannels[i];
channelIndexes[key].push_back(i);
}
for (const auto& op : threadblock["ops"]) {
Operation operation = {};
std::vector<uint32_t> chunkIndexes;
operation.type = static_cast<mscclpp::OperationType>(getOpType(op["name"]));
if (op.contains("ctype")) {
operation.channelType = convertToChannelType(op["ctype"]);
}
if (op.contains("i_cids")) {
if (operation.channelType == mscclpp::ChannelType::NVLS) {
BufferType srcBufferType = convertToBufferType(op["srcbuff"]);
operation.nvlsInputIndex =
channelIndexes[{srcBufferType, srcBufferType, ChannelType::NVLS}][op["i_cids"][0]["id"]];
chunkIndexes.push_back((uint32_t)op["srcoff"]);
} else {
operation.nInputs = op["i_cids"].size();
for (int i = 0; i < operation.nInputs; i++) {
BufferType srcBufferType = convertToBufferType(op["i_buff"]["src"]);
BufferType dstBufferType = convertToBufferType(op["i_buff"]["dst"]);
// Get the relevant channel index in rank channelInfos
operation.inputChannelIndexes[i] =
channelIndexes[{srcBufferType, dstBufferType, operation.channelType}][op["i_cids"][i]["id"]];
operation.inputOffsets[i] =
this->getOffset(rank, this->inputSize, this->outputSize, (uint32_t)op["i_cids"][i]["off"]) +
getConstOffset(srcBufferType);
chunkIndexes.push_back((uint32_t)op["i_cids"][i]["off"]);
}
}
}
// will have either srcs or i_cids
if (op.contains("srcs")) {
operation.nInputs = op["srcs"].size();
operation.inputBufferType = convertToBufferType(op["srcs"][0]["buff"]);
for (int i = 0; i < operation.nInputs; i++) {
operation.inputOffsets[i] =
this->getOffset(rank, this->inputSize, this->outputSize, (uint32_t)op["srcs"][i]["off"]) +
getConstOffset(operation.inputBufferType);
chunkIndexes.push_back((uint32_t)op["srcs"][i]["off"]);
}
}
if (op.contains("o_cids")) {
operation.nOutputs = op["o_cids"].size();
for (int i = 0; i < operation.nOutputs; i++) {
if (operation.channelType == mscclpp::ChannelType::NVLS) {
BufferType dstBufferType = convertToBufferType(op["dstbuff"]);
operation.nvlsInputIndex =
channelIndexes[{dstBufferType, dstBufferType, ChannelType::NVLS}][op["o_cids"][0]["id"]];
chunkIndexes.push_back((uint32_t)op["dstoff"]);
} else {
BufferType srcBufferType = convertToBufferType(op["o_buff"]["src"]);
BufferType dstBufferType = convertToBufferType(op["o_buff"]["dst"]);
operation.outputChannelIndexes[i] =
channelIndexes[{srcBufferType, dstBufferType, operation.channelType}][op["o_cids"][i]["id"]];
operation.outputOffsets[i] =
this->getOffset(rank, this->inputSize, this->outputSize, (uint32_t)op["o_cids"][i]["off"]) +
getConstOffset(dstBufferType);
chunkIndexes.push_back((uint32_t)op["o_cids"][i]["off"]);
}
}
}
// will have either dsts or o_cids
if (op.contains("dsts")) {
operation.nOutputs = op["dsts"].size();
operation.outputBufferType = convertToBufferType(op["dsts"][0]["buff"]);
for (int i = 0; i < operation.nOutputs; i++) {
operation.outputOffsets[i] =
this->getOffset(rank, this->inputSize, this->outputSize, (uint32_t)op["dsts"][i]["off"]) +
getConstOffset(operation.outputBufferType);
chunkIndexes.push_back((uint32_t)op["dsts"][i]["off"]);
}
}
if (op.contains("srcbuff")) {
operation.srcBufferType = convertToBufferType(op["srcbuff"]);
}
if (op.contains("srcoff")) {
operation.srcOffset = this->getOffset(rank, this->inputSize, this->outputSize, (uint32_t)op["srcoff"]);
chunkIndexes.push_back((uint32_t)op["srcoff"]);
}
if (op.contains("dstbuff")) {
operation.dstBufferType = convertToBufferType(op["dstbuff"]);
}
if (op.contains("dstoff")) {
operation.dstOffset = this->getOffset(rank, this->inputSize, this->outputSize, (uint32_t)op["dstoff"]);
chunkIndexes.push_back((uint32_t)op["dstoff"]);
}
if (op.contains("cnt")) {
operation.size =
this->getNChunkSize(rank, this->inputSize, this->outputSize, (uint32_t)op["cnt"], chunkIndexes);
}
if (op.contains("barrier_id")) {
operation.deviceSyncerIndex = op["barrier_id"];
}
if (op.contains("nthread_blocks")) {
operation.nThreadBlocks = op["nthread_blocks"];
}
ops.push_back(operation);
}
this->operations[rank].push_back(ops);
}
}
}
std::pair<size_t, u_int32_t> ExecutionPlan::Impl::calcSizePerRank(int rank, size_t inputSize, size_t outputSize) const {
std::pair<size_t, u_int32_t> sizePerRank;
if (this->inputChunks.at(rank) == 0 && this->outputChunks.at(rank) == 0) {
throw mscclpp::Error("Output or Input chunks must be greater than 0", mscclpp::ErrorCode::ExecutorError);
} else if (this->inputChunks.at(rank) != 0 && this->outputChunks.at(rank) != 0) {
if (inputSize / this->inputChunks.at(rank) != outputSize / this->outputChunks.at(rank))
throw mscclpp::Error("Size per chunks inconsistent", mscclpp::ErrorCode::ExecutorError);
else
sizePerRank = std::make_pair(inputSize, this->inputChunks.at(rank));
} else if (this->inputChunks.at(rank) != 0) {
sizePerRank = std::make_pair(inputSize, this->inputChunks.at(rank));
} else if (this->outputChunks.at(rank) != 0) {
sizePerRank = std::make_pair(outputSize, this->outputChunks.at(rank));
}
return sizePerRank;
}
size_t ExecutionPlan::Impl::getOffset(int rank, size_t inputSize, size_t outputSize, uint32_t chunkIndex,
uint32_t alignment) const {
if (inputSize % alignment != 0) {
throw Error("inputSize must be a multiple of alignment", ErrorCode::ExecutorError);
}
const int nGroups = this->chunkGroups.at(rank);
auto sizePerRank = calcSizePerRank(rank, inputSize, outputSize);
uint32_t nInputChunks = sizePerRank.second;
uint32_t nelems = sizePerRank.first / (alignment * sizeof(uint8_t));
if (nelems % nGroups != 0) {
throw Error("Input size must be a multiple of nGroups", ErrorCode::ExecutorError);
}
int nelemsPerGroup = nelems / nGroups;
int nChunksPerGroup = nInputChunks / nGroups;
uint32_t minNelems = nelemsPerGroup / nChunksPerGroup;
uint32_t remainder = nelemsPerGroup % nChunksPerGroup;
uint32_t groupIdx = chunkIndex / nChunksPerGroup;
uint32_t chunkIndexInGroup = chunkIndex % nChunksPerGroup;
uint32_t offset = groupIdx * nelemsPerGroup + chunkIndexInGroup * minNelems +
(chunkIndexInGroup % nelemsPerGroup < remainder ? chunkIndexInGroup % nelemsPerGroup : remainder);
return static_cast<size_t>(offset) * alignment;
}
size_t ExecutionPlan::Impl::getNChunkSize(int rank, size_t inputSize, size_t outputSize, uint32_t nChunks,
const std::vector<uint32_t> chunkIndexes) const {
size_t nChunkSize = 0;
for (uint32_t index : chunkIndexes) {
uint32_t beginOff = getOffset(rank, inputSize, outputSize, index);
uint32_t endOff = getOffset(rank, inputSize, outputSize, index + nChunks);
if (nChunkSize == 0) {
nChunkSize = endOff - beginOff;
} else if (nChunkSize != endOff - beginOff) {
throw Error("Inconsistent chunk size", ErrorCode::ExecutorError);
}
}
return nChunkSize;
}
size_t ExecutionPlan::Impl::getUpperBoundChunkSize(int rank, size_t inputSize, size_t outputSize) const {
auto sizePerRank = calcSizePerRank(rank, inputSize, outputSize);
uint32_t nChunks = sizePerRank.second;
return (sizePerRank.first + nChunks - 1) / nChunks;
}
void ExecutionPlan::Impl::reset() {
this->operations.clear();
this->channelInfos.clear();
this->nvlsInfos.clear();
this->threadblockSMChannelMap.clear();
this->threadblockProxyChannelMap.clear();
this->threadblockNvlsChannelMap.clear();
this->inputChunks.clear();
this->outputChunks.clear();
this->scratchChunks.clear();
this->chunkGroups.clear();
}
void ExecutionPlan::Impl::operationsReset() { this->operations.clear(); }
ExecutionPlan::ExecutionPlan(const std::string& planPath) : impl_(std::make_shared<Impl>(planPath)) {}
std::string ExecutionPlan::name() const { return this->impl_->name; }
std::string ExecutionPlan::collective() const { return this->impl_->collective; }
size_t ExecutionPlan::minMessageSize() const { return this->impl_->minMessageSize; }
size_t ExecutionPlan::maxMessageSize() const { return this->impl_->maxMessageSize; }
bool ExecutionPlan::isInPlace() const { return this->impl_->isInPlace; }
} // namespace mscclpp