Skip to content

Commit 6a261c9

Browse files
committed
Support cluster split functionality
1 parent 5ffc507 commit 6a261c9

2 files changed

Lines changed: 68 additions & 14 deletions

File tree

flagcx/core/cluster.cc

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
#include "cluster.h"
22
#include <cstring>
33

4+
flagcxResult_t parseClusterSplitList(const char *input,
5+
std::vector<int> &output) {
6+
if (input == NULL) {
7+
return flagcxSuccess;
8+
}
9+
10+
output.clear();
11+
std::stringstream ss(input);
12+
std::string token;
13+
14+
while (std::getline(ss, token, ',')) {
15+
try {
16+
int value = std::stoi(token);
17+
output.push_back(value);
18+
} catch (const std::exception &e) {
19+
WARN("Invalid Cluster split info, its format should be like '2,4,8,...'");
20+
return flagcxSystemError;
21+
}
22+
}
23+
24+
return flagcxSuccess;
25+
}
26+
427
flagcxResult_t flagcxCollectClusterInfos(const flagcxVendor *allData,
528
flagcxCommunicatorType_t *type,
629
int *homo_rank, int *homo_root_rank,
@@ -11,7 +34,7 @@ flagcxResult_t flagcxCollectClusterInfos(const flagcxVendor *allData,
1134
*homo_root_rank = 0;
1235
*homo_ranks = 1;
1336
*cluster_id = 0;
14-
*cluster_inter_rank = -1;
37+
*cluster_inter_rank = -1; // deprecated, to be removed
1538
*ncluster = 1;
1639
*type = flagcxCommunicatorHomo;
1740

@@ -55,23 +78,48 @@ flagcxResult_t flagcxCollectClusterInfos(const flagcxVendor *allData,
5578
}
5679

5780
if (*type == flagcxCommunicatorHybrid) {
58-
const char *useDev = flagcxGetEnv("FLAGCX_USEDEV");
59-
int useDev_;
60-
if (useDev == NULL) {
61-
useDev_ = -1;
62-
} else {
63-
useDev_ = std::stoi(useDev);
64-
}
65-
if (*homo_rank == useDev_) {
66-
*cluster_inter_rank = rank;
67-
}
68-
if (*homo_ranks <= useDev_ && *homo_rank == *homo_ranks - 1) {
69-
*cluster_inter_rank = rank;
70-
}
7181
*cluster_id = currCluster;
7282
*ncluster = numClusters;
7383
}
7484

85+
// split and obtain sub-clusters
86+
const char *clusterSplitInfo = flagcxGetEnv("FLAGCX_CLUSTER_SPLIT_LIST");
87+
std::vector<int> clusterSplitList;
88+
FLAGCXCHECK(parseClusterSplitList(clusterSplitInfo, clusterSplitList));
89+
int subClusterId = 0;
90+
for (int i = 0; i < currCluster; ++i) {
91+
subClusterId += clusterSplitList[i];
92+
}
93+
int subHomoRanks = (*homo_ranks) / clusterSplitList[currCluster];
94+
int hasRes =
95+
(((*homo_rank) / subHomoRanks) >= clusterSplitList[currCluster]) ? 1 : 0;
96+
subClusterId += (hasRes == 1) ? ((*homo_rank) / subHomoRanks) - 1
97+
: ((*homo_rank) / subHomoRanks);
98+
int subHomoRank = (hasRes == 1) ? subHomoRanks + ((*homo_rank) % subHomoRanks)
99+
: ((*homo_rank) % subHomoRanks);
100+
int subHomoRootRank = rank - subHomoRank;
101+
if (hasRes == 1 ||
102+
((*homo_rank) / subHomoRanks) == clusterSplitList[currCluster] - 1) {
103+
subHomoRanks += (*homo_ranks) % clusterSplitList[currCluster];
104+
}
105+
int subNClusters = 0;
106+
for (int i = 0; i < (*ncluster); ++i) {
107+
subNClusters += clusterSplitList[i];
108+
}
109+
*homo_rank = subHomoRank;
110+
*homo_root_rank = subHomoRootRank;
111+
*homo_ranks = subHomoRanks;
112+
*cluster_id = subClusterId;
113+
*ncluster = subNClusters;
114+
*type =
115+
(subNClusters > 1) ? flagcxCommunicatorHybrid : flagcxCommunicatorHomo;
116+
117+
INFO(FLAGCX_INIT,
118+
"rank = %d, nranks = %d, nclusters = %d, cluster_id = %d, "
119+
"homo_rank = %d, homo_root_rank = %d, homo_ranks = %d, type = %d",
120+
rank, nranks, *ncluster, *cluster_id, *homo_rank, *homo_root_rank,
121+
*homo_ranks, *type);
122+
75123
return flagcxSuccess;
76124
}
77125

flagcx/core/cluster.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44
#include "adaptor.h"
55
#include "flagcx.h"
66
#include "param.h"
7+
#include <iostream>
78
#include <map>
9+
#include <sstream>
810
#include <string>
11+
#include <vector>
12+
13+
flagcxResult_t parseClusterSplitList(const char *input,
14+
std::vector<int> &output);
915

1016
flagcxResult_t flagcxCollectClusterInfos(const flagcxVendor *allData,
1117
flagcxCommunicatorType_t *type,

0 commit comments

Comments
 (0)