ug4
serialize_interfaces.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
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_ALGEBRA__SERIALIZE_INTERFACES_H_
34 #define __H__LIB_ALGEBRA__SERIALIZE_INTERFACES_H_
35 
36 
37 #ifndef UG_PARALLEL
38 #error "This only works with a UG_PARALLEL define."
39 #endif
40 
41 #include "pcl/pcl.h"
42 #include "parallelization.h"
43 #include "common/serialization.h"
44 
45 namespace ug
46 {
47 
48 
49 template<typename TLocalToGlobal>
51  const IndexLayout::Interface &interface, const TLocalToGlobal &localToGlobal)
52 {
53  Serialize(stream, interface.size());
54  for(IndexLayout::Interface::const_iterator iter2 = interface.begin(); iter2 != interface.end(); ++iter2)
55  {
56  size_t localIndex = interface.get_element(iter2);
57  AlgebraID &globalIndex = localToGlobal[localIndex];
58  Serialize(stream, globalIndex);
59  }
60 }
61 
62 template<typename TLocalToGlobal>
64  const IndexLayout &layout, const TLocalToGlobal &localToGlobal)
65 {
66  size_t size = 0;
67  for(IndexLayout::const_iterator iter = layout.begin(); iter != layout.end(); ++iter)
68  size++;
69  Serialize(stream, size);
70  for(IndexLayout::const_iterator iter = layout.begin(); iter != layout.end(); ++iter)
71  {
72  const IndexLayout::Interface &interface = layout.interface(iter);
73  int pid = layout.proc_id(iter);
74  Serialize(stream, pid);
75  Serialize(stream, interface);
76  }
77 }
78 
79 
80 template<typename TGlobalToLocal>
82  IndexLayout::Interface &interface, const TGlobalToLocal &globalToLocal)
83 {
84 
85  size_t size;
86  Deserialize(stream, size);
87  for(size_t i=0; i<size; i++)
88  {
89  AlgebraID globalIndex;
90  Deserialize(stream, globalIndex);
91  size_t localIndex = globalToLocal[globalIndex];
92  interface.push_back(localIndex);
93  }
94 }
95 
96 template<typename TGlobalToLocal>
98  IndexLayout &layout, const TGlobalToLocal &globalToLocal)
99 {
100  size_t size;
101  Deserialize(stream, size);
102  for(size_t i=0; i<size; i++)
103  {
104  int pid = Deserialize<int>(stream);
105  DeserializeInterface(stream, layout.interface(pid), globalToLocal);
106  }
107 }
108 
109 
110 template<typename TLayout>
111 bool RemoveInterface(TLayout &layout, int pid)
112 {
113  typename TLayout::iterator it = find(layout, pid);
114  if(it == layout.end()) return false;
115  layout.erase(it);
116  return true;
117 }
118 // removes all interfaces which are within group
119 template<typename TLayout>
120 void RemoveInterfaces(TLayout &layout, std::vector<int> group)
121 {
122  for(size_t i=0; i<group.size(); i++)
123  RemoveInterface(layout, group[i]);
124 }
125 
126 template<typename TLayout>
127 bool AppendInterface(TLayout &layout, int pidSource, int pidAppendTo)
128 {
129  typename TLayout::iterator it = find(layout, pidSource);
130  if(it == layout.end()) return false;
131  typename TLayout::Interface interfaceSource = layout.interface(it);
132  if(interfaceSource.empty()) return false;
133  typename TLayout::Interface interfaceAppendTo = layout.interface(pidAppendTo);
134 
135  for(typename TLayout::Interface::iterator itSource = interfaceSource.begin(); itSource != interfaceSource.end(); ++itSource)
136  interfaceAppendTo.push_back(interfaceSource.get_element(itSource));
137  return true;
138 }
139 
140 template<typename TLayout>
141 void MergeInterfaces(TLayout &layout, const std::vector<int> pidSources, int pidAppendTo)
142 {
143  for(size_t i=0; i<pidSources.size(); i++)
144  {
145  AppendInterface(layout, pidSources[i], pidAppendTo);
146  RemoveInterface(layout, pidSources[i]);
147  }
148 }
149 
150 
151 template<typename TLayout>
152 void MergeInterfaces(TLayout &layout, const std::map<int, int> merge)
153 {
154  for(std::map<int, int>::const_iterator it = merge.begin(); it != merge.end(); ++it)
155  {
156  AppendInterface(layout, it->first, it->second);
157  RemoveInterface(layout, it->first);
158  }
159 }
160 
161 }
162 #endif /* SERIALIZE_INTERFACES.H */
You may add elements to this interface and iterate over them.
Definition: pcl_communication_structs.h:207
iterator push_back(const Element &elem)
Definition: pcl_communication_structs.h:245
iterator end()
Definition: pcl_communication_structs.h:293
iterator begin()
Definition: pcl_communication_structs.h:292
Element & get_element(iterator iter)
Definition: pcl_communication_structs.h:298
size_t size() const
returns the number of elements that are stored in the interface.
Definition: pcl_communication_structs.h:306
iterator end(size_t level=0)
returns the iterator to the last interface of the layout.
Definition: pcl_communication_structs.h:492
iterator begin(size_t level=0)
returns the iterator to the first interface of the layout.
Definition: pcl_communication_structs.h:486
int proc_id(iterator iter) const
returns the target process of the interface given in iterator
Definition: pcl_communication_structs.h:509
Interface & interface(iterator iter)
returns the interface to the given iterator.
Definition: pcl_communication_structs.h:505
InterfaceMap::const_iterator const_iterator
Definition: pcl_communication_structs.h:477
A Buffer for binary data.
Definition: binary_buffer.h:56
function table merge(t1, t2)
the ug namespace
void SerializeInterface(BinaryBuffer &stream, const IndexLayout::Interface &interface, const TLocalToGlobal &localToGlobal)
Definition: serialize_interfaces.h:50
IndexLayout::Interface::iterator find(IndexLayout::Interface &interface, size_t i)
Definition: parallel_index_layout.h:77
void DeserializeInterface(BinaryBuffer &stream, IndexLayout::Interface &interface, const TGlobalToLocal &globalToLocal)
Definition: serialize_interfaces.h:81
void Deserialize(TIStream &buf, ParallelVector< T > &v)
Deerialize for ParallelVector<T>
Definition: restart_bridge.cpp:112
void RemoveInterfaces(TLayout &layout, std::vector< int > group)
Definition: serialize_interfaces.h:120
bool AppendInterface(TLayout &layout, int pidSource, int pidAppendTo)
Definition: serialize_interfaces.h:127
void DeserializeLayout(BinaryBuffer &stream, IndexLayout &layout, const TGlobalToLocal &globalToLocal)
Definition: serialize_interfaces.h:97
void Serialize(TOStream &buf, const ParallelVector< T > &v)
Serialize for ParallelVector<T>
Definition: restart_bridge.cpp:103
bool RemoveInterface(TLayout &layout, int pid)
Definition: serialize_interfaces.h:111
void SerializeLayout(BinaryBuffer &stream, const IndexLayout &layout, const TLocalToGlobal &localToGlobal)
Definition: serialize_interfaces.h:63
void MergeInterfaces(TLayout &layout, const std::vector< int > pidSources, int pidAppendTo)
Definition: serialize_interfaces.h:141
this type is used to identify distributed objects.
Definition: algebra_id.h:46