43 #ifndef PANZER_SUM_IMPL_HPP
44 #define PANZER_SUM_IMPL_HPP
50 #include "Phalanx_MDField_Utilities.hpp"
53 #define PANZER_USE_FAST_SUM 0
58 template<
typename EvalT,
typename Traits>
63 std::string sum_name = p.
get<std::string>(
"Sum Name");
69 TEUCHOS_ASSERT(static_cast<int>(value_names->size()) < MAX_VALUES);
72 auto local_scalars = Kokkos::View<double *,typename PHX::DevLayout<double>::type,PHX::Device>(
"scalars",value_names->size());
79 for (std::size_t i=0; i < value_names->size(); ++i)
80 local_scalars(i) = scalars_v[i];
83 for (std::size_t i=0; i < value_names->size(); ++i)
84 local_scalars(i) = 1.0;
87 scalars = local_scalars;
89 sum = PHX::MDField<ScalarT>(sum_name, data_layout);
91 this->addEvaluatedField(sum);
93 for (std::size_t i=0; i < value_names->size(); ++i) {
94 values[i] = PHX::MDField<const ScalarT>( (*value_names)[i], data_layout);
95 this->addDependentField(values[i]);
105 std::string n =
"Sum Evaluator";
110 template<
typename EvalT,
typename Traits>
117 cell_data_size = sum.size() / sum.fieldTag().dataLayout().extent(0);
122 template<
typename EvalT,
typename TRAITS>
123 template<
unsigned int RANK>
124 KOKKOS_INLINE_FUNCTION
126 auto num_vals = scalars.extent(0);
131 for (std::size_t iv = 0; iv < num_vals; ++iv)
132 sum(i) += scalars(iv)*(values[iv](i));
136 const size_t dim_1 = sum.extent(1);
137 for (std::size_t j = 0; j < dim_1; ++j)
138 for (std::size_t iv = 0; iv < num_vals; ++iv)
139 sum(i,j) += scalars(iv)*(values[iv](i,j));
143 const size_t dim_1 = sum.extent(1),dim_2 = sum.extent(2);
144 for (std::size_t j = 0; j < dim_1; ++j)
145 for (std::size_t k = 0; k < dim_2; ++k)
146 for (std::size_t iv = 0; iv < num_vals; ++iv)
147 sum(i,j,k) += scalars(iv)*(values[iv](i,j,k));
151 const size_t dim_1 = sum.extent(1),dim_2 = sum.extent(2),dim_3 = sum.extent(3);
152 for (std::size_t j = 0; j < dim_1; ++j)
153 for (std::size_t k = 0; k < dim_2; ++k)
154 for (std::size_t l = 0; l < dim_3; ++l)
155 for (std::size_t iv = 0; iv < num_vals; ++iv)
156 sum(i,j,k,l) += scalars(iv)*(values[iv](i,j,k,l));
160 const size_t dim_1 = sum.extent(1),dim_2 = sum.extent(2),dim_3 = sum.extent(3),dim_4 = sum.extent(4);
161 for (std::size_t j = 0; j < dim_1; ++j)
162 for (std::size_t k = 0; k < dim_2; ++k)
163 for (std::size_t l = 0; l < dim_3; ++l)
164 for (std::size_t m = 0; m < dim_4; ++m)
165 for (std::size_t iv = 0; iv < num_vals; ++iv)
166 sum(i,j,k,l,m) += scalars(iv)*(values[iv](i,j,k,l,m));
170 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);
171 for (std::size_t j = 0; j < dim_1; ++j)
172 for (std::size_t k = 0; k < dim_2; ++k)
173 for (std::size_t l = 0; l < dim_3; ++l)
174 for (std::size_t m = 0; m < dim_4; ++m)
175 for (std::size_t n = 0; n < dim_5; ++n)
176 for (std::size_t iv = 0; iv < num_vals; ++iv)
177 sum(i,j,k,l,m,n) += scalars(iv)*(values[iv](i,j,k,l,m,n));
182 template<
typename EvalT,
typename Traits>
191 #if PANZER_USE_FAST_SUM
193 for (std::size_t j = 0; j < values.size(); ++j) {
195 PHX::MDFieldIterator<ScalarT> sum_it(sum);
196 PHX::MDFieldIterator<const ScalarT> values_it(values[j]);
199 ! (sum_it.done() || values_it.done());
200 ++sum_it, ++values_it)
201 *sum_it += scalars[j]*(*values_it);
204 size_t rank = sum.rank();
205 const size_t length = sum.extent(0);
208 Kokkos::parallel_for(Kokkos::RangePolicy<
PanzerSumTag<1> >(0, length), *
this);
212 Kokkos::parallel_for(Kokkos::RangePolicy<
PanzerSumTag<2> >(0, length), *
this);
216 Kokkos::parallel_for(Kokkos::RangePolicy<
PanzerSumTag<3> >(0, length), *
this);
220 Kokkos::parallel_for(Kokkos::RangePolicy<
PanzerSumTag<4> >(0, length), *
this);
224 Kokkos::parallel_for(Kokkos::RangePolicy<
PanzerSumTag<5> >(0, length), *
this);
228 Kokkos::parallel_for(Kokkos::RangePolicy<
PanzerSumTag<6> >(0, length), *
this);
241 template<
typename EvalT,
typename TRAITS,
typename Tag0>
245 std::string sum_name = p.
get<std::string>(
"Sum Name");
254 sum = PHX::MDField<ScalarT,Tag0>(sum_name, data_layout);
256 this->addEvaluatedField(sum);
258 values.resize(value_names->size());
259 for (std::size_t i=0; i < value_names->size(); ++i) {
260 values[i] = PHX::MDField<const ScalarT,Tag0>( (*value_names)[i], data_layout);
261 this->addDependentField(values[i]);
264 std::string n =
"SumStatic Rank 1 Evaluator";
270 template<
typename EvalT,
typename TRAITS,
typename Tag0>
275 for (std::size_t i = 0; i < sum.extent(0); ++i)
276 for (std::size_t d = 0; d < values.size(); ++d)
277 sum(i) += (values[d])(i);
283 template<
typename EvalT,
typename TRAITS,
typename Tag0,
typename Tag1>
287 std::string sum_name = p.
get<std::string>(
"Sum Name");
302 Kokkos::View<double*,typename PHX::DevLayout<double>::type,PHX::Device> scalars_nc
303 = Kokkos::View<double*,typename PHX::DevLayout<double>::type,PHX::Device>(
"scalars",scalar_values->size());
305 for(std::size_t i=0;i<scalar_values->size();i++)
306 scalars_nc(i) = (*scalar_values)[i];
308 scalars = scalars_nc;
317 sum = PHX::MDField<ScalarT,Tag0,Tag1>(sum_name, data_layout);
319 this->addEvaluatedField(sum);
321 values.resize(value_names->size());
322 for (std::size_t i=0; i < value_names->size(); ++i) {
323 values[i] = PHX::MDField<const ScalarT,Tag0,Tag1>( (*value_names)[i], data_layout);
324 this->addDependentField(values[i]);
326 numValues = value_names->size();
329 std::string n =
"SumStatic Rank 2 Evaluator";
335 template<
typename EvalT,
typename TRAITS,
typename Tag0,
typename Tag1>
337 SumStatic(
const std::vector<PHX::Tag<typename EvalT::ScalarT>> & inputs,
338 const std::vector<double> & scalar_values,
339 const PHX::Tag<typename EvalT::ScalarT> & output)
344 if(scalars.size()==0) {
350 Kokkos::View<double*,typename PHX::DevLayout<double>::type,PHX::Device> scalars_nc
351 = Kokkos::View<double*,typename PHX::DevLayout<double>::type,PHX::Device>(
"scalars",scalar_values.size());
353 for(std::size_t i=0;i<scalar_values.size();i++)
354 scalars_nc(i) = scalar_values[i];
356 scalars = scalars_nc;
363 this->addEvaluatedField(sum);
365 values.resize(inputs.size());
366 for (std::size_t i=0; i < inputs.size(); ++i) {
367 values[i] = inputs[i];
368 this->addDependentField(values[i]);
371 numValues = inputs.size();
373 std::string n =
"SumStatic Rank 2 Evaluator";
379 template<
typename EvalT,
typename TRAITS,
typename Tag0,
typename Tag1>
384 for (std::size_t i=0; i < values.size(); ++i)
385 value_views[i] = values[i].get_static_view();
390 template<
typename EvalT,
typename TRAITS,
typename Tag0,
typename Tag1>
398 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device,ScalarsTag>(0,sum.extent(0)), *
this);
400 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device,NoScalarsTag>(0,sum.extent(0)), *
this);
405 template<
typename EvalT,
typename TRAITS,
typename Tag0,
typename Tag1>
406 KOKKOS_INLINE_FUNCTION
410 for (
int i=0;i<numValues;i++) {
411 for (
int j = 0; j < sum.extent_int(1); ++j)
412 sum(c,j) += scalars(i)*value_views[i](c,j);
418 template<
typename EvalT,
typename TRAITS,
typename Tag0,
typename Tag1>
419 KOKKOS_INLINE_FUNCTION
423 for (
int i=0;i<numValues;i++) {
424 for (
int j = 0; j < sum.extent_int(1); ++j)
425 sum(c,j) += value_views[i](c,j);
482 template<
typename EvalT,
typename TRAITS,
typename Tag0,
typename Tag1,
typename Tag2>
485 const std::vector<std::string> & value_names,
489 p.
set<std::string>(
"Sum Name",sum_name);
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)
#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
bool isType(const std::string &name) const
void evaluateFields(typename TRAITS::EvalData d)
#define TEUCHOS_ASSERT(assertion_test)