Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_Sum_impl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Panzer: A partial differential equation assembly
5 // engine for strongly coupled complex multiphysics systems
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roger P. Pawlowski (rppawlo@sandia.gov) and
39 // Eric C. Cyr (eccyr@sandia.gov)
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef PANZER_SUM_IMPL_HPP
44 #define PANZER_SUM_IMPL_HPP
45 
46 #include <cstddef>
47 #include <string>
48 #include <vector>
49 
50 namespace panzer {
51 
52 //**********************************************************************
53 template<typename EvalT, typename Traits>
56  const Teuchos::ParameterList& p)
57 {
58  std::string sum_name = p.get<std::string>("Sum Name");
60  p.get<Teuchos::RCP<std::vector<std::string> > >("Values Names");
61  Teuchos::RCP<PHX::DataLayout> data_layout =
62  p.get< Teuchos::RCP<PHX::DataLayout> >("Data Layout");
63 
64  TEUCHOS_ASSERT(static_cast<int>(value_names->size()) < MAX_VALUES);
65 
66  // check if the user wants to scale each term independently
67  auto local_scalars = Kokkos::View<double *,typename PHX::DevLayout<double>::type,PHX::Device>("scalars",value_names->size());
68  if(p.isType<Teuchos::RCP<const std::vector<double> > >("Scalars")) {
69  auto scalars_v = *p.get<Teuchos::RCP<const std::vector<double> > >("Scalars");
70 
71  // safety/sanity check
72  TEUCHOS_ASSERT(scalars_v.size()==value_names->size());
73 
74  for (std::size_t i=0; i < value_names->size(); ++i)
75  local_scalars(i) = scalars_v[i];
76  }
77  else {
78  for (std::size_t i=0; i < value_names->size(); ++i)
79  local_scalars(i) = 1.0;
80  }
81 
82  scalars = local_scalars;
83 
84  sum = PHX::MDField<ScalarT>(sum_name, data_layout);
85 
86  this->addEvaluatedField(sum);
87 
88  for (std::size_t i=0; i < value_names->size(); ++i) {
89  values[i] = PHX::MDField<const ScalarT>( (*value_names)[i], data_layout);
90  this->addDependentField(values[i]);
91  }
92  /*
93  values.resize(value_names->size());
94  for (std::size_t i=0; i < value_names->size(); ++i) {
95  values[i] = PHX::MDField<const ScalarT>( (*value_names)[i], data_layout);
96  this->addDependentField(values[i]);
97  }
98  */
99 
100  std::string n = "Sum Evaluator";
101  this->setName(n);
102 }
103 
104 //**********************************************************************
105 template<typename EvalT, typename Traits>
106 void
109  typename Traits::SetupData /* worksets */,
110  PHX::FieldManager<Traits>& /* fm */)
111 {
112  cell_data_size = sum.size() / sum.fieldTag().dataLayout().extent(0);
113 }
114 
115 
116 //**********************************************************************
117 template<typename EvalT, typename TRAITS>
118 template<unsigned int RANK>
119 KOKKOS_INLINE_FUNCTION
121  auto num_vals = scalars.extent(0);
122 
123 
124  if (RANK == 1 )
125  {
126  for (std::size_t iv = 0; iv < num_vals; ++iv)
127  sum(i) += scalars(iv)*(values[iv](i));
128  }
129  else if (RANK == 2)
130  {
131  const size_t dim_1 = sum.extent(1);
132  for (std::size_t j = 0; j < dim_1; ++j)
133  for (std::size_t iv = 0; iv < num_vals; ++iv)
134  sum(i,j) += scalars(iv)*(values[iv](i,j));
135  }
136  else if (RANK == 3)
137  {
138  const size_t dim_1 = sum.extent(1),dim_2 = sum.extent(2);
139  for (std::size_t j = 0; j < dim_1; ++j)
140  for (std::size_t k = 0; k < dim_2; ++k)
141  for (std::size_t iv = 0; iv < num_vals; ++iv)
142  sum(i,j,k) += scalars(iv)*(values[iv](i,j,k));
143  }
144  else if (RANK == 4)
145  {
146  const size_t dim_1 = sum.extent(1),dim_2 = sum.extent(2),dim_3 = sum.extent(3);
147  for (std::size_t j = 0; j < dim_1; ++j)
148  for (std::size_t k = 0; k < dim_2; ++k)
149  for (std::size_t l = 0; l < dim_3; ++l)
150  for (std::size_t iv = 0; iv < num_vals; ++iv)
151  sum(i,j,k,l) += scalars(iv)*(values[iv](i,j,k,l));
152  }
153  else if (RANK == 5)
154  {
155  const size_t dim_1 = sum.extent(1),dim_2 = sum.extent(2),dim_3 = sum.extent(3),dim_4 = sum.extent(4);
156  for (std::size_t j = 0; j < dim_1; ++j)
157  for (std::size_t k = 0; k < dim_2; ++k)
158  for (std::size_t l = 0; l < dim_3; ++l)
159  for (std::size_t m = 0; m < dim_4; ++m)
160  for (std::size_t iv = 0; iv < num_vals; ++iv)
161  sum(i,j,k,l,m) += scalars(iv)*(values[iv](i,j,k,l,m));
162  }
163  else if (RANK == 6)
164  {
165  const size_t dim_1 = sum.extent(1),dim_2 = sum.extent(2),dim_3 = sum.extent(3),dim_4 = sum.extent(4),dim_5 = sum.extent(5);
166  for (std::size_t j = 0; j < dim_1; ++j)
167  for (std::size_t k = 0; k < dim_2; ++k)
168  for (std::size_t l = 0; l < dim_3; ++l)
169  for (std::size_t m = 0; m < dim_4; ++m)
170  for (std::size_t n = 0; n < dim_5; ++n)
171  for (std::size_t iv = 0; iv < num_vals; ++iv)
172  sum(i,j,k,l,m,n) += scalars(iv)*(values[iv](i,j,k,l,m,n));
173  }
174 }
175 
176 //**********************************************************************
177 template<typename EvalT, typename Traits>
178 void
181  typename Traits::EvalData /* workset */)
182 {
183 
184  sum.deep_copy(ScalarT(0.0));
185 
186  size_t rank = sum.rank();
187  const size_t length = sum.extent(0);
188  if (rank == 1 )
189  {
190  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<1> >(0, length), *this);
191  }
192  else if (rank == 2)
193  {
194  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<2> >(0, length), *this);
195  }
196  else if (rank == 3)
197  {
198  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<3> >(0, length), *this);
199  }
200  else if (rank == 4)
201  {
202  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<4> >(0, length), *this);
203  }
204  else if (rank == 5)
205  {
206  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<5> >(0, length), *this);
207  }
208  else if (rank == 6)
209  {
210  Kokkos::parallel_for(Kokkos::RangePolicy<PanzerSumTag<6> >(0, length), *this);
211  }
212  else
213  {
214  TEUCHOS_TEST_FOR_EXCEPTION(true, std::runtime_error, "ERROR: rank of sum is higher than supported");
215  }
216 }
217 
218 
219 //**********************************************************************
220 
221 template<typename EvalT, typename TRAITS,typename Tag0>
224 {
225  std::string sum_name = p.get<std::string>("Sum Name");
227  p.get<Teuchos::RCP<std::vector<std::string> > >("Values Names");
228  Teuchos::RCP<PHX::DataLayout> data_layout =
229  p.get< Teuchos::RCP<PHX::DataLayout> >("Data Layout");
230 
231  // sanity check
232  TEUCHOS_ASSERT(data_layout->rank()==1);
233 
234  sum = PHX::MDField<ScalarT,Tag0>(sum_name, data_layout);
235 
236  this->addEvaluatedField(sum);
237 
238  values.resize(value_names->size());
239  for (std::size_t i=0; i < value_names->size(); ++i) {
240  values[i] = PHX::MDField<const ScalarT,Tag0>( (*value_names)[i], data_layout);
241  this->addDependentField(values[i]);
242  }
243 
244  std::string n = "SumStatic Rank 1 Evaluator";
245  this->setName(n);
246 }
247 
248 //**********************************************************************
249 
250 template<typename EvalT, typename TRAITS,typename Tag0>
252 evaluateFields(typename TRAITS::EvalData /* d */)
253 {
254  sum.deep_copy(ScalarT(0.0));
255  for (std::size_t i = 0; i < sum.extent(0); ++i)
256  for (std::size_t d = 0; d < values.size(); ++d)
257  sum(i) += (values[d])(i);
258 }
259 
260 //**********************************************************************
261 //**********************************************************************
262 
263 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
266 {
267  std::string sum_name = p.get<std::string>("Sum Name");
269  p.get<Teuchos::RCP<std::vector<std::string> > >("Values Names");
270  Teuchos::RCP<PHX::DataLayout> data_layout =
271  p.get< Teuchos::RCP<PHX::DataLayout> >("Data Layout");
272 
273  // check if the user wants to scale each term independently
274  if(p.isType<Teuchos::RCP<const std::vector<double> > >("Scalars")) {
276  = p.get<Teuchos::RCP<const std::vector<double> > >("Scalars");
277 
278  // safety/sanity check
279  TEUCHOS_ASSERT(scalar_values->size()==value_names->size());
280  useScalars = true;
281 
282  Kokkos::View<double*,typename PHX::DevLayout<double>::type,PHX::Device> scalars_nc
283  = Kokkos::View<double*,typename PHX::DevLayout<double>::type,PHX::Device>("scalars",scalar_values->size());
284 
285  for(std::size_t i=0;i<scalar_values->size();i++)
286  scalars_nc(i) = (*scalar_values)[i];
287 
288  scalars = scalars_nc;
289  }
290  else {
291  useScalars = false;
292  }
293 
294  // sanity check
295  TEUCHOS_ASSERT(data_layout->rank()==2);
296 
297  sum = PHX::MDField<ScalarT,Tag0,Tag1>(sum_name, data_layout);
298 
299  this->addEvaluatedField(sum);
300 
301  values.resize(value_names->size());
302  for (std::size_t i=0; i < value_names->size(); ++i) {
303  values[i] = PHX::MDField<const ScalarT,Tag0,Tag1>( (*value_names)[i], data_layout);
304  this->addDependentField(values[i]);
305  }
306  numValues = value_names->size();
307  TEUCHOS_ASSERT(numValues<=MAX_VALUES);
308 
309  std::string n = "SumStatic Rank 2 Evaluator";
310  this->setName(n);
311 }
312 
313 //**********************************************************************
314 
315 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
317 SumStatic(const std::vector<PHX::Tag<typename EvalT::ScalarT>> & inputs,
318  const std::vector<double> & scalar_values,
319  const PHX::Tag<typename EvalT::ScalarT> & output)
320 {
321  TEUCHOS_ASSERT(scalar_values.size()==inputs.size());
322 
323  // check if the user wants to scale each term independently
324  if(scalars.size()==0) {
325  useScalars = false;
326  }
327  else {
328  useScalars = true;
329 
330  Kokkos::View<double*,typename PHX::DevLayout<double>::type,PHX::Device> scalars_nc
331  = Kokkos::View<double*,typename PHX::DevLayout<double>::type,PHX::Device>("scalars",scalar_values.size());
332 
333  for(std::size_t i=0;i<scalar_values.size();i++)
334  scalars_nc(i) = scalar_values[i];
335 
336  scalars = scalars_nc;
337  }
338 
339  // sanity check
340  TEUCHOS_ASSERT(inputs.size()<=MAX_VALUES);
341 
342  sum = output;
343  this->addEvaluatedField(sum);
344 
345  values.resize(inputs.size());
346  for (std::size_t i=0; i < inputs.size(); ++i) {
347  values[i] = inputs[i];
348  this->addDependentField(values[i]);
349  }
350 
351  numValues = inputs.size();
352 
353  std::string n = "SumStatic Rank 2 Evaluator";
354  this->setName(n);
355 }
356 
357 //**********************************************************************
358 
359 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
361 postRegistrationSetup(typename TRAITS::SetupData /* d */,
362  PHX::FieldManager<TRAITS>& /* fm */)
363 {
364  for (std::size_t i=0; i < values.size(); ++i)
365  value_views[i] = values[i].get_static_view();
366 }
367 
368 //**********************************************************************
369 
370 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
372 evaluateFields(typename TRAITS::EvalData /* d */)
373 {
374  sum.deep_copy(ScalarT(0.0));
375 
376  // Kokkos::parallel_for(sum.extent(0), *this);
377  if(useScalars)
378  Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device,ScalarsTag>(0,sum.extent(0)), *this);
379  else
380  Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device,NoScalarsTag>(0,sum.extent(0)), *this);
381 }
382 
383 //**********************************************************************
384 
385 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
386 KOKKOS_INLINE_FUNCTION
388 operator()(const ScalarsTag, const unsigned c ) const
389 {
390  for (int i=0;i<numValues;i++) {
391  for (int j = 0; j < sum.extent_int(1); ++j)
392  sum(c,j) += scalars(i)*value_views[i](c,j);
393  }
394 }
395 
396 //**********************************************************************
397 
398 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1>
399 KOKKOS_INLINE_FUNCTION
401 operator()(const NoScalarsTag, const unsigned c ) const
402 {
403  for (int i=0;i<numValues;i++) {
404  for (int j = 0; j < sum.extent_int(1); ++j)
405  sum(c,j) += value_views[i](c,j);
406  }
407 }
408 
409 
410 //**********************************************************************
411 //**********************************************************************
412 
413 /*
414 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1,typename Tag2>
415 SumStatic<EvalT,TRAITS,Tag0,Tag1,Tag2>::
416 SumStatic(const Teuchos::ParameterList& p)
417 {
418  std::string sum_name = p.get<std::string>("Sum Name");
419  Teuchos::RCP<std::vector<std::string> > value_names =
420  p.get<Teuchos::RCP<std::vector<std::string> > >("Values Names");
421  Teuchos::RCP<PHX::DataLayout> data_layout =
422  p.get< Teuchos::RCP<PHX::DataLayout> >("Data Layout");
423 
424  // sanity check
425  TEUCHOS_ASSERT(data_layout->rank()==3);
426 
427  sum = PHX::MDField<ScalarT,Tag0,Tag1,Tag2>(sum_name, data_layout);
428 
429  this->addEvaluatedField(sum);
430 
431  values.resize(value_names->size());
432  for (std::size_t i=0; i < value_names->size(); ++i) {
433  values[i] = PHX::MDField<ScalarT,Tag0,Tag1,Tag2>( (*value_names)[i], data_layout);
434  this->addDependentField(values[i]);
435  }
436 
437  std::string n = "Sum Evaluator";
438  this->setName(n);
439 }
440 */
441 
442 //**********************************************************************
443 
444 /*
445 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1,typename Tag2>
446 void SumStatic<EvalT,TRAITS,Tag0,Tag1,Tag2>::
447 evaluateFields(typename TRAITS::EvalData d)
448 {
449  sum.deep_copy(ScalarT(0.0));
450 
451  for (std::size_t d = 0; d < values.size(); ++d)
452  for (std::size_t i = 0; i < sum.extent(0); ++i)
453  for (std::size_t j = 0; j < sum.extent(1); ++j)
454  for (std::size_t k = 0; k < sum.extent(2); ++k)
455  sum(i,j,k) += (values[d])(i);
456 }
457 */
458 
459 //**********************************************************************
460 //**********************************************************************
461 
462 template<typename EvalT, typename TRAITS,typename Tag0,typename Tag1,typename Tag2>
464 buildStaticSumEvaluator(const std::string & sum_name,
465  const std::vector<std::string> & value_names,
466  const Teuchos::RCP<PHX::DataLayout> & data_layout)
467 {
469  p.set<std::string>("Sum Name",sum_name);
470  p.set<Teuchos::RCP<std::vector<std::string> > >("Values Names",Teuchos::rcp(new std::vector<std::string>(value_names)));
471  p.set< Teuchos::RCP<PHX::DataLayout> >("Data Layout",data_layout);
472 
474 }
475 
476 }
477 
478 #endif
Teuchos::RCP< PHX::Evaluator< TRAITS > > buildStaticSumEvaluator(const std::string &sum_name, const std::vector< std::string > &value_names, const Teuchos::RCP< PHX::DataLayout > &data_layout)
Sum(const Teuchos::ParameterList &p)
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
EvalT::ScalarT ScalarT
Definition: Panzer_Sum.hpp:120
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void postRegistrationSetup(typename Traits::SetupData d, PHX::FieldManager< Traits > &fm)
KOKKOS_INLINE_FUNCTION void operator()(PanzerSumTag< RANK >, const int &i) const
SumStatic(const Teuchos::ParameterList &p)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void evaluateFields(typename Traits::EvalData d)
typename EvalT::ScalarT ScalarT
Definition: Panzer_Sum.hpp:88
bool isType(const std::string &name) const
void evaluateFields(typename TRAITS::EvalData d)
#define TEUCHOS_ASSERT(assertion_test)