Skip to content

Commit 3d64ba1

Browse files
authored
Add nccl env intercept example code (flagos-ai#269)
1 parent 11dc7e4 commit 3d64ba1

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

flagcx/adaptor/tuner/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Compiler and flags for C++
2+
CXX = g++
3+
CXXFLAGS = -fPIC -Wall -Wextra -O2 -std=c++11
4+
LDFLAGS = -shared -pthread
5+
6+
# Target shared library name
7+
TARGET = libnccl_param.so
8+
9+
# Source files
10+
SOURCES = nccl_param.cc
11+
12+
# Object files (automatically generated from source files)
13+
OBJECTS = $(SOURCES:.cc=.o)
14+
15+
# Default target
16+
all: $(TARGET)
17+
18+
# Build the shared library
19+
$(TARGET): $(OBJECTS)
20+
$(CXX) $(LDFLAGS) -o $@ $(OBJECTS)
21+
22+
# Compile C++ source files to object files
23+
%.o: %.cc
24+
$(CXX) $(CXXFLAGS) -c $< -o $@
25+
26+
# Clean up build files
27+
clean:
28+
rm -f $(OBJECTS) $(TARGET)
29+
30+
# Phony targets
31+
.PHONY: all clean

flagcx/adaptor/tuner/nccl_param.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This is a example of intercept NCCL environment getter functions.
2+
#include <stdio.h>
3+
#include <cstring>
4+
#include <string>
5+
#include <cstdint>
6+
#include <pthread.h>
7+
#include <stdlib.h> // for setenv, getenv
8+
9+
static void ncclLoadParam(char const* env, int64_t deftVal, int64_t* value) {
10+
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
11+
pthread_mutex_lock(&mutex);
12+
const char* str = getenv(env);
13+
*value = deftVal;
14+
if (str && strlen(str) > 0) {
15+
try {
16+
*value = std::stoll(str);
17+
} catch (const std::exception& e) {
18+
*value = deftVal;
19+
}
20+
}
21+
pthread_mutex_unlock(&mutex);
22+
}
23+
24+
#define NCCL_PARAM(name, env, deftVal) \
25+
int64_t ncclParam##name() { \
26+
int64_t value = INT64_MIN; \
27+
ncclLoadParam("NCCL_" env, deftVal, &value); \
28+
return value; \
29+
}
30+
31+
// In order to intercept a NCCL environment getter function, add an additional line of NCCL_PRARM here.
32+
// Below is an example of intercepting NCCL_P2P_NVL_CHUNKSIZE env.
33+
NCCL_PARAM(P2pNvlChunkSize, "P2P_NVL_CHUNKSIZE", (1 << 19)); /* 512 kB */

flagcx/core/flagcx_tuner.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ struct flagcxTunerContext {
116116
static struct flagcxEnvConfig config1 = {
117117
"defaultConfig1",
118118
1,
119-
{FLAGCX_ENV_TYPE_COLL, "NCCL_P2P_NVL_CHUNKSIZE", "1024", "524288"}};
119+
{FLAGCX_ENV_TYPE_CREATION, "NCCL_P2P_NVL_CHUNKSIZE", "1024", "524288"}};
120120
static struct flagcxEnvConfig config2 = {
121121
"defaultConfig2",
122122
1,
123-
{FLAGCX_ENV_TYPE_COLL, "NCCL_P2P_NVL_CHUNKSIZE", "524288", "524288"}};
123+
{FLAGCX_ENV_TYPE_CREATION, "NCCL_P2P_NVL_CHUNKSIZE", "524288", "524288"}};
124124

125125
bool operator<(const struct flagcxCommTag &lhs,
126126
const struct flagcxCommTag &rhs) {

0 commit comments

Comments
 (0)