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 public:
00032 DataIndex fromIndex, toIndex;
00033 Box fromRegion;
00034 Box toRegion;
00035 int procID;
00036
00037 MotionItem(const DataIndex& a_from,
00038 const DataIndex& a_to,
00039 const Box& a_region);
00040 MotionItem(const DataIndex& a_from,
00041 const DataIndex& a_to,
00042 const Box& a_fromRegion,
00043 const Box& a_toRegion);
00044 void reverse();
00045
00046 };
00047
00048
00049 class CopierBuffer
00050 {
00051 public:
00052
00054 CopierBuffer():m_ncomps(0), m_sendbuffer(NULL), m_sendcapacity(0),
00055 m_recbuffer(NULL), m_reccapacity(0)
00056 {}
00057
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
00076 #ifndef DOXYGEN
00077
00078 struct bufEntry
00079 {
00080 void* bufPtr;
00081 size_t size;
00082 const MotionItem* item;
00083 unsigned int procID;
00084 bool operator < (const bufEntry& rhs) const
00085 {
00086 if(procID == rhs.procID)
00087 {
00088 const Box& left = item->toRegion;
00089 const Box& right= rhs.item->toRegion;
00090 if(left.smallEnd() == right.smallEnd())
00091 {
00092 return left.bigEnd().lexLT(right.bigEnd());
00093 }
00094 else
00095 {
00096 return item->toRegion < rhs.item->toRegion;
00097 }
00098 }
00099
00100 return procID < rhs.procID;
00101 }
00102 };
00103
00104 #endif
00105 mutable std::vector<bufEntry> m_fromMe;
00106 mutable std::vector<bufEntry> m_toMe;
00107
00108
00109 protected:
00110
00111 };
00112
00113
00115
00136 class Copier
00137 {
00138 public:
00139
00141 Copier():m_isDefined(false)
00142 {}
00143
00144 Copier(const Copier& a_rhs);
00145
00147 Copier(const DisjointBoxLayout& a_level, const BoxLayout& a_dest,
00148 bool a_exchange = false);
00149
00151 Copier(const DisjointBoxLayout& a_level, const BoxLayout& a_dest,
00152 const ProblemDomain& a_domain,
00153 bool a_exchange = false);
00154
00156 Copier(const DisjointBoxLayout& a_level,
00157 const BoxLayout& a_dest,
00158 const IntVect& a_destGhost,
00159 bool a_exchange = false);
00160
00162 Copier(const DisjointBoxLayout& a_level,
00163 const BoxLayout& a_dest,
00164 const ProblemDomain& a_domain,
00165 const IntVect& a_destGhost,
00166 bool a_exchange = false);
00167
00169 virtual ~Copier();
00170
00172 virtual void define(const DisjointBoxLayout& a_level, const BoxLayout& a_dest,
00173 bool a_exchange = false);
00174
00176 virtual void define(const DisjointBoxLayout& a_level,
00177 const BoxLayout& a_dest,
00178 const ProblemDomain& a_domain,
00179 bool a_exchange = false);
00180
00182 virtual void define(const DisjointBoxLayout& a_level,
00183 const BoxLayout& a_dest,
00184 const IntVect& a_destGhost,
00185 bool a_exchange = false);
00186
00188 virtual void define(const BoxLayout& a_level,
00189 const BoxLayout& a_dest,
00190 const ProblemDomain& a_domain,
00191 const IntVect& a_destGhost,
00192 bool a_exchange = false);
00193
00195 virtual void exchangeDefine(const DisjointBoxLayout& a_grids,
00196 const IntVect& a_ghost);
00197
00199
00203 void ghostDefine(const DisjointBoxLayout& a_src,
00204 const DisjointBoxLayout& a_dest,
00205 const ProblemDomain& a_domain,
00206 const IntVect& a_srcGhost);
00208 virtual void clear();
00209
00211
00215 Copier& operator= (const Copier& b);
00216
00217
00219
00222 void reverse();
00223
00225
00232 void trimEdges(const DisjointBoxLayout& a_exchangedLayout, const IntVect& ghost);
00233
00235
00236
00237
00238
00239
00240
00241
00242
00243 void coarsen(int a_refRatio);
00244
00245 int print() const;
00246
00247 bool bufferAllocated() const;
00248 void setBufferAllocated(bool arg) const;
00249
00250 int numLocalCellsToCopy() const;
00251 int numFromCellsToCopy() const;
00252 int numToCellsToCopy() const;
00253
00254 bool isDefined() const
00255 { return m_isDefined;}
00256
00257 CopierBuffer m_buffers;
00258
00259 protected:
00260
00261 friend class CopyIterator;
00262
00263 Vector<MotionItem*> m_localMotionPlan;
00264 Vector<MotionItem*> m_fromMotionPlan;
00265 Vector<MotionItem*> m_toMotionPlan;
00266
00267 friend void dumpmemoryatexit();
00268 static Pool s_motionItemPool;
00269 mutable bool buffersAllocated;
00270
00271
00272
00273 DisjointBoxLayout m_originPlan;
00274 BoxLayout m_dest;
00275
00276 bool m_isDefined;
00277
00278 void trimMotion(const DisjointBoxLayout& a_exchangedLayout, const IntVect& a_ghost,
00279 const Vector<MotionItem*>& a_oldItems, Vector<MotionItem*>& a_newItems);
00280
00281 void sort();
00282
00283
00284
00285 const ProblemDomain& getPhysDomain(const DisjointBoxLayout& a_level) const;
00286 };
00287
00288 std::ostream& operator<<(std::ostream& os, const Copier& copier);
00289
00290
00291
00292
00293
00294
00295 inline MotionItem::MotionItem(const DataIndex& a_from,
00296 const DataIndex& a_to,
00297 const Box& a_region)
00298 :fromIndex(a_from), toIndex(a_to), fromRegion(a_region),
00299 toRegion(a_region), procID(-1)
00300 {}
00301
00302 inline MotionItem::MotionItem(const DataIndex& a_from,
00303 const DataIndex& a_to,
00304 const Box& a_fromRegion,
00305 const Box& a_toRegion)
00306 :fromIndex(a_from), toIndex(a_to), fromRegion(a_fromRegion),
00307 toRegion(a_toRegion), procID(-1)
00308 {}
00309
00310 inline
00311 void MotionItem::reverse()
00312 {
00313 Box tmp(fromRegion);
00314 fromRegion=toRegion;
00315 toRegion=tmp;
00316 DataIndex tmpIndex(fromIndex);
00317 fromIndex = toIndex;
00318 toIndex = tmpIndex;
00319 }
00320
00321 class CopyIterator
00322 {
00323 public:
00324 enum local_from_to {LOCAL, FROM, TO};
00325
00326 inline CopyIterator(const Copier& a_copier, local_from_to);
00327
00328 inline const MotionItem& operator()() const;
00329
00330 inline void operator++();
00331
00332 inline bool ok() const;
00333
00334 inline void reset();
00335 private:
00336 const Vector<MotionItem*>* m_motionplanPtr;
00337 unsigned int m_current;
00338 };
00339
00340
00341
00342 inline CopyIterator::CopyIterator(const Copier& a_copier, local_from_to type)
00343 :m_current(0)
00344 {
00345 switch(type){
00346 case LOCAL:
00347 m_motionplanPtr = &(a_copier.m_localMotionPlan);
00348 break;
00349 case FROM:
00350 m_motionplanPtr = &(a_copier.m_fromMotionPlan);
00351 break;
00352 case TO:
00353 m_motionplanPtr = &(a_copier.m_toMotionPlan);
00354 break;
00355 default:
00356 MayDay::Error("illegal local_from_to option for CopyIterator");
00357 }
00358 }
00359
00360 inline const MotionItem& CopyIterator::operator()() const
00361 {
00362 CH_assert(m_current < m_motionplanPtr->size());
00363 return *(m_motionplanPtr->operator[](m_current));
00364 }
00365
00366 inline void CopyIterator::operator++() {++m_current;}
00367
00368 inline bool CopyIterator::ok() const
00369 {
00370 return m_current < m_motionplanPtr->size();
00371 }
00372
00373 inline void CopyIterator::reset()
00374 {
00375 m_current = 0;
00376 }
00377
00378 #include "NamespaceFooter.H"
00379 #endif