ug4
serialization_impl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__LIB_GRID__SERIALIZATION_IMPL__
34 #define __H__LIB_GRID__SERIALIZATION_IMPL__
35 
36 #include "serialization.h"
37 
38 namespace ug
39 {
40 
42 // DataSerializer
44 serialize(BinaryBuffer& out, Vertex* vrt) const
45 {
46  serialize(out, vrt, m_vrtSerializers);
47  serialize(out, vrt, m_gridSerializers);
48 }
49 
51 serialize(BinaryBuffer& out, Edge* edge) const
52 {
53  serialize(out, edge, m_edgeSerializers);
54  serialize(out, edge, m_gridSerializers);
55 }
56 
58 serialize(BinaryBuffer& out, Face* face) const
59 {
60  serialize(out, face, m_faceSerializers);
61  serialize(out, face, m_gridSerializers);
62 }
63 
65 serialize(BinaryBuffer& out, Volume* vol) const
66 {
67  serialize(out, vol, m_volSerializers);
68  serialize(out, vol, m_gridSerializers);
69 }
70 
71 template <class TIterator>
73 serialize(BinaryBuffer& out, TIterator begin, TIterator end) const
74 {
75  for(TIterator iter = begin; iter != end; ++iter)
76  serialize(out, *iter);
77 }
78 
79 template<class TGeomObj, class TSerializers>
81 serialize(BinaryBuffer& out, TGeomObj* o,
82  TSerializers& serializers) const
83 {
84 // This method performs the serialization
85  for(size_t i = 0; i < serializers.size(); ++i){
86  serializers[i]->write_data(out, o);
87  }
88 }
89 
90 
94 {
95  deserialize(in, vrt, m_vrtSerializers);
97 }
98 
100 deserialize(BinaryBuffer& in, Edge* edge)
101 {
102  deserialize(in, edge, m_edgeSerializers);
103  deserialize(in, edge, m_gridSerializers);
104 }
105 
107 deserialize(BinaryBuffer& in, Face* face)
108 {
109  deserialize(in, face, m_faceSerializers);
110  deserialize(in, face, m_gridSerializers);
111 }
112 
115 {
116  deserialize(in, vol, m_volSerializers);
117  deserialize(in, vol, m_gridSerializers);
118 }
119 
120 template <class TIterator>
122 deserialize(BinaryBuffer& in, TIterator begin, TIterator end)
123 {
124  for(TIterator iter = begin; iter != end; ++iter)
125  deserialize(in, *iter);
126 }
127 
128 template<class TGeomObj, class TDeserializers>
130 deserialize(BinaryBuffer& in, TGeomObj* o,
131  TDeserializers& deserializers)
132 {
133 // This method performs the deserialization
134  for(size_t i = 0; i < deserializers.size(); ++i){
135  deserializers[i]->read_data(in, o);
136  }
137 }
138 
139 
142 // SerializeAttachment
143 template <class TElem, class TAttachment>
144 bool SerializeAttachment(Grid& grid, TAttachment& attachment,
145  BinaryBuffer& out)
146 {
147  return SerializeAttachment<TElem, TAttachment>(
148  grid, attachment,
149  grid.begin<TElem>(),
150  grid.end<TElem>(),
151  out);
152 }
153 
155 // SerializeAttachment
156 template <class TElem, class TAttachment>
157 bool SerializeAttachment(Grid& grid, TAttachment& attachment,
158  typename geometry_traits<TElem>::iterator iterBegin,
159  typename geometry_traits<TElem>::iterator iterEnd,
160  BinaryBuffer& out)
161 {
162  if(!grid.has_attachment<TElem>(attachment))
163  return false;
164 
165 // copy data
167  typedef typename TAttachment::ValueType ValueType;
168 
169 // write a magic number at the beginning and at the end.
170  int magicNumber = 8304548;
171  out.write((char*)&magicNumber, sizeof(int));
172 
173 //TODO: remove the following test code.
174 // test: write a number-value to check whether it is send correctly
175 /*
176  number tNum = 1247.001234;
177  out.write((char*)&tNum, sizeof(number));
178 */
179  for(; iterBegin != iterEnd; ++iterBegin)
180  {
181  out.write((char*)&aa[*iterBegin], sizeof(ValueType));
182  }
183  out.write((char*)&magicNumber, sizeof(int));
184 
185  return true;
186 }
187 
188 
190 // DeserializeAttachment
191 template <class TElem, class TAttachment>
192 bool DeserializeAttachment(Grid& grid, TAttachment& attachment,
193  BinaryBuffer& in)
194 {
195  return DeserializeAttachment<TElem, TAttachment>(
196  grid, attachment, grid.begin<TElem>(),
197  grid.end<TElem>(), in);
198 }
199 
201 // DeserializeAttachment
202 template <class TElem, class TAttachment>
203 bool DeserializeAttachment(Grid& grid, TAttachment& attachment,
204  typename geometry_traits<TElem>::iterator iterBegin,
205  typename geometry_traits<TElem>::iterator iterEnd,
206  BinaryBuffer& in)
207 {
208  if(!grid.has_attachment<TElem>(attachment))
209  grid.attach_to<TElem>(attachment);
210 
211 // copy data
213  typedef typename TAttachment::ValueType ValueType;
214 
215 // compare with the magic number
216 
217  int magicNumber = 8304548;
218  int tInt;
219  in.read((char*)&tInt, sizeof(int));
220 
221  if(tInt != magicNumber){
222  UG_LOG(" WARNING: magic-number mismatch before read in DeserializeAttachment. Data-salad possible!\n");
223  return false;
224  }
225 
226 //TODO: remove the following test code.
227 // test: write a number-value to check whether it is send correctly
228 /*
229  number tNum;
230  in.read((char*)&tNum, sizeof(number));
231  if(tNum != 1247.001234){
232  UG_LOG("TEST-NUMBER TRANSMIT FAILED in DeserializeAttachment!\n");
233  return false;
234  }
235 */
236  for(; iterBegin != iterEnd; ++iterBegin)
237  {
238  in.read((char*)&aa[*iterBegin], sizeof(ValueType));
239  }
240  in.read((char*)&tInt, sizeof(int));
241 
242  if(tInt != magicNumber){
243  UG_LOG(" WARNING: magic-number mismatch after read in DeserializeAttachment. Data-salad possible!\n");
244  return false;
245  }
246 
247  return true;
248 }
249 }
250 
251 #endif
A Buffer for binary data.
Definition: binary_buffer.h:56
void read(char *buf, size_t size)
reads data of the given size (in bytes)
Definition: binary_buffer_impl.h:58
void write(const char *buf, size_t size)
writes data of the given size (in bytes)
Definition: binary_buffer_impl.h:71
Base-class for edges.
Definition: grid_base_objects.h:397
Faces are 2-dimensional objects.
Definition: grid_base_objects.h:510
the generic attachment-accessor for access to grids attachment pipes.
Definition: grid.h:182
void deserialize(BinaryBuffer &in, Vertex *vrt)
Deserializes data associated with the given object.
Definition: serialization_impl.hpp:93
std::vector< SPVolumeDataSerializer > m_volSerializers
Definition: serialization.h:275
std::vector< SPFaceDataSerializer > m_faceSerializers
Definition: serialization.h:274
std::vector< SPGridDataSerializer > m_gridSerializers
Definition: serialization.h:276
std::vector< SPEdgeDataSerializer > m_edgeSerializers
Definition: serialization.h:273
void serialize(BinaryBuffer &out, Vertex *vrt) const
Serializes data associated with the given object.
Definition: serialization_impl.hpp:44
std::vector< SPVertexDataSerializer > m_vrtSerializers
Definition: serialization.h:272
Manages the elements of a grid and their interconnection.
Definition: grid.h:132
void attach_to(IAttachment &attachment, bool passOnValues)
attach with custom pass-on-behaviour and unspecified default value.
Definition: grid_impl.hpp:296
bool has_attachment(IAttachment &attachment)
Definition: grid.h:796
geometry_traits< TGeomObj >::iterator begin()
Definition: grid_impl.hpp:164
geometry_traits< TGeomObj >::iterator end()
Definition: grid_impl.hpp:175
Base-class for all vertex-types.
Definition: grid_base_objects.h:231
Volumes are 3-dimensional objects.
Definition: grid_base_objects.h:754
Definition: grid_base_object_traits.h:68
bool SerializeAttachment(Grid &grid, TAttachment &attachment, BinaryBuffer &out)
Definition: serialization_impl.hpp:144
bool DeserializeAttachment(Grid &grid, TAttachment &attachment, BinaryBuffer &in)
copies attached values from a binary stream
Definition: serialization_impl.hpp:192
#define UG_LOG(msg)
Definition: log.h:367
the ug namespace