LifeV
Displayer.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 ************************************************************************
4 
5  This file is part of the LifeV Applications.
6  Copyright (C) 2009-2010 EPFL, Politecnico di Milano
7 
8  This library is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as
10  published by the Free Software Foundation; either version 2.1 of the
11  License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  USA
22 
23 ************************************************************************
24 */
25 /*!
26  * @file
27  *
28  * @brief Template input variables for more general output messages:
29  * now it is possible to display not only strings but also Int, Real, etc..
30  *
31  * @date 2009-03-02
32  * @author Paolo Crosetto <crosetto@iacspc70.epfl.ch>
33  * @author Cristiano Malossi <cristiano.malossi@epfl.ch>
34  *
35  * @maintainer Radu Popescu <radu.popescu@epfl.ch>
36  */
37 
38 #ifndef DISPLAYER_H
39 #define DISPLAYER_H 1
40 
41 
42 #include <Epetra_ConfigDefs.h>
43 #ifdef EPETRA_MPI
44 #include <Epetra_MpiComm.h>
45 #else
46 #include <Epetra_SerialComm.h>
47 #endif
48 
49 
50 #include <lifev/core/LifeV.hpp>
51 
52 namespace LifeV
53 {
54 
55 //! Displayer - This class is used to display messages in parallel simulations.
56 /*!
57  * @author Paolo Crosetto, Cristiano Malossi
58  *
59  * If a communicator is passed to the constructor only one processor (the leader) will print out the message.
60  * If no communicator is passed to the constructor every processor prints the messages.
61  */
62 class Displayer
63 {
64 public:
65 
66  //! @name Public Types
67  //@{
68 
71 
72  //@}
73 
74 
75  //! @name Constructors & Destructor
76  //@{
77 
78  //! Constructor
79  Displayer();
80  // This is dubious! Should avoid using default values for constructor parameters
81  explicit Displayer ( const commPtr_Type& comm );
82 
83  //! Copy constructor
84  /*!
85  * @param displayer Displayer
86  */
87  Displayer ( const Displayer& displayer );
88 
89  //! Destructor
90  virtual ~Displayer() {}
91 
92  //@}
93 
94 
95  //! @name Methods
96  //@{
97 
98  //! Print one message.
99  /*!
100  * @param message1 message to print out
101  */
102  template <typename T1>
103  void leaderPrint ( const T1& message1 ) const;
104 
105  //! Print two messages.
106  /*!
107  * @param message1 first message to print out
108  * @param message2 second message to print out
109  */
110  template <typename T1, typename T2>
111  void leaderPrint ( const T1& message1, const T2& message2 ) const;
112 
113  //! Print three messages.
114  /*!
115  * @param message1 first message to print out
116  * @param message2 second message to print out
117  * @param message3 third message to print out
118  */
119  template <typename T1, typename T2, typename T3>
120  void leaderPrint ( const T1& message1, const T2& message2, const T3& message3 ) const;
121 
122  //! Print the maximum value among the processors
123  /*!
124  * Take a Real input value from all processors in the communicator, computes the max,
125  * returns the max to all processors of the communicator.
126  * Then processor 0 of the communicator prints it.
127  * @param message1 message to print out
128  * @param localMax Int or Real local maximum value that we want to print
129  */
130  template <typename T1>
131  void leaderPrintMax ( const T1& message1, const Real& localMax ) const;
132 
133  //! Print the maximum value among the processors
134  /*!
135  * Take a Real input value from all processors in the communicator, computes the max,
136  * returns the max to all processors of the communicator.
137  * Then processor 0 of the communicator prints it.
138  * @param message1 message to print out
139  * @param message2 second message to print out
140  * @param localMax Int or Real local maximum value that we want to print
141  */
142  template <typename T1, typename T2>
143  void leaderPrintMax ( const T1& message1, const Real& localMax, const T2& message2 ) const;
144 
145  //! Determine if it is the leader
146  /*!
147  * @return true if it is process 0 of the communicator
148  */
149  const bool& isLeader() const;
150 
151  //! @name Set Methods
152  //@{
153 
154  //! Set the communicator
155  /*!
156  * @param comm the communicator
157  */
158  void setCommunicator ( const commPtr_Type& comm );
159 
160  //! @name Get Methods
161  //@{
162 
163  //! Get the communicator
164  /*!
165  * @return the communicator
166  */
167  const commPtr_Type& comm() const
168  {
169  return M_comm;
170  }
171 
172  //@}
173 
174 protected:
175 
177  bool M_verbose;
178 
179 };
180 
181 
182 // ===================================================
183 // Template implementation
184 // ===================================================
185 template <typename T1>
186 void
187 Displayer::leaderPrint ( const T1& message1 ) const
188 {
189  if ( M_verbose )
190  {
191  std::cout << message1 << std::flush;
192  }
193 }
194 
195 template <typename T1, typename T2>
196 void
197 Displayer::leaderPrint ( const T1& message1, const T2& message2 ) const
198 {
199  if ( M_verbose )
200  {
201  std::cout << message1 << message2 << std::flush;
202  }
203 }
204 
205 template <typename T1, typename T2, typename T3>
206 void
207 Displayer::leaderPrint ( const T1& message1, const T2& message2, const T3& message3 ) const
208 {
209  if ( M_verbose )
210  {
211  std::cout << message1 << message2 << message3 << std::flush;
212  }
213 }
214 
215 template <typename T1>
216 void
217 Displayer::leaderPrintMax ( const T1& message1, const Real& localMax ) const
218 {
219  if ( M_comm.get() )
220  {
221  Real num ( localMax );
222  Real globalMax;
223 
224  M_comm->MaxAll ( &num, &globalMax, 1 );
225  if ( M_verbose )
226  {
227  std::cout << message1 << globalMax << std::endl;
228  }
229  }
230  else
231  {
232  std::cout << message1 << localMax << std::endl;
233  }
234 }
235 
236 template <typename T1, typename T2>
237 void
238 Displayer::leaderPrintMax ( const T1& message1, const Real& localMax, const T2& message2 ) const
239 {
240  if ( M_comm.get() )
241  {
242  Real num ( localMax );
243  Real globalMax;
244 
245  M_comm->MaxAll ( &num, &globalMax, 1 );
246  if ( M_verbose )
247  {
248  std::cout << message1 << globalMax << message2 << std::endl;
249  }
250  }
251  else
252  {
253  std::cout << message1 << localMax << message2 << std::endl;
254  }
255 }
256 
257 } // Namespace LifeV
258 
259 #endif // DISPLAYER_H
virtual ~Displayer()
Destructor.
Definition: Displayer.hpp:90
Displayer(const commPtr_Type &comm)
Definition: Displayer.cpp:56
void leaderPrintMax(const T1 &message1, const Real &localMax) const
Print the maximum value among the processors.
Definition: Displayer.hpp:217
const commPtr_Type & comm() const
Get the communicator.
Definition: Displayer.hpp:167
std::shared_ptr< comm_Type > commPtr_Type
Definition: Displayer.hpp:70
void leaderPrint(const T1 &message1, const T2 &message2) const
Print two messages.
Definition: Displayer.hpp:197
void setCommunicator(const commPtr_Type &comm)
Set the communicator.
Definition: Displayer.cpp:86
void updateInverseJacobian(const UInt &iQuadPt)
const bool & isLeader() const
Determine if it is the leader.
Definition: Displayer.cpp:76
Epetra_Comm comm_Type
Definition: Displayer.hpp:69
Displayer(const Displayer &displayer)
Copy constructor.
Definition: Displayer.cpp:66
Displayer()
Constructor.
Definition: Displayer.cpp:46
void leaderPrint(const T1 &message1) const
Print one message.
Definition: Displayer.hpp:187
double Real
Generic real data.
Definition: LifeV.hpp:175
void leaderPrint(const T1 &message1, const T2 &message2, const T3 &message3) const
Print three messages.
Definition: Displayer.hpp:207
commPtr_Type M_comm
Definition: Displayer.hpp:176
void leaderPrintMax(const T1 &message1, const Real &localMax, const T2 &message2) const
Print the maximum value among the processors.
Definition: Displayer.hpp:238
Displayer - This class is used to display messages in parallel simulations.
Definition: Displayer.hpp:62