https://mooseframework.inl.gov
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
RestartableDataWriter Class Reference

Writer for restartable data, to be read by the RestartableDataReader. More...

#include <RestartableDataWriter.h>

Inheritance diagram for RestartableDataWriter:
[legend]

Public Member Functions

 RestartableDataWriter (MooseApp &app, RestartableDataMap &data)
 
 RestartableDataWriter (MooseApp &app, std::vector< RestartableDataMap > &data)
 
void write (std::ostream &header_stream, std::ostream &data_stream)
 Writes the restartable data to header stream header_stream and data stream data_stream. More...
 
std::vector< std::filesystem::path > write (const std::filesystem::path &folder_base)
 Writes the restartable data to the folder with base folder_base. More...
 
PerfGraphperfGraph ()
 Get the PerfGraph. More...
 
const Parallel::Communicatorcomm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Static Public Member Functions

static const std::string & getRestartableExt ()
 
static const std::string & restartableDataFile ()
 
static std::filesystem::path restartableDataFile (const std::filesystem::path &folder_base)
 
static const std::string & restartableHeaderFile ()
 
static std::filesystem::path restartableHeaderFile (const std::filesystem::path &folder_base)
 
static std::filesystem::path restartableDataFolder (const std::filesystem::path &folder_base)
 
static InputParameters validParams ()
 

Protected Types

typedef int COMPARE_HASH_CODE_TYPE
 The type to used for comparing hash codes (sanity checking) More...
 

Protected Member Functions

RestartableDataMapcurrentData (const THREAD_ID tid)
 
std::size_t dataSize () const
 
PerfID registerTimedSection (const std::string &section_name, const unsigned int level) const
 Call to register a named section for timing. More...
 
PerfID registerTimedSection (const std::string &section_name, const unsigned int level, const std::string &live_message, const bool print_dots=true) const
 Call to register a named section for timing. More...
 
std::string timedSectionName (const std::string &section_name) const
 

Protected Attributes

const std::variant< RestartableDataMap *, std::vector< RestartableDataMap > * > _data
 The data we wish to act on This is a variant so that we can act on threaded and non-threaded data. More...
 
MooseApp_pg_moose_app
 The MooseApp that owns the PerfGraph. More...
 
const std::string _prefix
 A prefix to use for all sections. More...
 
const Parallel::Communicator_communicator
 

Static Protected Attributes

static const unsigned int CURRENT_BACKUP_FILE_VERSION = 4
 The current version for the backup file. More...
 

Detailed Description

Writer for restartable data, to be read by the RestartableDataReader.

Definition at line 17 of file RestartableDataWriter.h.

Member Typedef Documentation

◆ COMPARE_HASH_CODE_TYPE

typedef int RestartableDataIO::COMPARE_HASH_CODE_TYPE
protectedinherited

The type to used for comparing hash codes (sanity checking)

Definition at line 84 of file RestartableDataIO.h.

Constructor & Destructor Documentation

◆ RestartableDataWriter() [1/2]

RestartableDataWriter::RestartableDataWriter ( MooseApp app,
RestartableDataMap data 
)

Definition at line 18 of file RestartableDataWriter.C.

19  : RestartableDataIO(app, data)
20 {
21 }
RestartableDataIO(MooseApp &app, RestartableDataMap &data)

◆ RestartableDataWriter() [2/2]

RestartableDataWriter::RestartableDataWriter ( MooseApp app,
std::vector< RestartableDataMap > &  data 
)

Definition at line 23 of file RestartableDataWriter.C.

24  : RestartableDataIO(app, data)
25 {
26 }
RestartableDataIO(MooseApp &app, RestartableDataMap &data)

Member Function Documentation

◆ currentData()

RestartableDataMap & RestartableDataIO::currentData ( const THREAD_ID  tid)
protectedinherited
Returns
The restartable data for thread tid

This exists so that we can support threaded and non-threaded data in _data

Definition at line 33 of file RestartableDataIO.C.

Referenced by RestartableDataReader::restore(), RestartableDataReader::restoreData(), and write().

34 {
35  mooseAssert(dataSize() > tid, "Invalid thread");
36 
37  if (std::holds_alternative<RestartableDataMap *>(_data))
38  return *std::get<RestartableDataMap *>(_data);
39  return (*std::get<std::vector<RestartableDataMap> *>(_data))[tid];
40 }
const std::variant< RestartableDataMap *, std::vector< RestartableDataMap > * > _data
The data we wish to act on This is a variant so that we can act on threaded and non-threaded data...
std::size_t dataSize() const
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1155

◆ dataSize()

std::size_t RestartableDataIO::dataSize ( ) const
protectedinherited
Returns
The size of _data

Definition at line 43 of file RestartableDataIO.C.

Referenced by RestartableDataIO::currentData(), RestartableDataReader::readHeader(), RestartableDataReader::restore(), and write().

44 {
45  return std::holds_alternative<RestartableDataMap *>(_data)
46  ? 1
47  : std::get<std::vector<RestartableDataMap> *>(_data)->size();
48 }
const std::variant< RestartableDataMap *, std::vector< RestartableDataMap > * > _data
The data we wish to act on This is a variant so that we can act on threaded and non-threaded data...
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1155

◆ getRestartableExt()

const std::string & RestartableDataIO::getRestartableExt ( )
staticinherited
Returns
The common extension for restartable data folders

Definition at line 51 of file RestartableDataIO.C.

Referenced by RestartableDataIO::restartableDataFolder().

52 {
53  static const std::string ext = ".rd";
54  return ext;
55 }

◆ perfGraph()

PerfGraph & PerfGraphInterface::perfGraph ( )
inherited

Get the PerfGraph.

Definition at line 78 of file PerfGraphInterface.C.

Referenced by CommonOutputAction::act(), PerfGraphData::finalize(), and PerfGraphOutput::output().

79 {
80  return _pg_moose_app.perfGraph();
81 }
MooseApp & _pg_moose_app
The MooseApp that owns the PerfGraph.
PerfGraph & perfGraph()
Get the PerfGraph for this app.
Definition: MooseApp.h:161

◆ registerTimedSection() [1/2]

PerfID PerfGraphInterface::registerTimedSection ( const std::string &  section_name,
const unsigned int  level 
) const
protectedinherited

Call to register a named section for timing.

Parameters
section_nameThe name of the code section to be timed
levelThe importance of the timer - lower is more important (0 will always come out)
Returns
The ID of the section - use when starting timing

Definition at line 53 of file PerfGraphInterface.C.

55 {
56  const auto timed_section_name = timedSectionName(section_name);
57  if (!moose::internal::getPerfGraphRegistry().sectionExists(timed_section_name))
58  return moose::internal::getPerfGraphRegistry().registerSection(timed_section_name, level);
59  else
60  return moose::internal::getPerfGraphRegistry().sectionID(timed_section_name);
61 }
PerfID registerSection(const std::string &section_name, const unsigned int level)
Call to register a named section for timing.
std::string timedSectionName(const std::string &section_name) const
PerfID sectionID(const std::string &section_name) const
Given a name return the PerfID The name of the section.
PerfGraphRegistry & getPerfGraphRegistry()
Get the global PerfGraphRegistry singleton.

◆ registerTimedSection() [2/2]

PerfID PerfGraphInterface::registerTimedSection ( const std::string &  section_name,
const unsigned int  level,
const std::string &  live_message,
const bool  print_dots = true 
) const
protectedinherited

Call to register a named section for timing.

Parameters
section_nameThe name of the code section to be timed
levelThe importance of the timer - lower is more important (0 will always come out)
live_messageThe message to be printed to the screen during execution
print_dotsWhether or not progress dots should be printed for this section
Returns
The ID of the section - use when starting timing

Definition at line 64 of file PerfGraphInterface.C.

68 {
69  const auto timed_section_name = timedSectionName(section_name);
70  if (!moose::internal::getPerfGraphRegistry().sectionExists(timed_section_name))
72  timedSectionName(section_name), level, live_message, print_dots);
73  else
74  return moose::internal::getPerfGraphRegistry().sectionID(timed_section_name);
75 }
PerfID registerSection(const std::string &section_name, const unsigned int level)
Call to register a named section for timing.
std::string timedSectionName(const std::string &section_name) const
PerfID sectionID(const std::string &section_name) const
Given a name return the PerfID The name of the section.
PerfGraphRegistry & getPerfGraphRegistry()
Get the global PerfGraphRegistry singleton.

◆ restartableDataFile() [1/2]

const std::string & RestartableDataIO::restartableDataFile ( )
staticinherited
Returns
The filename for the restartable data

Definition at line 58 of file RestartableDataIO.C.

Referenced by RestartableDataReader::isAvailable(), RestartableDataIO::restartableDataFile(), RestartableDataReader::setInput(), and write().

59 {
60  static const std::string file = "data";
61  return file;
62 }

◆ restartableDataFile() [2/2]

std::filesystem::path RestartableDataIO::restartableDataFile ( const std::filesystem::path &  folder_base)
staticinherited
Returns
The path to the restartable data file with base folder_base

Does not append .rd to the folder base

Definition at line 80 of file RestartableDataIO.C.

81 {
82  return folder_base / restartableDataFile();
83 }
static const std::string & restartableDataFile()

◆ restartableDataFolder()

std::filesystem::path RestartableDataIO::restartableDataFolder ( const std::filesystem::path &  folder_base)
staticinherited
Returns
The path to the restartable data folder with base folder_base

This just appends .rd

Definition at line 72 of file RestartableDataIO.C.

Referenced by MooseApp::metaDataFolderBase(), and MooseApp::restartFolderBase().

73 {
74  auto folder = folder_base;
75  folder += getRestartableExt();
76  return folder;
77 }
static const std::string & getRestartableExt()

◆ restartableHeaderFile() [1/2]

const std::string & RestartableDataIO::restartableHeaderFile ( )
staticinherited
Returns
The filename for the restartable header

Definition at line 65 of file RestartableDataIO.C.

Referenced by RestartableDataIO::restartableHeaderFile(), RestartableDataReader::setInput(), and write().

66 {
67  static const std::string file = "header";
68  return file;
69 }

◆ restartableHeaderFile() [2/2]

std::filesystem::path RestartableDataIO::restartableHeaderFile ( const std::filesystem::path &  folder_base)
staticinherited
Returns
The path to the restartable header file with base folder_base

Does not append .rd to the folder base

Definition at line 86 of file RestartableDataIO.C.

87 {
88  return folder_base / restartableHeaderFile();
89 }
static const std::string & restartableHeaderFile()

◆ timedSectionName()

std::string PerfGraphInterface::timedSectionName ( const std::string &  section_name) const
protectedinherited
Returns
The name of the timed section with the name section_name.

Optionally adds a prefix if one is defined.

Definition at line 47 of file PerfGraphInterface.C.

Referenced by PerfGraphInterface::registerTimedSection().

48 {
49  return _prefix.empty() ? "" : (_prefix + "::") + section_name;
50 }
const std::string _prefix
A prefix to use for all sections.

◆ validParams()

InputParameters PerfGraphInterface::validParams ( )
staticinherited

Definition at line 16 of file PerfGraphInterface.C.

Referenced by Convergence::validParams().

17 {
19  return params;
20 }
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
InputParameters emptyInputParameters()

◆ write() [1/2]

void RestartableDataWriter::write ( std::ostream &  header_stream,
std::ostream &  data_stream 
)

Writes the restartable data to header stream header_stream and data stream data_stream.

Definition at line 29 of file RestartableDataWriter.C.

Referenced by MooseApp::backup(), write(), and MooseApp::writeRestartableMetaData().

30 {
31  // Set everything as not stored
32  for (const auto tid : make_range(dataSize()))
33  for (auto & value : currentData(tid))
34  value.setNotStored({});
35 
36  // Header
37  char id[] = {'R', 'D'};
38  header_stream.write(id, 2);
39 
40  // File version
41  auto file_version = CURRENT_BACKUP_FILE_VERSION;
42  dataStore(header_stream, file_version, nullptr);
43 
44  // Type hash for basic hash
45  std::size_t compare_hash_code = typeid(COMPARE_HASH_CODE_TYPE).hash_code();
46  dataStore(header_stream, compare_hash_code, nullptr);
47 
48  // Number of procs
49  auto n_procs = n_processors();
50  dataStore(header_stream, n_procs, nullptr);
51 
52  // Number of data
53  auto num_data = dataSize();
54  dataStore(header_stream, num_data, nullptr);
55 
56  // Size of data for each thread
57  for (const auto tid : make_range(dataSize()))
58  {
59  std::size_t n_data = currentData(tid).size();
60  dataStore(header_stream, n_data, nullptr);
61  }
62 
63  // Write out the RestartableData header, and store the actual data separately
64  for (const auto tid : make_range(dataSize()))
65  for (auto & data : currentData(tid))
66  {
67  // Store the data
68  const std::size_t data_start_position = static_cast<std::size_t>(data_stream.tellp());
69  data.store(data_stream);
70  std::size_t data_size = static_cast<std::size_t>(data_stream.tellp()) - data_start_position;
71 
72  // Store name, size, type hash, and type in the header
73  mooseAssert(data.name().size(), "Empty name");
74  std::string name = data.name();
75  std::string type = data.typeId().name();
76  std::size_t type_hash_code = data.typeId().hash_code();
77  bool has_context = data.hasContext();
78  dataStore(header_stream, name, nullptr);
79  dataStore(header_stream, data_size, nullptr);
80  dataStore(header_stream, type_hash_code, nullptr);
81  dataStore(header_stream, type, nullptr);
82  dataStore(header_stream, has_context, nullptr);
83  }
84 }
std::string name(const ElemQuality q)
static const unsigned int CURRENT_BACKUP_FILE_VERSION
The current version for the backup file.
std::size_t dataSize() const
int COMPARE_HASH_CODE_TYPE
The type to used for comparing hash codes (sanity checking)
RestartableDataMap & currentData(const THREAD_ID tid)
processor_id_type n_processors() const
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
IntRange< T > make_range(T beg, T end)
void dataStore(std::ostream &stream, PenetrationInfo *&pinfo, void *context)

◆ write() [2/2]

std::vector< std::filesystem::path > RestartableDataWriter::write ( const std::filesystem::path &  folder_base)

Writes the restartable data to the folder with base folder_base.

Definition at line 87 of file RestartableDataWriter.C.

88 {
89  const auto header_file = restartableHeaderFile(folder_base);
90  const auto data_file = restartableDataFile(folder_base);
91 
92  // Make the folder if it doesn't exist
93  const auto dir = header_file.parent_path();
94  mooseAssert(dir == data_file.parent_path(), "Inconsistent directories");
95  if (!std::filesystem::exists(dir))
96  {
97  std::error_code err;
98  if (!std::filesystem::create_directory(dir, err))
99  mooseError("Unable to create restart directory\n",
100  std::filesystem::absolute(dir),
101  "\n\n",
102  err.message());
103  }
104 
105  // We want to keep track of the paths that we create so that we can
106  // return them for file management later (primarily removing old checkpoints)
107  std::vector<std::filesystem::path> paths;
108 
109  // Helper for opening a stream for use
110  auto open = [&paths](const std::filesystem::path & filename)
111  {
112  std::ofstream stream;
113  stream.open(filename.c_str(), std::ios::out | std::ios::binary);
114  if (!stream.is_open())
115  mooseError(
116  "Unable to open restart file ", std::filesystem::absolute(filename), " for writing");
117  paths.push_back(filename);
118  return stream;
119  };
120 
121  // Open and write
122  auto header_stream = open(header_file);
123  auto data_stream = open(data_file);
124  write(header_stream, data_stream);
125 
126  // Update the folder's modified time to now for consistency
127  // (this used in checking for the latest checkpoint)
128  const auto data_write_time = std::filesystem::last_write_time(header_file);
129  std::filesystem::last_write_time(dir, data_write_time);
130 
131  return paths;
132 }
OStreamProxy err
void write(std::ostream &header_stream, std::ostream &data_stream)
Writes the restartable data to header stream header_stream and data stream data_stream.
static const std::string & restartableDataFile()
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
static const std::string & restartableHeaderFile()

Member Data Documentation

◆ _data

const std::variant<RestartableDataMap *, std::vector<RestartableDataMap> *> RestartableDataIO::_data
protectedinherited

The data we wish to act on This is a variant so that we can act on threaded and non-threaded data.

Definition at line 79 of file RestartableDataIO.h.

Referenced by RestartableDataIO::currentData(), and RestartableDataIO::dataSize().

◆ _pg_moose_app

MooseApp& PerfGraphInterface::_pg_moose_app
protectedinherited

The MooseApp that owns the PerfGraph.

Definition at line 124 of file PerfGraphInterface.h.

Referenced by PerfGraphInterface::perfGraph().

◆ _prefix

const std::string PerfGraphInterface::_prefix
protectedinherited

A prefix to use for all sections.

Definition at line 127 of file PerfGraphInterface.h.

Referenced by PerfGraphInterface::timedSectionName().

◆ CURRENT_BACKUP_FILE_VERSION

const unsigned int RestartableDataIO::CURRENT_BACKUP_FILE_VERSION = 4
staticprotectedinherited

The current version for the backup file.

Definition at line 82 of file RestartableDataIO.h.

Referenced by RestartableDataReader::readHeader(), and write().


The documentation for this class was generated from the following files: