Skip to content

Commit e422ee2

Browse files
Copilotnjzjz
andcommitted
Remove MPI dependency from api_cc library to avoid linking issues
Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com>
1 parent 4c60b92 commit e422ee2

3 files changed

Lines changed: 19 additions & 26 deletions

File tree

doc/development/pytorch-profiler.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ export DP_PYTORCH_PROFILER_OUTPUT_DIR=./profiler_results
1414

1515
3. Check for profiler output in the specified directory:
1616
```bash
17-
# For single-rank or non-MPI usage
1817
ls -la ./profiler_results/pytorch_profiler_trace.json
18+
```
1919

20-
# For MPI usage, each rank gets its own file
21-
ls -la ./profiler_results/pytorch_profiler_trace_rank*.json
20+
For MPI applications, you can use different output directories per rank:
21+
```bash
22+
# Example for rank 0
23+
export DP_PYTORCH_PROFILER_OUTPUT_DIR=./profiler_results_rank0
24+
# Example for rank 1
25+
export DP_PYTORCH_PROFILER_OUTPUT_DIR=./profiler_results_rank1
2226
```
2327

2428
## Environment Variables
@@ -32,13 +36,11 @@ The profiler uses PyTorch's modern `torch::profiler` API and automatically:
3236
- Creates the output directory if it doesn't exist
3337
- Profiles all forward pass operations in DeepPotPT and DeepSpinPT
3438
- Saves profiling results to a JSON file when the object is destroyed
35-
- Automatically includes MPI rank in filename when MPI is available and initialized
3639

3740
## Output Files
3841

39-
- **Single-rank or non-MPI usage**: `pytorch_profiler_trace.json`
40-
- **MPI usage**: `pytorch_profiler_trace_rank{rank}.json` (e.g., `pytorch_profiler_trace_rank0.json`, `pytorch_profiler_trace_rank1.json`)
42+
- **All usage**: `pytorch_profiler_trace.json`
4143

42-
This ensures that each MPI rank saves its profiling data to a separate file, preventing conflicts in multi-rank simulations.
44+
For MPI applications, users can distinguish between ranks by setting different output directories per rank using the `DP_PYTORCH_PROFILER_OUTPUT_DIR` environment variable.
4345

4446
This is intended for development and debugging purposes.

source/api_cc/include/common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ void get_env_nthreads(int& num_intra_nthreads, int& num_inter_nthreads);
173173
void get_env_pytorch_profiler(bool& enable_profiler, std::string& output_dir);
174174

175175
/**
176-
* @brief Get MPI rank if MPI is available and initialized, otherwise return -1.
177-
* @return The MPI rank or -1 if MPI is not available/initialized.
176+
* @brief Get MPI rank. Currently disabled in api_cc to avoid MPI linking dependencies.
177+
* @return Always returns -1. Users can distinguish ranks using different output directories.
178178
**/
179179
int get_mpi_rank();
180180

source/api_cc/src/common.cc

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
#include <sys/stat.h>
1212
#include <errno.h>
1313

14-
// Try to include MPI if available - this will be a no-op if MPI is not available
15-
#ifdef __has_include
16-
#if __has_include(<mpi.h>)
17-
#include <mpi.h>
18-
#endif
19-
#endif
14+
// Note: MPI rank detection has been removed from api_cc library
15+
// to avoid MPI linking dependencies. The profiler will use a generic
16+
// filename. Users can still distinguish between ranks by using different
17+
// output directories per rank if needed.
2018

2119
#include "AtomMap.h"
2220
#include "device.h"
@@ -410,17 +408,10 @@ void deepmd::get_env_pytorch_profiler(bool& enable_profiler, std::string& output
410408
}
411409

412410
int deepmd::get_mpi_rank() {
413-
int rank = -1; // Use -1 to indicate MPI not available/initialized
414-
// Try to detect MPI at runtime
415-
#ifdef MPI_H
416-
int initialized = 0;
417-
if (MPI_Initialized(&initialized) == MPI_SUCCESS && initialized) {
418-
if (MPI_Comm_rank(MPI_COMM_WORLD, &rank) != MPI_SUCCESS) {
419-
rank = -1; // fallback to -1 if MPI_Comm_rank fails
420-
}
421-
}
422-
#endif
423-
return rank;
411+
// MPI rank detection removed from api_cc to avoid MPI linking dependencies
412+
// Always return -1 to indicate no MPI rank available
413+
// Users can distinguish between ranks by using different output directories
414+
return -1;
424415
}
425416

426417
bool deepmd::create_directories(const std::string& path) {

0 commit comments

Comments
 (0)