5#ifndef MEMORY_MEASUREMENT_H
6#define MEMORY_MEASUREMENT_H
35 gethostname(buffer, 256);
36 return std::string(buffer);
49 template<
typename T, std::
size_t NDIM>
55 template<
typename Archive>
72 template <
typename Archive>
80 template<
typename T, std::
size_t NDIM>
85 MADNESS_EXCEPTION(
"FunctionImpl: remote operation attempting to use a locally uninitialized object",0);
93 template<
typename T, std::
size_t NDIM>
95 const double toGB=double(
sizeof(
T))/(1024*1024*1024);
96 auto sz=
f.nCoeff_local();
97 if (
debug)
print(
"funcimpl<T,",
NDIM,
"> id",
f.id(),
"rank",
f.world.rank(),
"size in GB",sz*toGB);
111 if (
debug and (world.
rank()==0))
print(
"objects in this world ",all_objects);
113 for (
const auto& obj : all_objects) {
114 if (
auto funcimpl=cast_to_funcimpl_ptr<double,1>(obj))
add_memory_to_map(*funcimpl);
115 if (
auto funcimpl=cast_to_funcimpl_ptr<double,2>(obj))
add_memory_to_map(*funcimpl);
116 if (
auto funcimpl=cast_to_funcimpl_ptr<double,3>(obj))
add_memory_to_map(*funcimpl);
117 if (
auto funcimpl=cast_to_funcimpl_ptr<double,4>(obj))
add_memory_to_map(*funcimpl);
118 if (
auto funcimpl=cast_to_funcimpl_ptr<double,5>(obj))
add_memory_to_map(*funcimpl);
119 if (
auto funcimpl=cast_to_funcimpl_ptr<double,6>(obj))
add_memory_to_map(*funcimpl);
128 if (
debug)
print(
"searching worlds",all_worlds);
129 for (
auto world_id : all_worlds) {
149 for (
const auto& [memkey,memval] : memory_vec) {
157 auto accumulate_left =[](std::pair<int,double>&
a,
const std::pair<int,double>&
b) {
162 std::map<std::string,std::pair<int,double>> host_to_rank;
163 for (
const auto& [rank,hostname_and_rss] : rank_to_host) {
164 accumulate_left(host_to_rank[hostname_and_rss.first],std::pair<int,double>(rank,hostname_and_rss.second));
175 std::map<std::pair<std::string,long>,
double> memory_per_host;
177 memory_per_host[{memkey.hostname,memkey.rank}]+=memval.memory_GB;
181 std::vector<std::pair<std::pair<std::string,long>,
double>> memory_per_host_vec(memory_per_host.begin(),memory_per_host.end());
182 std::sort(memory_per_host_vec.begin(),memory_per_host_vec.end(),[](
const auto&
a,
const auto&
b){return a.first<b.first;});
184 return memory_per_host_vec;
191 const std::vector<std::pair<std::pair<std::string,long>,
double>>& mem_per_host_and_rank)
const {
192 std::map<std::string,double> mem_per_host;
193 for (
auto& [hostname_and_rank,memory] : mem_per_host_and_rank) {
194 auto hostname=hostname_and_rank.first;
195 mem_per_host[hostname]+=memory;
198 std::vector<std::pair<std::string,double>> mem_per_host_vec(mem_per_host.begin(),mem_per_host.end());
199 return mem_per_host_vec;
216 if (world.
rank()==0) {
217 print(
"final memory map:",msg);
218 print(
"hostname world rank DIM #funcs memory_GB");
220 constexpr std::size_t
bufsize=256;
227 std::sort(memory_vec.begin(),memory_vec.end(),[](
const std::pair<MemKey,MemInfo>&
a,
const std::pair<MemKey,MemInfo>&
b){return a.first<b.first;});
228 for (
const auto& [memkey,memval] : memory_vec) {
229 snprintf(line,
bufsize,
"%20s %12lu %5lu %5lu %5lu %e", memkey.hostname.c_str(), memkey.world_id, memkey.rank, memkey.DIM, memval.num_functions, memval.memory_GB);
230 print(std::string(line));
237 if (world.
rank()==0) {
238 print(
"memory per host");
240 print(
"hostname memory_GB nrank(universe) rss_GB/host");
242 for (
const auto& [hostname,memory] : info) {
243 snprintf(line,
bufsize,
"%20s %e %d %e", hostname.c_str(), memory,
244 host_to_nrank_and_rss[hostname].first, host_to_nrank_and_rss[hostname].second);
245 print(std::string(line));
248 if (world.
rank()==0) {
251 double total_rss=0.0;
252 for (
auto& [hostname,memory] : info) {
253 total_rss+=host_to_nrank_and_rss[hostname].second;
255 std::string word=
"all hosts";
256 snprintf(line,
bufsize,
"%20s %e %d %e",
257 word.c_str(), total_mem, world.
size(), total_rss);
258 print(std::string(line));
FunctionImpl holds all Function state to facilitate shallow copy semantics.
Definition funcimpl.h:945
measure the memory usage of all FunctionImpl objects of all worlds
Definition memory_measurement.h:17
static std::string get_hostname()
get the hostname of this machine, rank-local
Definition memory_measurement.h:33
const FunctionImpl< T, NDIM > * cast_to_funcimpl_ptr(const uniqueidT obj_id)
Definition memory_measurement.h:81
std::vector< std::pair< std::string, double > > memory_per_host_all_ranks(const std::vector< std::pair< std::pair< std::string, long >, double > > &mem_per_host_and_rank) const
accumulate the memory usage of all objects of all worlds over all ranks per host
Definition memory_measurement.h:190
void print_memory_map(World &world, std::string msg="")
Definition memory_measurement.h:213
std::map< MemKey, MemInfo > MemInfoMapT
Definition memory_measurement.h:78
MemInfoMapT world_memory_map
keeps track of the memory usage of all objects of one or many worlds on this rank
Definition memory_measurement.h:90
void reduce_map(World &universe)
gather all information of the map on rank 0 of the universe
Definition memory_measurement.h:142
friend bool operator<(const MemKey &lhs, const MemKey &other)
Definition memory_measurement.h:60
void clear_map()
reset the memory map
Definition memory_measurement.h:137
void search_world(World &world)
Definition memory_measurement.h:108
void add_memory_to_map(const FunctionImpl< T, NDIM > &f)
Definition memory_measurement.h:94
double total_memory(World &world) const
return the total memory usage over all hosts
Definition memory_measurement.h:203
static std::map< std::string, std::pair< int, double > > host_to_nrank_and_rss_map(World &universe)
given the hostname, return number of ranks and total rss on that node
Definition memory_measurement.h:156
static void measure_and_print(World &world)
measure the memory usage of all objects of all worlds
Definition memory_measurement.h:21
void search_all_worlds()
Definition memory_measurement.h:125
bool debug
Definition memory_measurement.h:91
std::vector< std::pair< std::pair< std::string, long >, double > > memory_per_host_and_rank(World &world) const
accumulate the memory usage of all objects of all worlds for this rank per host
Definition memory_measurement.h:173
void fence(bool debug=false)
Synchronizes all processes in communicator AND globally ensures no pending AM or tasks.
Definition worldgop.cc:161
std::vector< T > concat0(const std::vector< T > &v, size_t bufsz=1024 *1024)
Concatenate an STL vector of serializable stuff onto node 0.
Definition worldgop.h:955
Implements most parts of a globally addressable object (via unique ID).
Definition world_object.h:366
A parallel world class.
Definition world.h:132
static World & get_default()
Default World object accessor.
Definition world.h:260
static World * world_from_id(std::uint64_t id)
Convert a World ID to a World pointer.
Definition world.h:492
std::vector< uniqueidT > get_object_ids() const
Returns a vector of all unique IDs in this World.
Definition world.h:468
ProcessID rank() const
Returns the process rank in this World (same as MPI_Comm_rank()).
Definition world.h:320
static std::vector< unsigned long > get_world_ids()
return a vector containing all world ids
Definition world.h:476
ProcessID size() const
Returns the number of processes in this World (same as MPI_Comm_size()).
Definition world.h:330
WorldGopInterface & gop
Global operations.
Definition world.h:207
static bool exists(World *world)
Check if the World exists in the registry.
Definition world.h:249
std::optional< T * > ptr_from_id(uniqueidT id) const
Look up a local pointer from a world-wide unique ID.
Definition world.h:416
Class for unique global IDs.
Definition uniqueid.h:53
unsigned long get_world_id() const
Access the World ID.
Definition uniqueid.h:90
const std::size_t bufsize
Definition derivatives.cc:16
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:28
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
Main include file for MADNESS and defines Function interface.
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:226
NDIM & f
Definition mra.h:2528
std::map< long, std::pair< std::string, double > > rank_to_host_and_rss_map(World &universe)
return a mapping rank to hostname, return value on rank 0 only
Definition ranks_and_hosts.cpp:36
static const double b
Definition nonlinschro.cc:119
static const double a
Definition nonlinschro.cc:118
Definition memory_measurement.h:67
long num_functions
Definition memory_measurement.h:70
void serialize(Archive &ar) const
Definition memory_measurement.h:73
double memory_GB
Definition memory_measurement.h:71
MemInfo(const MemInfo &other)=default
Definition memory_measurement.h:39
MemKey(World &world)
Definition memory_measurement.h:45
unsigned long rank
Definition memory_measurement.h:41
MemKey(const MemKey &other)=default
std::size_t DIM
Definition memory_measurement.h:43
void serialize(Archive &ar) const
Definition memory_measurement.h:56
MemKey(const FunctionImpl< T, NDIM > &fimpl)
Definition memory_measurement.h:50
unsigned long world_id
Definition memory_measurement.h:40
std::string hostname
Definition memory_measurement.h:42
constexpr std::size_t NDIM
Definition testgconv.cc:54