Loading...
Searching...
No Matches
MemoryBudget.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
17
18#ifndef GPR_LINALG_MEMORY_BUDGET_H_
19#define GPR_LINALG_MEMORY_BUDGET_H_
20
21#include <algorithm>
22#include <cstdint>
23#include <cstdio>
24#include <filesystem>
25#include <fstream>
26#include <limits>
27#include <optional>
28#include <sstream>
29#include <stdexcept>
30#include <string>
31
32namespace gpr {
33namespace linalg {
34
35using AvailableMemory = std::optional<std::int64_t>;
36
37namespace memory_budget_detail {
38
39// The most restrictive known limit controls allocation. A measured zero is
40// exhausted memory; only a missing value means that no limit was readable.
41inline void restrictAvailableMemory(AvailableMemory& available,
42 std::int64_t bytes)
43{
44 bytes = std::max<std::int64_t>(0, bytes);
45 available = available ? std::min(*available, bytes) : bytes;
46}
47
48inline AvailableMemory readAvailableMemory(
49 const std::filesystem::path& meminfo_path,
50 const std::filesystem::path& membership_path,
51 const std::filesystem::path& cgroup_root)
52{
53 AvailableMemory available;
54 std::ifstream meminfo(meminfo_path);
55 std::string line;
56 while (std::getline(meminfo, line)) {
57 std::istringstream fields(line);
58 std::string key, unit;
59 std::int64_t kib = 0;
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;
64 break;
65 }
66 }
67 std::ifstream membership(membership_path);
68 while (std::getline(membership, line)) {
69 const auto first = line.find(':');
70 const auto second =
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);
75 const bool unified =
76 line.substr(0, first) == "0" && controllers.empty();
77 if (!unified &&
78 ("," + controllers + ",").find(",memory,") == std::string::npos)
79 continue;
80 const auto relative = std::filesystem::path(line.substr(second + 1))
81 .lexically_normal()
82 .relative_path();
83 auto root = cgroup_root.lexically_normal();
84 if (!unified) {
85 std::error_code error;
86 const auto memory = root / "memory";
87 root /= std::filesystem::is_directory(memory, error) ? "memory"
88 : controllers;
89 }
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;
95 // Each ancestor can set an allocation limit even when the task's
96 // own cgroup reports "max".
97 for (;;) {
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;
107 group = parent;
108 }
109 }
110 return available;
111}
112
113} // namespace memory_budget_detail
114
118{
119 return memory_budget_detail::readAvailableMemory(
120 "/proc/meminfo", "/proc/self/cgroup", "/sys/fs/cgroup");
121}
122
125inline std::int64_t availableHostBytes()
126{
127 return availableHostBudget().value_or(0);
128}
129
130constexpr bool fitsRankShare(std::int64_t needed, AvailableMemory available,
131 int node_ranks) noexcept
132{
133 return !available || needed <= *available / std::max(1, node_ranks);
134}
135
138constexpr bool fitsRankShare(std::int64_t needed, std::int64_t available,
139 int node_ranks) noexcept
140{
141 return available <= 0 || needed <= available / std::max(1, node_ranks);
142}
143
146inline void requireRankShare(const char* what, std::int64_t needed,
147 AvailableMemory available, int node_ranks)
148{
149 if (fitsRankShare(needed, available, node_ranks)) return;
150 const auto gib = [](std::int64_t bytes) {
151 char text[32];
152 std::snprintf(text, sizeof text, "%.1f GiB",
153 static_cast<double>(bytes) / (1024.0 * 1024.0 * 1024.0));
154 return std::string(text);
155 };
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.");
161}
162
163inline void requireRankShare(const char* what, std::int64_t needed,
164 std::int64_t available, int node_ranks)
165{
166 requireRankShare(what, needed,
167 available > 0 ? AvailableMemory{available} : std::nullopt,
168 node_ranks);
169}
170
171} // namespace linalg
172} // namespace gpr
173
174#endif // GPR_LINALG_MEMORY_BUDGET_H_
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.
Definition Macros.h:16