|
1 | 1 | #include "core/util/include/util.hpp" |
2 | 2 |
|
3 | 3 | #include <cstdlib> |
4 | | -#ifdef _WIN32 |
5 | 4 | #include <cstdint> |
6 | 5 | #include <iostream> |
7 | 6 | #include <memory> |
8 | 7 | #include <vector> |
9 | | -#endif |
10 | | - |
| 8 | +#include <algorithm> |
| 9 | +#include <array> |
11 | 10 | #include <filesystem> |
| 11 | +#include <ranges> |
12 | 12 | #include <string> |
| 13 | +#include <iostream> |
| 14 | +#include <libenvpp/env.hpp> |
| 15 | + |
| 16 | +//namespace { |
| 17 | +// |
| 18 | +//struct MpiEnvVar { |
| 19 | +// std::string_view name; |
| 20 | +// const char* description; |
| 21 | +//}; |
| 22 | +// |
| 23 | +//} // namespace |
13 | 24 |
|
14 | | -std::string ppc::util::GetAbsolutePath(const std::string &relative_path) { |
| 25 | +std::string ppc::util::GetAbsolutePath(const std::string& relative_path) { |
15 | 26 | const std::filesystem::path path = std::string(PPC_PATH_TO_PROJECT) + "/tasks/" + relative_path; |
16 | 27 | return path.string(); |
17 | 28 | } |
18 | 29 |
|
19 | 30 | int ppc::util::GetNumThreads() { |
20 | | -#ifdef _WIN32 |
21 | | - size_t len; |
22 | | - char omp_env[100]; |
23 | | - errno_t err = getenv_s(&len, omp_env, sizeof(omp_env), "PPC_NUM_THREADS"); |
24 | | - if (err != 0 || len == 0) { |
25 | | - omp_env[0] = '\0'; |
| 31 | + env::prefix pre("PPC"); |
| 32 | + |
| 33 | + const auto num_threads_id = pre.register_variable<unsigned>("NUM_THREADS"); |
| 34 | + |
| 35 | + auto validated = std::move(pre).parse_and_validate(); |
| 36 | + |
| 37 | + if (!validated.ok()) { |
| 38 | + std::cerr << validated.warning_message() << "\n"; |
| 39 | +// throw validated.error_message(); |
26 | 40 | } |
27 | | - int num_threads = std::atoi(omp_env); |
28 | | -#else |
29 | | - const char *omp_env = std::getenv("PPC_NUM_THREADS"); // NOLINT(concurrency-mt-unsafe) |
30 | | - int num_threads = (omp_env != nullptr) ? std::atoi(omp_env) : 1; |
31 | | -#endif |
32 | | - return num_threads; |
| 41 | + |
| 42 | + const auto num_threads = validated.get(num_threads_id); |
| 43 | + return num_threads ? static_cast<int>(*num_threads) : 1; |
33 | 44 | } |
| 45 | + |
| 46 | +//const std::array<MpiEnvVar, 13> kMpiEnvVars = {{{.name = "OMPI_COMM_WORLD_SIZE", .description = "OpenMPI"}, |
| 47 | +// {.name = "OMPI_UNIVERSE_SIZE", .description = "OpenMPI"}, |
| 48 | +// {.name = "PMI_SIZE", .description = "MPICH/Intel MPI"}, |
| 49 | +// {.name = "PMI_RANK", .description = "MPICH/Intel MPI"}, |
| 50 | +// {.name = "PMI_FD", .description = "MPICH/Intel MPI"}, |
| 51 | +// {.name = "HYDRA_CONTROL_FD", .description = "Hydra"}, |
| 52 | +// {.name = "PMIX_RANK", .description = "Cray PMI/PMIx"}, |
| 53 | +// {.name = "PMIX_NAMESPACE", .description = "Cray PMI/PMIx"}, |
| 54 | +// {.name = "SLURM_PROCID", .description = "SLURM"}, |
| 55 | +// {.name = "MPI_LOCALRANKID", .description = "IBM Spectrum MPI"}, |
| 56 | +// {.name = "MSMPI_RANK", .description = "Microsoft MPI"}, |
| 57 | +// {.name = "MSMPI_NODE_ID", .description = "Microsoft MPI"}, |
| 58 | +// {.name = "MSMPI_LOCALRANK", .description = "Microsoft MPI"}}}; |
| 59 | + |
| 60 | +//bool ppc::util::IsUnderMpirun() { |
| 61 | +// auto pre = env::prefix(""); |
| 62 | +// const auto parsed_and_validated_pre = pre.parse_and_validate(); |
| 63 | +// return std::ranges::any_of(kMpiEnvVars, [&](const auto& env_var) { |
| 64 | +// const auto mpi_env_id = pre.register_variable<std::string>(env_var.name); |
| 65 | +// |
| 66 | +// if (!parsed_and_validated_pre.ok()) { |
| 67 | +// std::cerr << parsed_and_validated_pre.warning_message(); |
| 68 | +// throw parsed_and_validated_pre.error_message(); |
| 69 | +// } |
| 70 | +// |
| 71 | +// if (parsed_and_validated_pre.get(mpi_env_id)) { |
| 72 | +// return true; |
| 73 | +// } else { |
| 74 | +// return false; |
| 75 | +// } |
| 76 | +// }); |
| 77 | +//} |
0 commit comments