-
Notifications
You must be signed in to change notification settings - Fork 72
add nccl env intercept example code #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Compiler and flags for C++ | ||
| CXX = g++ | ||
| CXXFLAGS = -fPIC -Wall -Wextra -O2 -std=c++11 | ||
| LDFLAGS = -shared -pthread | ||
|
|
||
| # Target shared library name | ||
| TARGET = libnccl_param.so | ||
|
|
||
| # Source files | ||
| SOURCES = nccl_param.cc | ||
|
|
||
| # Object files (automatically generated from source files) | ||
| OBJECTS = $(SOURCES:.cc=.o) | ||
|
|
||
| # Default target | ||
| all: $(TARGET) | ||
|
|
||
| # Build the shared library | ||
| $(TARGET): $(OBJECTS) | ||
| $(CXX) $(LDFLAGS) -o $@ $(OBJECTS) | ||
|
|
||
| # Compile C++ source files to object files | ||
| %.o: %.cc | ||
| $(CXX) $(CXXFLAGS) -c $< -o $@ | ||
|
|
||
| # Clean up build files | ||
| clean: | ||
| rm -f $(OBJECTS) $(TARGET) | ||
|
|
||
| # Phony targets | ||
| .PHONY: all clean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // This is a example of intercept NCCL environment getter functions. | ||
| #include <stdio.h> | ||
| #include <cstring> | ||
| #include <string> | ||
| #include <cstdint> | ||
| #include <pthread.h> | ||
| #include <stdlib.h> // for setenv, getenv | ||
|
|
||
| static void ncclLoadParam(char const* env, int64_t deftVal, int64_t* value) { | ||
| static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| pthread_mutex_lock(&mutex); | ||
| const char* str = getenv(env); | ||
| *value = deftVal; | ||
| if (str && strlen(str) > 0) { | ||
| try { | ||
| *value = std::stoll(str); | ||
| } catch (const std::exception& e) { | ||
| *value = deftVal; | ||
| } | ||
| } | ||
| pthread_mutex_unlock(&mutex); | ||
| } | ||
|
Comment on lines
+9
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function can be improved for better C++11 style and clarity:
Here is a suggested refactoring that applies both points: static void ncclLoadParam(char const* env, int64_t deftVal, int64_t* value) {
static std::mutex param_mutex;
std::lock_guard<std::mutex> lock(param_mutex);
const char* str = getenv(env);
*value = deftVal;
if (str && strlen(str) > 0) {
try {
*value = std::stoll(str);
} catch (const std::exception& e) {
// If parsing fails, *value keeps deftVal assigned earlier.
}
}
} |
||
|
|
||
| #define NCCL_PARAM(name, env, deftVal) \ | ||
| int64_t ncclParam##name() { \ | ||
| int64_t value = INT64_MIN; \ | ||
| ncclLoadParam("NCCL_" env, deftVal, &value); \ | ||
| return value; \ | ||
| } | ||
|
|
||
| // In order to intercept a NCCL environment getter function, add an additional line of NCCL_PRARM here. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // Below is an example of intercepting NCCL_P2P_NVL_CHUNKSIZE env. | ||
| NCCL_PARAM(P2pNvlChunkSize, "P2P_NVL_CHUNKSIZE", (1 << 19)); /* 512 kB */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
<stdio.h>header is not used in this file and can be removed to keep the includes clean. Additionally, for better C++ code style, consider using the C++-style headers<cstdlib>and<cstring>instead of<stdlib.h>and<cstring>.