00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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)
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 }
00204
00205 #endif // THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP