MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_Memory.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include <sstream>
11 #include <fstream>
12 #include "MueLu_Memory.hpp"
13 
14 #include <iostream> // TODO: remove
15 #include <unistd.h>
16 #include <time.h>
17 #ifdef MUELU_USE_MALLINFO
18 #include <malloc.h>
19 #endif
20 
21 //#define MUELU_USE_MALLINFO
22 
23 namespace MueLu {
24 
25 namespace MemUtils {
26 
27 std::string PrintMemoryUsage() {
28 #ifdef MUELU_USE_MALLINFO
29  struct mallinfo mem_stats = mallinfo();
30  double memory = mem_stats.hblkhd + mem_stats.usmblks + mem_stats.uordblks;
31 
32  char memchar[128];
33  sprintf(memchar, "%12.1f MB", memory / 1048576.0);
34  std::string mem(memchar);
35 
36  return mem;
37 #else
38  std::ostringstream mem;
39  std::ifstream proc("/proc/self/status");
40  std::string s;
41 
42  mem << PrintMemoryInfo() << " ";
43  while (getline(proc, s), !proc.fail()) {
44  if (s.substr(0, 6) == "VmSize") {
45  mem << s;
46  return mem.str();
47  }
48  }
49  return mem.str();
50 #endif
51 }
52 
53 std::string PrintMemoryInfo() {
54 #ifdef MUELU_USE_MALLINFO
55  struct mallinfo mem_stats = mallinfo();
56  double memory = mem_stats.hblkhd + mem_stats.usmblks + mem_stats.uordblks;
57 
58  char memchar[128];
59  sprintf(memchar, "%12.1f MB", memory / 1048576.0);
60  std::string mem(memchar);
61 
62  return mem;
63 #else
64  std::ostringstream mem;
65  std::ifstream proc("/proc/meminfo");
66  std::string s;
67  while (getline(proc, s), !proc.fail()) {
68  if (s.substr(0, 7) == "MemFree") {
69  mem << s;
70  return mem.str();
71  }
72  }
73  return mem.str();
74 #endif
75 }
76 
77 void ReportTimeAndMemory(Teuchos::Time const &timer, Teuchos::Comm<int> const &Comm) {
78  double maxTime = 0, minTime = 0, avgTime = 0;
79  double localTime = timer.totalElapsedTime();
80 #ifdef HAVE_MPI
81  int ntimers = 1, root = 0;
82  MPI_Reduce(&localTime, &maxTime, ntimers, MPI_DOUBLE, MPI_MAX, root, MPI_COMM_WORLD);
83  MPI_Reduce(&localTime, &minTime, ntimers, MPI_DOUBLE, MPI_MIN, root, MPI_COMM_WORLD);
84  MPI_Reduce(&localTime, &avgTime, ntimers, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);
85 #else
86  maxTime = localTime;
87  minTime = localTime;
88  avgTime = localTime;
89 #endif
90  avgTime /= Comm.getSize();
91  // std::cout << "(" << Comm.getRank() << ") " << localTime << std::endl;
92  if (Comm.getRank() == 0) {
93  std::cout << "&&&" << timer.name()
94  << " max=" << maxTime << " min=" << minTime << " avg=" << avgTime << std::endl;
95  std::cout << "&&&" << timer.name() << " " << MemUtils::PrintMemoryUsage() << std::endl;
96  }
97 } // ReportTimeAndMemory
98 
99 } // namespace MemUtils
100 
101 } // namespace MueLu
virtual int getSize() const =0
std::string PrintMemoryUsage()
virtual int getRank() const =0
void ReportTimeAndMemory(Teuchos::Time const &timer, Teuchos::Comm< int > const &Comm)
const std::string & name() const
double totalElapsedTime(bool readCurrentTime=false) const
std::string PrintMemoryInfo()