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 
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   truesize = nvar*numpts;
00372   dptr     = static_cast<T*>(s_Arena->alloc(truesize*sizeof(T)));
00373 
00374 #ifdef ENABLE_MEMORY_TRACKING  
00375   s_Arena->bytes+=truesize*sizeof(T)+sizeof(BaseFab<T>);
00376   if(s_Arena->bytes > s_Arena->peak) 
00377     s_Arena->peak = s_Arena->bytes;
00378 #endif
00379 
00380   //
00381   // Now call T::T() on the raw memo'ry so we have valid Ts.
00382   //
00383   T* ptr = dptr;
00384 
00385   for (int i = 0; i < truesize; i++, ptr++)
00386     new (ptr) T;
00387 }
00388 
00389 template <class T>
00390 inline
00391 void
00392 BaseFab<T>::undefine ()
00393 {
00394   //    assert(!(The_FAB_Arena == 0));
00395   //
00396   // Call T::~T() on the to-be-destroyed memory.
00397   //
00398   if(dptr == 0) return;
00399   T* ptr = dptr;
00400 
00401   for (int i = 0; i < truesize; i++, ptr++)
00402     ptr->~T();
00403 
00404   s_Arena->free(dptr);
00405 
00406 #ifdef ENABLE_MEMORY_TRACKING  
00407   s_Arena->bytes-= truesize*sizeof(T)+sizeof(BaseFab<T>);
00408 #endif
00409 
00410   dptr = 0;
00411 }
00412 
00413 template <class T>
00414 inline
00415 BaseFab<T>::BaseFab ()
00416   :
00417   domain(Box()),
00418   nvar(0),
00419   numpts(0),
00420   truesize(0),
00421   dptr(0)
00422 {}
00423 
00424 template <class T>
00425 inline
00426 BaseFab<T>::BaseFab (const Box& bx,
00427                      int        n)
00428   :
00429   domain(bx),
00430   nvar(n),
00431   numpts(bx.numPts()),
00432   dptr(0)
00433 {
00434   define();
00435 }
00436 
00437 
00438 template <class T>
00439 void
00440 BaseFab<T>::resize (const Box& b,
00441                     int        n)
00442 {
00443   //     nvar   = n;
00444   //     domain = b;
00445   //     numpts = domain.numPts();
00446 
00447   //     if (dptr == 0)
00448   //     {
00449   //         define();
00450   //     }
00451   //     else if (nvar*numpts > truesize)
00452   //     {
00453   //         undefine();
00454 
00455 //         define();
00456 //     }
00457   undefine();
00458   nvar = n;
00459   domain = b;
00460   numpts = domain.numPts();
00461   define();
00462 }
00463 
00464 template <class T>
00465 inline
00466 BaseFab<T>::~BaseFab ()
00467 {
00468   undefine();
00469 }
00470 
00471 template <class T>
00472 inline
00473 void
00474 BaseFab<T>::clear ()
00475 {
00476   undefine();
00477 
00478   domain = Box();
00479   nvar   = 0;
00480   numpts = 0;
00481 }
00482 
00483 template <class T>
00484 inline
00485 void
00486 BaseFab<T>::performCopy (const BaseFab<T>& src,
00487                          const Box&        srcbox,
00488                          int               srccomp,
00489                          const Box&        destbox,
00490                          int               destcomp,
00491                          int               numcomp)
00492 {
00493   assert(src.box().contains(srcbox));
00494   assert(box().contains(destbox));
00495   assert(destbox.sameSize(srcbox));
00496   assert(srccomp >= 0 && srccomp+numcomp <= src.nComp());
00497   assert(destcomp >= 0 && destcomp+numcomp <= nComp());
00498 
00499   ForAllThisBNNXCBN(T, destbox, destcomp, numcomp, src, srcbox, srccomp)
00500     {
00501       thisR = srcR;
00502     } EndForTX
00503         }
00504 
00505 template <class T>
00506 inline
00507 void
00508 BaseFab<T>::performSetVal (T         val,
00509                            const Box& bx,
00510                            int        ns,
00511                            int        num)
00512 {
00513   assert(domain.contains(bx));
00514   assert(ns >= 0 && ns + num <= nvar);
00515 
00516   if (bx == domain)
00517     {
00518       T* data = &dptr[ns*numpts];
00519       for (long i = 0, N = num*numpts; i < N; i++)
00520         {
00521           *data++ = val;
00522         }
00523     }
00524   else
00525     {
00526       ForAllThisBNN(T,bx,ns,num)
00527         {
00528           thisR = val;
00529         } EndFor
00530             }
00531 }
00532 
00533 
00534 template <class T>
00535 inline
00536 void
00537 BaseFab<T>::copy(const Box& RegionFrom, 
00538                  const Interval& Cdest, 
00539                  const Box& RegionTo, 
00540                  const BaseFab<T>& src, 
00541                  const Interval& Csrc)
00542 {
00543   if((this == &src) && (RegionFrom == RegionTo)) return;
00544   assert(Cdest.size() == Csrc.size()); 
00545   copy(src, RegionFrom, Csrc.begin(), RegionTo, 
00546        Cdest.begin(), Cdest.size());
00547 }
00548 
00549 template <class T>
00550 inline
00551 void
00552 BaseFab<T>::linearOut(void* buf, const Box& R, const Interval& comps) const
00553 {
00554   T* buffer = (T*)buf;
00555   ForAllThisBNN(T,R,comps.begin(), comps.size())
00556     {
00557       *buffer = thisR;
00558       ++buffer;
00559     } EndFor;
00560 }
00561 
00562 template <class T>
00563 inline
00564 void
00565 BaseFab<T>::linearIn(void* buf, const Box& R, const Interval& comps)
00566 {
00567   T* buffer = (T*)buf;
00568   ForAllThisBNN(T,R,comps.begin(), comps.size())
00569     {
00570       thisR = *buffer;
00571       ++buffer;
00572     } EndFor;
00573 
00574 }
00575 
00576 template <class T>
00577 inline
00578 int
00579 BaseFab<T>::size(const Box& box, const Interval& comps) const
00580 {
00581   return box.numPts()*sizeof(T)*comps.size();
00582 }
00583 
00584 template <class T>
00585 inline
00586 std::string
00587 BaseFab<T>::name()
00588 {
00589   std::string rtn = (typeid(T)).name();
00590   return rtn;
00591 }
00592 
00593 #endif /*CH_BASEFAB_H*/
00594 

Generated on Tue Jul 2 10:42:19 2002 for Chombo by doxygen1.2.16