Thyra
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
core
src
support
operator_vector
adapter_support
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>
68
class
DefaultSpmdMultiVectorFileIO
69
:
public
MultiVectorFileIOBase
<Scalar>
70
{
71
public
:
72
75
79
DefaultSpmdMultiVectorFileIO
(
80
const
std::string &extensionTagName =
""
81
,
const
int
numProcs = -1
82
,
const
int
procRank = -1
83
);
84
101
void
setFileNameExtension
(
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
114
115
bool
isCompatible
(
const
MultiVectorBase<Scalar>
&mv )
const
;
117
void
readMultiVectorFromFile
(
118
const
std::string &fileNameBase
119
,
Thyra::MultiVectorBase<Scalar>
*mv
120
)
const
;
122
void
writeMultiVectorToFile
(
123
const
Thyra::MultiVectorBase<Scalar>
&mv
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>
141
DefaultSpmdMultiVectorFileIO<Scalar>::DefaultSpmdMultiVectorFileIO
(
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>
153
void
DefaultSpmdMultiVectorFileIO<Scalar>::setFileNameExtension
(
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
169
DefaultSpmdMultiVectorFileIO<Scalar>::getLocalFileName
(
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>
181
bool
DefaultSpmdMultiVectorFileIO<Scalar>::isCompatible
(
182
const
MultiVectorBase<Scalar>
&mv
183
)
const
184
{
185
return
mvSerializer_.isCompatible(mv);
186
}
187
188
template
<
class
Scalar>
189
void
DefaultSpmdMultiVectorFileIO<Scalar>::readMultiVectorFromFile
(
190
const
std::string &fileNameBase
191
,
Thyra::MultiVectorBase<Scalar>
*mv
192
)
const
193
{
194
TEUCHOS_TEST_FOR_EXCEPT
(!mv);
195
const
std::string fileName =
getLocalFileName
(fileNameBase);
196
std::ifstream in_file(fileName.c_str());
197
TEUCHOS_TEST_FOR_EXCEPTION
(
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>
206
void
DefaultSpmdMultiVectorFileIO<Scalar>::writeMultiVectorToFile
(
207
const
Thyra::MultiVectorBase<Scalar>
&mv
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
Teuchos_Utils.hpp
Teuchos::Utils::getParallelExtension
static std::string getParallelExtension(int procRank=-1, int numProcs=-1)
Thyra::DefaultSpmdMultiVectorFileIO::getLocalFileName
std::string getLocalFileName(const std::string &fileNameBase) const
Definition
Thyra_DefaultSpmdMultiVectorFileIO.hpp:169
Thyra::DefaultSpmdMultiVectorFileIO::DefaultSpmdMultiVectorFileIO
DefaultSpmdMultiVectorFileIO(const std::string &extensionTagName="", const int numProcs=-1, const int procRank=-1)
Construct with file extension information (calls setFileNameExtension()).
Definition
Thyra_DefaultSpmdMultiVectorFileIO.hpp:141
Thyra::DefaultSpmdMultiVectorFileIO::readMultiVectorFromFile
void readMultiVectorFromFile(const std::string &fileNameBase, Thyra::MultiVectorBase< Scalar > *mv) const
Definition
Thyra_DefaultSpmdMultiVectorFileIO.hpp:189
Thyra::DefaultSpmdMultiVectorFileIO::isCompatible
bool isCompatible(const MultiVectorBase< Scalar > &mv) const
Definition
Thyra_DefaultSpmdMultiVectorFileIO.hpp:181
Thyra::DefaultSpmdMultiVectorFileIO::setFileNameExtension
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...
Definition
Thyra_DefaultSpmdMultiVectorFileIO.hpp:153
Thyra::DefaultSpmdMultiVectorFileIO::writeMultiVectorToFile
void writeMultiVectorToFile(const Thyra::MultiVectorBase< Scalar > &mv, const std::string &fileNameBase) const
Definition
Thyra_DefaultSpmdMultiVectorFileIO.hpp:206
Thyra::MultiVectorBase
Interface for a collection of column vectors called a multi-vector.
Definition
Thyra_MultiVectorBase_decl.hpp:496
Thyra::MultiVectorFileIOBase
Abstract strategy interface for reading and writing (multi)vector objects to and from files.
Definition
Thyra_MultiVectorFileIOBase.hpp:84
Thyra::SpmdMultiVectorSerializer
Concrete utility class for reading and writing SPMD-based MultiVectorBase objects to and from standar...
Definition
Thyra_SpmdMultiVectorSerializer_decl.hpp:58
TEUCHOS_TEST_FOR_EXCEPT
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
TEUCHOS_TEST_FOR_EXCEPTION
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Generated by
1.17.0