Thyra Version of the Day
Thyra_DefaultSpmdMultiVectorFileIO.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 // 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 Roscoe A. Bartlett (bartlettra@ornl.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP
00043 #define THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP
00044 
00045 #include "Thyra_MultiVectorFileIOBase.hpp"
00046 #include "Thyra_SpmdMultiVectorSerializer.hpp"
00047 #include "Teuchos_Utils.hpp"
00048 
00049 namespace Thyra {
00050 
00067 template<class Scalar>
00068 class DefaultSpmdMultiVectorFileIO
00069   : public MultiVectorFileIOBase<Scalar>
00070 {
00071 public:
00072 
00075 
00079   DefaultSpmdMultiVectorFileIO(
00080     const std::string      &extensionTagName  = ""
00081     ,const  int            numProcs           = -1
00082     ,const  int            procRank           = -1
00083     );
00084   
00101   void setFileNameExtension(
00102     const std::string      &extensionTagName  = ""
00103     ,const  int            numProcs           = -1
00104     ,const  int            procRank           = -1
00105     );
00106   
00108   std::string getLocalFileName( const std::string &fileNameBase ) const;
00109 
00111 
00115   bool isCompatible( const MultiVectorBase<Scalar> &mv ) const;
00117   void readMultiVectorFromFile(
00118     const std::string                 &fileNameBase
00119     ,Thyra::MultiVectorBase<Scalar>   *mv
00120     ) const;
00122   void writeMultiVectorToFile(
00123     const Thyra::MultiVectorBase<Scalar>   &mv
00124     ,const std::string                     &fileNameBase
00125     ) const;
00127 
00128 private:
00129 
00130   std::string    localFileNameExtension_;
00131   bool           useBinaryMode_;
00132 
00133   mutable SpmdMultiVectorSerializer<Scalar>   mvSerializer_;
00134   
00135 };
00136 
00137 // ///////////////////////////
00138 // Implementations
00139 
00140 template<class Scalar>
00141 DefaultSpmdMultiVectorFileIO<Scalar>::DefaultSpmdMultiVectorFileIO(
00142   const std::string      &extensionTagName
00143   ,const  int            numProcs
00144   ,const  int            procRank
00145   )
00146   :useBinaryMode_(false) // ToDo: Make this adjustable!
00147   ,mvSerializer_(useBinaryMode_)
00148 {
00149   setFileNameExtension(extensionTagName,numProcs,procRank);
00150 }
00151 
00152 template<class Scalar>
00153 void DefaultSpmdMultiVectorFileIO<Scalar>::setFileNameExtension(
00154   const std::string      &extensionTagName
00155   ,const  int            numProcs
00156   ,const  int            procRank
00157   )
00158 {
00159   const std::string
00160     endExtension = Teuchos::Utils::getParallelExtension(procRank,numProcs);
00161   if(extensionTagName.length())
00162     localFileNameExtension_ = extensionTagName+"."+endExtension;
00163   else
00164     localFileNameExtension_ = endExtension;
00165 }
00166 
00167 template<class Scalar>
00168 std::string
00169 DefaultSpmdMultiVectorFileIO<Scalar>::getLocalFileName( 
00170   const std::string &fileNameBase
00171   ) const
00172 {
00173   std::ostringstream parallelFileName;
00174   parallelFileName << fileNameBase;
00175   if(localFileNameExtension_.length())
00176     parallelFileName << "." << localFileNameExtension_;
00177   return parallelFileName.str();
00178 }
00179 
00180 template<class Scalar>
00181 bool DefaultSpmdMultiVectorFileIO<Scalar>::isCompatible(
00182   const MultiVectorBase<Scalar> &mv
00183   ) const
00184 {
00185   return mvSerializer_.isCompatible(mv);
00186 }
00187 
00188 template<class Scalar>
00189 void DefaultSpmdMultiVectorFileIO<Scalar>::readMultiVectorFromFile(
00190   const std::string                 &fileNameBase
00191   ,Thyra::MultiVectorBase<Scalar>   *mv
00192   ) const
00193 {
00194   TEUCHOS_TEST_FOR_EXCEPT(!mv);
00195   const std::string fileName = getLocalFileName(fileNameBase);
00196   std::ifstream in_file(fileName.c_str());
00197   TEUCHOS_TEST_FOR_EXCEPTION(
00198     in_file.eof(), std::logic_error
00199     ,"Error, the file \""<<fileName<<"\" could not be opened for input!"
00200     );
00201   mvSerializer_.binaryMode(useBinaryMode_);
00202   mvSerializer_.deserialize(in_file,mv);
00203 }
00204 
00205 template<class Scalar>
00206 void DefaultSpmdMultiVectorFileIO<Scalar>::writeMultiVectorToFile(
00207   const Thyra::MultiVectorBase<Scalar>   &mv
00208   ,const std::string                     &fileNameBase
00209   ) const
00210 {
00211   const std::string fileName = getLocalFileName(fileNameBase);
00212   std::ofstream out_file(fileName.c_str());
00213   mvSerializer_.binaryMode(useBinaryMode_);
00214   mvSerializer_.serialize(mv,out_file);
00215 }
00216 
00217 } // namespace Thyra
00218 
00219 #endif // THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines