Serializer.h 5.92 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
#include <boost/lexical_cast.hpp>
32

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

namespace AMDiS {

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

52
53
54
55
      Parameters::get(problem->getName() + "->output->serialization filename", 
		      name);
      Parameters::get(problem->getName() + "->output->write every i-th timestep", 
		      tsModulo);
56
      TEST_EXIT(name != "")("No filename!\n");
57
58

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

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

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

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

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

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

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

100
      std::ofstream out(name.c_str());
101
      TEST_EXIT(out.is_open())("Cannot open serialization file!\n");
102
      out.write(reinterpret_cast<const char*>(&amdisRevisionNumber), sizeof(int));
103
      problem->serialize(out);
104
105
106
      adaptInfo->serialize(out);
      out.close();

107
      MSG("problem serialized to %s \n", name.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
108
    }
109
110

  protected:
111
    /// Name of file to which the problem is serialized.
112
    std::string name;
113

114
    /// Pointer to the problem.
115
    ProblemType *problem;
116

117
    /// The problem is serialized every tsModulo-th timestep.
118
    int tsModulo;
119

120
    /// Current timestep number.
121
    int timestepNumber;
122
123
  };

124
  namespace SerUtil {
125

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

132
    template<typename T>
133
    void deserialize(std::istream& in, T& data)
134
    {
135
      in.read(reinterpret_cast<char*>(&data), sizeof(T));
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
165
166
167

    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);
    }



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

    template<typename T>
181
    void deserialize(std::istream& in, std::vector<T>& data)
182
    {
183
184
      data.clear();

185
      int vecSize = 0;
186
      deserialize(in, vecSize);
187
188
189
190
      data.resize(vecSize);

      for (int i = 0; i < vecSize; i++) {
	T v;
191
	deserialize(in, v);
192
193
194
195
	data[i] = v;
      }
    }

196
197


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

215
216
217
218
219
220
221
222
223
      int setSize = 0;
      deserialize(in, setSize);

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

225
226


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

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

    template<typename T1, typename T2>
243
    void deserialize(std::istream& in, std::map<T1, T2>& data)
244
    {
245
246
      data.clear();

247
      int mapSize = 0;
248
      deserialize(in, mapSize);
249
250
251
252

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

259
  }
260
261
262

}
#endif