dune-grid-glue  2.3.0
gridgluecommunicate.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_GRIDGLUE_ADAPTER_GRIDGLUECOMMUNICATE_HH
4 #define DUNE_GRIDGLUE_ADAPTER_GRIDGLUECOMMUNICATE_HH
5 
11 #include <dune/common/version.hh>
12 #include <dune/common/bartonnackmanifcheck.hh>
13 #include <dune/common/parallel/communicator.hh>
14 #include <dune/grid/common/datahandleif.hh>
15 #include <dune/grid/common/gridenums.hh>
16 
17 
18 namespace Dune {
19  namespace GridGlue {
20 
21  typedef std::pair<int, int> RankPair;
22  struct GlobalId : public std::pair<RankPair, unsigned int>
23  {
28  this->first.first = 0;
29  this->first.second = 0;
30  this->second = 0;
31  }
35  GlobalId(int i) {
36  this->first.first = i;
37  this->first.second = i;
38  this->second = 0;
39  }
45  GlobalId(int i, int j, unsigned int n) {
46  this->first.first = std::min(i,j);
47  this->first.second = std::max(i,j);
48  this->second = n;
49  }
50  };
51 
52  inline std::ostream& operator<<(std::ostream& os, const GlobalId & id)
53  {
54  os << "("
55  << id.first.first << "," << id.first.second << ","
56  << id.second << ")";
57  return os;
58  }
59 
72  template <class DataHandleImp, class DataTypeImp>
74  {
75  public:
77  typedef DataTypeImp DataType;
78 
79  protected:
80  // one should not create an explicit instance of this inteface object
82 
83  public:
84 
88  template<class RISType>
89  size_t size (RISType& i) const
90  {
91  CHECK_INTERFACE_IMPLEMENTATION((asImp().size(i)));
92  return asImp().size(i);
93  }
94 
100  template<class MessageBufferImp, class EntityType, class RISType>
101  void gather (MessageBufferImp& buff, const EntityType& e, const RISType & i) const
102  {
103  MessageBufferIF<MessageBufferImp> buffIF(buff);
104  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION((asImp().gather(buffIF,e,i)));
105  }
106 
114  template<class MessageBufferImp, class EntityType, class RISType>
115  void scatter (MessageBufferImp& buff, const EntityType& e, const RISType & i, size_t n)
116  {
117  MessageBufferIF<MessageBufferImp> buffIF(buff);
118  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION((asImp().scatter(buffIF,e,i,n)));
119  }
120 
121  private:
123  DataHandleImp& asImp () {
124  return static_cast<DataHandleImp &> (*this);
125  }
127  const DataHandleImp& asImp () const
128  {
129  return static_cast<const DataHandleImp &>(*this);
130  }
131  }; // end class CommDataHandleIF
132 
137  template<typename DT>
139  public:
140  typedef DT value_type;
141 
142  // Constructor
144  {
145  a=p;
146  i=0;
147  j=0;
148  }
149 
150  // write data to message buffer, acts like a stream !
151  template<class Y>
152  void write (const Y& data)
153  {
154  static_assert(( is_same<DT,Y>::value ), "DataType mismatch");
155  a[i++] = data;
156  }
157 
158  // read data from message buffer, acts like a stream !
159  template<class Y>
160  void read (Y& data) const
161  {
162  static_assert(( is_same<DT,Y>::value ), "DataType mismatch");
163  data = a[j++];
164  }
165 
166  size_t counter() const { return i; }
167 
168  void clear()
169  {
170  i = 0;
171  j = 0;
172  }
173 
174  // we need access to these variables in an assertion
175 #ifdef NDEBUG
176  private:
177 #endif
178  DT *a;
179  size_t i;
180  mutable size_t j;
181  };
182 
189  template<int dir>
191  {
192  public:
193  template<class CommInfo>
194  static const typename CommInfo::DataType& gather(const CommInfo& commInfo, size_t i, size_t j = 0)
195  {
196  // get Intersection
197  typedef typename CommInfo::GridGlue::Intersection Intersection;
198  Intersection ris(commInfo.gridglue->getIntersection(i));
199 
200  // fill buffer if we have a new intersection
201  if (j == 0)
202  {
203  commInfo.mbuffer.clear();
204  if (dir == Dune::ForwardCommunication)
205  {
206  // read from grid0
207  if(ris.self())
208  commInfo.data->gather(commInfo.mbuffer, ris.inside(), ris);
209  }
210  else // (dir == Dune::BackwardCommunication)
211  {
212  // read from grid1
213  if(ris.neighbor())
214  commInfo.data->gather(commInfo.mbuffer, ris.outside(), ris.flip());
215  }
216  }
217 
218  // return the j'th value in the buffer
219  assert(j < commInfo.mbuffer.i);
220  return commInfo.buffer[j];
221  }
222 
223  template<class CommInfo>
224  static void scatter(CommInfo& commInfo, const typename CommInfo::DataType& v, std::size_t i, std::size_t j = 0)
225  {
226  // extract GridGlue objects...
227  typedef typename CommInfo::GridGlue::Intersection Intersection;
228  Intersection ris(commInfo.gridglue->getIntersection(i));
229 
230  // get size if we have a new intersection
231  if (j == 0)
232  {
233  commInfo.mbuffer.clear();
234  commInfo.currentsize = commInfo.data->size(ris);
235  }
236 
237  // write entry to buffer
238  commInfo.buffer[j] = v;
239 
240  // write back the buffer if we are at the end of this intersection
241  if (j == commInfo.currentsize-1)
242  {
243  if (dir == Dune::ForwardCommunication)
244  {
245  // write to grid1
246  if(ris.neighbor())
247  commInfo.data->scatter(commInfo.mbuffer, ris.outside(), ris.flip(), commInfo.currentsize);
248  }
249  else // (dir == Dune::BackwardCommunication)
250  {
251  // write to grid0
252  if(ris.self())
253  commInfo.data->scatter(commInfo.mbuffer, ris.inside(), ris, commInfo.currentsize);
254  }
255  assert(commInfo.mbuffer.j <= commInfo.currentsize);
256  }
257  }
258  };
259 
262 
267  template <typename GG, class DataHandleImp, class DataTypeImp>
268  struct CommInfo
269  {
270  typedef DataTypeImp value_type;
271  typedef GG GridGlue;
272  typedef DataTypeImp DataType;
273 
274  CommInfo() : buffer(100), mbuffer(&buffer[0])
275  {}
276 
277  // tunnel information to the policy and the operators
278  const GridGlue * gridglue;
280 
281  // state variables
282  std::vector<DataType> buffer;
283  mutable ::Dune::GridGlue::StreamingMessageBuffer<DataType> mbuffer;
284  size_t currentsize;
285  Dune::CommunicationDirection dir;
286  };
287 
288  } // end namespace GridGlue
289 
290 #if HAVE_MPI
291 
295  template<typename GG, class DataHandleImp, class DataTypeImp>
296  struct CommPolicy< ::Dune::GridGlue::CommInfo<GG, DataHandleImp, DataTypeImp> >
297  {
301  typedef ::Dune::GridGlue::CommInfo<GG, DataHandleImp, DataTypeImp> Type;
302 
306  typedef DataTypeImp IndexedType;
307 
311  // typedef SizeOne IndexedTypeFlag;
312  typedef VariableSize IndexedTypeFlag;
313 
317  static size_t getSize(const Type& commInfo, size_t i)
318  {
319  // get Intersection
320  typedef typename Type::GridGlue::Intersection Intersection;
321  Intersection ris(commInfo.gridglue->getIntersection(i));
322 
323  // ask data handle for size
324  return commInfo.data->size(ris);
325  }
326  };
327 #endif
328 
329 } // end namespace Dune
330 #endif
DataTypeImp DataType
Definition: gridgluecommunicate.hh:272
Dune::CommunicationDirection dir
Definition: gridgluecommunicate.hh:285
void scatter(MessageBufferImp &buff, const EntityType &e, const RISType &i, size_t n)
Definition: gridgluecommunicate.hh:115
size_t counter() const
Definition: gridgluecommunicate.hh:166
const GridGlue * gridglue
Definition: gridgluecommunicate.hh:278
CommInfo()
Definition: gridgluecommunicate.hh:274
void gather(MessageBufferImp &buff, const EntityType &e, const RISType &i) const
pack data from user to message buffer
Definition: gridgluecommunicate.hh:101
describes the features of a data handle for communication in parallel runs using the GridGlue::commun...
Definition: gridgluecommunicate.hh:73
DataTypeImp IndexedType
The datatype that should be communicated.
Definition: gridgluecommunicate.hh:306
size_t currentsize
Definition: gridgluecommunicate.hh:284
GlobalId()
Definition: gridgluecommunicate.hh:27
std::vector< DataType > buffer
Definition: gridgluecommunicate.hh:282
static void scatter(CommInfo &commInfo, const typename CommInfo::DataType &v, std::size_t i, std::size_t j=0)
Definition: gridgluecommunicate.hh:224
std::ostream & operator<<(std::ostream &os, const GlobalId &id)
Definition: gridgluecommunicate.hh:52
DataTypeImp DataType
data type of data to communicate
Definition: gridgluecommunicate.hh:77
DT * a
Definition: gridgluecommunicate.hh:178
size_t j
Definition: gridgluecommunicate.hh:180
Definition: gridglue.hh:34
::Dune::GridGlue::CommInfo< GG, DataHandleImp, DataTypeImp > Type
The type of the GridGlueCommInfo.
Definition: gridgluecommunicate.hh:301
CommDataHandle()
Definition: gridgluecommunicate.hh:81
Definition: gridgluecommunicate.hh:22
forward gather scatter to user defined CommInfo class
Definition: gridgluecommunicate.hh:190
DataTypeImp value_type
Definition: gridgluecommunicate.hh:270
DT value_type
Definition: gridgluecommunicate.hh:140
::Dune::GridGlue::CommDataHandle< DataHandleImp, DataTypeImp > * data
Definition: gridgluecommunicate.hh:279
size_t size(RISType &i) const
Definition: gridgluecommunicate.hh:89
Definition: gridgluecommunicate.hh:138
mutable::Dune::GridGlue::StreamingMessageBuffer< DataType > mbuffer
Definition: gridgluecommunicate.hh:283
CommunicationOperator< Dune::BackwardCommunication > BackwardOperator
Definition: gridgluecommunicate.hh:261
StreamingMessageBuffer(DT *p)
Definition: gridgluecommunicate.hh:143
void write(const Y &data)
Definition: gridgluecommunicate.hh:152
std::pair< int, int > RankPair
Definition: gridgluecommunicate.hh:21
GlobalId(int i)
Definition: gridgluecommunicate.hh:35
CommunicationOperator< Dune::ForwardCommunication > ForwardOperator
Definition: gridgluecommunicate.hh:260
The intersection of two entities of the two patches of a GridGlue.
Definition: gridglue.hh:51
GG GridGlue
Definition: gridgluecommunicate.hh:271
void read(Y &data) const
Definition: gridgluecommunicate.hh:160
GlobalId(int i, int j, unsigned int n)
Definition: gridgluecommunicate.hh:45
size_t i
Definition: gridgluecommunicate.hh:179
static const CommInfo::DataType & gather(const CommInfo &commInfo, size_t i, size_t j=0)
Definition: gridgluecommunicate.hh:194
VariableSize IndexedTypeFlag
Each intersection can communicate a different number of objects.
Definition: gridgluecommunicate.hh:312
static size_t getSize(const Type &commInfo, size_t i)
Get the number of objects at an intersection.
Definition: gridgluecommunicate.hh:317
void clear()
Definition: gridgluecommunicate.hh:168
collects all GridGlue data requried for communication
Definition: gridgluecommunicate.hh:268