ShyLU  Version of the Day
shylu_debug_manager.hpp
Go to the documentation of this file.
1 
2 //@HEADER
3 // ************************************************************************
4 //
5 // ShyLU: Hybrid preconditioner package
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 Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 
49 #ifndef SHYLU_DEBUG_MANAGER_HPP
50 #define SHYLU_DEBUG_MANAGER_HPP
51 
52 // Using the #define for SHYLU_DEBUG makes the print() much faster when there
53 // is nothing to print. However, it would be nice to allow print() even in the
54 // release build. We can revisit this if the "if (debug level)" check
55 // becomes too expensive, especially while printing something in the innermost
56 // loop.
57 //#define SHYLU_DEBUG
58 
59 #include "ShyLUCore_config.h"
60 
61 #ifdef HAVE_SHYLUCORE_MPI
62 #include <mpi.h>
63 #endif
64 
65 #include "Teuchos_oblackholestream.hpp"
66 #include "Teuchos_RCP.hpp"
67 
72 {
73  public:
74 
75  DebugManager (int debugLevel = 0,
76  const Teuchos::RCP< std::ostream > &os = Teuchos::rcp(&std::cout,false)
77  );
78 
79  virtual ~DebugManager() {};
80 
81  inline void setOStream(const Teuchos::RCP<std::ostream> &os)
82  {
83  myOS_ = os;
84  };
85 
86  inline void setDebugLevel(int debugLevel) { debugLevel_ = debugLevel; };
87 
88  // TODO: Do we need this ?
89  inline std::ostream& stream()
90  {
91  if ( debugLevel_ && iPrint_ )
92  return *myOS_;
93  else
94  return myBHS_;
95  }
96 
97  inline Teuchos::RCP<std::ostream> getOStream() { return myOS_; };
98 
99  inline void print(int debugLevel, const std::string &output);
100 
101  inline void printInAllTasks(int debugLevel, const std::string &output);
102 
103  // The const char * versions of print functions are needed to avoid the
104  // expensive conversion in code like
105  // print(5, "I am here");
106  inline void print(int debugLevel, const char *output);
107 
108  inline void printInAllTasks(int debugLevel, const char *output);
109 
110  inline void error(const std::string &output);
111 
112  private:
113 
114  int debugLevel_;
117  bool iPrint_;
118  int myPID_;
119 };
120 
121 
122 DebugManager::DebugManager(int debugLevel, const Teuchos::RCP<std::ostream> &os)
123  :
124  debugLevel_(debugLevel),
125  myOS_(os)
126 {
127 #ifdef HAVE_SHYLUCORE_MPI
128  int mpiStarted = 0;
129  MPI_Initialized(&mpiStarted);
130  if (mpiStarted) MPI_Comm_rank(MPI_COMM_WORLD, &myPID_);
131  else myPID_=0;
132 #else
133  myPID_ = 0;
134 #endif
135  iPrint_ = (myPID_ == 0);
136 }
137 
138 inline void DebugManager::print(int debugLevel, const std::string &output)
139 {
140 //#ifdef SHYLU_DEBUG
141  if (debugLevel <= debugLevel_ && iPrint_)
142  *myOS_ << output;
143 //#endif
144 }
145 
146 inline void DebugManager::print(int debugLevel, const char *output)
147 {
148 //#ifdef SHYLU_DEBUG
149  if (debugLevel <= debugLevel_ && iPrint_)
150  *myOS_ << output;
151 //#endif
152 }
153 
154 inline void DebugManager::printInAllTasks(int debugLevel,
155  const std::string &output)
156 {
157 //#ifdef SHYLU_DEBUG
158  if (debugLevel <= debugLevel_)
159  *myOS_ << "PID =" << myPID_ << " " << output;
160 //#endif
161 }
162 
163 inline void DebugManager::printInAllTasks(int debugLevel,
164  const char *output)
165 {
166 //#ifdef SHYLU_DEBUG
167  if (debugLevel <= debugLevel_)
168  *myOS_ << "PID =" << myPID_ << " " << output;
169 //#endif
170 }
171 
172 // Errors show up for all PIDs, even in release builds
173 inline void DebugManager::error(const std::string &output)
174 {
175  *myOS_ << "PID =" << myPID_ << " " << output;
176 }
177 
178 #endif // SHYLU_DEBUG_MANAGER_HPP
Call reponsible for handeling debugging.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)