MueLu Version of the Day
Loading...
Searching...
No Matches
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
59namespace MueLu {
60
61 namespace MemUtils {
62
63 std::string PrintMemoryUsage() {
64
65
66#ifdef MUELU_USE_MALLINFO
67 struct mallinfo mem_stats = mallinfo();
68 double memory = mem_stats.hblkhd + mem_stats.usmblks + mem_stats.uordblks;
69
70 char memchar[128];
71 sprintf(memchar,"%12.1f MB",memory/1048576.0);
72 std::string mem(memchar);
73
74 return mem;
75#else
76 std::ostringstream mem;
77 std::ifstream proc("/proc/self/status");
78 std::string s;
79
80 mem << PrintMemoryInfo() << " ";
81 while(getline(proc, s), !proc.fail()) {
82 if(s.substr(0, 6) == "VmSize") {
83 mem << s;
84 return mem.str();
85 }
86 }
87 return mem.str();
88#endif
89
90 }
91
92 std::string PrintMemoryInfo() {
93
94#ifdef MUELU_USE_MALLINFO
95 struct mallinfo mem_stats = mallinfo();
96 double memory = mem_stats.hblkhd + mem_stats.usmblks + mem_stats.uordblks;
97
98 char memchar[128];
99 sprintf(memchar,"%12.1f MB",memory/1048576.0);
100 std::string mem(memchar);
101
102 return mem;
103#else
104 std::ostringstream mem;
105 std::ifstream proc("/proc/meminfo");
106 std::string s;
107 while(getline(proc, s), !proc.fail()) {
108 if(s.substr(0, 7) == "MemFree") {
109 mem << s;
110 return mem.str();
111 }
112
113 }
114 return mem.str();
115#endif
116 }
117
118#ifdef HAVE_MUELU_PROFILING
119 void ReportTimeAndMemory(Teuchos::Time const &timer, Teuchos::Comm<int> const &Comm)
120 {
121 double maxTime=0,minTime=0,avgTime=0;
122 double localTime = timer.totalElapsedTime();
123#ifdef HAVE_MPI
124 int ntimers=1, root=0;
125 MPI_Reduce(&localTime,&maxTime,ntimers,MPI_DOUBLE,MPI_MAX,root,MPI_COMM_WORLD);
126 MPI_Reduce(&localTime,&minTime,ntimers,MPI_DOUBLE,MPI_MIN,root,MPI_COMM_WORLD);
127 MPI_Reduce(&localTime,&avgTime,ntimers,MPI_DOUBLE,MPI_SUM,root,MPI_COMM_WORLD);
128#else
129 maxTime = localTime;
130 minTime = localTime;
131 avgTime = localTime;
132#endif
133 avgTime /= Comm.getSize();
134 //std::cout << "(" << Comm.getRank() << ") " << localTime << std::endl;
135 if (Comm.getRank()==0) {
136 std::cout << "&&&" << timer.name()
137 << " max=" << maxTime << " min=" << minTime << " avg=" << avgTime << std::endl;
138 std::cout << "&&&" << timer.name() << " " << MemUtils::PrintMemoryUsage() << std::endl;
139 }
140 } //ReportTimeAndMemory
141#else
142 void ReportTimeAndMemory(Teuchos::Time const &/* timer */, Teuchos::Comm<int> const &/* Comm */)
143 {
144 return;
145 }
146#endif
147
148 } //namespace MemUtils
149
150} //namespace MueLu
std::string PrintMemoryUsage()
void ReportTimeAndMemory(Teuchos::Time const &, Teuchos::Comm< int > const &)
std::string PrintMemoryInfo()
Namespace for MueLu classes and methods.