Thyra_SpmdMultiVectorFileIO.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef THYRA_SPMD_MULTI_VECTOR_FILE_IO_HPP
00030 #define THYRA_SPMD_MULTI_VECTOR_FILE_IO_HPP
00031 
00032 #include "Thyra_SpmdMultiVectorSerializer.hpp"
00033 #include "Teuchos_Utils.hpp"
00034 
00035 namespace Thyra {
00036 
00042 template<class Scalar>
00043 class SpmdMultiVectorFileIO {
00044 public:
00045 
00047   SpmdMultiVectorFileIO(
00048     int   procRank  = -1
00049     ,int  numProcs  = -1
00050     );
00051   
00057   void setProcRankAndSize(
00058     int   procRank  = -1
00059     ,int  numProcs  = -1
00060     );
00061   
00063   std::string getParallelFileName( const std::string &fileNameBase ) const;
00064 
00066   Teuchos::RefCountPtr<VectorBase<Scalar> >
00067   readVectorFromFile(
00068     const std::string                                            &fileNameBase
00069     ,const Teuchos::RefCountPtr<const VectorSpaceBase<Scalar> >  &vs
00070     ,const bool                                                  binary = false
00071     ) const;
00072 
00074   void writeToFile(
00075     const MultiVectorBase<Scalar>    &mv
00076     ,const std::string               &fileNameBase
00077     ,const bool                      binary         = false
00078     ) const;
00079 
00080 private:
00081 
00082   std::string parallelExtension_;
00083 
00084 };
00085 
00086 // ////////////////////////////
00087 // Implementations!
00088 
00089 template<class Scalar>
00090 SpmdMultiVectorFileIO<Scalar>::SpmdMultiVectorFileIO(
00091   int   procRank
00092   ,int  numProcs
00093   )
00094 {
00095   setProcRankAndSize(procRank,numProcs);
00096 }
00097 
00098 template<class Scalar>
00099 void SpmdMultiVectorFileIO<Scalar>::setProcRankAndSize(
00100   int   procRank
00101   ,int  numProcs
00102   )
00103 {
00104   parallelExtension_ = Teuchos::Utils::getParallelExtension(procRank,numProcs);
00105 }
00106 
00107 template<class Scalar>
00108 std::string
00109 SpmdMultiVectorFileIO<Scalar>::getParallelFileName( 
00110   const std::string &fileNameBase
00111   ) const
00112 {
00113   std::ostringstream parallelFileName;
00114   parallelFileName << fileNameBase;
00115   if(parallelExtension_.length())
00116     parallelFileName << "." << parallelExtension_;
00117   return parallelFileName.str();
00118 }
00119 
00120 template<class Scalar>
00121 Teuchos::RefCountPtr<VectorBase<Scalar> >
00122 SpmdMultiVectorFileIO<Scalar>::readVectorFromFile(
00123   const std::string                                            &fileNameBase
00124   ,const Teuchos::RefCountPtr<const VectorSpaceBase<Scalar> >  &vs
00125   ,const bool                                                  binary
00126   ) const
00127 {
00128   const std::string fileName = getParallelFileName(fileNameBase);
00129   std::ifstream in_file(fileName.c_str());
00130   TEST_FOR_EXCEPTION(
00131     in_file.eof(), std::logic_error
00132     ,"Error, the file \""<<fileName<<"\" could not be opened for input!"
00133     );
00134   Teuchos::RefCountPtr<VectorBase<Scalar> >
00135     vec = createMember(vs);
00136   SpmdMultiVectorSerializer<Scalar> mvSerializer(binary);
00137   mvSerializer.deserialize(in_file,&*vec);
00138   return vec;
00139 }
00140 
00141 template<class Scalar>
00142 void SpmdMultiVectorFileIO<Scalar>::writeToFile(
00143   const MultiVectorBase<Scalar>    &mv
00144   ,const std::string               &fileNameBase
00145   ,const bool                      binary
00146   ) const
00147 {
00148   const std::string fileName = getParallelFileName(fileNameBase);
00149   std::ofstream out_file(fileName.c_str());
00150   SpmdMultiVectorSerializer<Scalar> mvSerializer(binary);
00151   mvSerializer.serialize(mv,out_file);
00152 }
00153 
00154 } // namespace Thyra
00155 
00156 #endif // THYRA_SPMD_MULTI_VECTOR_FILE_IO_HPP

Generated on Thu Sep 18 12:32:32 2008 for Thyra Operator/Vector Support by doxygen 1.3.9.1