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 // BaseFab<Real> spaecializations
00035 //
00036 
00037 template < >
00038 void
00039 BaseFab<Real>::define();
00040 
00041 template < >
00042 void 
00043 BaseFab<Real>::undefine();
00044 
00045 template < >
00046 void
00047 BaseFab<Real>::setVal (Real val);
00048 
00049 
00050 template < >
00051 void
00052 BaseFab<int>::define();
00053 
00054 template < >
00055 void 
00056 BaseFab<int>::undefine();
00057 
00058 //
00059 // Implementation.=====================================
00060 //
00061 
00062 template <class T>
00063 Arena* BaseFab<T>::s_Arena = NULL;
00064 
00065 template <class T>
00066 inline
00067 int
00068 BaseFab<T>::nComp () const
00069 {
00070   return nvar;
00071 }
00072 
00073 template <class T>
00074 inline
00075 const Box&
00076 BaseFab<T>::box () const
00077 {
00078   return domain;
00079 }
00080 
00081 template <class T>
00082 inline
00083 const int*
00084 BaseFab<T>::size () const
00085 {
00086   return domain.size().getVect();
00087 }
00088 
00089 template <class T>
00090 inline
00091 const IntVect&
00092 BaseFab<T>::smallEnd () const
00093 {
00094   return domain.smallEnd();
00095 }
00096 
00097 template <class T>
00098 inline
00099 const IntVect&
00100 BaseFab<T>::bigEnd () const
00101 {
00102   return domain.bigEnd();
00103 }
00104 
00105 template <class T>
00106 inline
00107 const int*
00108 BaseFab<T>::loVect () const
00109 {
00110   return domain.loVect();
00111 }
00112 
00113 template <class T>
00114 inline
00115 const int*
00116 BaseFab<T>::hiVect () const
00117 {
00118   return domain.hiVect();
00119 }
00120 
00121 template <class T>
00122 inline
00123 bool
00124 BaseFab<T>::contains (const BaseFab<T>& fab) const
00125 {
00126   return box().contains(fab.box()) && nvar <= fab.nvar;
00127 }
00128 
00129 template <class T>
00130 inline
00131 bool
00132 BaseFab<T>::contains (const Box& bx) const
00133 {
00134   return box().contains(bx);
00135 }
00136 
00137 template <class T>
00138 inline
00139 T*
00140 BaseFab<T>::dataPtr (int n)
00141 {
00142   assert(!(dptr == 0));
00143   assert((n >= 0) && (n < nvar));
00144   return &dptr[n*numpts];
00145 }
00146 
00147 template <class T>
00148 inline
00149 const int*
00150 BaseFab<T>::nCompPtr () const
00151 {
00152   assert(!(dptr == 0));
00153   return &nvar;
00154 }
00155 
00156 template <class T>
00157 inline
00158 const T*
00159 BaseFab<T>::dataPtr (int n) const
00160 {
00161   assert(!(dptr == 0));
00162   assert((n >= 0) && (n < nvar));
00163   return &dptr[n*numpts];
00164 }
00165 
00166 template <class T>
00167 inline
00168 T&
00169 BaseFab<T>::operator() (const IntVect& p,
00170                         int            n)
00171 {
00172   assert(n >= 0);
00173   assert(n < nvar);
00174   assert(!(dptr == 0));
00175   assert(domain.contains(p));
00176 
00177   return dptr[domain.index(p)+n*numpts];
00178 }
00179 
00180 template <class T>
00181 inline
00182 T&
00183 BaseFab<T>::operator() (const IntVect& p)
00184 {
00185   assert(!(dptr == 0));
00186   assert(domain.contains(p));
00187 
00188   return dptr[domain.index(p)];
00189 }
00190 
00191 template <class T>
00192 inline
00193 const T&
00194 BaseFab<T>::operator() (const IntVect& p,
00195                         int            n) const
00196 {
00197   assert(n >= 0);
00198   assert(n < nvar);
00199   assert(!(dptr == 0));
00200   assert(domain.contains(p));
00201 
00202   return dptr[domain.index(p)+n*numpts];
00203 }
00204 
00205 template <class T>
00206 inline
00207 const T&
00208 BaseFab<T>::operator() (const IntVect& p) const
00209 {
00210   assert(!(dptr == 0));
00211   assert(domain.contains(p));
00212 
00213   return dptr[domain.index(p)];
00214 }
00215 
00216 template <class T>
00217 inline
00218 void
00219 BaseFab<T>::getVal  (T*             data,
00220                      const IntVect& pos,
00221                      int            n,
00222                      int            numcomp) const
00223 {
00224   const int loc      = domain.index(pos);
00225   const long size    = domain.numPts();
00226 
00227   assert(!(dptr == 0));
00228   assert(n >= 0 && n + numcomp <= nvar);
00229 
00230   for (int k = 0; k < numcomp; k++)
00231     data[k] = dptr[loc+(n+k)*size];
00232 }
00233 
00234 template <class T>
00235 inline
00236 void
00237 BaseFab<T>::getVal (T*             data,
00238                     const IntVect& pos) const
00239 {
00240   getVal(data,pos,0,nvar);
00241 }
00242 
00243 template <class T>
00244 inline
00245 BaseFab<T>&
00246 BaseFab<T>::shift (const IntVect& v)
00247 {
00248   domain += v;
00249   return *this;
00250 }
00251 
00252 template <class T>
00253 inline
00254 BaseFab<T>&
00255 BaseFab<T>::shift (int idir,
00256                    int n_cell)
00257 {
00258   domain.shift(idir,n_cell);
00259   return *this;
00260 }
00261 
00262 template <class T>
00263 inline
00264 BaseFab<T> &
00265 BaseFab<T>::shiftHalf (const IntVect& v)
00266 {
00267   domain.shiftHalf(v);
00268   return *this;
00269 }
00270 
00271 template <class T>
00272 inline
00273 BaseFab<T> &
00274 BaseFab<T>::shiftHalf (int idir,
00275                        int n_cell)
00276 {
00277   domain.shiftHalf(idir,n_cell);
00278   return *this;
00279 }
00280 
00281 template <class T>
00282 inline
00283 void
00284 BaseFab<T>::setVal (T val)
00285 {
00286   performSetVal(val,box(), 0, nvar);
00287 }
00288 
00289 template <class T>
00290 inline
00291 void
00292 BaseFab<T>::setVal (T          x,
00293                     const Box& bx,
00294                     int        n)
00295 {
00296   performSetVal(x,bx,n,1);
00297 }
00298 
00299 template <class T>
00300 inline
00301 void
00302 BaseFab<T>::setVal (T   x,
00303                     int n)
00304 {
00305   performSetVal(x,domain,n,1);
00306 }
00307 
00308 template <class T>
00309 inline
00310 void
00311 BaseFab<T>::setVal (T          x,
00312                     const Box& b,
00313                     int        ns,
00314                     int        num)
00315 {
00316   performSetVal(x,b,ns,num);
00317 }
00318 
00319 template <class T>
00320 inline
00321 BaseFab<T>&
00322 BaseFab<T>::copy (const BaseFab<T>& src,
00323                   const Box&        srcbox,
00324                   int               srccomp,
00325                   const Box&        destbox,
00326                   int               destcomp,
00327                   int               numcomp)
00328 {
00329   assert(srcbox.sameSize(destbox));
00330   assert(src.box().contains(srcbox));
00331   assert(domain.contains(destbox));
00332   assert(srccomp >= 0 && srccomp+numcomp <= src.nComp());
00333   assert(destcomp >= 0 && destcomp+numcomp <= nvar);
00334   performCopy(src,srcbox,srccomp,destbox,destcomp,numcomp);
00335   return *this;
00336 }
00337 
00338 template <class T>
00339 inline
00340 BaseFab<T>&
00341 BaseFab<T>::copy (const BaseFab<T>& src)
00342 {
00343   assert(nvar <= src.nvar);
00344   assert(domain.sameType(src.domain));
00345   Box overlap(domain);
00346   overlap &= src.domain;
00347   if (!overlap.isEmpty())
00348     performCopy(src,overlap,0,overlap,0,nvar);
00349   return *this;
00350 }
00351 
00352 template <class T>
00353 inline
00354 BaseFab<T>&
00355 BaseFab<T>::copy (const BaseFab<T>& src,
00356                   const Box&        destbox)
00357 {
00358   assert(nvar <= src.nvar);
00359   assert(domain.contains(destbox));
00360   Box overlap(destbox);
00361   overlap &= src.domain;
00362   if (!overlap.isEmpty())
00363     performCopy(src,overlap,0,overlap,0,nvar);
00364   return *this;
00365 }
00366 
00367 template <class T>
00368 inline
00369 BaseFab<T>&
00370 BaseFab<T>::copy (const BaseFab<T>& src,
00371                   int               srccomp,
00372                   int               destcomp,
00373                   int               numcomp)
00374 {
00375   assert(srccomp >= 0 && srccomp + numcomp <= src.nvar);
00376   assert(destcomp >= 0 && destcomp + numcomp <= nvar);
00377   Box overlap(domain);
00378   overlap &= src.domain;
00379   if (!overlap.isEmpty())
00380     performCopy(src,overlap,srccomp,overlap,destcomp,numcomp);
00381   return *this;
00382 }
00383 
00384 template <class T>
00385 inline
00386 void
00387 BaseFab<T>::define (const Interval& a_comps, BaseFab<T>& a_original)
00388 {
00389   undefine();
00390   domain   = a_original.domain;
00391   numpts   = a_original.numpts;
00392   truesize = a_original.numpts*a_comps.size();
00393   nvar     = a_comps.size();
00394   dptr     = a_original.dataPtr(a_comps.begin());
00395   aliased  = true;
00396 
00397   //resize(a_original.domain, a_comps.size(), a_original.dataPtr(a_comps.begin()));
00398 }
00399 
00400 
00401 template <class T>
00402 inline
00403 void
00404 BaseFab<T>::define ()
00405 {
00406   assert(nvar > 0);
00407   assert(dptr == 0);
00408   assert(numpts > 0);
00409   assert(!aliased);
00410   //    assert(!(The_FAB_Arena == 0));// not a sufficient test !!!
00411 #ifdef ENABLE_MEMORY_TRACKING  
00412   if(s_Arena == NULL) s_Arena = new BArena(name().c_str());
00413 #else
00414   if(s_Arena == NULL) s_Arena = new BArena("");
00415 #endif
00416   if(s_Arena == NULL)
00417     MayDay::Error("malloc in basefab failed");
00418   truesize = nvar*numpts;
00419   dptr     = static_cast<T*>(s_Arena->alloc(truesize*sizeof(T)));
00420 
00421 #ifdef ENABLE_MEMORY_TRACKING  
00422   s_Arena->bytes+=truesize*sizeof(T)+sizeof(BaseFab<T>);
00423   if(s_Arena->bytes > s_Arena->peak) 
00424     s_Arena->peak = s_Arena->bytes;
00425 #endif
00426 
00427   //
00428   // Now call T::T() on the raw memo'ry so we have valid Ts.
00429   //
00430   T* ptr = dptr;
00431 
00432   for (int i = 0; i < truesize; i++, ptr++)
00433     new (ptr) T;
00434 }
00435 
00436 template <class T>
00437 inline
00438 void
00439 BaseFab<T>::undefine ()
00440 {
00441   //    assert(!(The_FAB_Arena == 0));
00442   //
00443   // Call T::~T() on the to-be-destroyed memory.
00444   //
00445   if(aliased)
00446         {
00447           dptr = 0;
00448           return;
00449         }
00450   if(dptr == 0) return;
00451   T* ptr = dptr;
00452 
00453   for (int i = 0; i < truesize; i++, ptr++)
00454     ptr->~T();
00455 
00456   s_Arena->free(dptr);
00457 
00458 #ifdef ENABLE_MEMORY_TRACKING  
00459   s_Arena->bytes-= truesize*sizeof(T)+sizeof(BaseFab<T>);
00460 #endif
00461 
00462   dptr = 0;
00463 }
00464 
00465 template <class T>
00466 inline
00467 BaseFab<T>::BaseFab ()
00468   :
00469   domain(Box()),
00470   nvar(0),
00471   numpts(0),
00472   truesize(0),
00473   dptr(0),
00474   aliased(false)
00475 {}
00476 
00477 template <class T>
00478 inline
00479 BaseFab<T>::BaseFab (const Interval& a_comps, BaseFab<T>& a_original)
00480   :
00481   domain(a_original.domain),
00482   nvar(a_comps.size()),
00483   numpts(a_original.numpts),
00484   truesize(a_original.numpts*nvar),
00485   dptr(a_original.dataPtr(a_comps.begin())),
00486   aliased(true)
00487 {}
00488 
00489 template <class T>
00490 inline
00491 BaseFab<T>::BaseFab (const Box& bx,
00492                      int        n,
00493                                          T*         alias)
00494   :
00495   domain(bx),
00496   nvar(n),
00497   numpts(bx.numPts()),
00498   dptr(0),
00499   aliased(false)
00500 {
00501   if(alias != NULL)
00502         {
00503           dptr = alias;
00504           aliased = true;
00505         }
00506   else
00507         define();
00508 }
00509 
00510 
00511 template <class T>
00512 void
00513 BaseFab<T>::resize (const Box& b,
00514                     int        n,
00515                                         T*         alias)
00516 {
00517   //     nvar   = n;
00518   //     domain = b;
00519   //     numpts = domain.numPts();
00520 
00521   //     if (dptr == 0)
00522   //     {
00523   //         define();
00524   //     }
00525   //     else if (nvar*numpts > truesize)
00526   //     {
00527   //         undefine();
00528 
00529 //         define();
00530 //     }
00531   undefine();
00532   nvar = n;
00533   domain = b;
00534   numpts = domain.numPts();
00535   if(alias != NULL)
00536         {
00537           dptr = alias;
00538           aliased = true;
00539         }
00540   else
00541         {
00542           aliased = false;
00543           define();
00544         }
00545 }
00546 
00547 template <class T>
00548 inline
00549 BaseFab<T>::~BaseFab ()
00550 {
00551   undefine();
00552 }
00553 
00554 template <class T>
00555 inline
00556 void
00557 BaseFab<T>::clear ()
00558 {
00559   undefine();
00560 
00561   domain = Box();
00562   nvar   = 0;
00563   numpts = 0;
00564 }
00565 
00566 template <class T>
00567 inline
00568 void
00569 BaseFab<T>::performCopy (const BaseFab<T>& src,
00570                          const Box&        srcbox,
00571                          int               srccomp,
00572                          const Box&        destbox,
00573                          int               destcomp,
00574                          int               numcomp)
00575 {
00576   assert(src.box().contains(srcbox));
00577   assert(box().contains(destbox));
00578   assert(destbox.sameSize(srcbox));
00579   assert(srccomp >= 0 && srccomp+numcomp <= src.nComp());
00580   assert(destcomp >= 0 && destcomp+numcomp <= nComp());
00581 
00582   ForAllThisBNNXCBN(T, destbox, destcomp, numcomp, src, srcbox, srccomp)
00583     {
00584       thisR = srcR;
00585     } EndForTX
00586   ;
00587 }
00588 
00589 template <class T>
00590 inline
00591 void
00592 BaseFab<T>::performSetVal (T         val,
00593                            const Box& bx,
00594                            int        ns,
00595                            int        num)
00596 {
00597   assert(domain.contains(bx));
00598   assert(ns >= 0 && ns + num <= nvar);
00599 
00600   if (bx == domain)
00601     {
00602       T* data = &dptr[ns*numpts];
00603       for (long i = 0, N = num*numpts; i < N; i++)
00604         {
00605           *data++ = val;
00606         }
00607     }
00608   else
00609     {
00610       ForAllThisBNN(T,bx,ns,num)
00611         {
00612           thisR = val;
00613         } EndFor
00614             }
00615 }
00616 
00617 
00618 template <class T>
00619 inline
00620 void
00621 BaseFab<T>::copy(const Box& RegionFrom, 
00622                  const Interval& Cdest, 
00623                  const Box& RegionTo, 
00624                  const BaseFab<T>& src, 
00625                  const Interval& Csrc)
00626 {
00627   if((this == &src) && (RegionFrom == RegionTo)) return;
00628   assert(Cdest.size() == Csrc.size()); 
00629   copy(src, RegionFrom, Csrc.begin(), RegionTo, 
00630        Cdest.begin(), Cdest.size());
00631 }
00632 
00633 template <class T>
00634 inline
00635 void
00636 BaseFab<T>::linearOut(void* buf, const Box& R, const Interval& comps) const
00637 {
00638   T* buffer = (T*)buf;
00639   ForAllThisBNN(T,R,comps.begin(), comps.size())
00640     {
00641       *buffer = thisR;
00642       ++buffer;
00643     } EndFor;
00644 }
00645 
00646 template <class T>
00647 inline
00648 void
00649 BaseFab<T>::linearIn(void* buf, const Box& R, const Interval& comps)
00650 {
00651   T* buffer = (T*)buf;
00652   ForAllThisBNN(T,R,comps.begin(), comps.size())
00653     {
00654       thisR = *buffer;
00655       ++buffer;
00656     } EndFor;
00657 
00658 }
00659 
00660 template <class T>
00661 inline
00662 int
00663 BaseFab<T>::size(const Box& box, const Interval& comps) const
00664 {
00665   return box.numPts()*sizeof(T)*comps.size();
00666 }
00667 
00668 template <class T>
00669 inline
00670 std::string
00671 BaseFab<T>::name()
00672 {
00673   std::string rtn = (typeid(T)).name();
00674   return rtn;
00675 }
00676 
00677 #endif /*CH_BASEFAB_H*/
00678 

Generated on Wed Jun 2 13:53:31 2004 for Chombo&INSwithParticles by doxygen 1.3.2