Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_Integrator_GradBasisDotVector_impl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Panzer: A partial differential equation assembly
4 // engine for strongly coupled complex multiphysics systems
5 //
6 // Copyright 2011 NTESS and the Panzer contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 
11 #ifndef __Panzer_Integrator_GradBasisDotVector_impl_hpp__
12 #define __Panzer_Integrator_GradBasisDotVector_impl_hpp__
13 
15 //
16 // Include Files
17 //
19 
20 // Panzer
21 #include "Panzer_BasisIRLayout.hpp"
25 
26 namespace panzer
27 {
29  //
30  // Main Constructor
31  //
33  template<typename EvalT, typename Traits>
37  const std::string& resName,
38  const std::string& fluxName,
39  const panzer::BasisIRLayout& basis,
40  const panzer::IntegrationRule& ir,
41  const double& multiplier, /* = 1 */
42  const std::vector<std::string>& fmNames, /* =
43  std::vector<std::string>() */
44  const Teuchos::RCP<PHX::DataLayout>& vecDL /* = Teuchos::null */)
45  :
46  evalStyle_(evalStyle),
47  multiplier_(multiplier),
48  basisName_(basis.name())
49  {
50  using PHX::View;
51  using panzer::BASIS;
52  using panzer::Cell;
54  using panzer::IP;
55  using PHX::DataLayout;
56  using PHX::MDField;
57  using std::invalid_argument;
58  using std::logic_error;
59  using std::string;
60  using Teuchos::RCP;
61 
62  // Ensure the input makes sense.
63  TEUCHOS_TEST_FOR_EXCEPTION(resName == "", invalid_argument, "Error: " \
64  "Integrator_GradBasisDotVector called with an empty residual name.")
65  TEUCHOS_TEST_FOR_EXCEPTION(fluxName == "", invalid_argument, "Error: " \
66  "Integrator_GradBasisDotVector called with an empty flux name.")
67  RCP<const PureBasis> tmpBasis = basis.getBasis();
68  TEUCHOS_TEST_FOR_EXCEPTION(not tmpBasis->supportsGrad(), logic_error,
69  "Error: Integrator_GradBasisDotVector: Basis of type \""
70  << tmpBasis->name() << "\" does not support the gradient operator.")
71  RCP<DataLayout> tmpVecDL = ir.dl_vector;
72  if (not vecDL.is_null())
73  {
74  tmpVecDL = vecDL;
76  tmpVecDL->extent(2) < ir.dl_vector->extent(2), logic_error,
77  "Integrator_GradBasisDotVector: Dimension of space exceeds " \
78  "dimension of Vector Data Layout.");
79  } // end if (not vecDL.is_null())
80 
81  // Create the field for the vector-valued function we're integrating.
82  vector_ = MDField<const ScalarT, Cell, IP, Dim>(fluxName, tmpVecDL);
83  this->addDependentField(vector_);
84 
85  // Create the field that we're either contributing to or evaluating
86  // (storing).
87  field_ = MDField<ScalarT, Cell, BASIS>(resName, basis.functional);
89  this->addContributedField(field_);
90  else // if (evalStyle_ == EvaluatorStyle::EVALUATES)
91  this->addEvaluatedField(field_);
92 
93  // Add the dependent field multipliers, if there are any.
94  int i(0);
95  fieldMults_.resize(fmNames.size());
96  kokkosFieldMults_ = PHX::View<PHX::UnmanagedView<const ScalarT**>*>("GradBasisDotVector::KokkosFieldMultipliers",
97  fmNames.size());
98  for (const auto& name : fmNames)
99  {
100  fieldMults_[i++] = MDField<const ScalarT, Cell, IP>(name, ir.dl_scalar);
101  this->addDependentField(fieldMults_[i - 1]);
102  } // end loop over the field multipliers
103 
104  // Set the name of this object.
105  string n("Integrator_GradBasisDotVector (");
107  n += "CONTRIBUTES";
108  else // if (evalStyle_ == EvaluatorStyle::EVALUATES)
109  n += "EVALUATES";
110  n += "): " + field_.fieldTag().name();
111  this->setName(n);
112  } // end of Main Constructor
113 
115  //
116  // ParameterList Constructor
117  //
119  template<typename EvalT, typename Traits>
122  const Teuchos::ParameterList& p)
123  :
125  panzer::EvaluatorStyle::EVALUATES,
126  p.get<std::string>("Residual Name"),
127  p.get<std::string>("Flux Name"),
128  (*p.get<Teuchos::RCP<panzer::BasisIRLayout>>("Basis")),
129  (*p.get<Teuchos::RCP<panzer::IntegrationRule>>("IR")),
130  p.get<double>("Multiplier"),
131  p.isType<Teuchos::RCP<const std::vector<std::string>>>
132  ("Field Multipliers") ?
133  (*p.get<Teuchos::RCP<const std::vector<std::string>>>
134  ("Field Multipliers")) : std::vector<std::string>(),
135  p.isType<Teuchos::RCP<PHX::DataLayout>>("Vector Data Layout") ?
136  p.get<Teuchos::RCP<PHX::DataLayout>>("Vector Data Layout") :
137  Teuchos::null)
138  {
140  using Teuchos::RCP;
141 
142  // Ensure that the input ParameterList didn't contain any bogus entries.
143  RCP<ParameterList> validParams = this->getValidParameters();
144  p.validateParameters(*validParams);
145  } // end of ParameterList Constructor
146 
148  //
149  // postRegistrationSetup()
150  //
152  template<typename EvalT, typename Traits>
153  void
156  typename Traits::SetupData sd,
157  PHX::FieldManager<Traits>& /* fm */)
158  {
159  using panzer::getBasisIndex;
160  using std::size_t;
161 
162  // Get the PHX::Views of the field multipliers.
163  auto field_mults_host_mirror = Kokkos::create_mirror_view(kokkosFieldMults_);
164  for (size_t i(0); i < fieldMults_.size(); ++i)
165  field_mults_host_mirror(i) = fieldMults_[i].get_static_view();
166  Kokkos::deep_copy(kokkosFieldMults_,field_mults_host_mirror);
167 
168  // Determine the index in the Workset bases for our particular basis name.
169  basisIndex_ = getBasisIndex(basisName_, (*sd.worksets_)[0], this->wda);
170 
171  // Allocate temporary if not using shared memory
172  bool use_shared_memory = panzer::HP::inst().useSharedMemory<ScalarT>();
173  if (!use_shared_memory) {
174  if (Sacado::IsADType<ScalarT>::value) {
175  const auto fadSize = Kokkos::dimension_scalar(field_.get_view());
176  tmp_ = PHX::View<ScalarT*>("GradBasisDotVector::tmp_",field_.extent(0),fadSize);
177  } else {
178  tmp_ = PHX::View<ScalarT*>("GradBasisDotVector::tmp_",field_.extent(0));
179  }
180  }
181  } // end of postRegistrationSetup()
182 
184  //
185  // operator()() NO shared memory
186  //
188  template<typename EvalT, typename Traits>
189  template<int NUM_FIELD_MULT>
190  KOKKOS_INLINE_FUNCTION
191  void
194  const FieldMultTag<NUM_FIELD_MULT>& /* tag */,
195  const Kokkos::TeamPolicy<PHX::exec_space>::member_type& team) const
196  {
198  const int cell = team.league_rank();
199 
200  // Initialize the evaluated field.
201  const int numQP(vector_.extent(1)), numDim(vector_.extent(2)),
202  numBases(basis_.extent(1));
203  if (evalStyle_ == EvaluatorStyle::EVALUATES)
204  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases), [&] (const int basis) {
205  field_(cell, basis) = 0.0;
206  });
207 
208  for (int qp(0); qp < numQP; ++qp) {
209  for (int dim(0); dim < numDim; ++dim) {
210  if constexpr (NUM_FIELD_MULT == 0) {
211  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases), [&] (const int basis) {
212  field_(cell, basis) += basis_(cell, basis, qp, dim) * multiplier_ * vector_(cell, qp, dim);
213  });
214  }
215  else if constexpr (NUM_FIELD_MULT == 1) {
216  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases), [&] (const int basis) {
217  field_(cell, basis) += basis_(cell, basis, qp, dim) * multiplier_ * vector_(cell, qp, dim)
218  * kokkosFieldMults_(0)(cell, qp);
219  });
220  }
221  else if constexpr (NUM_FIELD_MULT == 2) {
222  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases), [&] (const int basis) {
223  field_(cell, basis) += basis_(cell, basis, qp, dim) * multiplier_ * vector_(cell, qp, dim)
224  * kokkosFieldMults_(0)(cell, qp) * kokkosFieldMults_(1)(cell, qp);
225  });
226  }
227  else if constexpr (NUM_FIELD_MULT == 3) {
228  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases), [&] (const int basis) {
229  field_(cell, basis) += basis_(cell, basis, qp, dim) * multiplier_ * vector_(cell, qp, dim)
230  * kokkosFieldMults_(0)(cell, qp) * kokkosFieldMults_(1)(cell, qp) * kokkosFieldMults_(2)(cell, qp);
231  });
232  }
233  else {
234  Kokkos::abort("Panzer_Integrator_GradBasisDotVector: NUM_FIELD_MULT out of range!");
235  }
236  } // end dim loop
237  } // end qp loop
238  } // end of operator()()
239 
241  //
242  // operator()() With shared memory
243  //
245  template<typename EvalT, typename Traits>
246  template<int NUM_FIELD_MULT>
247  KOKKOS_INLINE_FUNCTION
248  void
251  const SharedFieldMultTag<NUM_FIELD_MULT>& /* tag */,
252  const Kokkos::TeamPolicy<PHX::exec_space>::member_type& team) const
253  {
255  const int cell = team.league_rank();
256  const int numQP = vector_.extent(1);
257  const int numDim = vector_.extent(2);
258  const int numBases = basis_.extent(1);
259  const int fadSize = Kokkos::dimension_scalar(field_.get_view());
260 
261  scratch_view tmp_field;
262  if (Sacado::IsADType<ScalarT>::value) {
263  tmp_field = scratch_view(team.team_shmem(),numBases,fadSize);
264  }
265  else {
266  tmp_field = scratch_view(team.team_shmem(),numBases);
267  }
268 
269  // Initialize the evaluated field.
270  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases), [&] (const int basis) {
271  tmp_field(basis) = 0.0;
272  });
273 
274  // The following if-block is for the sake of optimization depending on the
275  // number of field multipliers.
276  for (int qp(0); qp < numQP; ++qp) {
277  for (int dim(0); dim < numDim; ++dim) {
278  if constexpr (NUM_FIELD_MULT == 0) {
279  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases), [&] (const int basis) {
280  tmp_field(basis) += basis_(cell, basis, qp, dim) * multiplier_ * vector_(cell, qp, dim);
281  });
282  }
283  else if constexpr (NUM_FIELD_MULT == 1) {
284  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases), [&] (const int basis) {
285  tmp_field(basis) += basis_(cell, basis, qp, dim) * multiplier_ * vector_(cell, qp, dim)
286  * kokkosFieldMults_(0)(cell, qp);
287  });
288  }
289  else if constexpr (NUM_FIELD_MULT == 2) {
290  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases), [&] (const int basis) {
291  tmp_field(basis) += basis_(cell, basis, qp, dim) * multiplier_ * vector_(cell, qp, dim)
292  * kokkosFieldMults_(0)(cell, qp) * kokkosFieldMults_(1)(cell, qp);
293  });
294  }
295  else if constexpr (NUM_FIELD_MULT == 3) {
296  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases), [&] (const int basis) {
297  tmp_field(basis) += basis_(cell, basis, qp, dim) * multiplier_ * vector_(cell, qp, dim)
298  * kokkosFieldMults_(0)(cell, qp) * kokkosFieldMults_(1)(cell, qp) * kokkosFieldMults_(2)(cell, qp);
299  });
300  }
301  else {
302  Kokkos::abort("Panzer_Integrator_GradBasisDotVector: NUM_FIELD_MULT out of range!");
303  }
304  } // end loop over the dimensions of the vector field
305  } // end loop over the quadrature points
306 
307  // Put final values into target field
308  if (evalStyle_ == EvaluatorStyle::EVALUATES) {
309  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases),[&] (const int basis) {
310  field_(cell,basis) = tmp_field(basis);
311  });
312  }
313  else { // Contributed
314  Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,numBases),[&] (const int basis) {
315  field_(cell,basis) += tmp_field(basis);
316  });
317  }
318 
319  } // end of operator()()
320 
322  //
323  // evaluateFields()
324  //
326  template<typename EvalT, typename Traits>
327  void
330  typename Traits::EvalData workset)
331  {
332  using Kokkos::parallel_for;
333  using Kokkos::TeamPolicy;
334 
335  // Grab the basis information.
336  basis_ = this->wda(workset).bases[basisIndex_]->weighted_grad_basis;
337 
338  bool use_shared_memory = panzer::HP::inst().useSharedMemory<ScalarT>();
339  if (use_shared_memory) {
340  int bytes;
341  if (Sacado::IsADType<ScalarT>::value) {
342  const int fadSize = Kokkos::dimension_scalar(field_.get_view());
343  bytes = scratch_view::shmem_size(basis_.extent(1),fadSize);
344  }
345  else
346  bytes = scratch_view::shmem_size(basis_.extent(1));
347 
348  // The following if-block is for the sake of optimization depending on the
349  // number of field multipliers. The parallel_fors will loop over the cells
350  // in the Workset and execute operator()() above.
351  if (fieldMults_.size() == 0) {
352  auto policy = panzer::HP::inst().teamPolicy<ScalarT,SharedFieldMultTag<0>,PHX::Device>(workset.num_cells).set_scratch_size(0,Kokkos::PerTeam(bytes));
353  parallel_for(this->getName(), policy, *this);
354  } else if (fieldMults_.size() == 1) {
355  auto policy = panzer::HP::inst().teamPolicy<ScalarT,SharedFieldMultTag<1>,PHX::Device>(workset.num_cells).set_scratch_size(0,Kokkos::PerTeam(bytes));
356  parallel_for(this->getName(), policy, *this);
357  } else if (fieldMults_.size() == 2) {
358  auto policy = panzer::HP::inst().teamPolicy<ScalarT,SharedFieldMultTag<2>,PHX::Device>(workset.num_cells).set_scratch_size(0,Kokkos::PerTeam(bytes));
359  parallel_for(this->getName(), policy, *this);
360  } else if (fieldMults_.size() == 3) {
361  auto policy = panzer::HP::inst().teamPolicy<ScalarT,SharedFieldMultTag<3>,PHX::Device>(workset.num_cells).set_scratch_size(0,Kokkos::PerTeam(bytes));
362  parallel_for(this->getName(), policy, *this);
363  } else {
364  TEUCHOS_TEST_FOR_EXCEPTION(fieldMults_.size() > 3,std::runtime_error,
365  "ERROR: Panzer_Integrator_GradBasisDotVector supports up to three field multipliers! User requested "
366  << fieldMults_.size() << "!");
367  }
368  }
369  else {
370  // The following if-block is for the sake of optimization depending on the
371  // number of field multipliers. The parallel_fors will loop over the cells
372  // in the Workset and execute operator()() above.
373  if (fieldMults_.size() == 0) {
374  auto policy = panzer::HP::inst().teamPolicy<ScalarT,FieldMultTag<0>,PHX::Device>(workset.num_cells);
375  parallel_for(this->getName(), policy, *this);
376  } else if (fieldMults_.size() == 1) {
377  auto policy = panzer::HP::inst().teamPolicy<ScalarT,FieldMultTag<1>,PHX::Device>(workset.num_cells);
378  parallel_for(this->getName(), policy, *this);
379  } else if (fieldMults_.size() == 2) {
380  auto policy = panzer::HP::inst().teamPolicy<ScalarT,FieldMultTag<2>,PHX::Device>(workset.num_cells);
381  parallel_for(this->getName(), policy, *this);
382  } else if (fieldMults_.size() == 3) {
383  auto policy = panzer::HP::inst().teamPolicy<ScalarT,FieldMultTag<3>,PHX::Device>(workset.num_cells);
384  parallel_for(this->getName(), policy, *this);
385  } else {
386  TEUCHOS_TEST_FOR_EXCEPTION(fieldMults_.size() > 3,std::runtime_error,
387  "ERROR: Panzer_Integrator_GradBasisDotVector supports up to three field multipliers! User requested "
388  << fieldMults_.size() << "!");
389  }
390  }
391  } // end of evaluateFields()
392 
394  //
395  // getValidParameters()
396  //
398  template<typename EvalT, typename TRAITS>
402  {
403  using panzer::BasisIRLayout;
405  using PHX::DataLayout;
406  using std::string;
407  using std::vector;
409  using Teuchos::RCP;
410  using Teuchos::rcp;
411 
412  // Create a ParameterList with all the valid keys we support.
413  RCP<ParameterList> p = rcp(new ParameterList);
414  p->set<string>("Residual Name", "?");
415  p->set<string>("Flux Name", "?");
416  RCP<BasisIRLayout> basis;
417  p->set("Basis", basis);
419  p->set("IR", ir);
420  p->set<double>("Multiplier", 1.0);
422  p->set("Field Multipliers", fms);
423  RCP<DataLayout> vecDL;
424  p->set("Vector Data Layout", vecDL);
425  return p;
426  } // end of getValidParameters()
427 
428 } // end of namespace panzer
429 
430 #endif // __Panzer_Integrator_GradBasisDotVector_impl_hpp__
int num_cells
DEPRECATED - use: numCells()
PHX::MDField< const ScalarT, panzer::Cell, panzer::IP, panzer::Dim > vector_
A field representing the vector-valued function we&#39;re integrating ( ).
Integrator_GradBasisDotVector(const panzer::EvaluatorStyle &evalStyle, const std::string &resName, const std::string &fluxName, const panzer::BasisIRLayout &basis, const panzer::IntegrationRule &ir, const double &multiplier=1, const std::vector< std::string > &fmNames=std::vector< std::string >(), const Teuchos::RCP< PHX::DataLayout > &vecDL=Teuchos::null)
Main Constructor.
Kokkos::TeamPolicy< TeamPolicyProperties...> teamPolicy(const int &league_size)
Returns a TeamPolicy for hierarchic parallelism.
PHX::MDField< ScalarT, panzer::Cell, panzer::BASIS > field_
A field to which we&#39;ll contribute, or in which we&#39;ll store, the result of computing this integral...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< Teuchos::ParameterList > getValidParameters() const
Get Valid Parameters.
KOKKOS_INLINE_FUNCTION void operator()(const FieldMultTag< NUM_FIELD_MULT > &tag, const Kokkos::TeamPolicy< PHX::exec_space >::member_type &team) const
Perform the integration.
This empty struct allows us to optimize operator()() depending on the number of field multipliers...
EvaluatorStyle
An indication of how an Evaluator will behave.
const panzer::EvaluatorStyle evalStyle_
An enum determining the behavior of this Evaluator.
Teuchos::RCP< const PureBasis > getBasis() const
std::vector< PHX::MDField< const ScalarT, panzer::Cell, panzer::IP > > fieldMults_
The (possibly empty) list of fields that are multipliers out in front of the integral ( ...
double multiplier
The scalar multiplier out in front of the integral ( ).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< PHX::DataLayout > dl_scalar
Data layout for scalar fields.
panzer::EvaluatorStyle evalStyle
The EvaluatorStyle of the parent Integrator_CurlBasisDotVector object.
std::vector< std::string >::size_type getBasisIndex(std::string basis_name, const panzer::Workset &workset, WorksetDetailsAccessor &wda)
Returns the index in the workset bases for a particular BasisIRLayout name.
void evaluateFields(typename Traits::EvalData d)
Evaluate Fields.
PHX::View< PHX::UnmanagedView< const ScalarT ** > * > kokkosFieldMults_
The PHX::View representation of the (possibly empty) list of fields that are multipliers out in front...
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
void postRegistrationSetup(typename Traits::SetupData d, PHX::FieldManager< Traits > &fm)
Post-Registration Setup.
Teuchos::RCP< PHX::DataLayout > dl_vector
Data layout for vector fields.
static HP & inst()
Private ctor.
const std::vector< std::pair< int, LocalOrdinal > > &pid_and_lid const
This empty struct allows us to optimize operator()() depending on the number of field multipliers...
Teuchos::RCP< PHX::DataLayout > functional
&lt;Cell,Basis&gt;
Teuchos::RCP< const std::vector< panzer::Workset > > worksets_
bool is_null() const