Skip to content

Commit

Permalink
print memory usage using getrusage
Browse files Browse the repository at this point in the history
  • Loading branch information
fbischoff committed Oct 30, 2024
1 parent a480733 commit 526696e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions src/madness/chem/CCStructures.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(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 {
Expand Down
4 changes: 4 additions & 0 deletions src/madness/misc/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 35 additions & 13 deletions src/madness/misc/unique_filename.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,43 @@
#include<madness/misc/misc.h>
#include<madness/misc/ran.h>

#if defined(HAVE_RESOURCE_H)
#include <sys/resource.h>
#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<int>();
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<int>();
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);
}
}

0 comments on commit 526696e

Please sign in to comment.