Chombo + EB + MF  3.2
LevelDataOps.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 // BVS, June 26, 2003
12 
13 // We can assume that template class T has null construction.
14 
15 #ifndef _LEVELDATAOPS_H_
16 #define _LEVELDATAOPS_H_
17 
18 #ifdef CH_MPI
19 #include <string>
20 #include <sstream>
21 #endif
22 #include "LevelData.H"
23 #include "RefCountedPtr.H"
24 #include "SPMD.H"
25 #include "Copier.H"
26 #include "NamespaceHeader.H"
27 
28 // default copy constructor and assign are fine.
29 
30 template <class T>
32 {
33 public:
36  {
37  }
38 
40  :m_levelFactory(a_factoryPtr)
41  {
42  }
43 
44  virtual ~LevelDataOps()
45  {
46  }
47 
48  virtual void define(const RefCountedPtr<DataFactory<T> >& a_factoryPtr)
49  {
50  m_levelFactory = a_factoryPtr;
51  }
52 
53  virtual void define(DataFactory<T>* a_rawPointer)
54  {
56  }
57 
58  virtual void create( LevelData<T>& a_lhs, const LevelData<T>& a_rhs);
59 
60  virtual void assign( LevelData<T>& a_lhs, const LevelData<T>& a_rhs) ;
61 
62  virtual Real dotProduct(const LevelData<T>& a_1, const LevelData<T>& a_2) ;
63 
64  virtual void mDotProduct(const LevelData<T>& a_1, const int a_sz, const LevelData<T> a_2arr[], Real a_mdots[]);
65 
66  virtual void incr( LevelData<T>& a_lhs, const LevelData<T>& a_x, Real a_scale) ;
67 
68  virtual void mult( LevelData<T>& a_lhs, const LevelData<T>& a_x);
69 
70  virtual void axby( LevelData<T>& a_lhs, const LevelData<T>& a_x,
71  const LevelData<T>& a_y, Real a_a, Real a_b) ;
72 
73  virtual void scale(LevelData<T>& a_lhs, const Real& a_scale) ;
74 
75  virtual void plus(LevelData<T>& a_lhs, const Real& a_inc) ;
76 
77  virtual void setToZero(LevelData<T>& a_lhs);
78 
79  virtual void setVal(LevelData<T>& a_lhs, const Real& a_val);
80 
81  virtual void copyToZero(LevelData<T>& a_lhs, const Copier& a_copier);
82 
83 
84 protected:
86 };
87 
88 //*******************************************************
89 // LevelDataOps Implementation
90 //*******************************************************
91 
92 
93 template <class T>
95 {
96  // a_lhs.define(a_rhs, *m_levelFactory);
97  a_lhs.define(a_rhs.disjointBoxLayout(), a_rhs.nComp(),
98  a_rhs.ghostVect(), *m_levelFactory);
99 }
100 
101 template <class T>
103 {
104  Interval interv(0, a_rhs.nComp()-1);
105  a_rhs.copyTo(interv, a_lhs, interv);
106 }
107 
108 template <class T>
110 {
111  const DisjointBoxLayout& dbl = a_1.disjointBoxLayout();
112  Real val = 0.0;
113  DataIterator dit=dbl.dataIterator(); int ompsize=dit.size();
114 #pragma omp parallel for reduction (+:val)
115  for(int i=0; i<ompsize; i++)
116  {
117  const DataIndex& d = dit[i];
118  val += a_1[d].dotProduct(a_2[d], dbl.get(d));
119  }
120 
121 
122 #ifdef CH_MPI
123  Real recv;
124  int result = MPI_Allreduce(&val, &recv, 1, MPI_CH_REAL,
125  MPI_SUM, Chombo_MPI::comm);
126  if ( result != 0 )
127  {
128  std::ostringstream msg;
129  msg << "LevelDataOps::dotProduct() called MPI_Allreduce() which returned error code " << result ;
130  MayDay::Warning( msg.str().c_str() );
131  }
132  val = recv;
133 #endif
134  return val;
135 
136 }
137 
138 /* multiple dot products (for GMRES) */
139 template <class T>
140 void LevelDataOps<T>::mDotProduct(const LevelData<T>& a_1, const int a_sz, const LevelData<T> a_2arr[], Real a_mdots[])
141 {
142  const DisjointBoxLayout& dbl = a_1.disjointBoxLayout();
143 
144  for (int ii=0;ii<a_sz;ii++)
145  {
146  Real val = 0.0;
147  const LevelData<T> &a_2 = a_2arr[ii];
148  DataIterator dit=dbl.dataIterator(); int ompsize=dit.size();
149 #pragma omp parallel for reduction (+:val)
150  for(int i=0; i<ompsize; i++)
151  {
152  const DataIndex& d = dit[i];
153  val += a_1[d].dotProduct(a_2[d], dbl.get(d));
154  }
155  a_mdots[ii] = val;
156  }
157 
158 #ifdef CH_MPI
159  Real *recv = new Real[a_sz];
160 
161  int result = MPI_Allreduce(a_mdots, recv, a_sz, MPI_CH_REAL,
162  MPI_SUM, Chombo_MPI::comm);
163  if ( result != 0 )
164  {
165  std::ostringstream msg;
166  msg << "LevelDataOps::mDotProduct() called MPI_Allreduce() which returned error code " << result ;
167  MayDay::Warning( msg.str().c_str() );
168  }
169  for (int ii=0;ii<a_sz;ii++)
170  {
171  a_mdots[ii] = recv[ii];
172  }
173 
174  delete [] recv;
175 #endif
176 }
177 
178 template <class T>
179 void LevelDataOps<T>:: incr( LevelData<T>& a_lhs, const LevelData<T>& a_rhs, Real a_scale)
180 {
181  // CH_assert(a_lhs.disjointBoxLayout() == a_rhs.disjointBoxLayout());
182  int numcomp = a_lhs.nComp();
183  int startcomp = 0;
184  DataIterator dit=a_lhs.dataIterator(); int count=dit.size();
185  #pragma omp parallel for
186  for(int i=0; i<count; i++)
187  {
188  const DataIndex& d=dit[i];
189  Box subbox = a_lhs.disjointBoxLayout()[d];
190  a_lhs[d].plus(a_rhs[d], subbox, subbox, a_scale, startcomp, startcomp, numcomp);
191  }
192 }
193 
194 template <class T>
196 {
197 
198  DataIterator dit=a_lhs.dataIterator(); int count=dit.size();
199 #pragma omp parallel for
200  for(int i=0; i<count; i++)
201  {
202  const DataIndex& d=dit[i];
203  a_lhs[d] *= a_rhs[d];
204  }
205 }
206 
207 template <class T>
209 {
210  DataIterator dit=a_lhs.dataIterator(); int count=dit.size();
211 #pragma omp parallel for
212  for(int i=0; i<count; i++)
213  {
214  const DataIndex& d=dit[i];
215  a_lhs[d].setVal(0.0);
216  }
217 }
218 template <class T>
219 void LevelDataOps<T>:: setVal( LevelData<T>& a_lhs, const Real& a_val)
220 {
221  DataIterator dit=a_lhs.dataIterator(); int count=dit.size();
222 #pragma omp parallel for
223  for(int i=0; i<count; i++)
224  {
225  const DataIndex& d=dit[i];
226  a_lhs[d].setVal(a_val);
227  }
228 }
229 
230 template <class T>
231 void LevelDataOps<T>:: copyToZero( LevelData<T>& a_lhs, const Copier& a_copier)
232 {
233  int nComp = a_lhs.nComp();
234  CopyIterator cit(a_copier, CopyIterator::LOCAL); int ccount=cit.size();
235  CopyIterator tit(a_copier, CopyIterator::TO); int tcount = tit.size();
236 
237 #pragma omp parallel
238  {
239 #pragma omp for nowait
240  for (int i=0; i<ccount; i++)
241  {
242  const MotionItem& item = cit[i];
243  a_lhs[item.toIndex].setVal(0, item.toRegion, 0, nComp);
244  }
245 #pragma omp for nowait
246  for (int i=0; i<tcount; i++)
247  {
248  const MotionItem& item = tit[i];
249  a_lhs[item.toIndex].setVal(0, item.toRegion, 0, nComp);
250  }
251  }
252 }
253 
254 template <class T>
256  const LevelData<T>& a_y, Real a, Real b)
257 {
258  DataIterator dit=a_lhs.dataIterator(); int count=dit.size();
259 #pragma omp parallel for
260  for(int i=0; i<count; i++)
261  {
262  const DataIndex& d=dit[i];
263  T& data = a_lhs[d];
264  data.copy(a_x[d]);
265  data.mult(a);
266  data.plus(a_y[d], b);
267  }
268 }
269 
270 template <class T>
271 void LevelDataOps<T>:: scale(LevelData<T>& a_lhs, const Real& a_scale)
272 {
273  DataIterator dit=a_lhs.dataIterator(); int count=dit.size();
274 #pragma omp parallel for
275  for(int i=0; i<count; i++)
276  {
277  const DataIndex& d=dit[i];
278  T& data = a_lhs[d];
279  data.mult(a_scale);
280  }
281 }
282 
283 template <class T>
284 void LevelDataOps<T>:: plus(LevelData<T>& a_lhs, const Real& a_inc)
285 {
286  DataIterator dit=a_lhs.dataIterator(); int count=dit.size();
287 #pragma omp parallel for
288  for(int i=0; i<count; i++)
289  {
290  const DataIndex& d=dit[i];
291  T& data = a_lhs[d];
292  data += a_inc;
293  }
294 }
295 
296 
297 
298 #include "NamespaceFooter.H"
299 #endif
LevelDataOps()
Definition: LevelDataOps.H:34
A reference-counting handle class.
Definition: RefCountedPtr.H:173
Definition: LevelDataOps.H:31
virtual void mult(LevelData< T > &a_lhs, const LevelData< T > &a_x)
Definition: LevelDataOps.H:195
virtual void axby(LevelData< T > &a_lhs, const LevelData< T > &a_x, const LevelData< T > &a_y, Real a_a, Real a_b)
Definition: LevelDataOps.H:255
A strange but true thing to make copying from one boxlayoutdata to another fast.
Definition: Copier.H:152
virtual void setVal(LevelData< T > &a_lhs, const Real &a_val)
Definition: LevelDataOps.H:219
DataIterator dataIterator() const
Definition: LayoutDataI.H:78
Definition: DataIterator.H:190
size_t size()
Definition: Copier.H:424
Definition: Copier.H:409
Definition: Copier.H:38
LevelDataOps(RefCountedPtr< DataFactory< T > > a_factoryPtr)
Definition: LevelDataOps.H:39
#define MPI_CH_REAL
Definition: REAL.H:34
Factory object to data members of a BoxLayoutData container.
Definition: BoxLayoutData.H:70
DataIndex toIndex
Definition: Copier.H:41
int size() const
Definition: DataIterator.H:218
Structure for passing component ranges in code.
Definition: Interval.H:23
new code
Definition: BoxLayoutData.H:170
virtual ~LevelDataOps()
Definition: LevelDataOps.H:44
virtual void setToZero(LevelData< T > &a_lhs)
Definition: LevelDataOps.H:208
double Real
Definition: REAL.H:33
virtual void scale(LevelData< T > &a_lhs, const Real &a_scale)
Definition: LevelDataOps.H:271
virtual void assign(LevelData< T > &a_lhs, const LevelData< T > &a_rhs)
Definition: LevelDataOps.H:102
virtual void define(const DisjointBoxLayout &dp, int comps, const IntVect &ghost=IntVect::Zero, const DataFactory< T > &a_factory=DefaultDataFactory< T >())
Definition: LevelDataI.H:90
virtual void copyTo(const Interval &srcComps, BoxLayoutData< T > &dest, const Interval &destComps) const
Definition: LevelDataI.H:218
A BoxLayout that has a concept of disjointedness.
Definition: DisjointBoxLayout.H:30
Box get(const LayoutIndex &it) const
Definition: BoxLayout.H:741
Box toRegion
Definition: Copier.H:43
const IntVect & ghostVect() const
Definition: LevelData.H:186
virtual void copyToZero(LevelData< T > &a_lhs, const Copier &a_copier)
Definition: LevelDataOps.H:231
virtual void plus(LevelData< T > &a_lhs, const Real &a_inc)
Definition: LevelDataOps.H:284
virtual void define(DataFactory< T > *a_rawPointer)
Definition: LevelDataOps.H:53
static void Warning(const char *const a_msg=m_nullString)
Print out message to cerr and continue.
virtual void mDotProduct(const LevelData< T > &a_1, const int a_sz, const LevelData< T > a_2arr[], Real a_mdots[])
Definition: LevelDataOps.H:140
A Rectangular Domain on an Integer Lattice.
Definition: Box.H:469
int nComp() const
Definition: BoxLayoutData.H:306
Definition: DataIndex.H:114
const DisjointBoxLayout & disjointBoxLayout() const
Definition: LevelData.H:225
Definition: Copier.H:407
Factory object to data members of a BoxLayoutData container.
Definition: BoxLayoutData.H:30
virtual void define(const RefCountedPtr< DataFactory< T > > &a_factoryPtr)
Definition: LevelDataOps.H:48
DataIterator dataIterator() const
Parallel iterator.
RefCountedPtr< DataFactory< T > > m_levelFactory
Definition: LevelDataOps.H:85
virtual Real dotProduct(const LevelData< T > &a_1, const LevelData< T > &a_2)
Definition: LevelDataOps.H:109
Definition: Copier.H:402
virtual void create(LevelData< T > &a_lhs, const LevelData< T > &a_rhs)
Definition: LevelDataOps.H:94
virtual void incr(LevelData< T > &a_lhs, const LevelData< T > &a_x, Real a_scale)
Definition: LevelDataOps.H:179