|
1 | 1 | #include "tuner/tuner_util.h" |
2 | 2 |
|
3 | | -#ifdef USE_NVIDIA_ADAPTOR |
4 | | -static struct flagcxEnvConfig config1 = { |
5 | | - "defaultConfig1", |
6 | | - 1, |
7 | | - {FLAGCX_ENV_TYPE_CREATION, "NCCL_P2P_NVL_CHUNKSIZE", "1024", "524288"}}; |
8 | | -static struct flagcxEnvConfig config2 = { |
9 | | - "defaultConfig2", |
10 | | - 1, |
11 | | - {FLAGCX_ENV_TYPE_CREATION, "NCCL_P2P_NVL_CHUNKSIZE", "524288", "524288"}}; |
12 | | - |
13 | | -// demo |
14 | | -flagcxResult_t loadConfigList(std::vector<struct flagcxEnvConfig> &cfgList) { |
15 | | - cfgList.push_back(config1); |
16 | | - cfgList.push_back(config2); |
17 | | - return flagcxSuccess; |
| 3 | +// Safely copy std::string to char buffer, ensuring NUL termination and truncation |
| 4 | +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'; |
| 9 | +} |
| 10 | + |
| 11 | +// Generate all combinations and return a vector of flagcxEnvConfig |
| 12 | +flagcxResult_t generateCandidate(std::vector<struct flagcxEnvConfig> &cfgList) { |
| 13 | + |
| 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 | + } |
| 19 | + |
| 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 | + } |
| 27 | + |
| 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 | + } |
| 37 | + } |
| 38 | + |
| 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; |
| 44 | + |
| 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; |
| 58 | + |
| 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); |
| 71 | + |
| 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 | + } |
| 76 | + |
| 77 | + cfgList.push_back(cfg); |
| 78 | + |
| 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; |
| 87 | + } |
| 88 | + |
| 89 | + return flagcxSuccess; |
18 | 90 | } |
19 | | -#endif |
|
0 commit comments