Skip to content

Commit 3f88aea

Browse files
authored
[CRL] Optimize P2P communication by introducing dynamic chunk sizing and improved buffer slicing (flagos-ai#319)
1 parent 9c4542f commit 3f88aea

4 files changed

Lines changed: 32 additions & 7 deletions

File tree

flagcx/core/group.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ static flagcxResult_t groupLaunch(struct flagcxAsyncJob *job_) {
233233
.proxyConn.connection;
234234
op->stream = p2p->stream;
235235
if (op->connection->transport == TRANSPORT_P2P) {
236-
op->args.chunkSize = flagcxP2pChunkSize;
236+
op->args.chunkSize = computeP2pChunkSize(p2p->bytes);
237237
op->args.chunkSteps =
238-
(p2p->bytes + flagcxP2pChunkSize - 1) / (flagcxP2pChunkSize);
238+
(p2p->bytes + op->args.chunkSize - 1) / (op->args.chunkSize);
239239
op->args.sendStepMask = flagcxP2pChunks - 1;
240240
setP2pSlotInfo(comm->rank, peer, p2p->bytes, p2p->dtype, 1,
241241
&op->args.p2pOpHash, &op->args.p2pSlotIdx);
@@ -312,9 +312,9 @@ static flagcxResult_t groupLaunch(struct flagcxAsyncJob *job_) {
312312
.proxyConn.connection;
313313
op->stream = p2p->stream;
314314
if (op->connection->transport == TRANSPORT_P2P) {
315-
op->args.chunkSize = flagcxP2pChunkSize;
315+
op->args.chunkSize = computeP2pChunkSize(p2p->bytes);
316316
op->args.chunkSteps =
317-
(p2p->bytes + flagcxP2pChunkSize - 1) / (flagcxP2pChunkSize);
317+
(p2p->bytes + op->args.chunkSize - 1) / (op->args.chunkSize);
318318
op->args.sendStepMask = flagcxP2pChunks - 1;
319319
setP2pSlotInfo(comm->rank, peer, p2p->bytes, p2p->dtype, 0,
320320
&op->args.p2pOpHash, &op->args.p2pSlotIdx);

flagcx/core/include/p2p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
extern int64_t flagcxP2pBufferSize;
1313
extern int64_t flagcxP2pChunkSize;
1414
extern int64_t flagcxP2pChunks;
15+
size_t computeP2pChunkSize(size_t nbytes);
1516
#define FLAGCX_P2P_MAX_STEPS 16
1617
#define FLAGCX_P2P_MAX_OPS \
1718
(FLAGCX_P2P_MAX_STEPS * 2) // Maximum number of concurrent P2P operation pairs

flagcx/core/init.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ static flagcxResult_t initTransportsRank(flagcxHeteroComm_t comm,
256256
FLAGCX_PARAM(P2pBufferSize, "P2P_BUFFER_SIZE",
257257
64L * 1024 * 1024); // default value to 64MB
258258
FLAGCX_PARAM(P2pChunkSize, "P2P_CHUNK_SIZE",
259-
4L * 1024 * 1024); // default value to 4MB
259+
16L * 1024 * 1024); // default value to 16MB
260260
FLAGCX_PARAM(NetBufferSize, "NET_BUFFER_SIZE",
261261
64L * 1024 * 1024); // default value to 64MB
262262
FLAGCX_PARAM(NetChunkSize, "NET_CHUNK_SIZE",

flagcx/core/p2p.cc

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@ int64_t flagcxP2pBufferSize;
1212
int64_t flagcxP2pChunkSize;
1313
int64_t flagcxP2pChunks;
1414

15+
size_t computeP2pChunkSize(size_t nbytes) {
16+
size_t dynamicBufferSize = flagcxP2pBufferSize;
17+
if (nbytes < (size_t)flagcxP2pBufferSize) {
18+
size_t msize = nbytes / (1024 * 1024);
19+
int adjustFactor = 0;
20+
if (msize >= 32)
21+
adjustFactor = 1;
22+
else if (msize >= 16)
23+
adjustFactor = 2;
24+
else if (msize >= 8)
25+
adjustFactor = 4;
26+
else if (msize >= 4)
27+
adjustFactor = 8;
28+
else if (msize >= 2)
29+
adjustFactor = 16;
30+
else if (msize >= 1)
31+
adjustFactor = 32;
32+
else
33+
adjustFactor = 64;
34+
dynamicBufferSize = flagcxP2pBufferSize / adjustFactor;
35+
}
36+
return dynamicBufferSize / flagcxP2pChunks;
37+
}
38+
1539
struct p2pIpcExpInfo {
1640
flagcxP2pIpcDesc ipcDesc;
1741
bool legacyIpcCap;
@@ -145,7 +169,7 @@ flagcxResult_t flagcxP2pProxySend(struct flagcxP2pResources *resources,
145169
args->subs[step].stepSize =
146170
std::min(args->chunkSize, size - args->totalCopySize);
147171
args->subs[step].stepBuff =
148-
resources->proxyInfo.recvFifo + (flagcxP2pChunkSize * step);
172+
resources->proxyInfo.recvFifo + (args->chunkSize * step);
149173

150174
FLAGCXCHECK(deviceAdaptor->deviceMemcpy(
151175
args->subs[step].stepBuff, (char *)data + args->totalCopySize,
@@ -266,7 +290,7 @@ flagcxResult_t flagcxP2pProxyRecv(struct flagcxP2pResources *resources,
266290
args->subs[step].stepSize =
267291
std::min(args->chunkSize, size - args->totalCopySize);
268292
args->subs[step].stepBuff =
269-
resources->proxyInfo.recvFifo + (flagcxP2pChunkSize * step);
293+
resources->proxyInfo.recvFifo + (args->chunkSize * step);
270294

271295
FLAGCXCHECK(deviceAdaptor->deviceMemcpy(
272296
(char *)data + args->totalCopySize, args->subs[step].stepBuff,

0 commit comments

Comments
 (0)