Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

BaseFabImplem.H

Go to the documentation of this file.
00001 /* _______              __
00002   / ___/ /  ___  __ _  / /  ___
00003  / /__/ _ \/ _ \/  ' \/ _ \/ _ \
00004  \___/_//_/\___/_/_/_/_.__/\___/ 
00005 */
00006 //
00007 // This software is copyright (C) by the Lawrence Berkeley
00008 // National Laboratory.  Permission is granted to reproduce
00009 // this software for non-commercial purposes provided that
00010 // this notice is left intact.
00011 // 
00012 // It is acknowledged that the U.S. Government has rights to
00013 // this software under Contract DE-AC03-765F00098 between
00014 // the U.S.  Department of Energy and the University of
00015 // California.
00016 //
00017 // This software is provided as a professional and academic
00018 // contribution for joint exchange. Thus it is experimental,
00019 // is provided ``as is'', with no warranties of any kind
00020 // whatsoever, no support, no promise of updates, or printed
00021 // documentation. By using this software, you acknowledge
00022 // that the Lawrence Berkeley National Laboratory and
00023 // Regents of the University of California shall have no
00024 // liability with respect to the infringement of other
00025 // copyrights by any part of this software.
00026 //
00027 
00028 #ifndef CH_BASEFABIMPLEM_H
00029 #define CH_BASEFABIMPLEM_H
00030 #include "BaseFabMacros.H"
00031 
00032 #include "MayDay.H"
00033 //
00034 // Implementation.=====================================
00035 //
00036 
00037 template <class T>
00038 Arena* BaseFab<T>::s_Arena = NULL;
00039 
00040 template <class T>
00041 inline
00042 int
00043 BaseFab<T>::nComp () const
00044 {
00045   return nvar;
00046 }
00047 
00048 template <class T>
00049 inline
00050 const Box&
00051 BaseFab<T>::box () const
00052 {
00053   return domain;
00054 }
00055 
00056 template <class T>
00057 inline
00058 const int*
00059 BaseFab<T>::size () const
00060 {
00061   return domain.size().getVect();
00062 }
00063 
00064 template <class T>
00065 inline
00066 const IntVect&
00067 BaseFab<T>::smallEnd () const
00068 {
00069   return domain.smallEnd();
00070 }
00071 
00072 template <class T>
00073 inline
00074 const IntVect&
00075 BaseFab<T>::bigEnd () const
00076 {
00077   return domain.bigEnd();
00078 }
00079 
00080 template <class T>
00081 inline
00082 const int*
00083 BaseFab<T>::loVect () const
00084 {
00085   return domain.loVect();
00086 }
00087 
00088 template <class T>
00089 inline
00090 const int*
00091 BaseFab<T>::hiVect () const
00092 {
00093   return domain.hiVect();
00094 }
00095 
00096 template <class T>
00097 inline
00098 bool
00099 BaseFab<T>::contains (const BaseFab<T>& fab) const
00100 {
00101   return box().contains(fab.box()) && nvar <= fab.nvar;
00102 }
00103 
00104 template <class T>
00105 inline
00106 bool
00107 BaseFab<T>::contains (const Box& bx) const
00108 {
00109   return box().contains(bx);
00110 }
00111 
00112 template <class T>
00113 inline
00114 T*
00115 BaseFab<T>::dataPtr (int n)
00116 {
00117   assert(!(dptr == 0));
00118   return &dptr[n*numpts];
00119 }
00120 
00121 template <class T>
00122 inline
00123 const int*
00124 BaseFab<T>::nCompPtr () const
00125 {
00126   assert(!(dptr == 0));
00127   return &nvar;
00128 }
00129 
00130 template <class T>
00131 inline
00132 const T*
00133 BaseFab<T>::dataPtr (int n) const
00134 {
00135   assert(!(dptr == 0));
00136   return &dptr[n*numpts];
00137 }
00138 
00139 template <class T>
00140 inline
00141 T&
00142 BaseFab<T>::operator() (const IntVect& p,
00143                         int            n)
00144 {
00145   assert(n >= 0);
00146   assert(n < nvar);
00147   assert(!(dptr == 0));
00148   assert(domain.contains(p));
00149 
00150   return dptr[domain.index(p)+n*numpts];
00151 }
00152 
00153 template <class T>
00154 inline
00155 T&
00156 BaseFab<T>::operator() (const IntVect& p)
00157 {
00158   assert(!(dptr == 0));
00159   assert(domain.contains(p));
00160 
00161   return dptr[domain.index(p)];
00162 }
00163 
00164 template <class T>
00165 inline
00166 const T&
00167 BaseFab<T>::operator() (const IntVect& p,
00168                         int            n) const
00169 {
00170   assert(n >= 0);
00171   assert(n < nvar);
00172   assert(!(dptr == 0));
00173   assert(domain.contains(p));
00174 
00175   return dptr[domain.index(p)+n*numpts];
00176 }
00177 
00178 template <class T>
00179 inline
00180 const T&
00181 BaseFab<T>::operator() (const IntVect& p) const
00182 {
00183   assert(!(dptr == 0));
00184   assert(domain.contains(p));
00185 
00186   return dptr[domain.index(p)];
00187 }
00188 
00189 template <class T>
00190 inline
00191 void
00192 BaseFab<T>::getVal  (T*             data,
00193                      const IntVect& pos,
00194                      int            n,
00195                      int            numcomp) const
00196 {
00197   const int loc      = domain.index(pos);
00198   const long size    = domain.numPts();
00199 
00200   assert(!(dptr == 0));
00201   assert(n >= 0 && n + numcomp <= nvar);
00202 
00203   for (int k = 0; k < numcomp; k++)
00204     data[k] = dptr[loc+(n+k)*size];
00205 }
00206 
00207 template <class T>
00208 inline
00209 void
00210 BaseFab<T>::getVal (T*             data,
00211                     const IntVect& pos) const
00212 {
00213   getVal(data,pos,0,nvar);
00214 }
00215 
00216 template <class T>
00217 inline
00218 BaseFab<T>&
00219 BaseFab<T>::shift (const IntVect& v)
00220 {
00221   domain += v;
00222   return *this;
00223 }
00224 
00225 template <class T>
00226 inline
00227 BaseFab<T>&
00228 BaseFab<T>::shift (int idir,
00229                    int n_cell)
00230 {
00231   domain.shift(idir,n_cell);
00232   return *this;
00233 }
00234 
00235 template <class T>
00236 inline
00237 BaseFab<T> &
00238 BaseFab<T>::shiftHalf (const IntVect& v)
00239 {
00240   domain.shiftHalf(v);
00241   return *this;
00242 }
00243 
00244 template <class T>
00245 inline
00246 BaseFab<T> &
00247 BaseFab<T>::shiftHalf (int idir,
00248                        int n_cell)
00249 {
00250   domain.shiftHalf(idir,n_cell);
00251   return *this;
00252 }
00253 
00254 template <class T>
00255 inline
00256 void
00257 BaseFab<T>::setVal (T val)
00258 {
00259   performSetVal(val,box(), 0, nvar);
00260 }
00261 
00262 template <class T>
00263 inline
00264 void
00265 BaseFab<T>::setVal (T          x,
00266                     const Box& bx,
00267                     int        n)
00268 {
00269   performSetVal(x,bx,n,1);
00270 }
00271 
00272 template <class T>
00273 inline
00274 void
00275 BaseFab<T>::setVal (T   x,
00276                     int n)
00277 {
00278   performSetVal(x,domain,n,1);
00279 }
00280 
00281 template <class T>
00282 inline
00283 void
00284 BaseFab<T>::setVal (T          x,
00285                     const Box& b,
00286                     int        ns,
00287                     int        num)
00288 {
00289   performSetVal(x,b,ns,num);
00290 }
00291 
00292 template <class T>
00293 inline
00294 BaseFab<T>&
00295 BaseFab<T>::copy (const BaseFab<T>& src,
00296                   const Box&        srcbox,
00297                   int               srccomp,
00298                   const Box&        destbox,
00299                   int               destcomp,
00300                   int               numcomp)
00301 {
00302   assert(srcbox.sameSize(destbox));
00303   assert(src.box().contains(srcbox));
00304   assert(domain.contains(destbox));
00305   assert(srccomp >= 0 && srccomp+numcomp <= src.nComp());
00306   assert(destcomp >= 0 && destcomp+numcomp <= nvar);
00307   performCopy(src,srcbox,srccomp,destbox,destcomp,numcomp);
00308   return *this;
00309 }
00310 
00311 template <class T>
00312 inline
00313 BaseFab<T>&
00314 BaseFab<T>::copy (const BaseFab<T>& src)
00315 {
00316   assert(nvar <= src.nvar);
00317   assert(domain.sameType(src.domain));
00318   Box overlap(domain);
00319   overlap &= src.domain;
00320   if (!overlap.isEmpty())
00321     performCopy(src,overlap,0,overlap,0,nvar);
00322   return *this;
00323 }
00324 
00325 template <class T>
00326 inline
00327 BaseFab<T>&
00328 BaseFab<T>::copy (const BaseFab<T>& src,
00329                   const Box&        destbox)
00330 {
00331   assert(nvar <= src.nvar);
00332   assert(domain.contains(destbox));
00333   Box overlap(destbox);
00334   overlap &= src.domain;
00335   if (!overlap.isEmpty())
00336     performCopy(src,overlap,0,overlap,0,nvar);
00337   return *this;
00338 }
00339 
00340 template <class T>
00341 inline
00342 BaseFab<T>&
00343 BaseFab<T>::copy (const BaseFab<T>& src,
00344                   int               srccomp,
00345                   int               destcomp,
00346                   int               numcomp)
00347 {
00348   assert(srccomp >= 0 && srccomp + numcomp <= src.nvar);
00349   assert(destcomp >= 0 && destcomp + numcomp <= nvar);
00350   Box overlap(domain);
00351   overlap &= src.domain;
00352   if (!overlap.isEmpty())
00353     performCopy(src,overlap,srccomp,overlap,destcomp,numcomp);
00354   return *this;
00355 }
00356 
00357 template <class T>
00358 inline
00359 void
00360 BaseFab<T>::define ()
00361 {
00362   assert(nvar > 0);
00363   assert(dptr == 0);
00364   assert(numpts > 0);
00365   //    assert(!(The_FAB_Arena == 0));// not a sufficient test !!!
00366 #ifdef ENABLE_MEMORY_TRACKING  
00367   if(s_Arena == NULL) s_Arena = new BArena(name().c_str());
00368 #else
00369   if(s_Arena == NULL) s_Arena = new BArena("");
00370 #endif
00371   if(s_Arena == NULL)
00372     MayDay::Error("malloc in basefab failed");
00373   truesize = nvar*numpts;
00374   dptr     = static_cast<T*>(s_Arena->alloc(truesize*sizeof(T)));
00375 
00376 #ifdef ENABLE_MEMORY_TRACKING  
00377   s_Arena->bytes+=truesize*sizeof(T)+sizeof(BaseFab<T>);
00378   if(s_Arena->bytes > s_Arena->peak) 
00379     s_Arena->peak = s_Arena->bytes;
00380 #endif
00381 
00382   //
00383   // Now call T::T() on the raw memo'ry so we have valid Ts.
00384   //
00385   T* ptr = dptr;
00386 
00387   for (int i = 0; i < truesize; i++, ptr++)
00388     new (ptr) T;
00389 }
00390 
00391 template <class T>
00392 inline
00393 void
00394 BaseFab<T>::undefine ()
00395 {
00396   //    assert(!(The_FAB_Arena == 0));
00397   //
00398   // Call T::~T() on the to-be-destroyed memory.
00399   //
00400   if(dptr == 0) return;
00401   T* ptr = dptr;
00402 
00403   for (int i = 0; i < truesize; i++, ptr++)
00404     ptr->~T();
00405 
00406   s_Arena->free(dptr);
00407 
00408 #ifdef ENABLE_MEMORY_TRACKING  
00409   s_Arena->bytes-= truesize*sizeof(T)+sizeof(BaseFab<T>);
00410 #endif
00411 
00412   dptr = 0;
00413 }
00414 
00415 template <class T>
00416 inline
00417 BaseFab<T>::BaseFab ()
00418   :
00419   domain(Box()),
00420   nvar(0),
00421   numpts(0),
00422   truesize(0),
00423   dptr(0)
00424 {}
00425 
00426 template <class T>
00427 inline
00428 BaseFab<T>::BaseFab (const Box& bx,
00429                      int        n)
00430   :
00431   domain(bx),
00432   nvar(n),
00433   numpts(bx.numPts()),
00434   dptr(0)
00435 {
00436   define();
00437 }
00438 
00439 
00440 template <class T>
00441 void
00442 BaseFab<T>::resize (const Box& b,
00443                     int        n)
00444 {
00445   //     nvar   = n;
00446   //     domain = b;
00447   //     numpts = domain.numPts();
00448 
00449   //     if (dptr == 0)
00450   //     {
00451   //         define();
00452   //     }
00453   //     else if (nvar*numpts > truesize)
00454   //     {
00455   //         undefine();
00456 
00457 //         define();
00458 //     }
00459   undefine();
00460   nvar = n;
00461   domain = b;
00462   numpts = domain.numPts();
00463   define();
00464 }
00465 
00466 template <class T>
00467 inline
00468 BaseFab<T>::~BaseFab ()
00469 {
00470   undefine();
00471 }
00472 
00473 template <class T>
00474 inline
00475 void
00476 BaseFab<T>::clear ()
00477 {
00478   undefine();
00479 
00480   domain = Box();
00481   nvar   = 0;
00482   numpts = 0;
00483 }
00484 
00485 template <class T>
00486 inline
00487 void
00488 BaseFab<T>::performCopy (const BaseFab<T>& src,
00489                          const Box&        srcbox,
00490                          int               srccomp,
00491                          const Box&        destbox,
00492                          int               destcomp,
00493                          int               numcomp)
00494 {
00495   assert(src.box().contains(srcbox));
00496   assert(box().contains(destbox));
00497   assert(destbox.sameSize(srcbox));
00498   assert(srccomp >= 0 && srccomp+numcomp <= src.nComp());
00499   assert(destcomp >= 0 && destcomp+numcomp <= nComp());
00500 
00501   ForAllThisBNNXCBN(T, destbox, destcomp, numcomp, src, srcbox, srccomp)
00502     {
00503       thisR = srcR;
00504     } EndForTX
00505         }
00506 
00507 template <class T>
00508 inline
00509 void
00510 BaseFab<T>::performSetVal (T         val,
00511                            const Box& bx,
00512                            int        ns,
00513                            int        num)
00514 {
00515   assert(domain.contains(bx));
00516   assert(ns >= 0 && ns + num <= nvar);
00517 
00518   if (bx == domain)
00519     {
00520       T* data = &dptr[ns*numpts];
00521       for (long i = 0, N = num*numpts; i < N; i++)
00522         {
00523           *data++ = val;
00524         }
00525     }
00526   else
00527     {
00528       ForAllThisBNN(T,bx,ns,num)
00529         {
00530           thisR = val;
00531         } EndFor
00532             }
00533 }
00534 
00535 
00536 template <class T>
00537 inline
00538 void
00539 BaseFab<T>::copy(const Box& RegionFrom, 
00540                  const Interval& Cdest, 
00541                  const Box& RegionTo, 
00542                  const BaseFab<T>& src, 
00543                  const Interval& Csrc)
00544 {
00545   if((this == &src) && (RegionFrom == RegionTo)) return;
00546   assert(Cdest.size() == Csrc.size()); 
00547   copy(src, RegionFrom, Csrc.begin(), RegionTo, 
00548        Cdest.begin(), Cdest.size());
00549 }
00550 
00551 template <class T>
00552 inline
00553 void
00554 BaseFab<T>::linearOut(void* buf, const Box& R, const Interval& comps) const
00555 {
00556   T* buffer = (T*)buf;
00557   ForAllThisBNN(T,R,comps.begin(), comps.size())
00558     {
00559       *buffer = thisR;
00560       ++buffer;
00561     } EndFor;
00562 }
00563 
00564 template <class T>
00565 inline
00566 void
00567 BaseFab<T>::linearIn(void* buf, const Box& R, const Interval& comps)
00568 {
00569   T* buffer = (T*)buf;
00570   ForAllThisBNN(T,R,comps.begin(), comps.size())
00571     {
00572       thisR = *buffer;
00573       ++buffer;
00574     } EndFor;
00575 
00576 }
00577 
00578 template <class T>
00579 inline
00580 int
00581 BaseFab<T>::size(const Box& box, const Interval& comps) const
00582 {
00583   return box.numPts()*sizeof(T)*comps.size();
00584 }
00585 
00586 template <class T>
00587 inline
00588 std::string
00589 BaseFab<T>::name()
00590 {
00591   std::string rtn = (typeid(T)).name();
00592   return rtn;
00593 }
00594 
00595 #endif /*CH_BASEFAB_H*/
00596 

Generated on Thu Aug 29 11:05:44 2002 for Chombo&INS by doxygen1.2.16