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 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #include <sstream>
47 #include <fstream>
48 #include "MueLu_Memory.hpp"
49 
50 #include <iostream> // TODO: remove
51 #include <unistd.h>
52 #include <time.h>
53 #ifdef MUELU_USE_MALLINFO
54 #include <malloc.h>
55 #endif
56 
57 //#define MUELU_USE_MALLINFO
58 
59 namespace MueLu {
60 
61 namespace MemUtils {
62 
63 std::string PrintMemoryUsage() {
64 #ifdef MUELU_USE_MALLINFO
65  struct mallinfo mem_stats = mallinfo();
66  double memory = mem_stats.hblkhd + mem_stats.usmblks + mem_stats.uordblks;
67 
68  char memchar[128];
69  sprintf(memchar, "%12.1f MB", memory / 1048576.0);
70  std::string mem(memchar);
71 
72  return mem;
73 #else
74  std::ostringstream mem;
75  std::ifstream proc("/proc/self/status");
76  std::string s;
77 
78  mem << PrintMemoryInfo() << " ";
79  while (getline(proc, s), !proc.fail()) {
80  if (s.substr(0, 6) == "VmSize") {
81  mem << s;
82  return mem.str();
83  }
84  }
85  return mem.str();
86 #endif
87 }
88 
89 std::string PrintMemoryInfo() {
90 #ifdef MUELU_USE_MALLINFO
91  struct mallinfo mem_stats = mallinfo();
92  double memory = mem_stats.hblkhd + mem_stats.usmblks + mem_stats.uordblks;
93 
94  char memchar[128];
95  sprintf(memchar, "%12.1f MB", memory / 1048576.0);
96  std::string mem(memchar);
97 
98  return mem;
99 #else
100  std::ostringstream mem;
101  std::ifstream proc("/proc/meminfo");
102  std::string s;
103  while (getline(proc, s), !proc.fail()) {
104  if (s.substr(0, 7) == "MemFree") {
105  mem << s;
106  return mem.str();
107  }
108  }
109  return mem.str();
110 #endif
111 }
112 
113 void ReportTimeAndMemory(Teuchos::Time const &timer, Teuchos::Comm<int> const &Comm) {
114  double maxTime = 0, minTime = 0, avgTime = 0;
115  double localTime = timer.totalElapsedTime();
116 #ifdef HAVE_MPI
117  int ntimers = 1, root = 0;
118  MPI_Reduce(&localTime, &maxTime, ntimers, MPI_DOUBLE, MPI_MAX, root, MPI_COMM_WORLD);
119  MPI_Reduce(&localTime, &minTime, ntimers, MPI_DOUBLE, MPI_MIN, root, MPI_COMM_WORLD);
120  MPI_Reduce(&localTime, &avgTime, ntimers, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);
121 #else
122  maxTime = localTime;
123  minTime = localTime;
124  avgTime = localTime;
125 #endif
126  avgTime /= Comm.getSize();
127  // std::cout << "(" << Comm.getRank() << ") " << localTime << std::endl;
128  if (Comm.getRank() == 0) {
129  std::cout << "&&&" << timer.name()
130  << " max=" << maxTime << " min=" << minTime << " avg=" << avgTime << std::endl;
131  std::cout << "&&&" << timer.name() << " " << MemUtils::PrintMemoryUsage() << std::endl;
132  }
133 } // ReportTimeAndMemory
134 
135 } // namespace MemUtils
136 
137 } // 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()