9#include <kernel/util/dist.hpp>
10#include <kernel/util/dist_file_io.hpp>
11#include <kernel/util/string.hpp>
12#include <kernel/util/binary_stream.hpp>
13#include <kernel/util/pack.hpp>
14#include <kernel/lafem/container.hpp>
75 template <
typename Object_>
96 return _object.get_checkpoint_size(config);
109 _object.restore_from_checkpoint_data(data);
122 return _object.set_checkpoint_data(data, config);
165 std::uint64_t checkpoint_size(0u);
166 std::uint64_t real_size(0u);
167 std::map<String, std::tuple<std::uint64_t, std::uint64_t>> sizes;
171 std::uint64_t identifierlength = (std::uint64_t)it.first.length();
172 std::uint64_t datalength = it.second->get_checkpoint_size(
_config);
174 checkpoint_size += identifierlength + datalength +
sizeof(std::uint64_t) +
sizeof(std::uint64_t);
175 real_size += identifierlength +
sizeof(std::uint64_t) +
sizeof(std::uint64_t);
176 sizes[it.first] = std::make_tuple(identifierlength, datalength);
179 buffer.reserve(checkpoint_size);
183 char * cidentifierlength =
reinterpret_cast<char *
>(&std::get<0>(sizes[it.first]));
184 buffer.insert(std::end(buffer), cidentifierlength, cidentifierlength +
sizeof(std::uint64_t));
185 buffer.insert(std::end(buffer), it.first.begin(), it.first.end());
186 std::uint64_t old_size = buffer.size();
187 buffer.insert(std::end(buffer),
sizeof(std::uint64_t), 0);
188 std::uint64_t ireal_size = it.second->set_checkpoint_data(buffer,
_config);
189 char * csize =
reinterpret_cast<char *
>(&ireal_size);
190 for(std::uint64_t i(0) ; i <
sizeof(std::uint64_t) ; ++i)
192 buffer[old_size + i] = csize[i];
194 real_size += ireal_size;
197 buffer.resize(real_size);
210 std::uint64_t stringsize;
211 std::uint64_t datasize(0);
219 ::memcpy(&stringsize,
_input_array.data() + i,
sizeof(std::uint64_t));
220 i +=
sizeof(std::uint64_t);
227 ::memcpy(&datasize,
_input_array.data() + i,
sizeof(std::uint64_t));
228 i +=
sizeof(std::uint64_t) + datasize;
242 std::vector<char> buffer;
244 std::vector<char> common(0);
264 std::vector<char> common(0);
325 if (identifiers.length() > 0)
329 identifiers += item.first;
345 template <
typename OT_>
349 auto checkpointable_object = std::make_shared<CheckpointableWrapper<OT_>>(object);
379 template <
typename OT_>
385 auto checkpointable_object = std::make_shared<CheckpointableWrapper<OT_>>(object);
387 std::uint64_t size(0);
389 ::memcpy(&size, in_data,
sizeof(std::uint64_t));
390 std::vector<char> buffer(in_data +
sizeof(std::uint64_t), in_data +
sizeof(std::uint64_t) + size);
391 checkpointable_object->restore_from_checkpoint_data(buffer);
393 if (add_to_checkpoint_control)
409 size_t pos = filename.rfind(
'.');
410 String extension = filename.substr(pos + 1);
411 String name = filename.substr(0, pos);
413 XASSERTM(name !=
"",
"no complete filename consisting of name.extension given");
415 if (extension ==
"cp")
421 XASSERTM(extension !=
"zcp",
"no zlib support, activate zlib or write uncompressed files with .cp extension instead");
422 XASSERTM(extension ==
"zcp",
"no valid checkpoint filename choosen");
444 size_t pos = filename.rfind(
'.');
445 String extension = filename.substr(pos + 1);
446 String name = filename.substr(0, pos);
448 XASSERTM(name !=
"",
"no complete filename consisting of name.extension given");
450 if (extension ==
"cp")
456 XABORTM(
"no valid checkpoint file choosen");
469 std::vector<char> buffer;
472 bs.write(
reinterpret_cast<char *
>(&slen),
sizeof(slen));
473 bs.write(buffer.data(),
static_cast<std::streamsize
>(buffer.size()));
492 char * buffer = bs.
data();
493 std::uint64_t size = *(std::uint64_t *)(buffer);
496 std::copy(buffer +
sizeof(std::uint64_t), buffer +
sizeof(std::uint64_t) + size - 1,
_input_array.data());
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
char * data()
Returns the data array of the stream.
Checkpoint/Restart infrastructure.
void set_config(FEAT::LAFEM::SerialConfig &conf)
Set a new serialize configuration.
void load(BinaryStream &bs)
Load checkpoint from stream.
void save(BinaryStream &bs)
Save checkpoint to a stream.
std::uint64_t _collect_checkpoint_data(std::vector< char > &buffer)
Build checkpoint buffer.
CheckpointControl(const Dist::Comm &comm, const LAFEM::SerialConfig &config=LAFEM::SerialConfig())
Constructor.
~CheckpointControl()
Destructor.
void _load(const String name)
Load checkpoint from disk.
std::vector< char > _input_array
Vector holding the array read from the input file during restore.
void add_object(String identifier, OT_ &object)
Register checkpointable object.
String get_identifier_list()
Retrieve a list of all items stored in the checkpoint.
void _restore_checkpoint_data()
Extract input buffer.
std::map< String, std::shared_ptr< Checkpointable > > _checkpointable_by_identifier
Mapping of identifier string to pointer to the checkpointable object.
void _save(const String name)
Write checkpoint files to disk.
void remove_object(String identifier)
Remove checkpointable object.
void load(const String filename)
Load checkpoint from disk.
const Dist::Comm & _comm
the mpi communicator identifying our mpi context
std::map< String, std::uint64_t > _offset_by_identifier
Mapping of identifier string to the offset in the input file.
void restore_object(String identifier, OT_ &object, bool add_to_checkpoint_control=true)
Restore checkpointable object.
FEAT::LAFEM::SerialConfig _config
The config class that controls the compression modes.
void save(const String filename)
Write checkpoint file to disk.
void clear_input()
Delete all read input.
Checkpoint interface class.
virtual void restore_from_checkpoint_data(std::vector< char > &data)=0
Extract object from checkpoint.
virtual std::uint64_t set_checkpoint_data(std::vector< char > &data, LAFEM::SerialConfig &config)=0
Collect checkpoint data from object.
virtual std::uint64_t get_checkpoint_size(LAFEM::SerialConfig &config)=0
Calculate size.
Wrapper class for checkpointable objects.
virtual void restore_from_checkpoint_data(std::vector< char > &data) override
Extract object from checkpoint.
CheckpointableWrapper(Object_ &obj)
Contructor.
virtual std::uint64_t get_checkpoint_size(LAFEM::SerialConfig &config) override
Calculate size.
virtual std::uint64_t set_checkpoint_data(std::vector< char > &data, LAFEM::SerialConfig &config) override
Collect checkpoint data from object.
static void write_combined(const std::vector< char > &common, const std::vector< char > &buffer, const String &filename, const Dist::Comm &comm, int root_rank=0)
Writes a combined shared/ordered binary file.
static void read_combined(std::vector< char > &common, std::vector< char > &buffer, const String &filename, const Dist::Comm &comm, int root_rank=0, bool bcast_common=true)
Reads a combined shared/ordered binary file as written by the write_combined function.
Config class for serialize parameter.
String class implementation.