votca 2026-dev
Loading...
Searching...
No Matches
memorybudget.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2026 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Standard includes
21#include <algorithm>
22#include <fstream>
23#include <sstream>
24
25#if defined(__APPLE__)
26#include <mach/mach.h>
27#endif
28
29// Local VOTCA includes
31
32namespace votca {
33namespace xtp {
34
35namespace {
36#if !defined(__APPLE__)
37// A "<key> <n> kB" entry of /proc/self/status in bytes, -1 if not there
38double ProcStatusBytes(const std::string& key) {
39 std::ifstream status("/proc/self/status");
40 std::string word;
41 while (status >> word) {
42 if (word == key) {
43 double kb = 0.0;
44 status >> kb;
45 return kb * 1024.0;
46 }
47 }
48 return -1.0;
49}
50#endif
51} // namespace
52
55
56void MemoryBudget::Set(double total_gb, Index concurrent_jobs) {
57 total_bytes_ = (total_gb > 0.0) ? total_gb * 1e9 : 0.0;
58 SetConcurrentJobs(concurrent_jobs);
59}
60
62 concurrent_jobs_ = std::max<Index>(1, concurrent_jobs);
63}
64
68
70#if defined(__APPLE__)
71 mach_task_basic_info_data_t info;
72 mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
73 if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
74 reinterpret_cast<task_info_t>(&info), &count) == KERN_SUCCESS) {
75 return double(info.resident_size);
76 }
77 return -1.0;
78#else
79 return ProcStatusBytes("VmRSS:");
80#endif
81}
82
84#if defined(__APPLE__)
85 mach_task_basic_info_data_t info;
86 mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
87 if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
88 reinterpret_cast<task_info_t>(&info), &count) == KERN_SUCCESS) {
89 return double(info.resident_size_max);
90 }
91 return -1.0;
92#else
93 return ProcStatusBytes("VmHWM:");
94#endif
95}
96
97std::string MemoryBudget::Usage() {
98 const double resident = ResidentBytes();
99 if (resident < 0.0) {
100 return "";
101 }
102 std::ostringstream out;
103 out.precision(4);
104 out << "memory in use " << resident * 1e-9 << " GB";
105 const double peak = PeakResidentBytes();
106 if (peak >= 0.0) {
107 out << ", peak " << peak * 1e-9 << " GB";
108 }
109 return out.str();
110}
111
113 if (!Given()) {
114 return DefaultAllowanceBytes();
115 }
116 const double share = JobBudgetBytes();
117 const double resident = std::max(0.0, ResidentBytes());
118 const double margin = std::max(1e9, 0.05 * share);
119 return std::max(0.0, share - resident / double(concurrent_jobs_) - margin);
120}
121
123 std::ostringstream out;
124 out.precision(3);
125 if (!Given()) {
126 out << "no --memory given, " << DefaultAllowanceBytes() * 1e-9
127 << " GB for optional buffers";
128 return out.str();
129 }
130 out << "budget " << JobBudgetBytes() * 1e-9 << " GB";
131 if (concurrent_jobs_ > 1) {
132 out << " per job (" << total_bytes_ * 1e-9 << " GB for " << concurrent_jobs_
133 << " jobs)";
134 }
135 const double resident = ResidentBytes();
136 if (resident >= 0.0) {
137 out << ", " << resident * 1e-9 << " GB in use";
138 }
139 return out.str();
140}
141
142} // namespace xtp
143} // namespace votca
static std::string Usage()
static double PeakResidentBytes()
Peak resident memory of the process so far (-1 if unknown).
static std::string Describe()
e.g. "budget 400 GB, 46.1 GB in use" or "no --memory given"
static void SetConcurrentJobs(Index concurrent_jobs)
static double DefaultAllowanceBytes()
static Index concurrent_jobs_
static void Set(double total_gb, Index concurrent_jobs=1)
total_gb <= 0: no budget given
static double ResidentBytes()
Resident memory of the process (-1 if unknown on this platform).
static double AvailableBytes()
static double total_bytes_
static double JobBudgetBytes()
The share of the budget of one job (0 without a budget).
Charge transport classes.
Definition ERIs.h:28
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26