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 //
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_DEF_HPP
44 #define RTOPPACK_RTOP_T_HELPERS_DEF_HPP
45 
46 
48 
49 
50 namespace RTOpPack {
51 
52 
53 //
54 // DefaultReductTarget
55 //
56 
57 
58 template<class ConcreteReductObj>
60 {
61  std::ostringstream oss;
62  oss
63  << "RTOpPack::DefaultReductTarget<"
65  << "{concreteReductObj="<<concreteReductObj_<<"}";
66  return oss.str();
67 }
68 
69 
70 } // namespace RTOpPack
71 
72 
73 template<class Scalar>
75  const RTOpT<Scalar> &op,
76  const int allowed_num_sub_vecs,
77  const int allowed_num_targ_sub_vecs,
78  const bool expect_reduct_obj,
79  const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
80  const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
81  const Ptr<const ReductTarget> &reduct_obj
82  )
83 {
84 
85  using Teuchos::as;
86 
87  const int num_sub_vecs = sub_vecs.size();
88  const int num_targ_sub_vecs = targ_sub_vecs.size();
89 
90  const std::string op_name_str = "op.name() = " + op.op_name();
91 
92  if (allowed_num_sub_vecs >= 0) {
93  TEUCHOS_TEST_FOR_EXCEPTION( allowed_num_sub_vecs != as<int>(sub_vecs.size()),
95  op_name_str<<": Error, sub_vecs.size()="<<sub_vecs.size()
96  <<" != allowed_num_sub_vecs="<<allowed_num_sub_vecs<<"!" );
97  }
98 
99  if (allowed_num_targ_sub_vecs >= 0) {
100  TEUCHOS_TEST_FOR_EXCEPTION( allowed_num_targ_sub_vecs != as<int>(targ_sub_vecs.size()),
102  op_name_str<<": Error, targ_sub_vecs.size()="<<targ_sub_vecs.size()
103  <<" != allowed_num_targ_sub_vecs="<<allowed_num_targ_sub_vecs<<"!" );
104  }
105 
106  TEUCHOS_TEST_FOR_EXCEPTION( sub_vecs.size() == 0 && targ_sub_vecs.size() == 0,
108  op_name_str<<": Error, apply_op(...) must be passed some vectors!"
109  );
110 
111  const index_type subDim =
112  (sub_vecs.size() ? sub_vecs[0].subDim() : targ_sub_vecs[0].subDim());
113 
114  const index_type globalOffset =
115  (sub_vecs.size() ? sub_vecs[0].globalOffset() : targ_sub_vecs[0].globalOffset());
116 
117  for (int k = 0; k < num_sub_vecs; ++k ) {
119  sub_vecs[k].subDim() != subDim || sub_vecs[k].globalOffset() != globalOffset,
121  op_name_str<<": Error, sub_vec["<<k<<"] "
122  "(subDim="<<sub_vecs[k].subDim()<<",globalOffset="<<sub_vecs[k].globalOffset()<<")"
123  " is not compatible with (subDim="<<subDim<<",globalOffset="<<globalOffset<<")!"
124  );
125  }
126 
127  for (int k = 0; k < num_targ_sub_vecs; ++k ) {
129  targ_sub_vecs[k].subDim() != subDim || targ_sub_vecs[k].globalOffset() != globalOffset,
131  op_name_str<<": Error, sub_vec["<<k<<"] "
132  "(subDim="<<targ_sub_vecs[k].subDim()<<",globalOffset="<<targ_sub_vecs[k].globalOffset()<<")"
133  " is not compatible with (subDim="<<subDim<<",globalOffset="<<globalOffset<<")!"
134  );
135  }
136 
137  if (expect_reduct_obj) {
138 
139  TEUCHOS_TEST_FOR_EXCEPTION( is_null(reduct_obj),
141  op_name_str<<": Error, expected a reduction target object!"
142  );
143 
144  const RCP<ReductTarget> dummy_reduct_obj = op.reduct_obj_create();
145 
146  // mfh 22 Sep 2015: Clang 3.6 warns when typeid's expression may
147  // have side effects. In this case, Ptr::operator* may throw in a
148  // debug build if the pointer inside is NULL. (Throwing an
149  // exception counts as a side effect here.)
150  //
151  // const std::type_info &reduct_obj_type = typeid(*reduct_obj);
152  // const std::type_info &dummy_reduct_obj_type = typeid(*dummy_reduct_obj);
153 
154  const ReductTarget& reduct_obj_thing = *reduct_obj;
155  const std::type_info& reduct_obj_type = typeid (reduct_obj_thing);
156  ReductTarget& dummy_reduct_obj_thing = *dummy_reduct_obj;
157  const std::type_info& dummy_reduct_obj_type = typeid (dummy_reduct_obj_thing);
158 
159  TEUCHOS_TEST_FOR_EXCEPTION( reduct_obj_type != dummy_reduct_obj_type,
161  op_name_str<<": Error, the type of the input reduct_obj = "
162  <<Teuchos::demangleName(reduct_obj_type.name())<<" is not equal to"
163  " the expected type "<<Teuchos::demangleName(dummy_reduct_obj_type.name())
164  <<"!"
165  );
166 
167  }
168 
169 }
170 
171 
172 
173 //
174 // Explicit Instantiation Macro
175 //
176 
177 
178 #define RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(SCALAR) \
179  \
180  template std::string DefaultReductTarget<SCALAR >::description() const;
181 
182 
183 #define RTOPPACK_RTOPT_HELPERS_INSTANT_SCALAR(SCALAR) \
184  \
185  RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(SCALAR) \
186  \
187  RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(ScalarIndex<SCALAR >) \
188  \
189  RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(SubVectorView<SCALAR >) \
190  \
191  template void validate_apply_op<SCALAR >( \
192  const RTOpT<SCALAR > &op, \
193  const int allowed_num_sub_vecs, \
194  const int allowed_num_targ_sub_vecs, \
195  const bool expect_reduct_obj, \
196  const ArrayView<const ConstSubVectorView<SCALAR > > &sub_vecs, \
197  const ArrayView<const SubVectorView<SCALAR > > &targ_sub_vecs, \
198  const Ptr<const ReductTarget> &reduct_obj \
199  ); \
200  \
201 
202 
203 
204 #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()