Plugins
memreduce.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2017: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter, Dmitry Logashenko
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 /* ug4 headers */
34 #include "pcl/pcl.h"
36 #include "common/serialization.h"
37 
41 #ifndef __H__UG__PLUGINS__ELECTROMAGNETISM__MEM_REDUCE__
42 #define __H__UG__PLUGINS__ELECTROMAGNETISM__MEM_REDUCE__
43 
44 namespace ug{
45 namespace Electromagnetism{
46 
58 template <typename TArray, typename TLayout, typename TOp>
59 class ComPol_MemOp : public pcl::ICommunicationPolicy<TLayout>
60 {
63 
64 public:
65  typedef TArray array_type;
67  typedef typename base_type::Layout Layout;
68  typedef typename base_type::Interface Interface;
69 
70 public:
72  ComPol_MemOp () : m_pMemDst(NULL), m_pMemSrc(NULL) {}
73 
75  ComPol_MemOp (TArray* pVec): m_pMemDst(pVec), m_pMemSrc(pVec) {}
76 
78  ComPol_MemOp (TArray* pMemDst, const TArray* pMemSrc)
79  : m_pMemDst(pMemDst), m_pMemSrc(pMemSrc) {}
80 
82  void set_src (const TArray* pMem) {m_pMemSrc = pMem;}
83 
85  void set_dst (TArray* pMemDst) {m_pMemDst = pMemDst;}
86 
88 
97  virtual int get_required_buffer_size (const Interface& interface)
98  {
99  return interface.size () * sizeof (value_type);
100  }
101 
103 
110  virtual bool collect (ug::BinaryBuffer& buff, const Interface& interface)
111  {
112  // check that vector has been set
113  if (m_pMemSrc == NULL) return false;
114 
115  // loop interface
116  for (typename Interface::const_iterator iter = interface.begin ();
117  iter != interface.end (); ++iter)
118  {
119  // get index
120  const size_t index = interface.get_element (iter);
121 
122  // write entry into buffer
123  Serialize (buff, (* m_pMemSrc) [index]);
124  }
125  return true;
126  }
127 
129 
135  virtual bool extract (ug::BinaryBuffer& buff, const Interface& interface)
136  {
137  // check that vector has been set
138  if(m_pMemDst == NULL) return false;
139 
140  // loop interface
141  for (typename Interface::const_iterator iter = interface.begin ();
142  iter != interface.end (); ++iter)
143  {
144  // get index
145  const size_t index = interface.get_element (iter);
146 
147  // write entry into the array
148  typename TArray::value_type entry;
149  Deserialize (buff, entry);
150  (* m_pMemDst) [index] = TOp::op ((* m_pMemDst) [index], entry);
151  }
152  return true;
153  }
154 
155 private:
156 // pointer to current vector
157  TArray* m_pMemDst;
158  const TArray* m_pMemSrc;
159 };
160 
167 template <typename TArray, typename TLayout>
169 {
172 
173 public:
174  typedef TArray array_type;
176  typedef typename base_type::Layout Layout;
177  typedef typename base_type::Interface Interface;
178 
179 public:
181  ComPol_MemCopy () : m_pMemDst(NULL), m_pMemSrc(NULL) {}
182 
184  ComPol_MemCopy (TArray* pVec): m_pMemDst(pVec), m_pMemSrc(pVec) {}
185 
187  ComPol_MemCopy (TArray* pMemDst, const TArray* pMemSrc)
188  : m_pMemDst(pMemDst), m_pMemSrc(pMemSrc) {}
189 
191  void set_src (const TArray* pMem) {m_pMemSrc = pMem;}
192 
194  void set_dst (TArray* pMemDst) {m_pMemDst = pMemDst;}
195 
197 
206  virtual int get_required_buffer_size (const Interface& interface)
207  {
208  return interface.size () * sizeof (typename TArray::value_type);
209  }
210 
212 
219  virtual bool collect (ug::BinaryBuffer& buff, const Interface& interface)
220  {
221  // check that vector has been set
222  if (m_pMemSrc == NULL) return false;
223 
224  // loop interface
225  for (typename Interface::const_iterator iter = interface.begin ();
226  iter != interface.end (); ++iter)
227  {
228  // get index
229  const size_t index = interface.get_element (iter);
230 
231  // write entry into buffer
232  Serialize (buff, (* m_pMemSrc) [index]);
233  }
234  return true;
235  }
236 
238 
244  virtual bool extract (ug::BinaryBuffer& buff, const Interface& interface)
245  {
246  // check that vector has been set
247  if(m_pMemDst == NULL) return false;
248 
249  // loop interface
250  for (typename Interface::const_iterator iter = interface.begin ();
251  iter != interface.end (); ++iter)
252  {
253  // get index
254  const size_t index = interface.get_element (iter);
255 
256  // write entry into array
257  Deserialize (buff, (* m_pMemDst) [index]);
258  }
259  return true;
260  }
261 
262 private:
263 // pointer to current vector
264  TArray* m_pMemDst;
265  const TArray* m_pMemSrc;
266 };
267 
274 template <typename TArray, typename TLayout, typename TOp>
276 (
277  TArray* pMem,
278  const TLayout& masterLayout,
279  const TLayout& slaveLayout,
281 )
282 {
283 // create a new communicator if required
285  pcl::InterfaceCommunicator<TLayout>& com = (!pCom)? tCom : *pCom;
286 
287 // STEP 1: Reduce slave values on the master
288 
289  // create the required communication policies
290  ComPol_MemOp<TArray, TLayout, TOp> cpMemOp (pMem);
291 
292  // perform communication
293  com.send_data (slaveLayout, cpMemOp);
294  com.receive_data (masterLayout, cpMemOp);
295  com.communicate ();
296 
297 // STEP 2: copy master values to slaves
298 
299  // create the required communication policies
300  ComPol_MemCopy<TArray, TLayout> cpMemCopy (pMem);
301 
302  // perform communication
303  com.send_data (masterLayout, cpMemCopy);
304  com.receive_data (slaveLayout, cpMemCopy);
305  com.communicate ();
306 };
307 
308 } // end namespace Electromagnetism
309 } // end namespace ug
310 
311 #endif // __H__UG__PLUGINS__ELECTROMAGNETISM__MEM_REDUCE__
312 
313 /* End of File */
bool communicate(int tag=749345)
void send_data(const Layout &layout, ICommunicationPolicy< TLayout > &commPol)
void receive_data(const Layout &layout, ICommunicationPolicy< TLayout > &commPol)
Definition: memreduce.h:169
const TArray * m_pMemSrc
Definition: memreduce.h:265
base_type::Interface Interface
Definition: memreduce.h:177
pcl::ICommunicationPolicy< TLayout > base_type
Definition: memreduce.h:171
ComPol_MemCopy(TArray *pMemDst, const TArray *pMemSrc)
Constructor setting the arrays.
Definition: memreduce.h:187
ComPol_MemCopy< TArray, TLayout > this_type
Definition: memreduce.h:170
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: memreduce.h:219
ComPol_MemCopy()
Default constructor.
Definition: memreduce.h:181
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
writes values from a buffer into the interface
Definition: memreduce.h:244
TArray * m_pMemDst
Definition: memreduce.h:264
ComPol_MemCopy(TArray *pVec)
Constructor setting the same arrays for the source and the destination.
Definition: memreduce.h:184
void set_src(const TArray *pMem)
set the source array
Definition: memreduce.h:191
array_type::value_type value_type
Definition: memreduce.h:175
TArray array_type
Definition: memreduce.h:174
virtual int get_required_buffer_size(const Interface &interface)
returns the buffer size
Definition: memreduce.h:206
void set_dst(TArray *pMemDst)
set the destination
Definition: memreduce.h:194
base_type::Layout Layout
Definition: memreduce.h:176
Definition: memreduce.h:60
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
writes values from a buffer into the interface
Definition: memreduce.h:135
const TArray * m_pMemSrc
Definition: memreduce.h:158
void set_dst(TArray *pMemDst)
set the destination
Definition: memreduce.h:85
base_type::Interface Interface
Definition: memreduce.h:68
ComPol_MemOp()
Default constructor.
Definition: memreduce.h:72
pcl::ICommunicationPolicy< TLayout > base_type
Definition: memreduce.h:62
void set_src(const TArray *pMem)
set the source array
Definition: memreduce.h:82
virtual int get_required_buffer_size(const Interface &interface)
returns the buffer size
Definition: memreduce.h:97
ComPol_MemOp(TArray *pVec)
Constructor setting the same arrays for the source and the destination.
Definition: memreduce.h:75
TArray array_type
Definition: memreduce.h:65
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: memreduce.h:110
TArray * m_pMemDst
Definition: memreduce.h:157
ComPol_MemOp(TArray *pMemDst, const TArray *pMemSrc)
Constructor setting the arrays.
Definition: memreduce.h:78
array_type::value_type value_type
Definition: memreduce.h:66
base_type::Layout Layout
Definition: memreduce.h:67
ComPol_MemOp< TArray, TLayout, TOp > this_type
Definition: memreduce.h:61
value_type & entry(std::size_t row, std::size_t col)
T value_type
void MemAllReduce(TArray *pMem, const TLayout &masterLayout, const TLayout &slaveLayout, pcl::InterfaceCommunicator< TLayout > *pCom=NULL)
Definition: memreduce.h:276
void Serialize(std::ostream &buff, const DenseMatrix< VariableArray2< T > > &mat)
void Deserialize(std::istream &buff, const DenseMatrix< VariableArray2< T > > &mat)