diff --git a/doc/src/pg_lib_properties.rst b/doc/src/pg_lib_properties.rst index cdc7617d91..25f99fe850 100644 --- a/doc/src/pg_lib_properties.rst +++ b/doc/src/pg_lib_properties.rst @@ -21,6 +21,11 @@ event as atoms are migrating between sub-domains. ----------------------- +.. doxygenfunction:: lammps_memory_usage + :project: progguide + +----------------------- + .. doxygenfunction:: lammps_get_mpi_comm :project: progguide diff --git a/src/info.cpp b/src/info.cpp index 108f902b39..ccf6eb4145 100644 --- a/src/info.cpp +++ b/src/info.cpp @@ -315,41 +315,23 @@ void Info::command(int narg, char **arg) } if (flags & MEMORY) { + double meminfo[3]; + + get_memory_info(meminfo); fputs("\nMemory allocation information (MPI rank 0):\n\n",out); - - double bytes = 0; - bytes += atom->memory_usage(); - bytes += neighbor->memory_usage(); - bytes += comm->memory_usage(); - bytes += update->memory_usage(); - bytes += force->memory_usage(); - bytes += modify->memory_usage(); - for (int i = 0; i < output->ndump; i++) - bytes += output->dump[i]->memory_usage(); - double mbytes = bytes/1024.0/1024.0; - fmt::print(out,"Total dynamically allocated memory: {:.4} Mbyte\n",mbytes); + fmt::print(out,"Total dynamically allocated memory: {:.4} Mbyte\n", + meminfo[0]); #if defined(_WIN32) - HANDLE phandle = GetCurrentProcess(); - PROCESS_MEMORY_COUNTERS_EX pmc; - GetProcessMemoryInfo(phandle,(PROCESS_MEMORY_COUNTERS *)&pmc,sizeof(pmc)); - fmt::print(out,"Non-shared memory use: {:.4} Mbyte\n", - (double)pmc.PrivateUsage/1048576.0); - fmt::print(out,"Maximum working set size: {:.4} Mbyte\n", - (double)pmc.PeakWorkingSetSize/1048576.0); + fmt::print(out,"Non-shared memory use: {:.4} Mbyte\n",meminfo[1]); + fmt::print(out,"Maximum working set size: {:.4} Mbyte\n",meminfo[2]); #else #if defined(__linux__) - struct mallinfo mi; - mi = mallinfo(); fmt::print(out,"Current reserved memory pool size: {:.4} Mbyte\n", - (double)mi.uordblks/1048576.0+(double)mi.hblkhd/1048576.0); + meminfo[1]); #endif - struct rusage ru; - if (getrusage(RUSAGE_SELF, &ru) == 0) { - fmt::print(out,"Maximum resident set size: {:.4} Mbyte\n", - (double)ru.ru_maxrss/1024.0); - } + fmt::print(out,"Maximum resident set size: {:.4} Mbyte\n",meminfo[2]); #endif } @@ -1300,6 +1282,41 @@ std::string Info::get_cxx_info() /* ---------------------------------------------------------------------- */ +void Info::get_memory_info(double *meminfo) +{ + double bytes = 0; + bytes += atom->memory_usage(); + bytes += neighbor->memory_usage(); + bytes += comm->memory_usage(); + bytes += update->memory_usage(); + bytes += force->memory_usage(); + bytes += modify->memory_usage(); + for (int i = 0; i < output->ndump; i++) + bytes += output->dump[i]->memory_usage(); + meminfo[0] = bytes/1024.0/1024.0; + meminfo[1] = 0; + meminfo[2] = 0; + +#if defined(_WIN32) + HANDLE phandle = GetCurrentProcess(); + PROCESS_MEMORY_COUNTERS_EX pmc; + GetProcessMemoryInfo(phandle,(PROCESS_MEMORY_COUNTERS *)&pmc,sizeof(pmc)); + meminfo[1] = (double)pmc.PrivateUsage/1048576.0; + meminfo[2] = (double)pmc.PeakWorkingSetSize/1048576.0; +#else +#if defined(__linux__) + struct mallinfo mi; + mi = mallinfo(); + meminfo[1] = (double)mi.uordblks/1048576.0+(double)mi.hblkhd/1048576.0; +#endif + struct rusage ru; + if (getrusage(RUSAGE_SELF, &ru) == 0) + meminfo[2] = (double)ru.ru_maxrss/1024.0; +#endif +} + +/* ---------------------------------------------------------------------- */ + char **Info::get_variable_names(int &num) { num = input->variable->nvar; return input->variable->names; diff --git a/src/info.h b/src/info.h index 65690ab28e..ae61600b68 100644 --- a/src/info.h +++ b/src/info.h @@ -52,6 +52,7 @@ class Info : protected Pointers { static std::string get_mpi_info(int &, int &); static std::string get_cxx_info(); + void get_memory_info(double *); char **get_variable_names(int &num); private: diff --git a/src/output.cpp b/src/output.cpp index aafbcd971a..d4fd5ae75e 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -21,6 +21,7 @@ #include "error.h" #include "force.h" #include "group.h" +#include "info.h" #include "input.h" #include "memory.h" #include "modify.h" @@ -817,28 +818,19 @@ void Output::create_restart(int narg, char **arg) void Output::memory_usage() { - double bytes = 0; - bytes += atom->memory_usage(); - bytes += neighbor->memory_usage(); - bytes += comm->memory_usage(); - bytes += update->memory_usage(); - bytes += force->memory_usage(); - bytes += modify->memory_usage(); - for (int i = 0; i < ndump; i++) bytes += dump[i]->memory_usage(); + double meminfo[3]; + Info info(lmp); - double mbytes = bytes/1024.0/1024.0; - double mbavg,mbmin,mbmax; + info.get_memory_info(meminfo); + double mbytes = meminfo[0]; + double mbmin,mbavg,mbmax; MPI_Reduce(&mbytes,&mbavg,1,MPI_DOUBLE,MPI_SUM,0,world); MPI_Reduce(&mbytes,&mbmin,1,MPI_DOUBLE,MPI_MIN,0,world); MPI_Reduce(&mbytes,&mbmax,1,MPI_DOUBLE,MPI_MAX,0,world); + mbavg /= comm->nprocs; - if (comm->me == 0) { - mbavg /= comm->nprocs; - if (screen) - fprintf(screen,"Per MPI rank memory allocation (min/avg/max) = " - "%.4g | %.4g | %.4g Mbytes\n",mbmin,mbavg,mbmax); - if (logfile) - fprintf(logfile,"Per MPI rank memory allocation (min/avg/max) = " - "%.4g | %.4g | %.4g Mbytes\n",mbmin,mbavg,mbmax); - } + if (comm->me == 0) + utils::logmesg(lmp,fmt::format("Per MPI rank memory allocation (min/avg/" + "max) = {:.4} | {:.4} | {:.4} Mbytes\n", + mbmin,mbavg,mbmax)); }