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 //
4 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP
43 #define THYRA_DEFAULT_SPMD_MULTI_VECTOR_FILE_IO_HPP
44 
45 #include "Thyra_MultiVectorFileIOBase.hpp"
46 #include "Thyra_SpmdMultiVectorSerializer.hpp"
47 #include "Teuchos_Utils.hpp"
48 
49 namespace Thyra {
50 
67 template<class Scalar>
69  : public MultiVectorFileIOBase<Scalar>
70 {
71 public:
72 
75 
80  const std::string &extensionTagName = ""
81  ,const int numProcs = -1
82  ,const int procRank = -1
83  );
84 
102  const std::string &extensionTagName = ""
103  ,const int numProcs = -1
104  ,const int procRank = -1
105  );
106 
108  std::string getLocalFileName( const std::string &fileNameBase ) const;
109 
111 
115  bool isCompatible( const MultiVectorBase<Scalar> &mv ) const;
118  const std::string &fileNameBase
120  ) const;
124  ,const std::string &fileNameBase
125  ) const;
127 
128 private:
129 
130  std::string localFileNameExtension_;
131  bool useBinaryMode_;
132 
133  mutable SpmdMultiVectorSerializer<Scalar> mvSerializer_;
134 
135 };
136 
137 // ///////////////////////////
138 // Implementations
139 
140 template<class Scalar>
142  const std::string &extensionTagName
143  ,const int numProcs
144  ,const int procRank
145  )
146  :useBinaryMode_(false) // ToDo: Make this adjustable!
147  ,mvSerializer_(useBinaryMode_)
148 {
149  setFileNameExtension(extensionTagName,numProcs,procRank);
150 }
151 
152 template<class Scalar>
154  const std::string &extensionTagName
155  ,const int numProcs
156  ,const int procRank
157  )
158 {
159  const std::string
160  endExtension = Teuchos::Utils::getParallelExtension(procRank,numProcs);
161  if(extensionTagName.length())
162  localFileNameExtension_ = extensionTagName+"."+endExtension;
163  else
164  localFileNameExtension_ = endExtension;
165 }
166 
167 template<class Scalar>
168 std::string
170  const std::string &fileNameBase
171  ) const
172 {
173  std::ostringstream parallelFileName;
174  parallelFileName << fileNameBase;
175  if(localFileNameExtension_.length())
176  parallelFileName << "." << localFileNameExtension_;
177  return parallelFileName.str();
178 }
179 
180 template<class Scalar>
182  const MultiVectorBase<Scalar> &mv
183  ) const
184 {
185  return mvSerializer_.isCompatible(mv);
186 }
187 
188 template<class Scalar>
190  const std::string &fileNameBase
192  ) const
193 {
195  const std::string fileName = getLocalFileName(fileNameBase);
196  std::ifstream in_file(fileName.c_str());
198  in_file.eof(), std::logic_error
199  ,"Error, the file \""<<fileName<<"\" could not be opened for input!"
200  );
201  mvSerializer_.binaryMode(useBinaryMode_);
202  mvSerializer_.deserialize(in_file,mv);
203 }
204 
205 template<class Scalar>
208  ,const std::string &fileNameBase
209  ) const
210 {
211  const std::string fileName = getLocalFileName(fileNameBase);
212  std::ofstream out_file(fileName.c_str());
213  mvSerializer_.binaryMode(useBinaryMode_);
214  mvSerializer_.serialize(mv,out_file);
215 }
216 
217 } // namespace Thyra
218 
219 #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...