OpenLB 1.7
Loading...
Searching...
No Matches
serializer.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2016 Mathias J. Krause, Benjamin Förster
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24
25#ifndef SERIALIZER_HH
26#define SERIALIZER_HH
27
28#include <algorithm>
29#include <iostream>
30#include <ostream>
31#include <fstream>
32#include "serializer.h"
34#include "core/singleton.h"
35#include "olbDebug.h"
36#include "io/fileName.h"
37#include "io/serializerIO.h"
38
39
40namespace olb {
41
43
44Serializer::Serializer(Serializable& serializable, std::string fileName)
45 : _serializable(serializable), _iBlock(0), _size(0), _fileName(fileName)
46{ }
47
48
50{
51 _iBlock = 0;
52}
53
54std::size_t Serializer::getSize() const
55{
56 return _size;
57}
58
59bool* Serializer::getNextBlock(std::size_t& sizeBlock, bool loadingMode)
60{
61 return _serializable.getBlock(_iBlock++, sizeBlock, loadingMode);
62}
63
64template<bool includeLogOutputDir>
65bool Serializer::load(std::string fileName, bool enforceUint)
66{
67 validateFileName(fileName);
68 std::ifstream istr(getFullFileName<includeLogOutputDir>(fileName).c_str());
69 if (istr) {
70 istr2serializer(*this, istr, enforceUint);
71 istr.close();
72 _serializable.postLoad();
73 return true;
74 }
75 else {
76 return false;
77 }
78}
79
80template<bool includeLogOutputDir>
81bool Serializer::save(std::string fileName, bool enforceUint)
82{
83 validateFileName(fileName);
84
85 // Determine binary size through `getSerializableSize()` method
87
88 std::ofstream ostr (getFullFileName<includeLogOutputDir>(fileName).c_str());
89 if (ostr) {
90 serializer2ostr(*this, ostr, enforceUint);
91 ostr.close();
92 return true;
93 }
94 else {
95 return false;
96 }
97}
98
99bool Serializer::load(const std::uint8_t* buffer)
100{
101 buffer2serializer(*this, buffer);
102 _serializable.postLoad();
103 return true;
104}
105
106bool Serializer::save(std::uint8_t* buffer)
107{
108 serializer2buffer(*this, buffer);
109 return true;
110}
111
112void Serializer::computeSize(bool enforceRecompute)
113{
114 // compute size (only if it wasn't computed yet or is enforced)
115 if (enforceRecompute || _size == 0) {
116 _size = _serializable.getSerializableSize();
117 }
118}
119
120void Serializer::validateFileName(std::string &fileName)
121{
122 if (fileName == "") {
123 fileName = _fileName;
124 }
125 if (fileName == "") {
126 fileName = "Serializable";
127 }
128}
129
130template<bool includeLogOutputDir>
131const std::string Serializer::getFullFileName(const std::string& fileName)
132{
133 if constexpr(includeLogOutputDir){
134 return singleton::directories().getLogOutDir() + createParallelFileName(fileName) + ".dat";
135 } else {
136 return createParallelFileName(fileName) + ".dat";
137 }
138}
139
140
141
143
144template<bool includeLogOutputDir>
145bool Serializable::save(std::string fileName, const bool enforceUint)
146{
147 Serializer tmpSerializer(*this, fileName);
148 return tmpSerializer.save<includeLogOutputDir>();
149}
150
151template<bool includeLogOutputDir>
152bool Serializable::load(std::string fileName, const bool enforceUint)
153{
154 Serializer tmpSerializer(*this, fileName);
155 return tmpSerializer.load<includeLogOutputDir>();
156}
157
158bool Serializable::save(std::uint8_t* buffer)
159{
160 Serializer tmpSerializer(*this);
161 return tmpSerializer.save(buffer);
162}
163
164bool Serializable::load(const std::uint8_t* buffer)
165{
166 Serializer tmpSerializer(*this);
167 return tmpSerializer.load(buffer);
168}
169
170} // namespace olb
171
172#endif
173
174
Base class for serializable objects of constant size. For dynamic size use BufferSerializable.
Definition serializer.h:145
virtual bool * getBlock(const std::size_t iBlock, std::size_t &sizeBlock, const bool loadingMode=false)=0
Returns the address of the i-th block and its size.
virtual std::size_t getSerializableSize() const =0
Returns the binary size of the data to be saved.
bool save(std::string fileName="", const bool enforceUint=false)
Save Serializable into file fileName
bool load(std::string fileName="", const bool enforceUint=false)
Load Serializable from file fileName
virtual void postLoad()
Definition serializer.h:191
Class for writing, reading, sending and receiving Serializable objects.
Definition serializer.h:41
void resetCounter()
Resets the _iBlock counter.
Definition serializer.hh:49
std::size_t getSize() const
Returns the total memory size in bits.
Definition serializer.hh:54
bool load(std::string fileName="", const bool enforceUint=false)
Loads a file and pushes the data into the serialized class. Always in parallel, i....
Definition serializer.hh:65
bool save(std::string fileName="", const bool enforceUint=false)
Save _serializable into file filename. Always in parallel, i.e. one file per rank.
Definition serializer.hh:81
void computeSize(const bool enforceRecompute=false)
computes _size based on the individual definition of getBlock()
Serializer(Serializable &serializable, std::string fileName="")
Constructor.
Definition serializer.hh:44
bool * getNextBlock(std::size_t &sizeBlock, const bool loadingMode)
Returns pointer to the memory of the current block and increments iBlock
Definition serializer.hh:59
std::string getLogOutDir() const
Definition singleton.h:89
These functions help you to create file names.
Wrapper functions that simplify the use of MPI.
Directories & directories()
Definition singleton.h:150
Top level namespace for all of OpenLB.
void serializer2buffer(Serializer &serializer, std::uint8_t *buffer)
processes data from a serializer to a given buffer
std::string createParallelFileName(std::string name, bool withSize=true)
for parallel io, e.g. adds "_rank0000001" for rank=1, and optional "_size0000016" if withSize==true
Definition fileName.hh:51
void serializer2ostr(Serializer &serializer, std::ostream &ostr, bool enforceUint=false)
processes data from a serializer to a given ostr, always in parallel
void buffer2serializer(Serializer &serializer, const std::uint8_t *buffer)
processes a buffer to a serializer
void istr2serializer(Serializer &serializer, std::istream &istr, bool enforceUint=false)
processes an istr to a serializer, always in parallel
Definition of singletons: global, publicly available information.