From 526696e0b1e7969329f24a9ca25f9f4fad3366e6 Mon Sep 17 00:00:00 2001 From: fbischoff Date: Wed, 30 Oct 2024 10:45:05 +0100 Subject: [PATCH] print memory usage using getrusage --- CMakeLists.txt | 1 + cmake/config.h.in | 3 ++ src/madness/chem/CCStructures.h | 12 ++++++++ src/madness/misc/misc.h | 4 +++ src/madness/misc/unique_filename.cc | 48 +++++++++++++++++++++-------- 5 files changed, 55 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b62e85176c2..32b9d79fcd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -479,6 +479,7 @@ endif() check_include_file(sys/stat.h HAVE_SYS_STAT_H) check_include_file(sys/types.h HAVE_SYS_TYPES_H) check_include_file(unistd.h HAVE_UNISTD_H) +check_include_file(sys/resource.h HAVE_RESOURCE_H) if(MADNESS_TASK_PROFILING) check_include_file(execinfo.h HAVE_EXECINFO_H) check_include_file(cxxabi.h HAVE_CXXABI_H) diff --git a/cmake/config.h.in b/cmake/config.h.in index b5297280762..baad7fab513 100644 --- a/cmake/config.h.in +++ b/cmake/config.h.in @@ -90,6 +90,9 @@ #ifndef HAVE_UNISTD_H #cmakedefine HAVE_UNISTD_H 1 #endif +#ifndef HAVE_RESOURCE_H +#cmakedefine HAVE_RESOURCE_H 1 +#endif #cmakedefine HAVE_ELEMENTAL_H 1 #cmakedefine HAVE_EL_H 1 diff --git a/src/madness/chem/CCStructures.h b/src/madness/chem/CCStructures.h index feb4bbc4176..c7e90fae492 100644 --- a/src/madness/chem/CCStructures.h +++ b/src/madness/chem/CCStructures.h @@ -72,6 +72,18 @@ assign_name(const PotentialType& inp); std::string assign_name(const FuncType& inp); +/// check memory usage using getrusage +inline void print_memory_usage(const World& world) { + long mem=get_memory_usage(); + std::string hostname=get_hostname(); + std::stringstream ss; + ss << "memory usage of process "<< world.rank()<< " on "<< hostname<< ": "<< mem/1024/1024<<"MB"; + std::string msg=ss.str(); + auto memusage=world.gop.concat0(std::vector(1,msg)); + std::sort(memusage.begin(),memusage.end()); + if (world.rank()==0) for (const auto& msg : memusage) print(msg); +} + // Little structure for formated output and to collect warnings // much room to improve struct CCMessenger { diff --git a/src/madness/misc/misc.h b/src/madness/misc/misc.h index f8ace2438f1..4fcd5607427 100644 --- a/src/madness/misc/misc.h +++ b/src/madness/misc/misc.h @@ -48,6 +48,10 @@ namespace madness { void gprofexit(int id, int nproc); /// creates a unique filename, using PBS ID if available std::string unique_fileid(); + + // get the memory usage of the current process in byte + long get_memory_usage(); + std::string get_hostname(); } #endif // MADNESS_MISC_MISC_H__INCLUDED diff --git a/src/madness/misc/unique_filename.cc b/src/madness/misc/unique_filename.cc index 7788ef328e4..749f5820f86 100644 --- a/src/madness/misc/unique_filename.cc +++ b/src/madness/misc/unique_filename.cc @@ -5,21 +5,43 @@ #include #include +#if defined(HAVE_RESOURCE_H) +#include +#endif + namespace madness { + std::string unique_fileid() { + std::string uniqueFileName; + + // Check if the PBS_JOBID environment variable is set + const char* pbsId = std::getenv("PBS_JOBID"); + if (pbsId != nullptr) { + uniqueFileName =std::string(pbsId); + } else { + // If PBS_ID is not available, generate a random number + int randomNumber = RandomValue(); + uniqueFileName = std::to_string(randomNumber); + } -std::string unique_fileid() { - std::string uniqueFileName; + return uniqueFileName; + } - // Check if the PBS_JOBID environment variable is set - const char* pbsId = std::getenv("PBS_JOBID"); - if (pbsId != nullptr) { - uniqueFileName =std::string(pbsId); - } else { - // If PBS_ID is not available, generate a random number - int randomNumber = RandomValue(); - uniqueFileName = std::to_string(randomNumber); + long get_memory_usage() { + long mem = 0; +//#ifdef HAVE_RESOURCE_H +#if defined(HAVE_RESOURCE_H) + struct rusage usage; + getrusage(RUSAGE_SELF, &usage); + mem = usage.ru_maxrss; +#endif + return mem; } - return uniqueFileName; -} -} + std::string get_hostname() { + char hostname[256]; +#if defined(HAVE_UNISTD_H) + gethostname(hostname, 256); +#endif + return std::string(hostname); + } +} \ No newline at end of file