Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_DefaultSpmdMultiVectorFileIO.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP
11 #define THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP
12 
13 #include "Thyra_MultiVectorFileIOBase.hpp"
14 #include "Thyra_SpmdMultiVectorSerializer.hpp"
15 #include "Teuchos_Utils.hpp"
16 
17 namespace Thyra {
18 
35 template<class Scalar>
37  : public MultiVectorFileIOBase<Scalar>
38 {
39 public:
40 
43 
48  const std::string &extensionTagName = ""
49  ,const int numProcs = -1
50  ,const int procRank = -1
51  );
52 
70  const std::string &extensionTagName = ""
71  ,const int numProcs = -1
72  ,const int procRank = -1
73  );
74 
76  std::string getLocalFileName( const std::string &fileNameBase ) const;
77 
79 
83  bool isCompatible( const MultiVectorBase<Scalar> &mv ) const;
86  const std::string &fileNameBase
88  ) const;
92  ,const std::string &fileNameBase
93  ) const;
95 
96 private:
97 
98  std::string localFileNameExtension_;
99  bool useBinaryMode_;
100 
101  mutable SpmdMultiVectorSerializer<Scalar> mvSerializer_;
102 
103 };
104 
105 // ///////////////////////////
106 // Implementations
107 
108 template<class Scalar>
110  const std::string &extensionTagName
111  ,const int numProcs
112  ,const int procRank
113  )
114  :useBinaryMode_(false) // ToDo: Make this adjustable!
115  ,mvSerializer_(useBinaryMode_)
116 {
117  setFileNameExtension(extensionTagName,numProcs,procRank);
118 }
119 
120 template<class Scalar>
122  const std::string &extensionTagName
123  ,const int numProcs
124  ,const int procRank
125  )
126 {
127  const std::string
128  endExtension = Teuchos::Utils::getParallelExtension(procRank,numProcs);
129  if(extensionTagName.length())
130  localFileNameExtension_ = extensionTagName+"."+endExtension;
131  else
132  localFileNameExtension_ = endExtension;
133 }
134 
135 template<class Scalar>
136 std::string
138  const std::string &fileNameBase
139  ) const
140 {
141  std::ostringstream parallelFileName;
142  parallelFileName << fileNameBase;
143  if(localFileNameExtension_.length())
144  parallelFileName << "." << localFileNameExtension_;
145  return parallelFileName.str();
146 }
147 
148 template<class Scalar>
150  const MultiVectorBase<Scalar> &mv
151  ) const
152 {
153  return mvSerializer_.isCompatible(mv);
154 }
155 
156 template<class Scalar>
158  const std::string &fileNameBase
160  ) const
161 {
163  const std::string fileName = getLocalFileName(fileNameBase);
164  std::ifstream in_file(fileName.c_str());
166  in_file.eof(), std::logic_error
167  ,"Error, the file \""<<fileName<<"\" could not be opened for input!"
168  );
169  mvSerializer_.binaryMode(useBinaryMode_);
170  mvSerializer_.deserialize(in_file,mv);
171 }
172 
173 template<class Scalar>
176  ,const std::string &fileNameBase
177  ) const
178 {
179  const std::string fileName = getLocalFileName(fileNameBase);
180  std::ofstream out_file(fileName.c_str());
181  mvSerializer_.binaryMode(useBinaryMode_);
182  mvSerializer_.serialize(mv,out_file);
183 }
184 
185 } // namespace Thyra
186 
187 #endif // THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP
DefaultSpmdMultiVectorFileIO(const std::string &extensionTagName="", const int numProcs=-1, const int procRank=-1)
Construct with file extension information (calls setFileNameExtension()).
Concrete utility class for reading and writing SPMD-based MultiVectorBase objects to and from standar...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void readMultiVectorFromFile(const std::string &fileNameBase, Thyra::MultiVectorBase< Scalar > *mv) const
void writeMultiVectorToFile(const Thyra::MultiVectorBase< Scalar > &mv, const std::string &fileNameBase) const
bool isCompatible(const MultiVectorBase< Scalar > &mv) const
Interface for a collection of column vectors called a multi-vector.
Abstract strategy interface for reading and writing (multi)vector objects to and from files...
void setFileNameExtension(const std::string &extensionTagName="", const int numProcs=-1, const int procRank=-1)
Set file name extension information to disambiguate files on different processes and from other files...
static std::string getParallelExtension(int procRank=-1, int numProcs=-1)
std::string getLocalFileName(const std::string &fileNameBase) const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
Concrete implementation of MultiVectorFileIO that reads and writes SPMD-based (multi)vectors to and f...