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 
17 #include <sstream>
18 #include <utility>
19 
24 {
25 public:
26  RestartableDataReader(MooseApp & app, RestartableDataMap & data, const bool force = false);
28  std::vector<RestartableDataMap> & data,
29  const bool force = false);
30 
35  struct InputStreams
36  {
37  std::unique_ptr<InputStream> header;
38  std::unique_ptr<InputStream> data;
39  };
40 
45  void setInput(std::unique_ptr<std::stringstream> header_stream,
46  std::unique_ptr<std::stringstream> data_stream);
50  void setInput(const std::filesystem::path & folder_base);
51 
55  bool isRestoring() const { return _streams.data != nullptr; }
56 
63  InputStreams clear();
64 
74  void restore(const DataNames & filter_names = {});
75 
91  template <typename T, typename... Args>
92  T & restoreData(const std::string & data_name,
93  const THREAD_ID tid = 0,
94  void * const context = nullptr,
95  Args &&... args);
96 
98  /*
99  * Enable/Disable errors to allow meta data to be created/loaded on different number or
100  * processors
101  *
102  * See LoadSurrogateModelAction for use case
103  */
105  {
107  }
109 
116  static bool isAvailable(const std::filesystem::path & folder_base);
117 
124  template <typename T>
125  bool hasData(const std::string & data_name, const THREAD_ID tid = 0) const
126  {
127  return hasData(data_name, typeid(T), tid);
128  }
129 
130 private:
134  struct HeaderEntry
135  {
137  std::streampos position;
139  std::size_t size;
141  std::size_t type_hash_code;
143  std::string type;
146  };
147 
154  bool
155  hasData(const std::string & data_name, const std::type_info & type, const THREAD_ID tid) const;
156 
160  std::vector<std::unordered_map<std::string, HeaderEntry>>
161  readHeader(InputStream & header_input) const;
162 
166  void deserializeValue(InputStream & data_input,
167  RestartableDataValue & value,
168  const HeaderEntry & header_entry) const;
169 
173  void requireRestoring() const;
174 
181  const HeaderEntry * queryHeader(const std::string & data_name, const THREAD_ID tid) const;
187  const HeaderEntry & getHeader(const std::string & data_name, const THREAD_ID tid) const;
188 
194  bool isSameType(const HeaderEntry & header_entry, const std::type_info & type) const;
195 
199  RestartableDataValue & restoreData(const std::string & data_name,
200  std::unique_ptr<RestartableDataValue> value,
201  const THREAD_ID tid);
204 
206  std::vector<std::unordered_map<std::string, HeaderEntry>> _header;
207 
210 
213 
215  const bool _force;
216 };
217 
218 template <typename T, typename... Args>
219 T &
220 RestartableDataReader::restoreData(const std::string & data_name,
221  const THREAD_ID tid /* = 0 */,
222  void * const context /* = nullptr */,
223  Args &&... args)
224 {
225  std::unique_ptr<RestartableDataValue> T_data =
226  std::make_unique<RestartableData<T>>(data_name, context, std::forward<Args>(args)...);
227  auto & value = restoreData(data_name, std::move(T_data), tid);
228  auto T_value = dynamic_cast<RestartableData<T> *>(&value);
229  mooseAssert(T_value, "Bad cast");
230  return T_value->set();
231 }
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:85
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)
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:209
std::vector< std::unordered_map< std::string, HeaderEntry > > _header
The loaded headers from the restart.
static bool isAvailable(const std::filesystem::path &folder_base)