Serializer.h 4.72 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
49
		    "%d", &tsModulo);
      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

Thomas Witkowski's avatar
Thomas Witkowski committed
56
    virtual ~Serializer() {}
57
58
59
60
61
62
63
64
65

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

66
67
68
      timestepNumber++;
      timestepNumber %= tsModulo;
      if ((timestepNumber != 0) && !force)
69
70
71
72
	return;

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

73
      std::ofstream out(name.c_str());
74
      TEST_EXIT(out.is_open())("Cannot open serialization file!\n");
75
      problem->serialize(out);
76
77
78
      adaptInfo->serialize(out);
      out.close();

79
      MSG("problem serialized to %s \n", name.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
80
    }
81
82

  protected:
83
    /// Name of file to which the problem is serialized.
84
    std::string name;
85

86
    /// Pointer to the problem.
87
    ProblemType *problem;
88

89
    /// The problem is serialized every tsModulo-th timestep.
90
    int tsModulo;
91

92
    /// Current timestep number.
93
    int timestepNumber;
94
95
  };

96
  namespace SerUtil {
97

98
    template<typename T>
99
    void serialize(std::ostream& out, T& data)
100
    {
101
      out.write(reinterpret_cast<const char*>(&data), sizeof(T));
102
    }   
103

104
    template<typename T>
105
    void deserialize(std::istream& in, T& data)
106
    {
107
      in.read(reinterpret_cast<char*>(&data), sizeof(T));
108
    }   
109

110
111

    template<typename T>
112
    void serialize(std::ostream& out, std::vector<T>& data)
113
114
    {
      int vecSize = data.size();
115
      serialize(out, vecSize);
116
117
118
      for (typename std::vector<T>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T v = *it;
119
	serialize(out, v);
120
121
122
123
      }
    }

    template<typename T>
124
    void deserialize(std::istream& in, std::vector<T>& data)
125
126
    {
      int vecSize = 0;
127
      deserialize(in, vecSize);
128
129
130
131
      data.resize(vecSize);

      for (int i = 0; i < vecSize; i++) {
	T v;
132
	deserialize(in, v);
133
134
135
136
137
	data[i] = v;
      }
    }


138
    template<typename T1, typename T2>
139
    void serialize(std::ostream& out, std::map<T1, T2>& data)
140
    {
141
      int mapSize = data.size();
142
      serialize(out, mapSize);
143
144
145
146
147

      for (typename std::map<T1,T2>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T1 v1 = it->first;
	T2 v2 = it->second;
148
149
	serialize(out, v1);
	serialize(out, v2);
150
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
151
    }
152
153

    template<typename T1, typename T2>
154
    void deserialize(std::istream& in, std::map<T1, T2>& data)
155
156
    {
      int mapSize = 0;
157
      deserialize(in, mapSize);
158
159
160
161

      for (int i = 0; i < mapSize; i++) {
	T1 v1;
	T2 v2;
162
163
	deserialize(in, v1);
	deserialize(in, v2);
164
165
166
167
	data[v1] = v2;
      }
    }

168
  }
169
170
171

}
#endif