Belos Version of the Day
BelosOutputManager.hpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ************************************************************************
00003 //
00004 //                 Belos: Block Linear Solvers Package
00005 //                  Copyright 2004 Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ************************************************************************
00040 //@HEADER
00041 
00042 #ifndef BELOS_OUTPUT_MANAGER_HPP
00043 #define BELOS_OUTPUT_MANAGER_HPP
00044 
00049 #include "BelosConfigDefs.hpp"
00050 #include "BelosTypes.hpp"
00051 #include "Teuchos_oblackholestream.hpp" 
00052 #include "Teuchos_RCP.hpp"
00053 
00054 #ifdef HAVE_MPI
00055 #include <mpi.h>
00056 #endif
00057 
00070 namespace Belos {
00071   
00072   template <class ScalarType>
00073   class OutputManager {
00074     
00075   public:
00076     
00078 
00079     
00081     OutputManager( int vb = Belos::Errors, const Teuchos::RCP< std::ostream > &os = Teuchos::rcp(&std::cout,false) );
00082     
00084     virtual ~OutputManager() {};
00086     
00088 
00089     
00091     void setOStream( const Teuchos::RCP<std::ostream> &os ) { myOS_ = os; };
00092     
00094     void setVerbosity( int vb ) { vb_ = vb; }; 
00095     
00097     
00099 
00100 
00102     std::ostream& stream( MsgType type ) 
00103     {
00104       if ( (type & vb_) && iPrint_ ) {
00105   return *myOS_;
00106       }
00107       return myBHS_;
00108     }
00109  
00111     Teuchos::RCP<std::ostream> getOStream() { return myOS_; };
00112     
00114     
00116 
00117     
00119 
00122     bool isVerbosity( MsgType type ) const { return (( type == Belos::Errors ) || ( vb_ & type )); }; 
00123     
00125 
00127 
00128     
00130     void print( MsgType type, const std::string output );
00131 
00133 
00134   private:
00135     
00137 
00138     
00140     OutputManager( const OutputManager<ScalarType>& OM );
00141     
00143     OutputManager<ScalarType>& operator=( const OutputManager<ScalarType>& OM );
00144     
00146     
00147     int vb_;
00148     Teuchos::RCP<std::ostream> myOS_; 
00149     Teuchos::oblackholestream myBHS_;  
00150     bool iPrint_;
00151   };
00152   
00153   template<class ScalarType>
00154   OutputManager<ScalarType>::OutputManager( int vb, const Teuchos::RCP<std::ostream> &os ) :
00155     vb_(vb),
00156     myOS_(os)
00157   {
00158     int MyPID;
00159 #ifdef HAVE_MPI
00160     // Initialize MPI
00161     int mpiStarted = 0;
00162     MPI_Initialized(&mpiStarted);
00163     if (mpiStarted) MPI_Comm_rank(MPI_COMM_WORLD, &MyPID);
00164     else MyPID=0;
00165 #else 
00166     MyPID = 0;
00167 #endif
00168     iPrint_ = (MyPID == 0);
00169   }
00170  
00171   template<class ScalarType>
00172   void OutputManager<ScalarType>::print( MsgType type, const std::string output ) {
00173   if ( (type & vb_) && iPrint_ ) {
00174     *myOS_ << output;
00175   }
00176 }
00177  
00178 } // end Belos namespace
00179 
00180 #endif
00181 
00182 // end of file BelosOutputManager.hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines