18#ifndef GPR_LINALG_MEMORY_BUDGET_H_
19#define GPR_LINALG_MEMORY_BUDGET_H_
37namespace memory_budget_detail {
44 bytes = std::max<std::int64_t>(0, bytes);
45 available = available ? std::min(*available, bytes) : bytes;
49 const std::filesystem::path& meminfo_path,
50 const std::filesystem::path& membership_path,
51 const std::filesystem::path& cgroup_root)
54 std::ifstream meminfo(meminfo_path);
56 while (std::getline(meminfo, line)) {
57 std::istringstream fields(line);
58 std::string key, unit;
60 if (fields >> key >> kib >> unit && key ==
"MemAvailable:" &&
61 unit ==
"kB" && kib >= 0 &&
62 kib <= std::numeric_limits<std::int64_t>::max() / 1024) {
63 available = kib * 1024;
67 std::ifstream membership(membership_path);
68 while (std::getline(membership, line)) {
69 const auto first = line.find(
':');
71 line.find(
':', first == std::string::npos ? 0 : first + 1);
72 if (first == std::string::npos || second == std::string::npos)
continue;
73 const std::string controllers =
74 line.substr(first + 1, second - first - 1);
76 line.substr(0, first) ==
"0" && controllers.empty();
78 (
"," + controllers +
",").find(
",memory,") == std::string::npos)
80 const auto relative = std::filesystem::path(line.substr(second + 1))
83 auto root = cgroup_root.lexically_normal();
85 std::error_code error;
86 const auto memory = root /
"memory";
87 root /= std::filesystem::is_directory(
memory, error) ?
"memory"
90 const char* limit_name =
91 unified ?
"memory.max" :
"memory.limit_in_bytes";
92 const char* usage_name =
93 unified ?
"memory.current" :
"memory.usage_in_bytes";
94 auto group = root / relative;
98 std::ifstream maximum(group / limit_name);
99 std::ifstream current(group / usage_name);
100 std::int64_t limit = 0, used = 0;
101 if (maximum >> limit && current >> used && limit >= 0 && used >= 0)
102 restrictAvailableMemory(available,
103 used >= limit ? 0 : limit - used);
104 if (group == root)
break;
105 const auto parent = group.parent_path();
106 if (parent == group)
break;
119 return memory_budget_detail::readAvailableMemory(
120 "/proc/meminfo",
"/proc/self/cgroup",
"/sys/fs/cgroup");
131 int node_ranks)
noexcept
133 return !available || needed <= *available / std::max(1, node_ranks);
139 int node_ranks)
noexcept
141 return available <= 0 || needed <= available / std::max(1, node_ranks);
150 const auto gib = [](std::int64_t bytes) {
152 std::snprintf(text,
sizeof text,
"%.1f GiB",
153 static_cast<double>(bytes) / (1024.0 * 1024.0 * 1024.0));
154 return std::string(text);
156 throw std::runtime_error(
157 std::string(what) +
" needs " + gib(needed) +
" per rank; " +
158 gib(*available / std::max(1, node_ranks)) +
" is available per rank (" +
159 gib(*available) +
" on the node over " + std::to_string(node_ranks) +
160 " ranks). Run on more nodes or with fewer ranks per node.");
164 std::int64_t available,
int node_ranks)
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
std::optional< std::int64_t > AvailableMemory
AvailableMemory availableHostBudget()
void requireRankShare(const char *what, std::int64_t needed, AvailableMemory available, int node_ranks)
constexpr bool fitsRankShare(std::int64_t needed, AvailableMemory available, int node_ranks) noexcept
std::int64_t availableHostBytes()
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.