Support Software for Vector Reduction/Transformation Operators  Version of the Day
RTOpPack_RTOpTHelpers_def.hpp
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 
47 #include "RTOpPack_RTOpTHelpers_decl.hpp"
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<"
64  <<TypeNameTraits<ConcreteReductObj>::name()<<">"
65  << "{concreteReductObj="<<concreteReductObj_<<"}";
66  return oss.str();
67 }
68 
69 
70 } // namespace RTOpPack
71 
72 
73 template<class Scalar>
74 void RTOpPack::validate_apply_op(
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 ) {
118  TEUCHOS_TEST_FOR_EXCEPTION(
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 ) {
128  TEUCHOS_TEST_FOR_EXCEPTION(
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  const std::type_info &reduct_obj_type = typeid(*reduct_obj);
147  const std::type_info &dummy_reduct_obj_type = typeid(*dummy_reduct_obj);
148 
149  TEUCHOS_TEST_FOR_EXCEPTION( reduct_obj_type != dummy_reduct_obj_type,
151  op_name_str<<": Error, the type of the input reduct_obj = "
152  <<Teuchos::demangleName(reduct_obj_type.name())<<" is not equal to"
153  " the expected type "<<Teuchos::demangleName(dummy_reduct_obj_type.name())
154  <<"!"
155  );
156 
157  }
158 
159 }
160 
161 
162 
163 //
164 // Explicit Instantiation Macro
165 //
166 
167 
168 #define RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(SCALAR) \
169  \
170  template std::string DefaultReductTarget<SCALAR >::description() const;
171 
172 
173 #define RTOPPACK_RTOPT_HELPERS_INSTANT_SCALAR(SCALAR) \
174  \
175  RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(SCALAR) \
176  \
177  RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(ScalarIndex<SCALAR >) \
178  \
179  RTOPPACK_RTOPT_HELPERS_DEFAULTREDUCTTARGET_INSTANT(SubVectorView<SCALAR >) \
180  \
181  template void validate_apply_op<SCALAR >( \
182  const RTOpT<SCALAR > &op, \
183  const int allowed_num_sub_vecs, \
184  const int allowed_num_targ_sub_vecs, \
185  const bool expect_reduct_obj, \
186  const ArrayView<const ConstSubVectorView<SCALAR > > &sub_vecs, \
187  const ArrayView<const SubVectorView<SCALAR > > &targ_sub_vecs, \
188  const Ptr<const ReductTarget> &reduct_obj \
189  ); \
190  \
191 
192 
193 
194 #endif // RTOPPACK_RTOP_T_HELPERS_DEF_HPP
std::string op_name() const
Teuchos::RCP< ReductTarget > reduct_obj_create() const