https://mooseframework.inl.gov
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 {
26 public:
27  RestartableDataReader(MooseApp & app, RestartableDataMap & data, const bool force = false);
29  std::vector<RestartableDataMap> & data,
30  const bool force = false);
31 
36  struct InputStreams
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  */
119  {
121  }
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 
144 private:
148  struct HeaderEntry
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 
232 template <typename T, typename... Args>
233 T &
234 RestartableDataReader::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 }
Reader for restartable data written by the RestartableDataWriter.
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...
std::streampos position
The position in the stream at which this data is.
void requireRestoring() const
Checks whether or not we&#39;re currently restoring and errors if not.
Class for doing restart.
Struct that describes data in the header.
std::size_t size
The size of this data.
std::unique_ptr< InputStream > header
Base class for MOOSE-based applications.
Definition: MooseApp.h:108
bool has_context
Whether or not this data had context.
const HeaderEntry & getHeader(const std::string &data_name, const THREAD_ID tid) const
Structure that contains the input streams for the reader.
Helper class that hands out input streams to an underlying, managed stream of arbitrary type...
Definition: InputStream.h:22
std::vector< std::unordered_map< std::string, HeaderEntry > > readHeader(InputStream &header_input) const
Internal method for reading the header (stored by RestartableDataWriter)
Storage for restartable data that is ordered based on insertion order.
const HeaderEntry * queryHeader(const std::string &data_name, const THREAD_ID tid) const
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void setErrorOnLoadWithDifferentNumberOfProcessors(bool value)
bool isSameType(const HeaderEntry &header_entry, const std::type_info &type) const
RestartableDataReader(MooseApp &app, RestartableDataMap &data, const bool force=false)
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 ...
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.
bool _is_restoring
Whether or not we&#39;re currently restoring.
std::string type
The type for this data.
Concrete definition of a parameter value for a specified type.
const bool _force
Whether or not to forcefully attempt to read despite incompatibilities.
bool hasData(const std::string &data_name, const THREAD_ID tid=0) const
std::unique_ptr< InputStream > data
void deserializeValue(InputStream &data_input, RestartableDataValue &value, const HeaderEntry &header_entry) const
Internal method for deserializing (restoring from backup into a value)
std::unordered_set< std::string > DataNames
bool _error_on_different_number_of_processors
Whether or not to error with a different number of processors.
InputStreams clear()
Clears the contents of the reader (header stream, data stream, header)
void restore(const DataNames &filter_names={})
Restores the restartable data.
InputStreams _streams
The inputs for reading.
std::size_t type_hash_code
The hash code for this data (typeid(T).hash_code())
Abstract definition of a RestartableData value.
unsigned int THREAD_ID
Definition: MooseTypes.h:237
std::vector< std::unordered_map< std::string, HeaderEntry > > _header
The loaded headers from the restart.
static bool isAvailable(const std::filesystem::path &folder_base)