RTOp Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RTOpPack_RTOpTHelpers_decl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // RTOp: Interfaces and Support Software for Vector Reduction Transformation
5 // Operations
6 // Copyright (2006) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
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 Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef RTOPPACK_RTOP_T_HELPERS_DECL_HPP
44 #define RTOPPACK_RTOP_T_HELPERS_DECL_HPP
45 
46 
47 //#define RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
48 
49 
50 #include <typeinfo>
51 
52 
53 #include "RTOpPack_RTOpT.hpp"
55 #include "Teuchos_ScalarTraits.hpp"
56 #include "Teuchos_dyn_cast.hpp"
58 
59 
60 #ifdef RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
61 # include "Teuchos_VerboseObject.hpp"
62 namespace RTOpPack { extern bool rtop_helpers_dump_all; }
63 #endif // RTOPPACK_RTOPT_HELPER_DUMP_OUTPUT
64 
65 
66 namespace RTOpPack {
67 
68 
73 template<class Scalar>
74 struct ScalarIndex {
76  Scalar scalar;
80  ScalarIndex( const Scalar &_scalar, const Ordinal &_index )
81  : scalar(_scalar), index(_index)
82  {}
85  : scalar(ScalarTraits<Scalar>::zero()), index(-1)
86  {}
87 };
88 
89 
94 template<class Scalar>
95 std::ostream& operator<<(std::ostream &out, const ScalarIndex<Scalar> &scalarIndex)
96 {
97  out << "{"<<scalarIndex.scalar<<","<<scalarIndex.index<<"}";
98  return out;
99 }
100 
101 
105 template <class Scalar>
106 class PrimitiveTypeTraits<Scalar, ScalarIndex<Scalar> > {
107 public:
113  static int numPrimitiveObjs() { return ScalarPrimitiveTypeTraits::numPrimitiveObjs(); }
115  static int numIndexObjs() { return 1; }
117  static int numCharObjs() { return 0; }
119  static void extractPrimitiveObjs(
120  const ScalarIndex<Scalar> &obj,
121  const ArrayView<primitiveType> &primitiveObjs,
122  const ArrayView<index_type> &indexObjs,
123  const ArrayView<char> &charObjs
124  )
125  {
126  assertInput(primitiveObjs, indexObjs, charObjs);
127  ScalarPrimitiveTypeTraits::extractPrimitiveObjs(
128  obj.scalar, primitiveObjs, Teuchos::null, Teuchos::null );
129  indexObjs[0] = obj.index;
130  }
132  static void loadPrimitiveObjs(
133  const ArrayView<const primitiveType> &primitiveObjs,
134  const ArrayView<const index_type> &indexObjs,
135  const ArrayView<const char> &charObjs,
136  const Ptr<ScalarIndex<Scalar> > &obj
137  )
138  {
139  assertInput(primitiveObjs, indexObjs, charObjs);
140  ScalarPrimitiveTypeTraits::loadPrimitiveObjs(
141  primitiveObjs, Teuchos::null, Teuchos::null,
142  Teuchos::outArg(obj->scalar) );
143  obj->index = indexObjs[0];
144  }
145 private:
146  static void assertInput(
147  const ArrayView<const primitiveType> &primitiveObjs,
148  const ArrayView<const index_type> &indexObjs,
149  const ArrayView<const char> &charObjs
150  )
151  {
152 #ifdef TEUCHOS_DEBUG
154  primitiveObjs.size()!=ScalarPrimitiveTypeTraits::numPrimitiveObjs()
155  || indexObjs.size()!=1
156  || charObjs.size()!=0 );
157 #else
158  (void)primitiveObjs;
159  (void)indexObjs;
160  (void)charObjs;
161 #endif
162  }
163 };
164 
165 
170 template<class ConcreteReductObj>
172 public:
174  DefaultReductTarget( const ConcreteReductObj &concreteReductObj )
175  : concreteReductObj_(concreteReductObj)
176  {}
178  void set( const ConcreteReductObj &concreteReductObj )
179  { concreteReductObj_ = concreteReductObj; }
181  const ConcreteReductObj& get() const
182  { return concreteReductObj_; }
184  std::string description() const;
185 private:
186  ConcreteReductObj concreteReductObj_;
187 };
188 
189 
194 template<class ConcreteReductObj>
196 defaultReductTarget( const ConcreteReductObj &concreteReductObj )
197 {
198  return Teuchos::rcp(
199  new DefaultReductTarget<ConcreteReductObj>(concreteReductObj));
200 }
201 
202 
225 template<class Scalar>
226 void validate_apply_op(
227  const RTOpT<Scalar> &op,
228  const int allowed_num_sub_vecs,
229  const int allowed_num_targ_sub_vecs,
230  const bool expect_reduct_obj,
231  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
232  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
233  const Ptr<const ReductTarget> &reduct_obj
234  );
235 
236 
237 //
238 // Reduction object operator support
239 //
240 
241 
244 
245 
247 template<class ConcreteReductObj, int ReductionType>
249 public:
251  inline void operator()(const ConcreteReductObj& in_reduct, ConcreteReductObj& inout_reduct) const
252  {
253  return in_reduct.this_reduction_type_needs_a_specialization();
254  }
255 };
256 
257 
259 template<class ConcreteReductObj>
260 class BasicReductObjReductionOp<ConcreteReductObj, REDUCT_TYPE_SUM> {
261 public:
263  inline void operator()(const ConcreteReductObj& in_reduct, ConcreteReductObj& inout_reduct) const
264  {
265  inout_reduct += in_reduct;
266  }
267 };
268 
269 
271 template<class ConcreteReductObj>
272 class BasicReductObjReductionOp<ConcreteReductObj, REDUCT_TYPE_MAX> {
273 public:
275  inline void operator()(const ConcreteReductObj& in_reduct, ConcreteReductObj& inout_reduct) const
276  {
277  inout_reduct = std::max(inout_reduct, in_reduct);
278  }
279 };
280 
281 
283 template<class ConcreteReductObj>
284 class BasicReductObjReductionOp<ConcreteReductObj, REDUCT_TYPE_MIN> {
285 public:
287  inline void operator()(const ConcreteReductObj& in_reduct, ConcreteReductObj& inout_reduct) const
288  {
289  inout_reduct = std::min(inout_reduct, in_reduct);
290  }
291 };
292 
293 
295 template<class Scalar>
297 public:
299  inline void operator()(const Scalar& in_reduct, Scalar& inout_reduct) const
300  {
301  inout_reduct += in_reduct;
302  }
303 };
304 // 2008/07/03: rabart: Above: I have broken from the Thyra guideline of
305 // passing in-out arguments as const Ptr<Type>& and used raw non-const
306 // reference Type& instead to allow the user function to be more readable.
307 
308 
310 template<class Scalar, class ConcreteReductObj, class ReductObjReduction>
311 class ROpScalarReductionWithOpBase : public RTOpT<Scalar>
312 {
313 public:
314 
317 
320 
323  const ConcreteReductObj &initReductObjValue_in = ScalarTraits<Scalar>::zero(),
324  ReductObjReduction reductObjReduction_in = ReductObjReduction()
325  )
326  : initReductObjValue_(initReductObjValue_in),
327  reductObjReduction_(reductObjReduction_in)
328  {}
329 
331  const ConcreteReductObj& getRawVal( const ReductTarget &reduct_obj ) const
332  {
333  using Teuchos::dyn_cast;
334  return dyn_cast<const DefaultReductTarget<ConcreteReductObj> >(reduct_obj).get();
335  }
336 
338  void setRawVal( const ConcreteReductObj &rawVal,
339  const Ptr<ReductTarget> &reduct_obj
340  ) const
341  {
342  using Teuchos::dyn_cast;
343  dyn_cast<DefaultReductTarget<ConcreteReductObj> >(*reduct_obj).set(rawVal);
344  }
345 
347  ConcreteReductObj operator()(const ReductTarget& reduct_obj ) const
348  {
349  return this->getRawVal(reduct_obj);
350  }
351 
354 
357  const Ptr<int> &num_values,
358  const Ptr<int> &num_indexes,
359  const Ptr<int> &num_chars
360  ) const
361  {
363  *num_values = PTT::numPrimitiveObjs();
364  *num_indexes = PTT::numIndexObjs();
365  *num_chars = PTT::numCharObjs();
366  }
367 
370  {
371  return Teuchos::rcp(
372  new DefaultReductTarget<ConcreteReductObj>(initReductObjValue()));
373  }
374 
377  const ReductTarget& in_reduct_obj, const Ptr<ReductTarget>& inout_reduct_obj
378  ) const
379  {
380  const ConcreteReductObj scalar_in_reduct_obj = this->getRawVal(in_reduct_obj);
381  ConcreteReductObj scalar_inout_reduct_obj = this->getRawVal(*inout_reduct_obj);
382  reductObjReduction_(scalar_in_reduct_obj, scalar_inout_reduct_obj);
383  this->setRawVal( scalar_inout_reduct_obj, inout_reduct_obj );
384  }
385 
387  void reduct_obj_reinit_impl( const Ptr<ReductTarget> &reduct_obj ) const
388  {
389  setRawVal( initReductObjValue(), reduct_obj );
390  }
391 
394  const ReductTarget &reduct_obj,
395  const ArrayView<primitive_value_type> &value_data,
396  const ArrayView<index_type> &index_data,
397  const ArrayView<char_type> &char_data
398  ) const
399  {
401  PTT::extractPrimitiveObjs( getRawVal(reduct_obj),
402  value_data, index_data, char_data );
403  }
404 
407  const ArrayView<const primitive_value_type> &value_data,
408  const ArrayView<const index_type> &index_data,
409  const ArrayView<const char_type> &char_data,
410  const Ptr<ReductTarget> &reduct_obj
411  ) const
412  {
414  ConcreteReductObj concrete_reduct_obj;
415  PTT::loadPrimitiveObjs( value_data, index_data, char_data,
416  Teuchos::outArg(concrete_reduct_obj) );
417  this->setRawVal( concrete_reduct_obj, reduct_obj );
418  }
419 
421 
422 protected:
423 
425  STANDARD_MEMBER_COMPOSITION_MEMBERS( ConcreteReductObj, initReductObjValue );
426 
427 private:
428 
429  ReductObjReduction reductObjReduction_;
430 
431 };
432 
433 
434 //
435 // ROp 1 vector scalar reduction
436 //
437 
438 
440 template<class Scalar, class ConcreteReductObj, class EleWiseReduction,
441  class ReductObjReduction = SumScalarReductObjReduction<ConcreteReductObj> >
443  : public ROpScalarReductionWithOpBase<Scalar, ConcreteReductObj, ReductObjReduction>
444 {
445 public:
446 
449 
452  const ConcreteReductObj &initReductObjValue_in = ConcreteReductObj(),
453  EleWiseReduction eleWiseReduction_in = EleWiseReduction(),
454  ReductObjReduction reductObjReduction_in = ReductObjReduction()
455  )
456  : base_t(initReductObjValue_in, reductObjReduction_in),
457  eleWiseReduction_(eleWiseReduction_in)
458  {}
459 
462 
465  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
466  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
467  const Ptr<ReductTarget> &reduct_obj_inout
468  ) const
469  {
470  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
471  using Teuchos::dyn_cast;
472 
473 #ifdef TEUCHOS_DEBUG
474  validate_apply_op<Scalar>(*this, 1, 0, true,
475  sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
476 #else
477  (void)targ_sub_vecs;
478 #endif
479 
481  dyn_cast<DefaultReductTarget<ConcreteReductObj> >(*reduct_obj_inout);
482  ConcreteReductObj reduct = reduct_obj.get();
483 
484  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
485 
486  const_iter_t v0_val = sub_vecs[0].values().begin();
487  const ptrdiff_t v0_s = sub_vecs[0].stride();
488 
489  if ( v0_s == 1 ) {
490  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
491  eleWiseReduction_( *v0_val++, reduct);
492  }
493  else {
494  for( Teuchos_Ordinal i = 0; i < subDim; ++i, v0_val += v0_s )
495  eleWiseReduction_( *v0_val, reduct);
496  }
497 
498  reduct_obj.set(reduct);
499 
500  }
501 
503 
504 private:
505 
506  EleWiseReduction eleWiseReduction_;
507 
508 };
509 
510 
512 #define RTOP_ROP_1_REDUCT_SCALAR_CUSTOM_DEFAULT( ROP_CLASS_NAME, REDUCT_SCALAR, \
513  BASIC_REDUCT_TYPE_ENUM, CUSTOM_DEFAULT \
514  ) \
515  \
516  template<class Scalar, class ReductScalar> \
517  class ROP_CLASS_NAME ## EleWiseReduction \
518  { \
519  public: \
520  inline void operator()( \
521  const Scalar &v0, \
522  ReductScalar &reduct \
523  ) const; \
524  }; \
525  \
526  \
527  template<class Scalar> \
528  class ROP_CLASS_NAME \
529  : public RTOpPack::ROp_1_ScalarReduction< \
530  Scalar, \
531  REDUCT_SCALAR, \
532  ROP_CLASS_NAME ## EleWiseReduction<Scalar, REDUCT_SCALAR >, \
533  RTOpPack::BasicReductObjReductionOp<REDUCT_SCALAR, BASIC_REDUCT_TYPE_ENUM> > \
534  { \
535  typedef RTOpPack::ROp_1_ScalarReduction< \
536  Scalar, \
537  REDUCT_SCALAR, \
538  ROP_CLASS_NAME ## EleWiseReduction<Scalar, REDUCT_SCALAR >, \
539  RTOpPack::BasicReductObjReductionOp<REDUCT_SCALAR, BASIC_REDUCT_TYPE_ENUM> > \
540  base_t; \
541  public: \
542  ROP_CLASS_NAME() \
543  : base_t(CUSTOM_DEFAULT) \
544  { \
545  this->setOpNameBase( #ROP_CLASS_NAME ); \
546  } \
547  }; \
548  \
549  \
550  template<class Scalar, class ReductScalar> \
551  void ROP_CLASS_NAME ## EleWiseReduction<Scalar, ReductScalar>::operator()( \
552  const Scalar &v0, ReductScalar &reduct \
553  ) const
554 
555 
557 #define RTOP_ROP_1_REDUCT_SCALAR( ROP_CLASS_NAME, REDUCT_SCALAR, \
558  BASIC_REDUCT_TYPE_ENUM \
559  ) \
560  RTOP_ROP_1_REDUCT_SCALAR_CUSTOM_DEFAULT(ROP_CLASS_NAME, REDUCT_SCALAR, \
561  BASIC_REDUCT_TYPE_ENUM, Teuchos::ScalarTraits<REDUCT_SCALAR >::zero() )
562 
563 
564 //
565 // ROp 1 coordinate-variant vector scalar reduction
566 //
567 
568 
571 template<
572  class Scalar,
573  class ReductScalar,
574  class EleWiseReduction,
575  class ReductObjReduction = SumScalarReductObjReduction<ReductScalar>
576  >
578  : public ROpScalarReductionWithOpBase<Scalar, ReductScalar, ReductObjReduction>
579 {
580 public:
581 
584 
587 
590  const ReductScalar &initReductObjValue_in = ReductScalar(),
591  EleWiseReduction eleWiseReduction_in = EleWiseReduction(),
592  ReductObjReduction reductObjReduction_in = ReductObjReduction()
593  )
594  : base_t(initReductObjValue_in, reductObjReduction_in),
595  eleWiseReduction_(eleWiseReduction_in)
596  {}
597 
599  void setEleWiseReduction(EleWiseReduction eleWiseReduction_in)
600  { eleWiseReduction_ = eleWiseReduction_in; }
601 
603  const EleWiseReduction& getEleWiseReduction() const
604  { return eleWiseReduction_; }
605 
607 
610 
612  bool coord_invariant_impl() const { return false; }
613 
616  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
617  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
618  const Ptr<ReductTarget> &reduct_obj_inout
619  ) const
620  {
621  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
622  using Teuchos::dyn_cast;
623 
624 #ifdef TEUCHOS_DEBUG
625  validate_apply_op<Scalar>(*this, 1, 0, true,
626  sub_vecs, targ_sub_vecs, reduct_obj_inout);
627 #else
628  (void)targ_sub_vecs;
629 #endif
630 
632  dyn_cast<DefaultReductTarget<ReductScalar> >(*reduct_obj_inout);
633  ReductScalar reduct = reduct_obj.get();
634 
635  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
636 
637  const_iter_t v0_val = sub_vecs[0].values().begin();
638  const ptrdiff_t v0_s = sub_vecs[0].stride();
639 
640  RTOpPack::index_type global_i = sub_vecs[0].globalOffset();
641 
642  if ( v0_s == 1 ) {
643  for( Teuchos_Ordinal i = 0; i < subDim; ++i, ++global_i )
644  eleWiseReduction_( global_i, *v0_val++, reduct);
645  }
646  else {
647  for( Teuchos_Ordinal i = 0; i < subDim; ++i, v0_val += v0_s, ++global_i )
648  eleWiseReduction_( global_i, *v0_val, reduct);
649  }
650 
651  reduct_obj.set(reduct);
652 
653  }
654 
656 
657 private:
658 
659  EleWiseReduction eleWiseReduction_;
660 
661 };
662 
663 
664 //
665 // ROp 2 vector scalar reduction
666 //
667 
668 
670 template<
671  class Scalar,
672  class ReductScalar,
673  class EleWiseReduction,
674  class ReductObjReduction = SumScalarReductObjReduction<ReductScalar>
675  >
677  : public ROpScalarReductionWithOpBase<Scalar, ReductScalar, ReductObjReduction>
678 {
679 public:
680 
684 
687  const ReductScalar &initReductObjValue_in = ReductScalar(),
688  EleWiseReduction eleWiseReduction_in = EleWiseReduction(),
689  ReductObjReduction reductObjReduction_in = ReductObjReduction()
690  )
691  : base_t(initReductObjValue_in, reductObjReduction_in),
692  eleWiseReduction_(eleWiseReduction_in)
693  {}
694 
697 
700  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
701  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
702  const Ptr<ReductTarget> &reduct_obj_inout
703  ) const
704  {
705  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
706  using Teuchos::dyn_cast;
707 
708 #ifdef TEUCHOS_DEBUG
709  validate_apply_op<Scalar>(*this, 2, 0, true,
710  sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
711 #else
712  (void)targ_sub_vecs;
713 #endif
714 
715  DefaultReductTarget<Scalar> &reduct_obj =
716  dyn_cast<DefaultReductTarget<Scalar> >(*reduct_obj_inout);
717  Scalar reduct = reduct_obj.get();
718 
719  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
720 
721  const_iter_t v0_val = sub_vecs[0].values().begin();
722  const ptrdiff_t v0_s = sub_vecs[0].stride();
723  const_iter_t v1_val = sub_vecs[1].values().begin();
724  const ptrdiff_t v1_s = sub_vecs[1].stride();
725 
726  if( v0_s == 1 && v1_s == 1 ) {
727  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
728  eleWiseReduction_( *v0_val++, *v1_val++, reduct);
729  }
730  else {
731  for( Teuchos_Ordinal i = 0; i < subDim; ++i, v0_val += v0_s, v1_val += v1_s )
732  eleWiseReduction_( *v0_val, *v1_val, reduct);
733  }
734 
735  reduct_obj.set(reduct);
736 
737  }
738 
740 
741 private:
742 
743  EleWiseReduction eleWiseReduction_;
744 
745 };
746 
747 
751 #define RTOP_ROP_2_REDUCT_SCALAR( ROP_CLASS_NAME, REDUCT_SCALAR, \
752  BASIC_REDUCT_TYPE_ENUM \
753  ) \
754  \
755  template<class Scalar, class ReductScalar> \
756  class ROP_CLASS_NAME ## EleWiseReduction \
757  { \
758  public: \
759  inline void operator()(const Scalar &v0, \
760  const Scalar &v1, \
761  ReductScalar &reduct \
762  ) const; \
763  }; \
764  \
765  \
766  template<class Scalar> \
767  class ROP_CLASS_NAME \
768  : public RTOpPack::ROp_2_ScalarReduction< \
769  Scalar, \
770  REDUCT_SCALAR, \
771  ROP_CLASS_NAME ## EleWiseReduction<Scalar, REDUCT_SCALAR >, \
772  RTOpPack::BasicReductObjReductionOp<REDUCT_SCALAR, BASIC_REDUCT_TYPE_ENUM> > \
773  { \
774  public: \
775  ROP_CLASS_NAME() \
776  { \
777  this->setOpNameBase( #ROP_CLASS_NAME ); \
778  this->initReductObjValue(ScalarTraits<REDUCT_SCALAR >::zero()); \
779  } \
780  }; \
781  \
782  template<class Scalar, class ReductScalar> \
783  void ROP_CLASS_NAME ## EleWiseReduction<Scalar, ReductScalar>::operator()( \
784  const Scalar &v0, const Scalar &v1, ReductScalar &reduct) const
785 
786 
787 //
788 // TOp 0 to 1 vector transformation
789 //
790 
791 
793 template<class Scalar, class EleWiseTransformation>
794 class TOp_0_1_Base : public RTOpT<Scalar>
795 {
796 public:
797 
800  EleWiseTransformation eleWiseTransformation = EleWiseTransformation()
801  )
802  : eleWiseTransformation_(eleWiseTransformation)
803  {}
804 
807 
810  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
811  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
812  const Ptr<ReductTarget> &reduct_obj_inout
813  ) const
814  {
815  typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
816 
817 #ifdef TEUCHOS_DEBUG
818  validate_apply_op<Scalar>(*this, 0, 1, false,
819  sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
820 #else
821  (void)sub_vecs;
822  (void)reduct_obj_inout;
823 #endif
824 
825  const RTOpPack::index_type subDim = targ_sub_vecs[0].subDim();
826 
827  iter_t z0_val = targ_sub_vecs[0].values().begin();
828  const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
829 
830  if ( z0_s == 1 ) {
831  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
832  eleWiseTransformation_( *z0_val++);
833  }
834  else {
835  for( Teuchos_Ordinal i = 0; i < subDim; ++i, z0_val += z0_s )
836  eleWiseTransformation_( *z0_val);
837  }
838 
839  }
840 
842 
843 private:
844 
845  EleWiseTransformation eleWiseTransformation_;
846 
847 };
848 
849 
852 template<class Scalar, class EleWiseTransformation>
853 class TOp_0_1_CoordVariantBase : public RTOpT<Scalar>
854 {
855 public:
856 
859  EleWiseTransformation eleWiseTransformation = EleWiseTransformation()
860  )
861  : eleWiseTransformation_(eleWiseTransformation)
862  {}
863 
865  void setEleWiseTransformation(EleWiseTransformation eleWiseTransformation)
866  {
867  eleWiseTransformation_ = eleWiseTransformation;
868  }
869 
871  const EleWiseTransformation& getEleWiseTransformation() const
872  {
873  return eleWiseTransformation_;
874  }
875 
878 
880  bool coord_invariant_impl() const { return false; }
881 
884  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
885  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
886  const Ptr<ReductTarget> &reduct_obj_inout
887  ) const
888  {
889  typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
890 
891 #ifdef TEUCHOS_DEBUG
892  validate_apply_op<Scalar>(*this, 0, 1, false,
893  sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
894 #else
895  (void)sub_vecs;
896  (void)reduct_obj_inout;
897 #endif
898 
899  const RTOpPack::index_type subDim = targ_sub_vecs[0].subDim();
900 
901  iter_t z0_val = targ_sub_vecs[0].values().begin();
902  const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
903 
904  RTOpPack::index_type global_i = targ_sub_vecs[0].globalOffset();
905 
906  if ( z0_s == 1 ) {
907  for( Teuchos_Ordinal i = 0; i < subDim; ++i, ++global_i )
908  eleWiseTransformation_(global_i, *z0_val++);
909  }
910  else {
911  for( Teuchos_Ordinal i = 0; i < subDim; ++i, z0_val += z0_s, ++global_i )
912  eleWiseTransformation_(global_i, *z0_val);
913  }
914 
915  }
916 
918 
919 private:
920 
921  EleWiseTransformation eleWiseTransformation_;
922 
923 };
924 
925 
926 //
927 // TOp 1 to 1 vector transformation
928 //
929 
930 
932 template<class Scalar, class EleWiseTransformation>
933 class TOp_1_1_Base : public RTOpT<Scalar>
934 {
935 public:
936 
939  EleWiseTransformation eleWiseTransformation = EleWiseTransformation()
940  )
941  : eleWiseTransformation_(eleWiseTransformation)
942  {}
943 
946 
949  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
950  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
951  const Ptr<ReductTarget> &reduct_obj_inout
952  ) const
953  {
954  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
955  typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
956 
957 #ifdef TEUCHOS_DEBUG
958  validate_apply_op<Scalar>(*this, 1, 1, false,
959  sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
960 #else
961  (void)reduct_obj_inout;
962 #endif
963 
964  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
965 
966  const_iter_t v0_val = sub_vecs[0].values().begin();
967  const ptrdiff_t v0_s = sub_vecs[0].stride();
968 
969  iter_t z0_val = targ_sub_vecs[0].values().begin();
970  const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
971 
972  if ( v0_s == 1 && z0_s == 1 ) {
973  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
974  eleWiseTransformation_( *v0_val++, *z0_val++);
975  }
976  else {
977  for( Teuchos_Ordinal i = 0; i < subDim; ++i, v0_val += v0_s, z0_val += z0_s )
978  eleWiseTransformation_( *v0_val, *z0_val);
979  }
980 
981  }
982 
984 
985 private:
986 
987  EleWiseTransformation eleWiseTransformation_;
988 
989 };
990 
991 
993 #define RTOP_TOP_1_1( TOP_CLASS_NAME ) \
994  \
995  template<class Scalar> \
996  class TOP_CLASS_NAME ## EleWiseTransformation \
997  { \
998  public: \
999  inline void operator()( const Scalar &v0, Scalar &z0 ) const; \
1000  }; \
1001  \
1002  \
1003  template<class Scalar> \
1004  class TOP_CLASS_NAME \
1005  : public RTOpPack::TOp_1_1_Base< Scalar, \
1006  TOP_CLASS_NAME ## EleWiseTransformation<Scalar> > \
1007  { \
1008  public: \
1009  TOP_CLASS_NAME() \
1010  { \
1011  this->setOpNameBase( #TOP_CLASS_NAME ); \
1012  } \
1013  }; \
1014  \
1015  \
1016  template<class Scalar> \
1017  void TOP_CLASS_NAME ## EleWiseTransformation<Scalar>::operator()( \
1018  const Scalar &v0, Scalar &z0 \
1019  ) const
1020 
1021 
1022 //
1023 // TOp 2 to 1 vector transformation
1024 //
1025 
1026 
1028 template<class Scalar, class EleWiseTransformation>
1029 class TOp_2_1_Base : public RTOpT<Scalar>
1030 {
1031 public:
1032 
1035  EleWiseTransformation eleWiseTransformation = EleWiseTransformation()
1036  )
1037  : eleWiseTransformation_(eleWiseTransformation)
1038  {}
1039 
1042 
1045  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
1046  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
1047  const Ptr<ReductTarget> &reduct_obj_inout
1048  ) const
1049  {
1050  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
1051  typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
1052 
1053 #ifdef TEUCHOS_DEBUG
1054  validate_apply_op<Scalar>(*this, 2, 1, false,
1055  sub_vecs, targ_sub_vecs, reduct_obj_inout);
1056 #else
1057  (void)reduct_obj_inout;
1058 #endif
1059 
1060  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
1061 
1062  const_iter_t v0_val = sub_vecs[0].values().begin();
1063  const ptrdiff_t v0_s = sub_vecs[0].stride();
1064 
1065  const_iter_t v1_val = sub_vecs[1].values().begin();
1066  const ptrdiff_t v1_s = sub_vecs[1].stride();
1067 
1068  iter_t z0_val = targ_sub_vecs[0].values().begin();
1069  const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
1070 
1071  if ( v0_s == 1 && v1_s == 1 && z0_s == 1 ) {
1072  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
1073  eleWiseTransformation_( *v0_val++, *v1_val++, *z0_val++ );
1074  }
1075  else {
1076  for(
1077  Teuchos_Ordinal i = 0;
1078  i < subDim;
1079  ++i, v0_val += v0_s, v1_val += v1_s, z0_val += z0_s
1080  )
1081  {
1082  eleWiseTransformation_( *v0_val, *v1_val, *z0_val );
1083  }
1084  }
1085 
1086  }
1087 
1088 
1090 
1091 private:
1092 
1093  EleWiseTransformation eleWiseTransformation_;
1094 
1095 };
1096 
1098 template<class Scalar, class EleWiseTransformation>
1099 class TOp_3_1_Base : public RTOpT<Scalar>
1100 {
1101 public:
1102 
1105  EleWiseTransformation eleWiseTransformation = EleWiseTransformation()
1106  )
1107  : eleWiseTransformation_(eleWiseTransformation)
1108  {}
1109 
1112 
1115  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
1116  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
1117  const Ptr<ReductTarget> &reduct_obj_inout
1118  ) const
1119  {
1120  typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
1121  typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
1122 
1123 #ifdef TEUCHOS_DEBUG
1124  validate_apply_op<Scalar>(*this, 3, 1, false,
1125  sub_vecs, targ_sub_vecs, reduct_obj_inout);
1126 #endif
1127 
1128  const RTOpPack::index_type subDim = sub_vecs[0].subDim();
1129 
1130  const_iter_t v0_val = sub_vecs[0].values().begin();
1131  const ptrdiff_t v0_s = sub_vecs[0].stride();
1132 
1133  const_iter_t v1_val = sub_vecs[1].values().begin();
1134  const ptrdiff_t v1_s = sub_vecs[1].stride();
1135 
1136  const_iter_t v2_val = sub_vecs[2].values().begin();
1137  const ptrdiff_t v2_s = sub_vecs[2].stride();
1138 
1139  iter_t z0_val = targ_sub_vecs[0].values().begin();
1140  const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
1141 
1142  if ( v0_s == 1 && v1_s == 1 && v2_s == 1 && z0_s == 1 ) {
1143  for( Teuchos_Ordinal i = 0; i < subDim; ++i )
1144  eleWiseTransformation_( *v0_val++, *v1_val++, *v2_val++, *z0_val++ );
1145  }
1146  else {
1147  for(
1148  Teuchos_Ordinal i = 0;
1149  i < subDim;
1150  ++i, v0_val += v0_s, v1_val += v1_s, v2_val += v2_s, z0_val += z0_s
1151  )
1152  {
1153  eleWiseTransformation_( *v0_val, *v1_val, *v2_val, *z0_val );
1154  }
1155  }
1156 
1157  }
1158 
1160 
1161 private:
1162 
1163  EleWiseTransformation eleWiseTransformation_;
1164 
1165 };
1166 
1167 
1168 } // namespace RTOpPack
1169 
1170 
1171 #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! .
static void assertInput(const ArrayView< const primitiveType > &primitiveObjs, const ArrayView< const index_type > &indexObjs, const ArrayView< const char > &charObjs)
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
Class for a changeable sub-vector.
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())
size_type size() const
Base class for transformations for 0 input and 1 output vector.
TOp_0_1_Base(EleWiseTransformation eleWiseTransformation=EleWiseTransformation())
void set(const ConcreteReductObj &concreteReductObj)
Teuchos_Ordinal index_type
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.
EleWiseTransformation eleWiseTransformation_
TOp_1_1_Base(EleWiseTransformation eleWiseTransformation=EleWiseTransformation())
Class for a non-changeable sub-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
ROpScalarReductionWithOpBase< Scalar, ConcreteReductObj, ReductObjReduction > base_t
Teuchos::RCP< ReductTarget > reduct_obj_create_impl() const
Templated interface to vector reduction/transformation operators {abstract}.
Simple struct for a Scalar and an Ordinal object.
TEUCHOS_ORDINAL_TYPE Teuchos_Ordinal
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Specialization where the scalar type is the same as the concrete object type.
void setEleWiseTransformation(EleWiseTransformation eleWiseTransformation)
A templated traits class for decomposing object into an array of primitive objects.
void operator()(const ConcreteReductObj &in_reduct, ConcreteReductObj &inout_reduct) const
ROpScalarReductionWithOpBase(const ConcreteReductObj &initReductObjValue_in=ScalarTraits< Scalar >::zero(), ReductObjReduction reductObjReduction_in=ReductObjReduction())
T_To & dyn_cast(T_From &from)
TOp_2_1_Base(EleWiseTransformation eleWiseTransformation=EleWiseTransformation())
PrimitiveTypeTraits< Scalar, Scalar >::primitiveType primitive_value_type
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.
Abstract base class for all reduction objects.
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
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
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
void validate_apply_op(const RTOpT< Scalar > &op, const int allowed_num_sub_vecs, const int allowed_num_targ_sub_vecs, const bool expect_reduct_obj, const ArrayView< const ConstSubVectorView< Scalar > > &sub_vecs, const ArrayView< const SubVectorView< Scalar > > &targ_sub_vecs, const Ptr< const ReductTarget > &reduct_obj)
Validate the input to an apply_op(...) function.
Base class for transformations for 3 input and 1 output vector.
EleWiseTransformation eleWiseTransformation_
const ConcreteReductObj & getRawVal(const ReductTarget &reduct_obj) const
ScalarIndex(const Scalar &_scalar, const Ordinal &_index)
Ptr< const T > getConst() const
void reduct_obj_reinit_impl(const Ptr< ReductTarget > &reduct_obj) const
Teuchos_Ordinal Ordinal
const EleWiseTransformation & getEleWiseTransformation() const
ROp_1_CoordVariantScalarReduction(const ReductScalar &initReductObjValue_in=ReductScalar(), EleWiseReduction eleWiseReduction_in=EleWiseReduction(), ReductObjReduction reductObjReduction_in=ReductObjReduction())
EleWiseTransformation eleWiseTransformation_
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)