|
Thyra Package Browser (Single Doxygen Collection) Version of the Day
|
00001 // *********************************************************************** 00002 // 00003 // Thyra: Interfaces and Support for Abstract Numerical Algorithms 00004 // Copyright (2004) Sandia Corporation 00005 // 00006 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00007 // license for use of this work by or on behalf of the U.S. Government. 00008 // 00009 // This library is free software; you can redistribute it and/or modify 00010 // it under the terms of the GNU Lesser General Public License as 00011 // published by the Free Software Foundation; either version 2.1 of the 00012 // License, or (at your option) any later version. 00013 // 00014 // This library is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // Lesser General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU Lesser General Public 00020 // License along with this library; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00022 // USA 00023 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00024 // 00025 // *********************************************************************** 00026 // @HEADER 00027 00028 #ifndef THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP 00029 #define THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP 00030 00031 #include "Thyra_MultiVectorFileIOBase.hpp" 00032 #include "Thyra_SpmdMultiVectorSerializer.hpp" 00033 #include "Teuchos_Utils.hpp" 00034 00035 namespace Thyra { 00036 00053 template<class Scalar> 00054 class DefaultSpmdMultiVectorFileIO 00055 : public MultiVectorFileIOBase<Scalar> 00056 { 00057 public: 00058 00061 00065 DefaultSpmdMultiVectorFileIO( 00066 const std::string &extensionTagName = "" 00067 ,const int numProcs = -1 00068 ,const int procRank = -1 00069 ); 00070 00087 void setFileNameExtension( 00088 const std::string &extensionTagName = "" 00089 ,const int numProcs = -1 00090 ,const int procRank = -1 00091 ); 00092 00094 std::string getLocalFileName( const std::string &fileNameBase ) const; 00095 00097 00101 bool isCompatible( const MultiVectorBase<Scalar> &mv ) const; 00103 void readMultiVectorFromFile( 00104 const std::string &fileNameBase 00105 ,Thyra::MultiVectorBase<Scalar> *mv 00106 ) const; 00108 void writeMultiVectorToFile( 00109 const Thyra::MultiVectorBase<Scalar> &mv 00110 ,const std::string &fileNameBase 00111 ) const; 00113 00114 private: 00115 00116 std::string localFileNameExtension_; 00117 bool useBinaryMode_; 00118 00119 mutable SpmdMultiVectorSerializer<Scalar> mvSerializer_; 00120 00121 }; 00122 00123 // /////////////////////////// 00124 // Implementations 00125 00126 template<class Scalar> 00127 DefaultSpmdMultiVectorFileIO<Scalar>::DefaultSpmdMultiVectorFileIO( 00128 const std::string &extensionTagName 00129 ,const int numProcs 00130 ,const int procRank 00131 ) 00132 :useBinaryMode_(false) // ToDo: Make this adjustable! 00133 ,mvSerializer_(useBinaryMode_) 00134 { 00135 setFileNameExtension(extensionTagName,numProcs,procRank); 00136 } 00137 00138 template<class Scalar> 00139 void DefaultSpmdMultiVectorFileIO<Scalar>::setFileNameExtension( 00140 const std::string &extensionTagName 00141 ,const int numProcs 00142 ,const int procRank 00143 ) 00144 { 00145 const std::string 00146 endExtension = Teuchos::Utils::getParallelExtension(procRank,numProcs); 00147 if(extensionTagName.length()) 00148 localFileNameExtension_ = extensionTagName+"."+endExtension; 00149 else 00150 localFileNameExtension_ = endExtension; 00151 } 00152 00153 template<class Scalar> 00154 std::string 00155 DefaultSpmdMultiVectorFileIO<Scalar>::getLocalFileName( 00156 const std::string &fileNameBase 00157 ) const 00158 { 00159 std::ostringstream parallelFileName; 00160 parallelFileName << fileNameBase; 00161 if(localFileNameExtension_.length()) 00162 parallelFileName << "." << localFileNameExtension_; 00163 return parallelFileName.str(); 00164 } 00165 00166 template<class Scalar> 00167 bool DefaultSpmdMultiVectorFileIO<Scalar>::isCompatible( 00168 const MultiVectorBase<Scalar> &mv 00169 ) const 00170 { 00171 return mvSerializer_.isCompatible(mv); 00172 } 00173 00174 template<class Scalar> 00175 void DefaultSpmdMultiVectorFileIO<Scalar>::readMultiVectorFromFile( 00176 const std::string &fileNameBase 00177 ,Thyra::MultiVectorBase<Scalar> *mv 00178 ) const 00179 { 00180 TEST_FOR_EXCEPT(!mv); 00181 const std::string fileName = getLocalFileName(fileNameBase); 00182 std::ifstream in_file(fileName.c_str()); 00183 TEST_FOR_EXCEPTION( 00184 in_file.eof(), std::logic_error 00185 ,"Error, the file \""<<fileName<<"\" could not be opened for input!" 00186 ); 00187 mvSerializer_.binaryMode(useBinaryMode_); 00188 mvSerializer_.deserialize(in_file,mv); 00189 } 00190 00191 template<class Scalar> 00192 void DefaultSpmdMultiVectorFileIO<Scalar>::writeMultiVectorToFile( 00193 const Thyra::MultiVectorBase<Scalar> &mv 00194 ,const std::string &fileNameBase 00195 ) const 00196 { 00197 const std::string fileName = getLocalFileName(fileNameBase); 00198 std::ofstream out_file(fileName.c_str()); 00199 mvSerializer_.binaryMode(useBinaryMode_); 00200 mvSerializer_.serialize(mv,out_file); 00201 } 00202 00203 } // namespace Thyra 00204 00205 #endif // THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP
1.7.4