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_def.hpp
Go to the documentation of this file.
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_DEF_HPP
12 #define RTOPPACK_RTOP_T_HELPERS_DEF_HPP
13 
14 
16 
17 
18 namespace RTOpPack {
19 
20 
21 //
22 // DefaultReductTarget
23 //
24 
25 
26 template<class ConcreteReductObj>
28 {
29  std::ostringstream oss;
30  oss
31  << "RTOpPack::DefaultReductTarget<"
33  << "{concreteReductObj="<<concreteReductObj_<<"}";
34  return oss.str();
35 }
36 
37 
38 } // namespace RTOpPack
39 
40 
41 template<class Scalar>
43  const RTOpT<Scalar> &op,
44  const int allowed_num_sub_vecs,
45  const int allowed_num_targ_sub_vecs,
46  const bool expect_reduct_obj,
47  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
48  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
49  const Ptr<const ReductTarget> &reduct_obj
50  )
51 {
52 
53  using Teuchos::as;
54 
55  const int num_sub_vecs = sub_vecs.size();
56  const int num_targ_sub_vecs = targ_sub_vecs.size();
57 
58  const std::string op_name_str = "op.name() = " + op.op_name();
59 
60  if (allowed_num_sub_vecs >= 0) {
61  TEUCHOS_TEST_FOR_EXCEPTION( allowed_num_sub_vecs != as<int>(sub_vecs.size()),
63  op_name_str<<": Error, sub_vecs.size()="<<sub_vecs.size()
64  <<" != allowed_num_sub_vecs="<<allowed_num_sub_vecs<<"!" );
65  }
66 
67  if (allowed_num_targ_sub_vecs >= 0) {
68  TEUCHOS_TEST_FOR_EXCEPTION( allowed_num_targ_sub_vecs != as<int>(targ_sub_vecs.size()),
70  op_name_str<<": Error, targ_sub_vecs.size()="<<targ_sub_vecs.size()
71  <<" != allowed_num_targ_sub_vecs="<<allowed_num_targ_sub_vecs<<"!" );
72  }
73 
74  TEUCHOS_TEST_FOR_EXCEPTION( sub_vecs.size() == 0 && targ_sub_vecs.size() == 0,
76  op_name_str<<": Error, apply_op(...) must be passed some vectors!"
77  );
78 
79  const index_type subDim =
80  (sub_vecs.size() ? sub_vecs[0].subDim() : targ_sub_vecs[0].subDim());
81 
82  const index_type globalOffset =
83  (sub_vecs.size() ? sub_vecs[0].globalOffset() : targ_sub_vecs[0].globalOffset());
84 
85  for (int k = 0; k < num_sub_vecs; ++k ) {
87  sub_vecs[k].subDim() != subDim || sub_vecs[k].globalOffset() != globalOffset,
89  op_name_str<<": Error, sub_vec["<<k<<"] "
90  "(subDim="<<sub_vecs[k].subDim()<<",globalOffset="<<sub_vecs[k].globalOffset()<<")"
91  " is not compatible with (subDim="<<subDim<<",globalOffset="<<globalOffset<<")!"
92  );
93  }
94 
95  for (int k = 0; k < num_targ_sub_vecs; ++k ) {
97  targ_sub_vecs[k].subDim() != subDim || targ_sub_vecs[k].globalOffset() != globalOffset,
99  op_name_str<<": Error, sub_vec["<<k<<"] "
100  "(subDim="<<targ_sub_vecs[k].subDim()<<",globalOffset="<<targ_sub_vecs[k].globalOffset()<<")"
101  " is not compatible with (subDim="<<subDim<<",globalOffset="<<globalOffset<<")!"
102  );
103  }
104 
105  if (expect_reduct_obj) {
106 
107  TEUCHOS_TEST_FOR_EXCEPTION( is_null(reduct_obj),
109  op_name_str<<": Error, expected a reduction target object!"
110  );
111 
112  const RCP<ReductTarget> dummy_reduct_obj = op.reduct_obj_create();
113 
114  // mfh 22 Sep 2015: Clang 3.6 warns when typeid's expression may
115  // have side effects. In this case, Ptr::operator* may throw in a
116  // debug build if the pointer inside is NULL. (Throwing an
117  // exception counts as a side effect here.)
118  //
119  // const std::type_info &reduct_obj_type = typeid(*reduct_obj);
120  // const std::type_info &dummy_reduct_obj_type = typeid(*dummy_reduct_obj);
121 
122  const ReductTarget& reduct_obj_thing = *reduct_obj;
123  const std::type_info& reduct_obj_type = typeid (reduct_obj_thing);
124  ReductTarget& dummy_reduct_obj_thing = *dummy_reduct_obj;
125  const std::type_info& dummy_reduct_obj_type = typeid (dummy_reduct_obj_thing);
126 
127  TEUCHOS_TEST_FOR_EXCEPTION( reduct_obj_type != dummy_reduct_obj_type,
129  op_name_str<<": Error, the type of the input reduct_obj = "
130  <<Teuchos::demangleName(reduct_obj_type.name())<<" is not equal to"
131  " the expected type "<<Teuchos::demangleName(dummy_reduct_obj_type.name())
132  <<"!"
133  );
134 
135  }
136 
137 }
138 
139 
140 
141 //
142 // Explicit Instantiation Macro
143 //
144 
145 
146 #define RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(SCALAR) \
147  \
148  template std::string DefaultReductTarget<SCALAR >::description() const;
149 
150 
151 #define RTOPPACK_RTOPT_HELPERS_INSTANT_SCALAR(SCALAR) \
152  \
153  RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(SCALAR) \
154  \
155  RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(ScalarIndex<SCALAR >) \
156  \
157  RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(SubVectorView<SCALAR >) \
158  \
159  template void validate_apply_op<SCALAR >( \
160  const RTOpT<SCALAR > &op, \
161  const int allowed_num_sub_vecs, \
162  const int allowed_num_targ_sub_vecs, \
163  const bool expect_reduct_obj, \
164  const ArrayView<const ConstSubVectorView<SCALAR > > &sub_vecs, \
165  const ArrayView<const SubVectorView<SCALAR > > &targ_sub_vecs, \
166  const Ptr<const ReductTarget> &reduct_obj \
167  ); \
168  \
169 
170 
171 
172 #endif // RTOPPACK_RTOP_T_HELPERS_DEF_HPP
std::string op_name() const
Return the name (as a null-terminated C-style string) of the operator.
bool is_null(const boost::shared_ptr< T > &p)
TEUCHOSCORE_LIB_DLL_EXPORT std::string demangleName(const std::string &mangledName)
Class for a changeable sub-vector.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos_Ordinal index_type
Class for a non-changeable sub-vector.
Templated interface to vector reduction/transformation operators {abstract}.
Abstract base class for all reduction objects.
TypeTo as(const TypeFrom &t)
Teuchos::RCP< ReductTarget > reduct_obj_create() const
Creates a new reduction target object initialized and ready to be used in a reduction.
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.
static std::string name()