Serializer.h 5.63 KB
Newer Older
1
// ============================================================================
2
3
4
5
6
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
7
// ==  TU Dresden                                                            ==
8
// ==                                                                        ==
9
10
11
// ==  Institut fr Wissenschaftliches Rechnen                               ==
// ==  Zellescher Weg 12-14                                                  ==
// ==  01069 Dresden                                                         ==
12
13
14
15
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
16
// ==  https://gforge.zih.tu-dresden.de/projects/amdis/                      ==
17
18
19
20
21
22
23
24
// ==                                                                        ==
// ============================================================================

/** \file Serializer.h */

#ifndef AMDIS_SERIALIZER_H
#define AMDIS_SERIALIZER_H

25
#include <map>
26
27
#include "boost/lexical_cast.hpp"

28
29
30
31
32
33
34
35
36
37
#include "FileWriter.h"
#include "Parameters.h"
#include "AdaptInfo.h"

namespace AMDiS {

  template<typename ProblemType>
  class Serializer : public FileWriterInterface
  {
  public:
38
39
40
41
42
    Serializer(ProblemType *prob) 
      : name(""), 
	problem(prob),
	tsModulo(1), 
	timestepNumber(-1)
Thomas Witkowski's avatar
Thomas Witkowski committed
43
    {
44
45
46
47
      FUNCNAME("Serializer::Serializer()");

      GET_PARAMETER(0, problem->getName() + "->output->serialization filename", &name);
      GET_PARAMETER(0, problem->getName() + "->output->write every i-th timestep", 
48
		    "%d", &tsModulo);
49
      TEST_EXIT(name != "")("No filename!\n");
50
51

#if HAVE_PARALLEL_DOMAIN_AMDIS
52
      name += ".p" + boost::lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank());
53
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
54
    }
55

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

    Serializer(ProblemType *prob, std::string filename, int writeEveryIth)
      : name(filename),
	problem(prob),
	tsModulo(writeEveryIth),
	timestepNumber(-1)
    {
      FUNCNAME("Serializer::Serializer()");

      TEST_EXIT(name != "")("No filename!\n");

#if HAVE_PARALLEL_DOMAIN_AMDIS
      name += ".p" + boost::lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank());
#endif      
    }


Thomas Witkowski's avatar
Thomas Witkowski committed
73
    virtual ~Serializer() {}
74
75
76
77
78
79
80
81
82

    virtual void writeFiles(AdaptInfo *adaptInfo, 
			    bool force,
			    int level = -1,
			    Flag traverseFlag = Mesh::CALL_LEAF_EL,
			    bool (*writeElem)(ElInfo*) = NULL) 
    {
      FUNCNAME("Serializer::writeFiles()");

83
84
85
      timestepNumber++;
      timestepNumber %= tsModulo;
      if ((timestepNumber != 0) && !force)
86
87
88
89
	return;

      TEST_EXIT(adaptInfo)("No AdaptInfo\n");

90
      std::ofstream out(name.c_str());
91
      TEST_EXIT(out.is_open())("Cannot open serialization file!\n");
92
      problem->serialize(out);
93
94
95
      adaptInfo->serialize(out);
      out.close();

96
      MSG("problem serialized to %s \n", name.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
97
    }
98
99

  protected:
100
    /// Name of file to which the problem is serialized.
101
    std::string name;
102

103
    /// Pointer to the problem.
104
    ProblemType *problem;
105

106
    /// The problem is serialized every tsModulo-th timestep.
107
    int tsModulo;
108

109
    /// Current timestep number.
110
    int timestepNumber;
111
112
  };

113
  namespace SerUtil {
114

115
    template<typename T>
116
    void serialize(std::ostream& out, T& data)
117
    {
118
      out.write(reinterpret_cast<const char*>(&data), sizeof(T));
119
    }   
120

121
    template<typename T>
122
    void deserialize(std::istream& in, T& data)
123
    {
124
      in.read(reinterpret_cast<char*>(&data), sizeof(T));
125
    }   
126

127
128

    template<typename T>
129
    void serialize(std::ostream& out, std::vector<T>& data)
130
131
    {
      int vecSize = data.size();
132
      serialize(out, vecSize);
133
134
135
      for (typename std::vector<T>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T v = *it;
136
	serialize(out, v);
137
138
139
140
      }
    }

    template<typename T>
141
    void deserialize(std::istream& in, std::vector<T>& data)
142
143
    {
      int vecSize = 0;
144
      deserialize(in, vecSize);
145
146
147
148
      data.resize(vecSize);

      for (int i = 0; i < vecSize; i++) {
	T v;
149
	deserialize(in, v);
150
151
152
153
	data[i] = v;
      }
    }

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    template<typename T>
    void serialize(std::ostream& out, std::set<T>& data)
    {
      int setSize = data.size();
      serialize(out, setSize);
      for (typename std::set<T>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T v = *it;
	serialize(out, v);
      }
    }

    template<typename T>
    void deserialize(std::istream& in, std::set<T>& data)
    {
      int setSize = 0;
      deserialize(in, setSize);

      for (int i = 0; i < setSize; i++) {
	T v;
	deserialize(in, v);
	data.insert(v);
      }
    }
178

179
    template<typename T1, typename T2>
180
    void serialize(std::ostream& out, std::map<T1, T2>& data)
181
    {
182
      int mapSize = data.size();
183
      serialize(out, mapSize);
184
185
186
187
188

      for (typename std::map<T1,T2>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T1 v1 = it->first;
	T2 v2 = it->second;
189
190
	serialize(out, v1);
	serialize(out, v2);
191
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
192
    }
193
194

    template<typename T1, typename T2>
195
    void deserialize(std::istream& in, std::map<T1, T2>& data)
196
197
    {
      int mapSize = 0;
198
      deserialize(in, mapSize);
199
200
201
202

      for (int i = 0; i < mapSize; i++) {
	T1 v1;
	T2 v2;
203
204
	deserialize(in, v1);
	deserialize(in, v2);
205
206
207
208
	data[v1] = v2;
      }
    }

209
  }
210
211
212

}
#endif