Mneme
Mneme a tool allowing recording the execution of a GPU (CUDA/HIP) kernel and replaying that kernel as an independent executable.
Loading...
Searching...
No Matches
MnemeRank.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cerrno>
4#include <climits>
5#include <cstdlib>
6#include <iostream>
7#include <optional>
8#include <string>
9#include <string_view>
10
11namespace mneme {
12
13namespace rank_detail {
14
15inline std::optional<int> parseInteger(std::string_view Text) {
16 if (Text.empty())
17 return std::nullopt;
18
19 std::string NullTerminated(Text);
20 errno = 0;
21 char *End = nullptr;
22 long Parsed = std::strtol(NullTerminated.c_str(), &End, 10);
23 if (errno == ERANGE || End == NullTerminated.c_str() || *End != '\0' ||
24 Parsed > INT_MAX || Parsed < INT_MIN)
25 return std::nullopt;
26
27 return static_cast<int>(Parsed);
28}
29
30inline void warnMalformedRankEnvironmentValue(const char *Name,
31 std::string_view Value) {
32 std::cerr << "[mneme] Ignoring malformed rank environment variable " << Name
33 << "='" << Value << "'\n";
34}
35
36inline std::optional<int> firstSetRankEnv(const char *const *Names) {
37 for (const char *Name = *Names; Name; Name = *++Names) {
38 const char *Value = std::getenv(Name);
39 if (!Value)
40 continue;
41
42 auto Parsed = parseInteger(Value);
43 if (!Parsed || *Parsed < 0) {
45 continue;
46 }
47 return Parsed;
48 }
49 return std::nullopt;
50}
51
52} // namespace rank_detail
53
54inline std::optional<int> detectDistributedRank() {
55 static const char *const RankVars[] = {
56 "FLUX_TASK_RANK", "OMPI_COMM_WORLD_RANK", "PMI_RANK",
57 "MPI_RANK", "SLURM_PROCID", "JSM_NAMESPACE_RANK",
58 "PMIX_RANK", "PBS_TASKNUM", nullptr};
59 return rank_detail::firstSetRankEnv(RankVars);
60}
61
62} // namespace mneme
Definition MnemeRank.hpp:13
std::optional< int > parseInteger(std::string_view Text)
Definition MnemeRank.hpp:15
std::optional< int > firstSetRankEnv(const char *const *Names)
Definition MnemeRank.hpp:36
void warnMalformedRankEnvironmentValue(const char *Name, std::string_view Value)
Definition MnemeRank.hpp:30
Definition MnemeAnnotation.cpp:3
std::optional< int > detectDistributedRank()
Definition MnemeRank.hpp:54