Serializer.h 5.82 KB
Newer Older
1
// ============================================================================
2
3
4
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
5
// ==  http://www.amdis-fem.org                                              ==
6
7
// ==                                                                        ==
// ============================================================================
8
9
10
11
12
13
14
15
16
17
18
19
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


20
21
22
23
24
25

/** \file Serializer.h */

#ifndef AMDIS_SERIALIZER_H
#define AMDIS_SERIALIZER_H

26
27
28
29
#if HAVE_PARALLEL_DOMAIN_AMDIS
#include <mpi.h>
#endif

30
#include <map>
31
32
#include "boost/lexical_cast.hpp"

33
#include "io/FileWriter.h"
34
35
36
37
38
39
40
41
42
#include "Parameters.h"
#include "AdaptInfo.h"

namespace AMDiS {

  template<typename ProblemType>
  class Serializer : public FileWriterInterface
  {
  public:
43
44
45
46
47
    Serializer(ProblemType *prob) 
      : name(""), 
	problem(prob),
	tsModulo(1), 
	timestepNumber(-1)
Thomas Witkowski's avatar
Thomas Witkowski committed
48
    {
49
50
51
52
      FUNCNAME("Serializer::Serializer()");

      GET_PARAMETER(0, problem->getName() + "->output->serialization filename", &name);
      GET_PARAMETER(0, problem->getName() + "->output->write every i-th timestep", 
53
		    "%d", &tsModulo);
54
      TEST_EXIT(name != "")("No filename!\n");
55
56

#if HAVE_PARALLEL_DOMAIN_AMDIS
57
      name += ".p" + boost::lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank());
58
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
59
    }
60

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

    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
78
    virtual ~Serializer() {}
79
80
81
82
83
84
85
86
87

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

88
89
90
      TEST_EXIT(tsModulo > 0)
	("Parameter 'write every ith timestep' must be larger than zero!\n");

91
92
93
      timestepNumber++;
      timestepNumber %= tsModulo;
      if ((timestepNumber != 0) && !force)
94
95
96
97
	return;

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

98
      std::ofstream out(name.c_str());
99
      TEST_EXIT(out.is_open())("Cannot open serialization file!\n");
100
      problem->serialize(out);
101
102
103
      adaptInfo->serialize(out);
      out.close();

104
      MSG("problem serialized to %s \n", name.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
105
    }
106
107

  protected:
108
    /// Name of file to which the problem is serialized.
109
    std::string name;
110

111
    /// Pointer to the problem.
112
    ProblemType *problem;
113

114
    /// The problem is serialized every tsModulo-th timestep.
115
    int tsModulo;
116

117
    /// Current timestep number.
118
    int timestepNumber;
119
120
  };

121
  namespace SerUtil {
122

123
    template<typename T>
124
    void serialize(std::ostream& out, T& data)
125
    {
126
      out.write(reinterpret_cast<const char*>(&data), sizeof(T));
127
    }   
128

129
    template<typename T>
130
    void deserialize(std::istream& in, T& data)
131
    {
132
      in.read(reinterpret_cast<char*>(&data), sizeof(T));
133
    }   
134

135

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

    void serialize(std::ostream& out, DofEdge& data);

    void deserialize(std::istream& in, DofEdge& data);



    void serialize(std::ostream& out, DofFace& data);

    void deserialize(std::istream& in, DofFace& data);



    template<typename T, typename U>
    void serialize(std::ostream& out, std::pair<T, U>& data)
    {
      serialize(out, data.first);
      serialize(out, data.second);
    }

    template<typename T, typename U>
    void deserialize(std::istream& in, std::pair<T, U>& data)
    {
      deserialize(in, data.first);
      deserialize(in, data.second);
    }



165
    template<typename T>
166
    void serialize(std::ostream& out, std::vector<T>& data)
167
168
    {
      int vecSize = data.size();
169
      serialize(out, vecSize);
170
171
172
      for (typename std::vector<T>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T v = *it;
173
	serialize(out, v);
174
175
176
177
      }
    }

    template<typename T>
178
    void deserialize(std::istream& in, std::vector<T>& data)
179
    {
180
181
      data.clear();

182
      int vecSize = 0;
183
      deserialize(in, vecSize);
184
185
186
187
      data.resize(vecSize);

      for (int i = 0; i < vecSize; i++) {
	T v;
188
	deserialize(in, v);
189
190
191
192
	data[i] = v;
      }
    }

193
194


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    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)
    {
210
211
      data.clear();

212
213
214
215
216
217
218
219
220
      int setSize = 0;
      deserialize(in, setSize);

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

222
223


224
    template<typename T1, typename T2>
225
    void serialize(std::ostream& out, std::map<T1, T2>& data)
226
    {
227
      int mapSize = data.size();
228
      serialize(out, mapSize);
229
230
231
232
233

      for (typename std::map<T1,T2>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T1 v1 = it->first;
	T2 v2 = it->second;
234
235
	serialize(out, v1);
	serialize(out, v2);
236
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
237
    }
238
239

    template<typename T1, typename T2>
240
    void deserialize(std::istream& in, std::map<T1, T2>& data)
241
    {
242
243
      data.clear();

244
      int mapSize = 0;
245
      deserialize(in, mapSize);
246
247
248
249

      for (int i = 0; i < mapSize; i++) {
	T1 v1;
	T2 v2;
250
251
	deserialize(in, v1);
	deserialize(in, v2);
252
253
254
255
	data[v1] = v2;
      }
    }

256
  }
257
258
259

}
#endif