https://mooseframework.inl.gov
Loading...
Searching...
No Matches
RestartableDataReader.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#pragma once
11
12#include "RestartableDataIO.h"
13
14#include "RestartableData.h"
15#include "InputStream.h"
16#include "MoosePassKey.h"
17
18#include <sstream>
19#include <utility>
20
25{
26public:
27 RestartableDataReader(MooseApp & app, RestartableDataMap & data, const bool force = false);
29 std::vector<RestartableDataMap> & data,
30 const bool force = false);
31
37 {
38 std::unique_ptr<InputStream> header;
39 std::unique_ptr<InputStream> data;
40 };
41
46 void setInput(std::unique_ptr<std::stringstream> header_stream,
47 std::unique_ptr<std::stringstream> data_stream);
51 void setInput(const std::filesystem::path & folder_base);
52
56 bool isRestoring() const { return _streams.data != nullptr; }
57
68 const THREAD_ID tid,
70
77 InputStreams clear();
78
88 void restore(const DataNames & filter_names = {});
89
105 template <typename T, typename... Args>
106 T & restoreData(const std::string & data_name,
107 const THREAD_ID tid = 0,
108 void * const context = nullptr,
109 Args &&... args);
110
112 /*
113 * Enable/Disable errors to allow meta data to be created/loaded on different number or
114 * processors
115 *
116 * See LoadSurrogateModelAction for use case
117 */
123
130 static bool isAvailable(const std::filesystem::path & folder_base);
131
138 template <typename T>
139 bool hasData(const std::string & data_name, const THREAD_ID tid = 0) const
140 {
141 return hasData(data_name, typeid(T), tid);
142 }
143
144private:
149 {
151 std::streampos position;
153 std::size_t size;
155 std::size_t type_hash_code;
157 std::string type;
160 };
161
168 bool
169 hasData(const std::string & data_name, const std::type_info & type, const THREAD_ID tid) const;
170
174 std::vector<std::unordered_map<std::string, HeaderEntry>>
175 readHeader(InputStream & header_input) const;
176
180 void deserializeValue(InputStream & data_input,
181 RestartableDataValue & value,
182 const HeaderEntry & header_entry) const;
183
187 void requireRestoring() const;
188
195 const HeaderEntry * queryHeader(const std::string & data_name, const THREAD_ID tid) const;
201 const HeaderEntry & getHeader(const std::string & data_name, const THREAD_ID tid) const;
202
208 bool isSameType(const HeaderEntry & header_entry, const std::type_info & type) const;
209
213 RestartableDataValue & restoreData(const std::string & data_name,
214 std::unique_ptr<RestartableDataValue> value,
215 const THREAD_ID tid);
218
220 std::vector<std::unordered_map<std::string, HeaderEntry>> _header;
221
224
227
229 const bool _force;
230};
231
232template <typename T, typename... Args>
233T &
234RestartableDataReader::restoreData(const std::string & data_name,
235 const THREAD_ID tid /* = 0 */,
236 void * const context /* = nullptr */,
237 Args &&... args)
238{
239 std::unique_ptr<RestartableDataValue> T_data =
240 std::make_unique<RestartableData<T>>(data_name, context, std::forward<Args>(args)...);
241 auto & value = restoreData(data_name, std::move(T_data), tid);
242 auto T_value = dynamic_cast<RestartableData<T> *>(&value);
243 mooseAssert(T_value, "Bad cast");
244 return T_value->set();
245}
unsigned int THREAD_ID
Definition MooseTypes.h:237
std::unordered_set< std::string > DataNames
Helper class that hands out input streams to an underlying, managed stream of arbitrary type.
Definition InputStream.h:23
Base class for MOOSE-based applications.
Definition MooseApp.h:110
Class for doing restart.
Storage for restartable data that is ordered based on insertion order.
Reader for restartable data written by the RestartableDataWriter.
std::vector< std::unordered_map< std::string, HeaderEntry > > readHeader(InputStream &header_input) const
Internal method for reading the header (stored by RestartableDataWriter)
void setErrorOnLoadWithDifferentNumberOfProcessors(bool value)
const HeaderEntry * queryHeader(const std::string &data_name, const THREAD_ID tid) const
bool hasData(const std::string &data_name, const THREAD_ID tid=0) const
bool _is_restoring
Whether or not we're currently restoring.
InputStreams _streams
The inputs for reading.
void deserializeValue(InputStream &data_input, RestartableDataValue &value, const HeaderEntry &header_entry) const
Internal method for deserializing (restoring from backup into a value)
bool restoreDataIfAvailable(RestartableDataValue &value, const THREAD_ID tid, Moose::PassKey< MooseApp >)
Restores value in place from the open reader if it is present in the checkpoint and has not yet been ...
const HeaderEntry & getHeader(const std::string &data_name, const THREAD_ID tid) const
T & restoreData(const std::string &data_name, const THREAD_ID tid=0, void *const context=nullptr, Args &&... args)
Restores the data with name data_name of type T.
static bool isAvailable(const std::filesystem::path &folder_base)
InputStreams clear()
Clears the contents of the reader (header stream, data stream, header)
std::vector< std::unordered_map< std::string, HeaderEntry > > _header
The loaded headers from the restart.
const bool _force
Whether or not to forcefully attempt to read despite incompatibilities.
bool _error_on_different_number_of_processors
Whether or not to error with a different number of processors.
void restore(const DataNames &filter_names={})
Restores the restartable data.
void requireRestoring() const
Checks whether or not we're currently restoring and errors if not.
bool isSameType(const HeaderEntry &header_entry, const std::type_info &type) const
void setInput(std::unique_ptr< std::stringstream > header_stream, std::unique_ptr< std::stringstream > data_stream)
Sets the input stream for reading from the stringstreams header_stream and data_stream for the header...
Abstract definition of a RestartableData value.
Concrete definition of a parameter value for a specified type.
Struct that describes data in the header.
std::string type
The type for this data.
bool has_context
Whether or not this data had context.
std::streampos position
The position in the stream at which this data is.
std::size_t type_hash_code
The hash code for this data (typeid(T).hash_code())
std::size_t size
The size of this data.
Structure that contains the input streams for the reader.
std::unique_ptr< InputStream > header
std::unique_ptr< InputStream > data