Skip to content

Commit 037cb2c

Browse files
committed
Update code format
1 parent e2efee6 commit 037cb2c

7 files changed

Lines changed: 131 additions & 129 deletions

File tree

.pre-commit-config.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
repos:
2+
- repo: local
3+
hooks:
4+
- id: clang-format-dry-run
5+
name: clang-format-dry-run
6+
entry: clang-format --dry-run --Werror
7+
files: \.(cpp|cc|h|hpp)$
8+
language: system
9+
210
- repo: local
311
hooks:
412
- id: clang-format
513
name: clang-format
6-
entry: clang-format --dry-run --Werror
14+
entry: clang-format -i --style=file
715
files: \.(cpp|cc|h|hpp)$
816
language: system
917

flagcx/adaptor/device/cuda_adaptor.cc

100755100644
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ flagcxResult_t cudaAdaptorStreamWaitEvent(flagcxStream_t stream,
237237
flagcxResult_t cudaAdaptorEventCreate(flagcxEvent_t *event) {
238238
(*event) = NULL;
239239
flagcxCalloc(event, 1);
240-
DEVCHECK(cudaEventCreateWithFlags((cudaEvent_t *)(*event),
241-
cudaEventDefault));
240+
DEVCHECK(cudaEventCreateWithFlags((cudaEvent_t *)(*event), cudaEventDefault));
242241
return flagcxSuccess;
243242
}
244243

flagcx/adaptor/tuner/nccl_tuner.cc

100755100644
Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,16 @@
11
#include "tuner/tuner_util.h"
22

3-
43
#ifdef USE_NVIDIA_ADAPTOR
54

6-
static EnvVar algo(
7-
"NCCL_ALGO",
8-
{"ring", "tree"},
9-
"ring"
10-
);
11-
12-
static EnvVar proto(
13-
"NCCL_PROTO",
14-
{"LL", "LL128", "Simple"},
15-
"Simple"
16-
);
17-
18-
static EnvVar thread(
19-
"NCCL_NTHREADS",
20-
{"128", "256"},
21-
"256"
22-
);
23-
24-
static EnvVar minChannel(
25-
"NCCL_MIN_NCHANNELS",
26-
{"16", "32"},
27-
"16"
28-
);
29-
30-
static EnvVar chunkSize(
31-
"NCCL_P2P_NVL_CHUNKSIZE",
32-
{"1024", "2048"},
33-
"1024"
34-
);
5+
static EnvVar algo("NCCL_ALGO", {"ring", "tree"}, "ring");
6+
7+
static EnvVar proto("NCCL_PROTO", {"LL", "LL128", "Simple"}, "Simple");
8+
9+
static EnvVar thread("NCCL_NTHREADS", {"128", "256"}, "256");
10+
11+
static EnvVar minChannel("NCCL_MIN_NCHANNELS", {"16", "32"}, "16");
12+
13+
static EnvVar chunkSize("NCCL_P2P_NVL_CHUNKSIZE", {"1024", "2048"}, "1024");
3514

3615
std::vector<EnvVar> vars = {algo, proto, thread, minChannel, chunkSize};
3716

flagcx/adaptor/tuner/param/nccl_param.cc

100755100644
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
// This is a example of intercept NCCL environment getter functions.
2-
#include <stdio.h>
3-
#include <cstring>
4-
#include <string>
52
#include <cstdint>
3+
#include <cstring>
64
#include <pthread.h>
7-
#include <stdlib.h> // for setenv, getenv
5+
#include <stdio.h>
6+
#include <stdlib.h> // for setenv, getenv
7+
#include <string>
88

9-
static void ncclLoadParam(char const* env, int64_t deftVal, int64_t* value) {
9+
static void ncclLoadParam(char const *env, int64_t deftVal, int64_t *value) {
1010
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1111
pthread_mutex_lock(&mutex);
12-
const char* str = getenv(env);
12+
const char *str = getenv(env);
1313
*value = deftVal;
1414
if (str && strlen(str) > 0) {
1515
try {
1616
*value = std::stoll(str);
17-
} catch (const std::exception& e) {
17+
} catch (const std::exception &e) {
1818
*value = deftVal;
1919
}
2020
}
2121
pthread_mutex_unlock(&mutex);
2222
}
2323

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; \
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; \
2929
}
3030

31-
// In order to intercept a NCCL environment getter function, add an additional line of NCCL_PARAM here.
32-
// Below is an example of intercepting NCCL_P2P_NVL_CHUNKSIZE / NTHREADS / MIN_NCHANNELS env.
31+
// In order to intercept a NCCL environment getter function, add an additional
32+
// line of NCCL_PARAM here. Below is an example of intercepting
33+
// NCCL_P2P_NVL_CHUNKSIZE / NTHREADS / MIN_NCHANNELS env.
3334

3435
NCCL_PARAM(P2pNvlChunkSize, "P2P_NVL_CHUNKSIZE", (1 << 19)); /* 512 kB */
35-
NCCL_PARAM(Nthreads, "NTHREADS", (1 << 8)); /*256*/
36-
NCCL_PARAM(MinNchannels, "MIN_NCHANNELS", (1 << 10)); /*1024*/
36+
NCCL_PARAM(Nthreads, "NTHREADS", (1 << 8)); /*256*/
37+
NCCL_PARAM(MinNchannels, "MIN_NCHANNELS", (1 << 10)); /*1024*/

flagcx/adaptor/tuner/tuner_util.cc

100755100644
Lines changed: 85 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,105 @@
11
#include "tuner/tuner_util.h"
22

3-
// Safely copy std::string to char buffer, ensuring NUL termination and truncation
3+
// Safely copy std::string to char buffer, ensuring NUL termination and
4+
// truncation
45
static void safeStrCopy(char *dst, size_t dstSize, const std::string &src) {
5-
if (dstSize == 0) return;
6-
size_t copyLen = std::min(dstSize - 1, src.size());
7-
if (copyLen > 0) memcpy(dst, src.data(), copyLen);
8-
dst[copyLen] = '\0';
6+
if (dstSize == 0)
7+
return;
8+
size_t copyLen = std::min(dstSize - 1, src.size());
9+
if (copyLen > 0)
10+
memcpy(dst, src.data(), copyLen);
11+
dst[copyLen] = '\0';
912
}
1013

1114
// Generate all combinations and return a vector of flagcxEnvConfig
1215
flagcxResult_t generateCandidate(std::vector<struct flagcxEnvConfig> &cfgList) {
1316

14-
// Return empty if there are no environment variables
15-
if (vars.empty()){
16-
INFO(FLAGCX_INIT, "Invalid number of environment variables: 0");
17-
return flagcxInvalidArgument;
18-
}
17+
// Return empty if there are no environment variables
18+
if (vars.empty()) {
19+
INFO(FLAGCX_INIT, "Invalid number of environment variables: 0");
20+
return flagcxInvalidArgument;
21+
}
1922

20-
// If the number of variables exceeds the structure capacity, truncate
21-
if (vars.size() > (size_t)FLAGCX_ENV_LIST_MAX_LENGTH) {
22-
INFO(FLAGCX_INIT, "The number of environment variables exceeds the maximum length defined by FLAGCX_ENV_LIST_MAX_LENGTH");
23-
vars.resize(FLAGCX_ENV_LIST_MAX_LENGTH); // Truncate the vars vector
24-
INFO(FLAGCX_INIT, "The number of environment variables has been truncated to FLAGCX_ENV_LIST_MAX_LENGTH (%d)",FLAGCX_ENV_LIST_MAX_LENGTH);
25-
return flagcxSuccess;
26-
}
23+
// If the number of variables exceeds the structure capacity, truncate
24+
if (vars.size() > (size_t)FLAGCX_ENV_LIST_MAX_LENGTH) {
25+
INFO(FLAGCX_INIT, "The number of environment variables exceeds the maximum "
26+
"length defined by FLAGCX_ENV_LIST_MAX_LENGTH");
27+
vars.resize(FLAGCX_ENV_LIST_MAX_LENGTH); // Truncate the vars vector
28+
INFO(FLAGCX_INIT,
29+
"The number of environment variables has been truncated to "
30+
"FLAGCX_ENV_LIST_MAX_LENGTH (%d)",
31+
FLAGCX_ENV_LIST_MAX_LENGTH);
32+
return flagcxSuccess;
33+
}
2734

28-
// Prepare candidate value lists for each variable (at least one empty string to ensure uniform combination logic)
29-
std::vector<std::vector<std::string>> lists;
30-
lists.reserve(vars.size());
31-
for (const auto &v : vars) {
32-
if (v.choices.empty()) {
33-
lists.emplace_back(std::vector<std::string>{""});
34-
} else {
35-
lists.emplace_back(v.choices);
36-
}
35+
// Prepare candidate value lists for each variable (at least one empty string
36+
// to ensure uniform combination logic)
37+
std::vector<std::vector<std::string>> lists;
38+
lists.reserve(vars.size());
39+
for (const auto &v : vars) {
40+
if (v.choices.empty()) {
41+
lists.emplace_back(std::vector<std::string>{""});
42+
} else {
43+
lists.emplace_back(v.choices);
3744
}
45+
}
3846

39-
// Use an index vector to iterate through the Cartesian product (multi-dimensional counter)
40-
size_t nvars = lists.size();
41-
std::vector<size_t> idx(nvars, 0);
42-
bool done = (nvars == 0);
43-
unsigned long numCandidate = 0;
47+
// Use an index vector to iterate through the Cartesian product
48+
// (multi-dimensional counter)
49+
size_t nvars = lists.size();
50+
std::vector<size_t> idx(nvars, 0);
51+
bool done = (nvars == 0);
52+
unsigned long numCandidate = 0;
4453

45-
while (!done) {
46-
// Construct a flagcxEnvConfig and zero-initialize
47-
flagcxEnvConfig cfg;
48-
memset(&cfg, 0, sizeof(cfg)); // this zeroes commTag and all fields; adjust if you want non-zero defaults
49-
50-
std::string tagStr = "Config " + std::to_string(numCandidate);
51-
if (tagStr.size() < sizeof(cfg.commTag.tag)) {
52-
safeStrCopy(cfg.commTag.tag, sizeof(cfg.commTag.tag), tagStr);
53-
} else {
54-
INFO(FLAGCX_INIT, "Tag string too long, potential buffer overflow");
55-
return flagcxInvalidArgument;
56-
}
57-
cfg.envCount = 0;
54+
while (!done) {
55+
// Construct a flagcxEnvConfig and zero-initialize
56+
flagcxEnvConfig cfg;
57+
memset(&cfg, 0, sizeof(cfg)); // this zeroes commTag and all fields; adjust
58+
// if you want non-zero defaults
5859

59-
// Fill envs
60-
for (size_t i = 0; i < nvars; ++i) {
61-
flagcxEnvEntity &ent = cfg.envs[i];
62-
// type
63-
ent.type = FLAGCX_ENV_TYPE_CREATION;
64-
// name
65-
safeStrCopy(ent.name, sizeof(ent.name), vars[i].name);
66-
// value
67-
const std::string &val = lists[i][idx[i]];
68-
safeStrCopy(ent.value, sizeof(ent.value), val);
69-
// defaultValue
70-
safeStrCopy(ent.defaultValue, sizeof(ent.defaultValue), vars[i].defaultValue);
60+
std::string tagStr = "Config " + std::to_string(numCandidate);
61+
if (tagStr.size() < sizeof(cfg.commTag.tag)) {
62+
safeStrCopy(cfg.commTag.tag, sizeof(cfg.commTag.tag), tagStr);
63+
} else {
64+
INFO(FLAGCX_INIT, "Tag string too long, potential buffer overflow");
65+
return flagcxInvalidArgument;
66+
}
67+
cfg.envCount = 0;
68+
69+
// Fill envs
70+
for (size_t i = 0; i < nvars; ++i) {
71+
flagcxEnvEntity &ent = cfg.envs[i];
72+
// type
73+
ent.type = FLAGCX_ENV_TYPE_CREATION;
74+
// name
75+
safeStrCopy(ent.name, sizeof(ent.name), vars[i].name);
76+
// value
77+
const std::string &val = lists[i][idx[i]];
78+
safeStrCopy(ent.value, sizeof(ent.value), val);
79+
// defaultValue
80+
safeStrCopy(ent.defaultValue, sizeof(ent.defaultValue),
81+
vars[i].defaultValue);
7182

72-
cfg.envCount++;
73-
// Stop if exceeding the maximum allowed envs (should not happen since we truncated vars earlier)
74-
if (cfg.envCount >= FLAGCX_ENV_LIST_MAX_LENGTH) break;
75-
}
83+
cfg.envCount++;
84+
// Stop if exceeding the maximum allowed envs (should not happen since we
85+
// truncated vars earlier)
86+
if (cfg.envCount >= FLAGCX_ENV_LIST_MAX_LENGTH)
87+
break;
88+
}
7689

77-
cfgList.push_back(cfg);
90+
cfgList.push_back(cfg);
7891

79-
// Increment counter (from least significant to most significant)
80-
for (int i = (int)nvars - 1; i >= 0; --i) {
81-
idx[i]++;
82-
if (idx[i] < lists[i].size()) break;
83-
idx[i] = 0;
84-
if (i == 0) done = true;
85-
}
86-
numCandidate += 1;
92+
// Increment counter (from least significant to most significant)
93+
for (int i = (int)nvars - 1; i >= 0; --i) {
94+
idx[i]++;
95+
if (idx[i] < lists[i].size())
96+
break;
97+
idx[i] = 0;
98+
if (i == 0)
99+
done = true;
87100
}
101+
numCandidate += 1;
102+
}
88103

89-
return flagcxSuccess;
104+
return flagcxSuccess;
90105
}

flagcx/adaptor/tuner/tuner_util.h

100755100644
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
#define FLAGCX_TUNER_UTIL_H_
33

44
#include "tuner.h" // struct flagcxEnvConfig
5-
#include <vector>
65
#include <string>
6+
#include <vector>
77

8-
// This is a demonstration function that provide a way to load all config list for a specific GPU.
8+
// This is a demonstration function that provide a way to load all config list
9+
// for a specific GPU.
910

1011
struct EnvVar {
11-
std::string name;
12-
std::vector<std::string> choices;
13-
std::string defaultValue;
14-
EnvVar(std::string n="") : name(std::move(n)) {}
15-
EnvVar(std::string n, std::vector<std::string> c, std::string d = "")
12+
std::string name;
13+
std::vector<std::string> choices;
14+
std::string defaultValue;
15+
EnvVar(std::string n = "") : name(std::move(n)) {}
16+
EnvVar(std::string n, std::vector<std::string> c, std::string d = "")
1617
: name(std::move(n)), choices(std::move(c)), defaultValue(std::move(d)) {}
1718
};
1819

flagcx/core/flagcx_tuner.cc

100755100644
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#include "timer.h"
55
#include "tuner/tuner_util.h"
66
#include <cfloat>
7+
#include <iostream>
78
#include <map>
89
#include <sstream>
910
#include <string>
1011
#include <vector>
11-
#include <iostream>
1212

1313
// A category of collective operation. the minimal unit for tuning.
1414
struct TunerCollCategory {
@@ -144,8 +144,7 @@ flagcxResult_t flagcxTunerInit(size_t nRanks, size_t nNodes,
144144
void **context) {
145145
struct flagcxTunerContext *ctx = new struct flagcxTunerContext;
146146
FLAGCXCHECK(generateCandidate(ctx->configList));
147-
INFO(FLAGCX_TUNING,
148-
"Candidate number: %ld.", ctx->configList.size());
147+
INFO(FLAGCX_TUNING, "Candidate number: %ld.", ctx->configList.size());
149148
ctx->logger = logFunction;
150149
*context = ctx;
151150

0 commit comments

Comments
 (0)