consolidate memory allocation information into a single function (in Info)

This commit is contained in:
Axel Kohlmeyer
2020-09-15 01:48:38 -04:00
parent 0183e999c9
commit 4b64be77e0
4 changed files with 61 additions and 46 deletions

View File

@ -21,6 +21,11 @@ event as atoms are migrating between sub-domains.
----------------------- -----------------------
.. doxygenfunction:: lammps_memory_usage
:project: progguide
-----------------------
.. doxygenfunction:: lammps_get_mpi_comm .. doxygenfunction:: lammps_get_mpi_comm
:project: progguide :project: progguide

View File

@ -315,41 +315,23 @@ void Info::command(int narg, char **arg)
} }
if (flags & MEMORY) { if (flags & MEMORY) {
double meminfo[3];
get_memory_info(meminfo);
fputs("\nMemory allocation information (MPI rank 0):\n\n",out); fputs("\nMemory allocation information (MPI rank 0):\n\n",out);
fmt::print(out,"Total dynamically allocated memory: {:.4} Mbyte\n",
double bytes = 0; meminfo[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);
#if defined(_WIN32) #if defined(_WIN32)
HANDLE phandle = GetCurrentProcess(); fmt::print(out,"Non-shared memory use: {:.4} Mbyte\n",meminfo[1]);
PROCESS_MEMORY_COUNTERS_EX pmc; fmt::print(out,"Maximum working set size: {:.4} Mbyte\n",meminfo[2]);
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);
#else #else
#if defined(__linux__) #if defined(__linux__)
struct mallinfo mi;
mi = mallinfo();
fmt::print(out,"Current reserved memory pool size: {:.4} Mbyte\n", fmt::print(out,"Current reserved memory pool size: {:.4} Mbyte\n",
(double)mi.uordblks/1048576.0+(double)mi.hblkhd/1048576.0); meminfo[1]);
#endif #endif
struct rusage ru; fmt::print(out,"Maximum resident set size: {:.4} Mbyte\n",meminfo[2]);
if (getrusage(RUSAGE_SELF, &ru) == 0) {
fmt::print(out,"Maximum resident set size: {:.4} Mbyte\n",
(double)ru.ru_maxrss/1024.0);
}
#endif #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) { char **Info::get_variable_names(int &num) {
num = input->variable->nvar; num = input->variable->nvar;
return input->variable->names; return input->variable->names;

View File

@ -52,6 +52,7 @@ class Info : protected Pointers {
static std::string get_mpi_info(int &, int &); static std::string get_mpi_info(int &, int &);
static std::string get_cxx_info(); static std::string get_cxx_info();
void get_memory_info(double *);
char **get_variable_names(int &num); char **get_variable_names(int &num);
private: private:

View File

@ -21,6 +21,7 @@
#include "error.h" #include "error.h"
#include "force.h" #include "force.h"
#include "group.h" #include "group.h"
#include "info.h"
#include "input.h" #include "input.h"
#include "memory.h" #include "memory.h"
#include "modify.h" #include "modify.h"
@ -817,28 +818,19 @@ void Output::create_restart(int narg, char **arg)
void Output::memory_usage() void Output::memory_usage()
{ {
double bytes = 0; double meminfo[3];
bytes += atom->memory_usage(); Info info(lmp);
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 mbytes = bytes/1024.0/1024.0; info.get_memory_info(meminfo);
double mbavg,mbmin,mbmax; double mbytes = meminfo[0];
double mbmin,mbavg,mbmax;
MPI_Reduce(&mbytes,&mbavg,1,MPI_DOUBLE,MPI_SUM,0,world); 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,&mbmin,1,MPI_DOUBLE,MPI_MIN,0,world);
MPI_Reduce(&mbytes,&mbmax,1,MPI_DOUBLE,MPI_MAX,0,world); MPI_Reduce(&mbytes,&mbmax,1,MPI_DOUBLE,MPI_MAX,0,world);
if (comm->me == 0) {
mbavg /= comm->nprocs; mbavg /= comm->nprocs;
if (screen)
fprintf(screen,"Per MPI rank memory allocation (min/avg/max) = " if (comm->me == 0)
"%.4g | %.4g | %.4g Mbytes\n",mbmin,mbavg,mbmax); utils::logmesg(lmp,fmt::format("Per MPI rank memory allocation (min/avg/"
if (logfile) "max) = {:.4} | {:.4} | {:.4} Mbytes\n",
fprintf(logfile,"Per MPI rank memory allocation (min/avg/max) = " mbmin,mbavg,mbmax));
"%.4g | %.4g | %.4g Mbytes\n",mbmin,mbavg,mbmax);
}
} }