Serializer.h 4.7 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
28
29
30
31
32
33
34
35
36
#include "FileWriter.h"
#include "Parameters.h"
#include "AdaptInfo.h"
#include "ProblemStatBase.h"

namespace AMDiS {

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

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

#if HAVE_PARALLEL_DOMAIN_AMDIS
      name += ".p";
      name += MPI::COMM_WORLD.Get_rank();
#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
99
    template<typename T>
    void serialize(std::ostream &out, T *data)
100
    {
101
102
      out.write(reinterpret_cast<const char*>(data), sizeof(T));
    }   
103

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

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

    template<typename T>
    void serialize(std::ostream &out, std::vector<T> &data)
    {
      int vecSize = data.size();
      serialize(out, &vecSize);
      for (typename std::vector<T>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T v = *it;
	serialize(out, &v);
      }
    }

    template<typename T>
    void deserialize(std::istream &in, std::vector<T> &data)
    {
      int vecSize = 0;
      deserialize(in, &vecSize);
      data.resize(vecSize);

      for (int i = 0; i < vecSize; i++) {
	T v;
	deserialize(in, &v);
	data[i] = v;
      }
    }


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

      for (typename std::map<T1,T2>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T1 v1 = it->first;
	T2 v2 = it->second;
	serialize(out, &v1);
	serialize(out, &v2);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
151
    }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

    template<typename T1, typename T2>
    void deserialize(std::istream &in, std::map<T1, T2> &data)
    {
      int mapSize = 0;
      deserialize(in, &mapSize);

      for (int i = 0; i < mapSize; i++) {
	T1 v1;
	T2 v2;
	deserialize(in, &v1);
	deserialize(in, &v2);
	data[v1] = v2;
      }
    }

168
  }
169
170
171

}
#endif