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
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
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 }
00155
00156 #endif // THYRA_SPMD_MULTI_VECTOR_FILE_IO_HPP