Skip to content

Commit 3fe09c8

Browse files
committed
Support cluster split functionality
1 parent 5ffc507 commit 3fe09c8

2 files changed

Lines changed: 67 additions & 13 deletions

File tree

flagcx/core/cluster.cc

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

4+
flagcxResult_t parseClusterSplitList(const char *input,
5+
std::vector<int> &output) {
6+
output.clear();
7+
std::stringstream ss(input);
8+
std::string token;
9+
10+
while (std::getline(ss, token, ',')) {
11+
try {
12+
int value = std::stoi(token);
13+
output.push_back(value);
14+
} catch (const std::exception &e) {
15+
WARN("Invalid cluster split info, its format should be like '2,4,8,...'");
16+
return flagcxSystemError;
17+
}
18+
}
19+
20+
return flagcxSuccess;
21+
}
22+
423
flagcxResult_t flagcxCollectClusterInfos(const flagcxVendor *allData,
524
flagcxCommunicatorType_t *type,
625
int *homo_rank, int *homo_root_rank,
@@ -11,7 +30,7 @@ flagcxResult_t flagcxCollectClusterInfos(const flagcxVendor *allData,
1130
*homo_root_rank = 0;
1231
*homo_ranks = 1;
1332
*cluster_id = 0;
14-
*cluster_inter_rank = -1;
33+
*cluster_inter_rank = -1; // deprecated, to be removed
1534
*ncluster = 1;
1635
*type = flagcxCommunicatorHomo;
1736

@@ -55,21 +74,50 @@ flagcxResult_t flagcxCollectClusterInfos(const flagcxVendor *allData,
5574
}
5675

5776
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);
77+
*cluster_id = currCluster;
78+
*ncluster = numClusters;
79+
}
80+
81+
// split and obtain sub-clusters
82+
const char *clusterSplitInfo = flagcxGetEnv("FLAGCX_CLUSTER_SPLIT_LIST");
83+
if (clusterSplitInfo != NULL) {
84+
std::vector<int> clusterSplitList;
85+
FLAGCXCHECK(parseClusterSplitList(clusterSplitInfo, clusterSplitList));
86+
if (*ncluster != int(clusterSplitList.size())) {
87+
WARN("Invalid cluster split info, its length should be equal to the "
88+
"number of homogeneous cluster");
89+
return flagcxSystemError;
6490
}
65-
if (*homo_rank == useDev_) {
66-
*cluster_inter_rank = rank;
91+
92+
int subClusterId = 0;
93+
for (int i = 0; i < currCluster; ++i) {
94+
subClusterId += clusterSplitList[i];
6795
}
68-
if (*homo_ranks <= useDev_ && *homo_rank == *homo_ranks - 1) {
69-
*cluster_inter_rank = rank;
96+
int subHomoRanks = (*homo_ranks) / clusterSplitList[currCluster];
97+
int hasRes =
98+
(((*homo_rank) / subHomoRanks) >= clusterSplitList[currCluster]) ? 1
99+
: 0;
100+
subClusterId += (hasRes == 1) ? ((*homo_rank) / subHomoRanks) - 1
101+
: ((*homo_rank) / subHomoRanks);
102+
int subHomoRank = (hasRes == 1)
103+
? subHomoRanks + ((*homo_rank) % subHomoRanks)
104+
: ((*homo_rank) % subHomoRanks);
105+
int subHomoRootRank = rank - subHomoRank;
106+
if (hasRes == 1 ||
107+
((*homo_rank) / subHomoRanks) == clusterSplitList[currCluster] - 1) {
108+
subHomoRanks += (*homo_ranks) % clusterSplitList[currCluster];
70109
}
71-
*cluster_id = currCluster;
72-
*ncluster = numClusters;
110+
int subNClusters = 0;
111+
for (int i = 0; i < (*ncluster); ++i) {
112+
subNClusters += clusterSplitList[i];
113+
}
114+
*homo_rank = subHomoRank;
115+
*homo_root_rank = subHomoRootRank;
116+
*homo_ranks = subHomoRanks;
117+
*cluster_id = subClusterId;
118+
*ncluster = subNClusters;
119+
*type =
120+
(subNClusters > 1) ? flagcxCommunicatorHybrid : flagcxCommunicatorHomo;
73121
}
74122

75123
return flagcxSuccess;

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)