Unverified Commit 4b64be77 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

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

parent 0183e999
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -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

+44 −27
Original line number Diff line number Diff line
@@ -315,41 +315,23 @@ void Info::command(int narg, char **arg)
  }

  if (flags & MEMORY) {
    double meminfo[3];

    fputs("\nMemory allocation information (MPI rank 0):\n\n",out);
    get_memory_info(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();
    double mbytes = bytes/1024.0/1024.0;
    fmt::print(out,"Total dynamically allocated memory: {:.4} Mbyte\n",mbytes);
    fputs("\nMemory allocation information (MPI rank 0):\n\n",out);
    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;
+1 −0
Original line number Diff line number Diff line
@@ -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:
+12 −20
Original line number Diff line number Diff line
@@ -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 mbytes = bytes/1024.0/1024.0;
  double mbavg,mbmin,mbmax;
  double meminfo[3];
  Info info(lmp);

  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);

  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));
}