00001 #ifdef CH_LANG_CC
00002
00003
00004
00005
00006
00007
00008
00009 #endif
00010
00011 #ifndef _COPIER_H_
00012 #define _COPIER_H_
00013
00014 #include "DisjointBoxLayout.H"
00015 #include "Pool.H"
00016 #include "Vector.H"
00017 #include "ProblemDomain.H"
00018 #include <unordered_map>
00019 #include <cstdint>
00020 #include "NamespaceHeader.H"
00021
00022
00023 typedef std::unordered_map<uint64_t, LayoutIndex> LMap;
00024
00025
00026 class CopyIterator;
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 class MotionItem
00037 {
00038 public:
00039 DataIndex fromIndex, toIndex;
00040 Box fromRegion;
00041 Box toRegion;
00042 int procID;
00043
00044 MotionItem(const DataIndex& a_from,
00045 const DataIndex& a_to,
00046 const Box& a_region);
00047 MotionItem(const DataIndex& a_from,
00048 const DataIndex& a_to,
00049 const Box& a_fromRegion,
00050 const Box& a_toRegion);
00051 void reverse();
00052
00053 bool operator==(const MotionItem& rhs)
00054 { return fromIndex == rhs.fromIndex && toIndex == rhs.toIndex &&
00055 fromRegion == rhs.fromRegion && toRegion == rhs.toRegion &&
00056 procID == rhs.procID;}
00057 };
00058
00059 class CopierBuffer
00060 {
00061 public:
00062
00063
00064 CopierBuffer():m_ncomps(0), m_sendbuffer(NULL), m_sendcapacity(0),
00065 m_recbuffer(NULL), m_reccapacity(0)
00066 {}
00067
00068
00069 virtual ~CopierBuffer();
00070
00071 void clear();
00072
00073 bool isDefined(int ncomps) const
00074 { return ncomps == m_ncomps;}
00075
00076 mutable int m_ncomps;
00077
00078 mutable void* m_sendbuffer;
00079
00080 mutable size_t m_sendcapacity;
00081 mutable void* m_recbuffer;
00082
00083 mutable size_t m_reccapacity;
00084
00085 #ifndef DOXYGEN
00086
00087 struct bufEntry
00088 {
00089 void* bufPtr;
00090 size_t size;
00091 const MotionItem* item;
00092 unsigned int procID;
00093 bool operator < (const bufEntry& rhs) const
00094 {
00095 if (procID == rhs.procID)
00096 {
00097 const Box& left = item->toRegion;
00098 const Box& right= rhs.item->toRegion;
00099 if (left.smallEnd() == right.smallEnd())
00100 {
00101 return left.bigEnd().lexLT(right.bigEnd());
00102 }
00103 else
00104 {
00105 return item->toRegion < rhs.item->toRegion;
00106 }
00107 }
00108
00109 return procID < rhs.procID;
00110 }
00111 };
00112
00113 #endif
00114 mutable std::vector<bufEntry> m_fromMe;
00115 mutable std::vector<bufEntry> m_toMe;
00116 mutable std::vector<std::vector<bufEntry> > m_toMeUnpack;
00117
00118
00119 protected:
00120
00121 };
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 class Copier
00146 {
00147 public:
00148
00149
00150 Copier():m_isDefined(false)
00151 {}
00152
00153 Copier(const Copier& a_rhs);
00154
00155
00156 Copier(const DisjointBoxLayout& a_level,
00157 const BoxLayout& a_dest,
00158 bool a_exchange = false,
00159 IntVect a_shift = IntVect::Zero);
00160
00161
00162 Copier(const DisjointBoxLayout& a_level,
00163 const BoxLayout& a_dest,
00164 const ProblemDomain& a_domain,
00165 bool a_exchange = false,
00166 IntVect a_shift = IntVect::Zero);
00167
00168
00169 Copier(const DisjointBoxLayout& a_level,
00170 const BoxLayout& a_dest,
00171 const IntVect& a_destGhost,
00172 bool a_exchange = false,
00173 IntVect a_shift = IntVect::Zero);
00174
00175
00176 Copier(const DisjointBoxLayout& a_level,
00177 const BoxLayout& a_dest,
00178 const ProblemDomain& a_domain,
00179 const IntVect& a_destGhost,
00180 bool a_exchange = false,
00181 IntVect a_shift = IntVect::Zero);
00182
00183
00184 virtual ~Copier();
00185
00186
00187 virtual void define(const DisjointBoxLayout& a_level,
00188 const BoxLayout& a_dest,
00189 bool a_exchange = false,
00190 IntVect a_shift = IntVect::Zero);
00191
00192
00193 virtual void define(const DisjointBoxLayout& a_level,
00194 const BoxLayout& a_dest,
00195 const ProblemDomain& a_domain,
00196 bool a_exchange = false,
00197 IntVect a_shift = IntVect::Zero);
00198
00199
00200 virtual void define(const DisjointBoxLayout& a_level,
00201 const BoxLayout& a_dest,
00202 const IntVect& a_destGhost,
00203 bool a_exchange = false,
00204 IntVect a_shift = IntVect::Zero);
00205
00206
00207 virtual void define(const BoxLayout& a_level,
00208 const BoxLayout& a_dest,
00209 const ProblemDomain& a_domain,
00210 const IntVect& a_destGhost,
00211 bool a_exchange = false,
00212 IntVect a_shift = IntVect::Zero);
00213
00214
00215 virtual void exchangeDefine(const DisjointBoxLayout& a_grids,
00216 const IntVect& a_ghost,
00217 bool a_includeSelf=false);
00218
00219 void defineFixedBoxSize(const DisjointBoxLayout& a_src,
00220 const LMap& a_lmap,
00221 const IntVect& a_ghost,
00222 const ProblemDomain& a_domain,
00223 bool a_includeSelf=false,
00224 bool a_reverse=false);
00225
00226 void defineFixedSizeNodesCollect(const DisjointBoxLayout& a_layout,
00227 const LMap& a_lmap,
00228 const IntVect& a_ghostSrc,
00229 const IntVect& a_ghostDst,
00230 const ProblemDomain& a_domain);
00231
00232
00233
00234
00235
00236
00237 void ghostDefine(const DisjointBoxLayout& a_src,
00238 const DisjointBoxLayout& a_dest,
00239 const ProblemDomain& a_domain,
00240 const IntVect& a_srcGhost);
00241
00242
00243
00244
00245
00246
00247
00248
00249 void ghostDefine(const DisjointBoxLayout& a_src,
00250 const DisjointBoxLayout& a_dest,
00251 const ProblemDomain& a_domain,
00252 const IntVect& a_srcGhost,
00253 const IntVect& a_destGhost);
00254
00255
00256 virtual void clear();
00257
00258
00259
00260
00261
00262
00263 Copier& operator= (const Copier& b);
00264
00265
00266
00267
00268
00269 void reverse();
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 void trimEdges(const DisjointBoxLayout& a_exchangedLayout, const IntVect& ghost);
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 void coarsen(int a_refRatio);
00291
00292
00293
00294
00295 bool operator==(const Copier& rhs) const;
00296
00297 int print() const;
00298
00299 bool bufferAllocated() const;
00300 void setBufferAllocated(bool arg) const;
00301
00302 int numLocalCellsToCopy() const;
00303 int numFromCellsToCopy() const;
00304 int numToCellsToCopy() const;
00305
00306 bool isDefined() const
00307 { return m_isDefined;}
00308
00309 CopierBuffer m_buffers;
00310
00311 std::vector<IndexTM<int,2> > m_range;
00312 protected:
00313
00314 friend class CopyIterator;
00315
00316
00317 Vector<MotionItem*> m_localMotionPlan;
00318 Vector<MotionItem*> m_fromMotionPlan;
00319 Vector<MotionItem*> m_toMotionPlan;
00320
00321 friend void dumpmemoryatexit();
00322 static Pool s_motionItemPool;
00323 mutable bool buffersAllocated;
00324
00325
00326
00327 DisjointBoxLayout m_originPlan;
00328 BoxLayout m_dest;
00329
00330 bool m_isDefined;
00331
00332 void trimMotion(const DisjointBoxLayout& a_exchangedLayout, const IntVect& a_ghost,
00333 const Vector<MotionItem*>& a_oldItems, Vector<MotionItem*>& a_newItems);
00334
00335 void sort();
00336
00337
00338 const ProblemDomain& getPhysDomain(const DisjointBoxLayout& a_level) const;
00339 };
00340
00341 std::ostream& operator<<(std::ostream& os, const Copier& copier);
00342
00343
00344
00345
00346
00347 inline MotionItem::MotionItem(const DataIndex& a_from,
00348 const DataIndex& a_to,
00349 const Box& a_region)
00350 :fromIndex(a_from), toIndex(a_to), fromRegion(a_region),
00351 toRegion(a_region), procID(-1)
00352 {
00353 }
00354
00355 inline MotionItem::MotionItem(const DataIndex& a_from,
00356 const DataIndex& a_to,
00357 const Box& a_fromRegion,
00358 const Box& a_toRegion)
00359 :fromIndex(a_from), toIndex(a_to), fromRegion(a_fromRegion),
00360 toRegion(a_toRegion), procID(-1)
00361 {
00362 }
00363
00364 inline
00365 void MotionItem::reverse()
00366 {
00367 Box tmp(fromRegion);
00368 fromRegion=toRegion;
00369 toRegion=tmp;
00370 DataIndex tmpIndex(fromIndex);
00371 fromIndex = toIndex;
00372 toIndex = tmpIndex;
00373 }
00374
00375 class CopyIterator
00376 {
00377 public:
00378 enum local_from_to
00379 {
00380 LOCAL,
00381 FROM,
00382 TO
00383 };
00384
00385 inline CopyIterator(const Copier& a_copier, local_from_to);
00386
00387 inline const MotionItem& operator()() const;
00388
00389 inline const MotionItem& operator[](size_t index) const;
00390
00391 inline void operator++();
00392
00393 inline bool ok() const;
00394
00395 inline void reset();
00396
00397 inline size_t size(){ return m_motionplanPtr->size();}
00398
00399 private:
00400 const Vector<MotionItem*>* m_motionplanPtr;
00401 unsigned int m_current;
00402 };
00403
00404
00405
00406 inline CopyIterator::CopyIterator(const Copier& a_copier, local_from_to type)
00407 :m_current(0)
00408 {
00409 switch(type)
00410 {
00411 case LOCAL:
00412 m_motionplanPtr = &(a_copier.m_localMotionPlan);
00413 break;
00414 case FROM:
00415 m_motionplanPtr = &(a_copier.m_fromMotionPlan);
00416 break;
00417 case TO:
00418 m_motionplanPtr = &(a_copier.m_toMotionPlan);
00419 break;
00420 default:
00421 MayDay::Error("illegal local_from_to option for CopyIterator");
00422 }
00423 }
00424
00425 inline const MotionItem& CopyIterator::operator()() const
00426 {
00427 CH_assert(m_current < m_motionplanPtr->size());
00428 return *(m_motionplanPtr->operator[](m_current));
00429 }
00430
00431 inline const MotionItem& CopyIterator::operator[](size_t a_index) const
00432 {
00433 CH_assert(a_index < m_motionplanPtr->size());
00434 return *(m_motionplanPtr->operator[](a_index));
00435 }
00436
00437 inline void CopyIterator::operator++()
00438 {
00439 ++m_current;
00440 }
00441
00442 inline bool CopyIterator::ok() const
00443 {
00444 return m_current < m_motionplanPtr->size();
00445 }
00446
00447 inline void CopyIterator::reset()
00448 {
00449 m_current = 0;
00450 }
00451
00452 #include "NamespaceFooter.H"
00453 #endif