Support Software for Vector Reduction/Transformation Operators  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RTOpPack_RTOpTHelpers_decl.hpp
1 // @HEADER
2 // *****************************************************************************
3 // RTOp: Interfaces and Support Software for Vector Reduction Transformation
4 // Operations
5 //
6 // Copyright 2006 NTESS and the RTOp contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 
11 #ifndef RTOPPACK_RTOP_T_HELPERS_DECL_HPP
12 #define RTOPPACK_RTOP_T_HELPERS_DECL_HPP
13 
14 
15 //#define RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
16 
17 
18 #include <typeinfo>
19 
20 
21 #include "RTOpPack_RTOpT.hpp"
23 #include "Teuchos_ScalarTraits.hpp"
24 #include "Teuchos_dyn_cast.hpp"
26 
27 
28 #ifdef RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
29 # include "Teuchos_VerboseObject.hpp"
30 namespace RTOpPack { extern bool rtop_helpers_dump_all; }
31 #endif // RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
32 
33 
34 namespace RTOpPack {
35 
36 
41 template<class Scalar>
42 struct ScalarIndex {
44  Scalar scalar;
46  Ordinal index;
48  ScalarIndex( const Scalar &_scalar, const Ordinal &_index )
49  : scalar(_scalar), index(_index)
50  {}
53  : scalar(ScalarTraits<Scalar>::zero()), index(-1)
54  {}
55 };
56 
57 
62 template<class Scalar>
63 std::ostream& operator<<(std::ostream &out, const ScalarIndex<Scalar> &scalarIndex)
64 {
65  out << "{"<<scalarIndex.scalar<<","<<scalarIndex.index<<"}";
66  return out;
67 }
68 
69 
73 template <class Scalar>
74 class PrimitiveTypeTraits<Scalar, ScalarIndex<Scalar> > {
75 public:
81  static int numPrimitiveObjs() { return ScalarPrimitiveTypeTraits::numPrimitiveObjs(); }
83  static int numIndexObjs() { return 1; }
85  static int numCharObjs() { return 0; }
87  static void extractPrimitiveObjs(
88  const ScalarIndex<Scalar> &obj,
89  const ArrayView<primitiveType> &primitiveObjs,
90  const ArrayView<index_type> &indexObjs,
91  const ArrayView<char> &charObjs
92  )
93  {
94  assertInput(primitiveObjs, indexObjs, charObjs);
95  ScalarPrimitiveTypeTraits::extractPrimitiveObjs(
96  obj.scalar, primitiveObjs, Teuchos::null, Teuchos::null );
97  indexObjs[0] = obj.index;
98  }
100  static void loadPrimitiveObjs(
101  const ArrayView<const primitiveType> &primitiveObjs,
102  const ArrayView<const index_type> &indexObjs,
103  const ArrayView<const char> &charObjs,
104  const Ptr<ScalarIndex<Scalar> > &obj
105  )
106  {
107  assertInput(primitiveObjs, indexObjs, charObjs);
108  ScalarPrimitiveTypeTraits::loadPrimitiveObjs(
109  primitiveObjs, Teuchos::null, Teuchos::null,
110  Teuchos::outArg(obj->scalar) );
111  obj->index = indexObjs[0];
112  }
113 private:
114  static void assertInput(
115  const ArrayView<const primitiveType> &primitiveObjs,
116  const ArrayView<const index_type> &indexObjs,
117  const ArrayView<const char> &charObjs
118  )
119  {
120 #ifdef TEUCHOS_DEBUG
122  primitiveObjs.size()!=ScalarPrimitiveTypeTraits::numPrimitiveObjs()
123  || indexObjs.size()!=1
124  || charObjs.size()!=0 );
125 #else
126  (void)primitiveObjs;
127  (void)indexObjs;
128  (void)charObjs;
129 #endif
130  }
131 };
132 
133 
138 template<class ConcreteReductObj>
140 public:
142  DefaultReductTarget( const ConcreteReductObj &concreteReductObj )
143  : concreteReductObj_(concreteReductObj)
144  {}
146  void set( const ConcreteReductObj &concreteReductObj )
147  { concreteReductObj_ = concreteReductObj; }
149  const ConcreteReductObj& get() const
150  { return concreteReductObj_; }
152  std::string description() const;
153 private:
154  ConcreteReductObj concreteReductObj_;
155 };
156 
157 
162 template<class ConcreteReductObj>
163 const RCP<DefaultReductTarget<ConcreteReductObj> >
164 defaultReductTarget( const ConcreteReductObj &concreteReductObj )
165 {
166  return Teuchos::rcp(
167  new DefaultReductTarget<ConcreteReductObj>(concreteReductObj));
168 }
169 
170 
193 template<class Scalar>
194 void validate_apply_op(
195  const RTOpT<Scalar> &op,
196  const int allowed_num_sub_vecs,
197  const int allowed_num_targ_sub_vecs,
198  const bool expect_reduct_obj,
199  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
200  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
201  const Ptr<const ReductTarget> &reduct_obj
202  );
203 
204 
205 //
206 // Reduction object operator support
207 //
208 
209 
211 enum EBasicReductTypes { REDUCT_TYPE_SUM, REDUCT_TYPE_MAX, REDUCT_TYPE_MIN };
212 
213 
215 template<class ConcreteReductObj, int ReductionType>
217 public:
219  inline void operator()(const ConcreteReductObj& in_reduct, ConcreteReductObj& inout_reduct) const
220  {
221  return in_reduct.this_reduction_type_needs_a_specialization();
222  }
223 };
224 
225 
227 template<class ConcreteReductObj>
228 class BasicReductObjReductionOp<ConcreteReductObj, REDUCT_TYPE_SUM> {
229 public:
231  inline void operator()(const ConcreteReductObj& in_reduct, ConcreteReductObj& inout_reduct) const
232  {
233  inout_reduct += in_reduct;
234  }
235 };
236 
237 
239 template<class ConcreteReductObj>
240 class BasicReductObjReductionOp<ConcreteReductObj, REDUCT_TYPE_MAX> {
241 public:
243  inline void operator()(const ConcreteReductObj& in_reduct, ConcreteReductObj& inout_reduct) const
244  {
245  inout_reduct = std::max(inout_reduct, in_reduct);
246  }
247 };
248 
249 
251 template<class ConcreteReductObj>
252 class BasicReductObjReductionOp<ConcreteReductObj, REDUCT_TYPE_MIN> {
253 public:
255  inline void operator()(const ConcreteReductObj& in_reduct, ConcreteReductObj& inout_reduct) const
256  {
257  inout_reduct = std::min(inout_reduct, in_reduct);
258  }
259 };
260 
261 
263 template<class Scalar>
265 public:
267  inline void operator()(const Scalar& in_reduct, Scalar& inout_reduct) const
268  {
269  inout_reduct += in_reduct;
270  }
271 };
272 // 2008/07/03: rabart: Above: I have broken from the Thyra guideline of
273 // passing in-out arguments as const Ptr<Type>& and used raw non-const
274 // reference Type& instead to allow the user function to be more readable.
275 
276 
278 template<class Scalar, class ConcreteReductObj, class ReductObjReduction>
279 class ROpScalarReductionWithOpBase : public RTOpT<Scalar>
280 {
281 public:
282 
285 
288 
291  const ConcreteReductObj &initReductObjValue_in = ScalarTraits<Scalar>::zero(),
292  ReductObjReduction reductObjReduction_in = ReductObjReduction()
293  )
294  : initReductObjValue_(initReductObjValue_in),
295  reductObjReduction_(reductObjReduction_in)
296  {}
297 
299  const ConcreteReductObj& getRawVal( const ReductTarget &reduct_obj ) const
300  {
301  using Teuchos::dyn_cast;
302  return dyn_cast<const DefaultReductTarget<ConcreteReductObj> >(reduct_obj).get();
303  }
304 
306  void setRawVal( const ConcreteReductObj &rawVal,
307  const Ptr<ReductTarget> &reduct_obj
308  ) const
309  {
310  using Teuchos::dyn_cast;
311  dyn_cast<DefaultReductTarget<ConcreteReductObj> >(*reduct_obj).set(rawVal);
312  }
313 
315  ConcreteReductObj operator()(const ReductTarget& reduct_obj ) const
316  {
317  return this->getRawVal(reduct_obj);
318  }
319 
322 
325  const Ptr<int> &num_values,
326  const Ptr<int> &num_indexes,
327  const Ptr<int> &num_chars
328  ) const
329  {
331  *num_values = PTT::numPrimitiveObjs();
332  *num_indexes = PTT::numIndexObjs();
333  *num_chars = PTT::numCharObjs();
334  }
335 
338  {
339  return Teuchos::rcp(
340  new DefaultReductTarget<ConcreteReductObj>(initReductObjValue()));
341  }
342 
345  const ReductTarget& in_reduct_obj, const Ptr<ReductTarget>& inout_reduct_obj
346  ) const
347  {
348  const ConcreteReductObj scalar_in_reduct_obj = this->getRawVal(in_reduct_obj);
349  ConcreteReductObj scalar_inout_reduct_obj = this->getRawVal(*inout_reduct_obj);
350  reductObjReduction_(scalar_in_reduct_obj, scalar_inout_reduct_obj);
351  this->setRawVal( scalar_inout_reduct_obj, inout_reduct_obj );
352  }
353 
355  void reduct_obj_reinit_impl( const Ptr<ReductTarget> &reduct_obj ) const
356  {
357  setRawVal( initReductObjValue(), reduct_obj );
358  }
359 
362  const ReductTarget &reduct_obj,
363  const ArrayView<primitive_value_type> &value_data,
364  const ArrayView<index_type> &index_data,
365  const ArrayView<char_type> &char_data
366  ) const
367  {
369  PTT::extractPrimitiveObjs( getRawVal(reduct_obj),
370  value_data, index_data, char_data );
371  }
372 
375  const ArrayView<const primitive_value_type> &value_data,
376  const ArrayView<const index_type> &index_data,
377  const ArrayView<const char_type> &char_data,
378  const Ptr<ReductTarget> &reduct_obj
379  ) const
380  {
382  ConcreteReductObj concrete_reduct_obj;
383  PTT::loadPrimitiveObjs( value_data, index_data, char_data,
384  Teuchos::outArg(concrete_reduct_obj) );
385  this->setRawVal( concrete_reduct_obj, reduct_obj );
386  }
387 
389 
390 protected:
391 
393  STANDARD_MEMBER_COMPOSITION_MEMBERS( ConcreteReductObj, initReductObjValue );
394 
395 private:
396 
397  ReductObjReduction reductObjReduction_;
398 
399 };
400 
401 
402 //
403 // ROp 1 vector scalar reduction
404 //
405 
406 
408 template<class Scalar, class ConcreteReductObj, class EleWiseReduction,
409  class ReductObjReduction = SumScalarReductObjReduction<ConcreteReductObj> >
411  : public ROpScalarReductionWithOpBase<Scalar, ConcreteReductObj, ReductObjReduction>
412 {
413 public:
414 
417 
420  const ConcreteReductObj &initReductObjValue_in = ConcreteReductObj(),
421  EleWiseReduction eleWiseReduction_in = EleWiseReduction(),
422  ReductObjReduction reductObjReduction_in = ReductObjReduction()
423  )
424  : base_t(initReductObjValue_in, reductObjReduction_in),
425  eleWiseReduction_(eleWiseReduction_in)
426  {}
427 
430 
433  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
434  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
435  const Ptr<ReductTarget> &reduct_obj_inout
436  ) const
437  {
438  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
439  using Teuchos::dyn_cast;
440 
441 #ifdef TEUCHOS_DEBUG
442  validate_apply_op<Scalar>(*this, 1, 0, true,
443  sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
444 #else
445  (void)targ_sub_vecs;
446 #endif
447 
449  dyn_cast<DefaultReductTarget<ConcreteReductObj> >(*reduct_obj_inout);
450  ConcreteReductObj reduct = reduct_obj.get();
451 
452  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
453 
454  const_iter_t v0_val = sub_vecs[0].values().begin();
455  const ptrdiff_t v0_s = sub_vecs[0].stride();
456 
457  if ( v0_s == 1 ) {
458  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
459  eleWiseReduction_( *v0_val++, reduct);
460  }
461  else {
462  for( Teuchos_Ordinal i = 0; i < subDim; ++i, v0_val += v0_s )
463  eleWiseReduction_( *v0_val, reduct);
464  }
465 
466  reduct_obj.set(reduct);
467 
468  }
469 
471 
472 private:
473 
474  EleWiseReduction eleWiseReduction_;
475 
476 };
477 
478 
480 #define RTOP_ROP_1_REDUCT_SCALAR_CUSTOM_DEFAULT( ROP_CLASS_NAME, REDUCT_SCALAR, \
481  BASIC_REDUCT_TYPE_ENUM, CUSTOM_DEFAULT \
482  ) \
483  \
484  template<class Scalar, class ReductScalar> \
485  class ROP_CLASS_NAME ## EleWiseReduction \
486  { \
487  public: \
488  inline void operator()( \
489  const Scalar &v0, \
490  ReductScalar &reduct \
491  ) const; \
492  }; \
493  \
494  \
495  template<class Scalar> \
496  class ROP_CLASS_NAME \
497  : public RTOpPack::ROp_1_ScalarReduction< \
498  Scalar, \
499  REDUCT_SCALAR, \
500  ROP_CLASS_NAME ## EleWiseReduction<Scalar, REDUCT_SCALAR >, \
501  RTOpPack::BasicReductObjReductionOp<REDUCT_SCALAR, BASIC_REDUCT_TYPE_ENUM> > \
502  { \
503  typedef RTOpPack::ROp_1_ScalarReduction< \
504  Scalar, \
505  REDUCT_SCALAR, \
506  ROP_CLASS_NAME ## EleWiseReduction<Scalar, REDUCT_SCALAR >, \
507  RTOpPack::BasicReductObjReductionOp<REDUCT_SCALAR, BASIC_REDUCT_TYPE_ENUM> > \
508  base_t; \
509  public: \
510  ROP_CLASS_NAME() \
511  : base_t(CUSTOM_DEFAULT) \
512  { \
513  this->setOpNameBase( #ROP_CLASS_NAME ); \
514  } \
515  }; \
516  \
517  \
518  template<class Scalar, class ReductScalar> \
519  void ROP_CLASS_NAME ## EleWiseReduction<Scalar, ReductScalar>::operator()( \
520  const Scalar &v0, ReductScalar &reduct \
521  ) const
522 
523 
525 #define RTOP_ROP_1_REDUCT_SCALAR( ROP_CLASS_NAME, REDUCT_SCALAR, \
526  BASIC_REDUCT_TYPE_ENUM \
527  ) \
528  RTOP_ROP_1_REDUCT_SCALAR_CUSTOM_DEFAULT(ROP_CLASS_NAME, REDUCT_SCALAR, \
529  BASIC_REDUCT_TYPE_ENUM, Teuchos::ScalarTraits<REDUCT_SCALAR >::zero() )
530 
531 
532 //
533 // ROp 1 coordinate-variant vector scalar reduction
534 //
535 
536 
539 template<
540  class Scalar,
541  class ReductScalar,
542  class EleWiseReduction,
543  class ReductObjReduction = SumScalarReductObjReduction<ReductScalar>
544  >
546  : public ROpScalarReductionWithOpBase<Scalar, ReductScalar, ReductObjReduction>
547 {
548 public:
549 
552 
555 
558  const ReductScalar &initReductObjValue_in = ReductScalar(),
559  EleWiseReduction eleWiseReduction_in = EleWiseReduction(),
560  ReductObjReduction reductObjReduction_in = ReductObjReduction()
561  )
562  : base_t(initReductObjValue_in, reductObjReduction_in),
563  eleWiseReduction_(eleWiseReduction_in)
564  {}
565 
567  void setEleWiseReduction(EleWiseReduction eleWiseReduction_in)
568  { eleWiseReduction_ = eleWiseReduction_in; }
569 
571  const EleWiseReduction& getEleWiseReduction() const
572  { return eleWiseReduction_; }
573 
575 
578 
580  bool coord_invariant_impl() const { return false; }
581 
584  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
585  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
586  const Ptr<ReductTarget> &reduct_obj_inout
587  ) const
588  {
589  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
590  using Teuchos::dyn_cast;
591 
592 #ifdef TEUCHOS_DEBUG
593  validate_apply_op<Scalar>(*this, 1, 0, true,
594  sub_vecs, targ_sub_vecs, reduct_obj_inout);
595 #else
596  (void)targ_sub_vecs;
597 #endif
598 
600  dyn_cast<DefaultReductTarget<ReductScalar> >(*reduct_obj_inout);
601  ReductScalar reduct = reduct_obj.get();
602 
603  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
604 
605  const_iter_t v0_val = sub_vecs[0].values().begin();
606  const ptrdiff_t v0_s = sub_vecs[0].stride();
607 
608  RTOpPack::index_type global_i = sub_vecs[0].globalOffset();
609 
610  if ( v0_s == 1 ) {
611  for( Teuchos_Ordinal i = 0; i < subDim; ++i, ++global_i )
612  eleWiseReduction_( global_i, *v0_val++, reduct);
613  }
614  else {
615  for( Teuchos_Ordinal i = 0; i < subDim; ++i, v0_val += v0_s, ++global_i )
616  eleWiseReduction_( global_i, *v0_val, reduct);
617  }
618 
619  reduct_obj.set(reduct);
620 
621  }
622 
624 
625 private:
626 
627  EleWiseReduction eleWiseReduction_;
628 
629 };
630 
631 
632 //
633 // ROp 2 vector scalar reduction
634 //
635 
636 
638 template<
639  class Scalar,
640  class ReductScalar,
641  class EleWiseReduction,
642  class ReductObjReduction = SumScalarReductObjReduction<ReductScalar>
643  >
645  : public ROpScalarReductionWithOpBase<Scalar, ReductScalar, ReductObjReduction>
646 {
647 public:
648 
652 
655  const ReductScalar &initReductObjValue_in = ReductScalar(),
656  EleWiseReduction eleWiseReduction_in = EleWiseReduction(),
657  ReductObjReduction reductObjReduction_in = ReductObjReduction()
658  )
659  : base_t(initReductObjValue_in, reductObjReduction_in),
660  eleWiseReduction_(eleWiseReduction_in)
661  {}
662 
665 
668  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
669  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
670  const Ptr<ReductTarget> &reduct_obj_inout
671  ) const
672  {
673  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
674  using Teuchos::dyn_cast;
675 
676 #ifdef TEUCHOS_DEBUG
677  validate_apply_op<Scalar>(*this, 2, 0, true,
678  sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
679 #else
680  (void)targ_sub_vecs;
681 #endif
682 
683  DefaultReductTarget<Scalar> &reduct_obj =
684  dyn_cast<DefaultReductTarget<Scalar> >(*reduct_obj_inout);
685  Scalar reduct = reduct_obj.get();
686 
687  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
688 
689  const_iter_t v0_val = sub_vecs[0].values().begin();
690  const ptrdiff_t v0_s = sub_vecs[0].stride();
691  const_iter_t v1_val = sub_vecs[1].values().begin();
692  const ptrdiff_t v1_s = sub_vecs[1].stride();
693 
694  if( v0_s == 1 && v1_s == 1 ) {
695  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
696  eleWiseReduction_( *v0_val++, *v1_val++, reduct);
697  }
698  else {
699  for( Teuchos_Ordinal i = 0; i < subDim; ++i, v0_val += v0_s, v1_val += v1_s )
700  eleWiseReduction_( *v0_val, *v1_val, reduct);
701  }
702 
703  reduct_obj.set(reduct);
704 
705  }
706 
708 
709 private:
710 
711  EleWiseReduction eleWiseReduction_;
712 
713 };
714 
715 
719 #define RTOP_ROP_2_REDUCT_SCALAR( ROP_CLASS_NAME, REDUCT_SCALAR, \
720  BASIC_REDUCT_TYPE_ENUM \
721  ) \
722  \
723  template<class Scalar, class ReductScalar> \
724  class ROP_CLASS_NAME ## EleWiseReduction \
725  { \
726  public: \
727  inline void operator()(const Scalar &v0, \
728  const Scalar &v1, \
729  ReductScalar &reduct \
730  ) const; \
731  }; \
732  \
733  \
734  template<class Scalar> \
735  class ROP_CLASS_NAME \
736  : public RTOpPack::ROp_2_ScalarReduction< \
737  Scalar, \
738  REDUCT_SCALAR, \
739  ROP_CLASS_NAME ## EleWiseReduction<Scalar, REDUCT_SCALAR >, \
740  RTOpPack::BasicReductObjReductionOp<REDUCT_SCALAR, BASIC_REDUCT_TYPE_ENUM> > \
741  { \
742  public: \
743  ROP_CLASS_NAME() \
744  { \
745  this->setOpNameBase( #ROP_CLASS_NAME ); \
746  this->initReductObjValue(ScalarTraits<REDUCT_SCALAR >::zero()); \
747  } \
748  }; \
749  \
750  template<class Scalar, class ReductScalar> \
751  void ROP_CLASS_NAME ## EleWiseReduction<Scalar, ReductScalar>::operator()( \
752  const Scalar &v0, const Scalar &v1, ReductScalar &reduct) const
753 
754 
755 //
756 // TOp 0 to 1 vector transformation
757 //
758 
759 
761 template<class Scalar, class EleWiseTransformation>
762 class TOp_0_1_Base : public RTOpT<Scalar>
763 {
764 public:
765 
768  EleWiseTransformation eleWiseTransformation = EleWiseTransformation()
769  )
770  : eleWiseTransformation_(eleWiseTransformation)
771  {}
772 
775 
778  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
779  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
780  const Ptr<ReductTarget> &reduct_obj_inout
781  ) const
782  {
783  typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
784 
785 #ifdef TEUCHOS_DEBUG
786  validate_apply_op<Scalar>(*this, 0, 1, false,
787  sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
788 #else
789  (void)sub_vecs;
790  (void)reduct_obj_inout;
791 #endif
792 
793  const RTOpPack::index_type subDim = targ_sub_vecs[0].subDim();
794 
795  iter_t z0_val = targ_sub_vecs[0].values().begin();
796  const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
797 
798  if ( z0_s == 1 ) {
799  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
800  eleWiseTransformation_( *z0_val++);
801  }
802  else {
803  for( Teuchos_Ordinal i = 0; i < subDim; ++i, z0_val += z0_s )
804  eleWiseTransformation_( *z0_val);
805  }
806 
807  }
808 
810 
811 private:
812 
813  EleWiseTransformation eleWiseTransformation_;
814 
815 };
816 
817 
820 template<class Scalar, class EleWiseTransformation>
821 class TOp_0_1_CoordVariantBase : public RTOpT<Scalar>
822 {
823 public:
824 
827  EleWiseTransformation eleWiseTransformation = EleWiseTransformation()
828  )
829  : eleWiseTransformation_(eleWiseTransformation)
830  {}
831 
833  void setEleWiseTransformation(EleWiseTransformation eleWiseTransformation)
834  {
835  eleWiseTransformation_ = eleWiseTransformation;
836  }
837 
839  const EleWiseTransformation& getEleWiseTransformation() const
840  {
841  return eleWiseTransformation_;
842  }
843 
846 
848  bool coord_invariant_impl() const { return false; }
849 
852  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
853  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
854  const Ptr<ReductTarget> &reduct_obj_inout
855  ) const
856  {
857  typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
858 
859 #ifdef TEUCHOS_DEBUG
860  validate_apply_op<Scalar>(*this, 0, 1, false,
861  sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
862 #else
863  (void)sub_vecs;
864  (void)reduct_obj_inout;
865 #endif
866 
867  const RTOpPack::index_type subDim = targ_sub_vecs[0].subDim();
868 
869  iter_t z0_val = targ_sub_vecs[0].values().begin();
870  const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
871 
872  RTOpPack::index_type global_i = targ_sub_vecs[0].globalOffset();
873 
874  if ( z0_s == 1 ) {
875  for( Teuchos_Ordinal i = 0; i < subDim; ++i, ++global_i )
876  eleWiseTransformation_(global_i, *z0_val++);
877  }
878  else {
879  for( Teuchos_Ordinal i = 0; i < subDim; ++i, z0_val += z0_s, ++global_i )
880  eleWiseTransformation_(global_i, *z0_val);
881  }
882 
883  }
884 
886 
887 private:
888 
889  EleWiseTransformation eleWiseTransformation_;
890 
891 };
892 
893 
894 //
895 // TOp 1 to 1 vector transformation
896 //
897 
898 
900 template<class Scalar, class EleWiseTransformation>
901 class TOp_1_1_Base : public RTOpT<Scalar>
902 {
903 public:
904 
907  EleWiseTransformation eleWiseTransformation = EleWiseTransformation()
908  )
909  : eleWiseTransformation_(eleWiseTransformation)
910  {}
911 
914 
917  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
918  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
919  const Ptr<ReductTarget> &reduct_obj_inout
920  ) const
921  {
922  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
923  typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
924 
925 #ifdef TEUCHOS_DEBUG
926  validate_apply_op<Scalar>(*this, 1, 1, false,
927  sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
928 #else
929  (void)reduct_obj_inout;
930 #endif
931 
932  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
933 
934  const_iter_t v0_val = sub_vecs[0].values().begin();
935  const ptrdiff_t v0_s = sub_vecs[0].stride();
936 
937  iter_t z0_val = targ_sub_vecs[0].values().begin();
938  const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
939 
940  if ( v0_s == 1 && z0_s == 1 ) {
941  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
942  eleWiseTransformation_( *v0_val++, *z0_val++);
943  }
944  else {
945  for( Teuchos_Ordinal i = 0; i < subDim; ++i, v0_val += v0_s, z0_val += z0_s )
946  eleWiseTransformation_( *v0_val, *z0_val);
947  }
948 
949  }
950 
952 
953 private:
954 
955  EleWiseTransformation eleWiseTransformation_;
956 
957 };
958 
959 
961 #define RTOP_TOP_1_1( TOP_CLASS_NAME ) \
962  \
963  template<class Scalar> \
964  class TOP_CLASS_NAME ## EleWiseTransformation \
965  { \
966  public: \
967  inline void operator()( const Scalar &v0, Scalar &z0 ) const; \
968  }; \
969  \
970  \
971  template<class Scalar> \
972  class TOP_CLASS_NAME \
973  : public RTOpPack::TOp_1_1_Base< Scalar, \
974  TOP_CLASS_NAME ## EleWiseTransformation<Scalar> > \
975  { \
976  public: \
977  TOP_CLASS_NAME() \
978  { \
979  this->setOpNameBase( #TOP_CLASS_NAME ); \
980  } \
981  }; \
982  \
983  \
984  template<class Scalar> \
985  void TOP_CLASS_NAME ## EleWiseTransformation<Scalar>::operator()( \
986  const Scalar &v0, Scalar &z0 \
987  ) const
988 
989 
990 //
991 // TOp 2 to 1 vector transformation
992 //
993 
994 
996 template<class Scalar, class EleWiseTransformation>
997 class TOp_2_1_Base : public RTOpT<Scalar>
998 {
999 public:
1000 
1003  EleWiseTransformation eleWiseTransformation = EleWiseTransformation()
1004  )
1005  : eleWiseTransformation_(eleWiseTransformation)
1006  {}
1007 
1010 
1013  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
1014  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
1015  const Ptr<ReductTarget> &reduct_obj_inout
1016  ) const
1017  {
1018  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
1019  typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
1020 
1021 #ifdef TEUCHOS_DEBUG
1022  validate_apply_op<Scalar>(*this, 2, 1, false,
1023  sub_vecs, targ_sub_vecs, reduct_obj_inout);
1024 #else
1025  (void)reduct_obj_inout;
1026 #endif
1027 
1028  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
1029 
1030  const_iter_t v0_val = sub_vecs[0].values().begin();
1031  const ptrdiff_t v0_s = sub_vecs[0].stride();
1032 
1033  const_iter_t v1_val = sub_vecs[1].values().begin();
1034  const ptrdiff_t v1_s = sub_vecs[1].stride();
1035 
1036  iter_t z0_val = targ_sub_vecs[0].values().begin();
1037  const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
1038 
1039  if ( v0_s == 1 && v1_s == 1 && z0_s == 1 ) {
1040  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
1041  eleWiseTransformation_( *v0_val++, *v1_val++, *z0_val++ );
1042  }
1043  else {
1044  for(
1045  Teuchos_Ordinal i = 0;
1046  i < subDim;
1047  ++i, v0_val += v0_s, v1_val += v1_s, z0_val += z0_s
1048  )
1049  {
1050  eleWiseTransformation_( *v0_val, *v1_val, *z0_val );
1051  }
1052  }
1053 
1054  }
1055 
1056 
1058 
1059 private:
1060 
1061  EleWiseTransformation eleWiseTransformation_;
1062 
1063 };
1064 
1066 template<class Scalar, class EleWiseTransformation>
1067 class TOp_3_1_Base : public RTOpT<Scalar>
1068 {
1069 public:
1070 
1073  EleWiseTransformation eleWiseTransformation = EleWiseTransformation()
1074  )
1075  : eleWiseTransformation_(eleWiseTransformation)
1076  {}
1077 
1080 
1083  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
1084  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
1085  const Ptr<ReductTarget> &reduct_obj_inout
1086  ) const
1087  {
1088  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
1089  typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
1090 
1091 #ifdef TEUCHOS_DEBUG
1092  validate_apply_op<Scalar>(*this, 3, 1, false,
1093  sub_vecs, targ_sub_vecs, reduct_obj_inout);
1094 #endif
1095 
1096  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
1097 
1098  const_iter_t v0_val = sub_vecs[0].values().begin();
1099  const ptrdiff_t v0_s = sub_vecs[0].stride();
1100 
1101  const_iter_t v1_val = sub_vecs[1].values().begin();
1102  const ptrdiff_t v1_s = sub_vecs[1].stride();
1103 
1104  const_iter_t v2_val = sub_vecs[2].values().begin();
1105  const ptrdiff_t v2_s = sub_vecs[2].stride();
1106 
1107  iter_t z0_val = targ_sub_vecs[0].values().begin();
1108  const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
1109 
1110  if ( v0_s == 1 && v1_s == 1 && v2_s == 1 && z0_s == 1 ) {
1111  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
1112  eleWiseTransformation_( *v0_val++, *v1_val++, *v2_val++, *z0_val++ );
1113  }
1114  else {
1115  for(
1116  Teuchos_Ordinal i = 0;
1117  i < subDim;
1118  ++i, v0_val += v0_s, v1_val += v1_s, v2_val += v2_s, z0_val += z0_s
1119  )
1120  {
1121  eleWiseTransformation_( *v0_val, *v1_val, *v2_val, *z0_val );
1122  }
1123  }
1124 
1125  }
1126 
1128 
1129 private:
1130 
1131  EleWiseTransformation eleWiseTransformation_;
1132 
1133 };
1134 
1135 
1136 } // namespace RTOpPack
1137 
1138 
1139 #endif // RTOPPACK_RTOP_T_HELPERS_DECL_HPP
void get_reduct_type_num_entries_impl(const Ptr< int > &num_values, const Ptr< int > &num_indexes, const Ptr< int > &num_chars) const
Base class for scalar reduction RTOps with one input vector.
void apply_op_impl(const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< ReductTarget > &reduct_obj_inout) const
Base class for coordinate-variant scalar reduction RTOps with one input vector.
void apply_op_impl(const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< ReductTarget > &reduct_obj_inout) const
Simple ReductTarget subclass for simple scalar objects.
ROp_2_ScalarReduction(const ReductScalar &initReductObjValue_in=ReductScalar(), EleWiseReduction eleWiseReduction_in=EleWiseReduction(), ReductObjReduction reductObjReduction_in=ReductObjReduction())
Null reduction object reduction operator.
TOp_0_1_CoordVariantBase(EleWiseTransformation eleWiseTransformation=EleWiseTransformation())
bool coord_invariant_impl() const
This RTOp is NOT coordinate invariant! .
const RCP< DefaultReductTarget< ConcreteReductObj > > defaultReductTarget(const ConcreteReductObj &concreteReductObj)
Nonmember constructor.
void apply_op_impl(const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< ReductTarget > &reduct_obj_inout) const
STANDARD_MEMBER_COMPOSITION_MEMBERS(ConcreteReductObj, initReductObjValue)
void apply_op_impl(const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< ReductTarget > &reduct_obj_inout) const
Base class for coordinate variant transformations for 0 input and 1 output vector.
ConcreteReductObj operator()(const ReductTarget &reduct_obj) const
Base class for transformations for 2 input and 1 output vector.
DefaultReductTarget(const ConcreteReductObj &concreteReductObj)
void operator()(const Scalar &in_reduct, Scalar &inout_reduct) const
void setEleWiseReduction(EleWiseReduction eleWiseReduction_in)
ROp_1_ScalarReduction(const ConcreteReductObj &initReductObjValue_in=ConcreteReductObj(), EleWiseReduction eleWiseReduction_in=EleWiseReduction(), ReductObjReduction reductObjReduction_in=ReductObjReduction())
T_To & dyn_cast(T_From &from)
Base class for transformations for 0 input and 1 output vector.
TOp_0_1_Base(EleWiseTransformation eleWiseTransformation=EleWiseTransformation())
void set(const ConcreteReductObj &concreteReductObj)
const ConcreteReductObj & get() const
void operator()(const ConcreteReductObj &in_reduct, ConcreteReductObj &inout_reduct) const
Base class for transformations for 1 input and 1 output vector.
TOp_1_1_Base(EleWiseTransformation eleWiseTransformation=EleWiseTransformation())
void apply_op_impl(const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< ReductTarget > &reduct_obj_inout) const
ROpScalarReductionWithOpBase< Scalar, ConcreteReductObj, ReductObjReduction > base_t
Teuchos::RCP< ReductTarget > reduct_obj_create_impl() const
Simple struct for a Scalar and an Ordinal object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setEleWiseTransformation(EleWiseTransformation eleWiseTransformation)
void operator()(const ConcreteReductObj &in_reduct, ConcreteReductObj &inout_reduct) const
ROpScalarReductionWithOpBase(const ConcreteReductObj &initReductObjValue_in=ScalarTraits< Scalar >::zero(), ReductObjReduction reductObjReduction_in=ReductObjReduction())
TOp_2_1_Base(EleWiseTransformation eleWiseTransformation=EleWiseTransformation())
void apply_op_impl(const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< ReductTarget > &reduct_obj_inout) const
virtual void reduce_reduct_objs_impl(const ReductTarget &in_reduct_obj, const Ptr< ReductTarget > &inout_reduct_obj) const
Base class for scalar reduction RTOps with two input vectors.
ROpScalarReductionWithOpBase< Scalar, ReductScalar, ReductObjReduction > base_t
void apply_op_impl(const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< ReductTarget > &reduct_obj_inout) const
void apply_op_impl(const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< ReductTarget > &reduct_obj_inout) const
static void loadPrimitiveObjs(const ArrayView< const primitiveType > &primitiveObjs, const ArrayView< const index_type > &indexObjs, const ArrayView< const char > &charObjs, const Ptr< ScalarIndex< Scalar > > &obj)
void extract_reduct_obj_state_impl(const ReductTarget &reduct_obj, const ArrayView< primitive_value_type > &value_data, const ArrayView< index_type > &index_data, const ArrayView< char_type > &char_data) const
TOp_3_1_Base(EleWiseTransformation eleWiseTransformation=EleWiseTransformation())
void load_reduct_obj_state_impl(const ArrayView< const primitive_value_type > &value_data, const ArrayView< const index_type > &index_data, const ArrayView< const char_type > &char_data, const Ptr< ReductTarget > &reduct_obj) const
void operator()(const ConcreteReductObj &in_reduct, ConcreteReductObj &inout_reduct) const
bool coord_invariant_impl() const
This RTOp is NOT coordinate invariant! .
static void extractPrimitiveObjs(const ScalarIndex< Scalar > &obj, const ArrayView< primitiveType > &primitiveObjs, const ArrayView< index_type > &indexObjs, const ArrayView< char > &charObjs)
ROpScalarReductionWithOpBase< Scalar, ReductScalar, ReductObjReduction > base_t
void operator()(const ConcreteReductObj &in_reduct, ConcreteReductObj &inout_reduct) const
RTOpT< Scalar >::primitive_value_type primitive_value_type
void setRawVal(const ConcreteReductObj &rawVal, const Ptr< ReductTarget > &reduct_obj) const
Base class for transformations for 3 input and 1 output vector.
const ConcreteReductObj & getRawVal(const ReductTarget &reduct_obj) const
ScalarIndex(const Scalar &_scalar, const Ordinal &_index)
void reduct_obj_reinit_impl(const Ptr< ReductTarget > &reduct_obj) const
const EleWiseTransformation & getEleWiseTransformation() const
ROp_1_CoordVariantScalarReduction(const ReductScalar &initReductObjValue_in=ReductScalar(), EleWiseReduction eleWiseReduction_in=EleWiseReduction(), ReductObjReduction reductObjReduction_in=ReductObjReduction())
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)