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