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