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 
00053 #ifdef HAVE_MPI
00054 #include <mpi.h>
00055 #endif
00056 
00069 namespace Belos {
00070   
00071   template <class ScalarType>
00072   class OutputManager {
00073     
00074   public:
00075     
00077 
00078     
00080     OutputManager( int vb = Belos::Errors, const Teuchos::RCP<std::ostream> &os = Teuchos::rcp(&std::cout,false) );
00081     
00083     virtual ~OutputManager() {};
00085     
00087 
00088     
00090     void setOStream( const Teuchos::RCP<std::ostream> &os ) { myOS_ = os; };
00091     
00093     void setVerbosity( int vb ) { vb_ = vb; }; 
00094     
00096     
00098 
00099 
00101     std::ostream& stream( MsgType type ) 
00102     {
00103       if ( (type & vb_) && iPrint_ ) {
00104   return *myOS_;
00105       }
00106       return myBHS_;
00107     }
00108  
00110     Teuchos::RCP<std::ostream> getOStream() { return myOS_; };
00111     
00113     
00115 
00116     
00118 
00121     bool isVerbosity( MsgType type ) const { return (( type == Belos::Errors ) || ( vb_ & type )); }; 
00122     
00124 
00126 
00127     
00129     void print( MsgType type, const std::string output );
00130 
00132 
00133   private:
00134     
00136 
00137     
00139     OutputManager( const OutputManager<ScalarType>& OM );
00140     
00142     OutputManager<ScalarType>& operator=( const OutputManager<ScalarType>& OM );
00143     
00145     
00146     int vb_;
00147     Teuchos::RCP<std::ostream> myOS_; 
00148     Teuchos::oblackholestream myBHS_;  
00149     bool iPrint_;
00150   };
00151   
00152   template<class ScalarType>
00153   OutputManager<ScalarType>::OutputManager( int vb, const Teuchos::RCP<std::ostream> &os ) :
00154     vb_(vb),
00155     myOS_(os)
00156   {
00157     int MyPID;
00158 #ifdef HAVE_MPI
00159     // Initialize MPI
00160     int mpiStarted = 0;
00161     MPI_Initialized(&mpiStarted);
00162     if (mpiStarted) MPI_Comm_rank(MPI_COMM_WORLD, &MyPID);
00163     else MyPID=0;
00164 #else 
00165     MyPID = 0;
00166 #endif
00167     iPrint_ = (MyPID == 0);
00168   }
00169  
00170   template<class ScalarType>
00171   void OutputManager<ScalarType>::print( MsgType type, const std::string output ) {
00172   if ( (type & vb_) && iPrint_ ) {
00173     *myOS_ << output;
00174   }
00175 }
00176  
00177 } // end Belos namespace
00178 
00179 #endif
00180 
00181 // end of file BelosOutputManager.hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines