Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tpetra_RTI.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef TPETRA_RTI_HPP
43 #define TPETRA_RTI_HPP
44 
45 #include <Teuchos_Tuple.hpp>
46 #include <Teuchos_Assert.hpp>
47 
48 #include "Tpetra_ConfigDefs.hpp"
49 
50 #include "Tpetra_Operator.hpp"
51 #include "Tpetra_Import.hpp"
52 #include "Tpetra_Export.hpp"
53 #include "Tpetra_Vector.hpp"
54 #include "Tpetra_RTI_detail.hpp"
55 
56 namespace Tpetra {
57 
58  namespace RTI {
59 
66  template <class T>
67  class ZeroOp {
68  public:
69  static inline T identity() {return Teuchos::ScalarTraits<T>::zero();}
70  };
71 
78  template <class T>
79  class OneOp {
80  public:
81  static inline T identity() {return Teuchos::ScalarTraits<T>::one();}
82  };
83 
110  template <class GOP, class ROP, class IOP>
112  public:
113  typedef GOP GenOP;
114  typedef ROP RedOP;
115  typedef IOP IdOP;
116  GenOP genop;
117  RedOP redop;
118  ReductionGlob(GenOP gop, RedOP rop) : genop(gop), redop(rop) {}
119  };
120 
122  template <class TxOP, class GOP, class ROP, class IOP>
124  public:
125  typedef TxOP TOP;
126  typedef GOP GenOP;
127  typedef ROP RedOP;
128  typedef IOP IdOP;
129  TOP top;
130  GenOP genop;
131  RedOP redop;
132  TransformReductionGlob(TOP txop, GenOP gop, RedOP rop) : top(txop), genop(gop), redop(rop) {}
133  };
134 
136  template <class IOP, class GOP, class ROP>
137  inline ReductionGlob<GOP,ROP,IOP> reductionGlob(GOP gop, ROP rop)
138  {
139  return ReductionGlob<GOP,ROP,IOP>(gop,rop);
140  }
141 
143  template <class IOP, class TOP, class GOP, class ROP>
144  inline TransformReductionGlob<TOP,GOP,ROP,IOP> reductionGlob(TOP top, GOP gop, ROP rop)
145  {
146  return TransformReductionGlob<TOP,GOP,ROP,IOP>(top,gop,rop);
147  }
148 
150 
154  template <class S, class LO, class GO, class Node, class OP>
155  void unary_transform(Vector<S,LO,GO,Node> &vec_inout, OP op)
156  {
158  Tpetra::RTI::detail::unary_transform(vec_inout, Adapter_op);
159  }
160 
162 
166  template <class S1, class S2, class LO, class GO, class Node, class OP>
167  void binary_transform(Vector<S1,LO,GO,Node> &vec_inout, const Vector<S2,LO,GO,Node> &vec_in2, OP op)
168  {
169 #ifdef HAVE_TPETRA_DEBUG
170  TEUCHOS_TEST_FOR_EXCEPTION( vec_inout.getLocalLength() != vec_in2.getLocalLength(), std::runtime_error,
171  "Tpetra::RTI::binary_transform(vec_inout,vec_in2): vec_in2 and vec_inout must have the same local length.");
172 #endif
174  Tpetra::RTI::detail::binary_transform(vec_inout, vec_in2, adapter_op);
175  }
176 
178 
182  template <class S1, class S2, class S3, class LO, class GO, class Node, class OP>
183  void tertiary_transform(Vector<S1,LO,GO,Node> &vec_inout, const Vector<S2,LO,GO,Node> &vec_in2, const Vector<S3,LO,GO,Node> &vec_in3, OP op)
184  {
185 #ifdef HAVE_TPETRA_DEBUG
186  TEUCHOS_TEST_FOR_EXCEPTION( vec_inout.getLocalLength() != vec_in2.getLocalLength() || vec_in2.getLocalLength() != vec_in3.getLocalLength(), std::runtime_error,
187  "Tpetra::RTI::tertiary_transform(vec_inout,vec_in2,vec_in3): vec_inout, vec_in2 and vec_in3 must have the same local length.");
188 #endif
190  Tpetra::RTI::detail::tertiary_transform(vec_inout, vec_in2, vec_in3, adapter_op);
191  }
192 
194 
199  template <class S, class LO, class GO, class Node, class Glob>
200  typename Glob::RedOP::result_type
201  reduce( const Vector<S,LO,GO,Node> &vec_in, Glob glob)
202  {
204  return Tpetra::RTI::detail::reduce(vec_in, adapter_op);
205  }
206 
208 
213  template <class S1, class S2, class LO, class GO, class Node, class Glob>
214  typename Glob::RedOP::result_type
215  reduce( const Vector<S1,LO,GO,Node> &vec_in1, const Vector<S2,LO,GO,Node> &vec_in2, Glob glob)
216  {
217 #ifdef HAVE_TPETRA_DEBUG
218  TEUCHOS_TEST_FOR_EXCEPTION( vec_in1.getLocalLength() != vec_in2.getLocalLength(), std::runtime_error,
219  "Tpetra::RTI::reduce(vec_in1,vec_in2): vec_in1 and vec_in2 must have the same local length.");
220 #endif
222  return Tpetra::RTI::detail::reduce(vec_in1, vec_in2, adapter_op);
223  }
224 
226 
232  template <class S1, class S2, class S3, class LO, class GO, class Node, class Glob>
233  typename Glob::RedOP::result_type
234  reduce(const Vector<S1,LO,GO,Node> &vec_in1, const Vector<S2,LO,GO,Node> &vec_in2, const Vector<S3,LO,GO,Node> &vec_in3, Glob glob)
235  {
236 #ifdef HAVE_TPETRA_DEBUG
237  TEUCHOS_TEST_FOR_EXCEPTION( vec_in1.getLocalLength() != vec_in2.getLocalLength() || vec_in2.getLocalLength() != vec_in3.getLocalLength(),
238  std::runtime_error, "Tpetra::RTI::reduce(vec_in1,vec_in2): vec_in1 and vec_in2 must have the same local length.");
239 #endif
241  return Tpetra::RTI::detail::reduce(vec_in1, vec_in2, vec_in3, adapter_op);
242  }
243 
245 
251  template <class S1, class S2, class LO, class GO, class Node,class Glob>
252  typename Glob::RedOP::result_type
254  {
255 #ifdef HAVE_TPETRA_DEBUG
256  TEUCHOS_TEST_FOR_EXCEPTION( vec_inout.getLocalLength() != vec_in2.getLocalLength(), std::runtime_error,
257  "Tpetra::RTI::binary_pre_transform_reduce(vec_in1,vec_in2): vec_in1 and vec_in2 must have the same local length.");
258 #endif
260  return Tpetra::RTI::detail::transform_reduce(vec_inout, vec_in2, adapter_op);
261  }
262 
264 
271  template <class S1, class S2, class S3, class LO, class GO, class Node,class Glob>
272  typename Glob::RedOP::result_type
274  {
275 #ifdef HAVE_TPETRA_DEBUG
276  TEUCHOS_TEST_FOR_EXCEPTION( vec_inout.getLocalLength() != vec_in2.getLocalLength() && vec_in2.getLocalLength() != vec_in3.getLocalLength(),
277  std::runtime_error, "Tpetra::RTI::tertiary_pre_transform_reduce(vec_in1,vec_in2,vec_in3): vec_in1, vec_in2 and vec_in3 must have the same local length.");
278 #endif
280  return Tpetra::RTI::detail::transform_reduce(vec_inout, vec_in2, vec_in3, adapter_op);
281  }
282 
283  } // end of namespace RTI
284 
285 } // end of namespace Tpetra
286 
287 #define TPETRA_UNARY_TRANSFORM(out,expr) \
288  Tpetra::RTI::unary_transform( *out, [=](decltype((out)->meanValue()) out) \
289  {return expr;})
290 
291 #define TPETRA_BINARY_TRANSFORM(outVec, inVec, expr) \
292  Tpetra::RTI::binary_transform( *outVec, *inVec, [=] (decltype ((outVec)->meanValue ()) outVec, \
293  decltype ((inVec)->meanValue ()) inVec) \
294  { return expr; })
295 
296 #define TPETRA_TERTIARY_TRANSFORM(out,in2,in3,expr) \
297  Tpetra::RTI::tertiary_transform( *out, *in2, *in3, \
298  [=](decltype((out)->meanValue()) out, \
299  decltype((in2)->meanValue()) in2, \
300  decltype((in3)->meanValue()) in3) \
301  {return expr;})
302 
303 
304 #define TPETRA_REDUCE1(in, gexp, id, robj ) \
305  Tpetra::RTI::reduce( *in, \
306  Tpetra::RTI::reductionGlob<id>( [=]( decltype((in)->meanValue()) in ) \
307  { return gexp; }, \
308  robj ) )
309 
310 #define TPETRA_REDUCE2(in1vec, in2vec, gexp, id, robj ) \
311  Tpetra::RTI::reduce (*in1vec, *in2vec, \
312  Tpetra::RTI::reductionGlob<id> ([=] (decltype((in1vec)->meanValue ()) in1vec, \
313  decltype((in2vec)->meanValue ()) in2vec ) \
314  { return gexp; }, \
315  robj))
316 
317 #define TPETRA_REDUCE3(in1,in2,in3, gexp, id, robj ) \
318  Tpetra::RTI::reduce( *in1, *in2, *in3, \
319  Tpetra::RTI::reductionGlob<id>( [=]( decltype((in1)->meanValue()) in1, \
320  decltype((in2)->meanValue()) in2, \
321  decltype((in3)->meanValue()) in3 ) \
322  { return gexp; }, \
323  robj ) )
324 
325 #define TPETRA_BINARY_PRETRANSFORM_REDUCE(out,in, texp, gexp, id, robj ) \
326  Tpetra::RTI::binary_pre_transform_reduce( *out, *in, \
327  Tpetra::RTI::reductionGlob<id>( [=]( decltype((out)->meanValue()) out, \
328  decltype((in)->meanValue()) in ) \
329  { return texp; }, \
330  [=]( decltype((out)->meanValue()) out, \
331  decltype((in)->meanValue()) in ) \
332  { return gexp; }, \
333  robj ) )
334 
335 #define TPETRA_TERTIARY_PRETRANSFORM_REDUCE(out,in2,in3, texp, gexp, id, robj ) \
336  Tpetra::RTI::tertiary_pre_transform_reduce( *out, *in2, *in3, \
337  Tpetra::RTI::reductionGlob<id>( [=]( decltype((out)->meanValue()) out, \
338  decltype((in2)->meanValue()) in2, \
339  decltype((in3)->meanValue()) in3 ) \
340  { return texp; }, \
341  [=]( decltype((out)->meanValue()) out, \
342  decltype((in2)->meanValue()) in2, \
343  decltype((in3)->meanValue()) in3 ) \
344  { return gexp; }, \
345  robj ) )
346 
347 #endif // TPETRA_RTI_HPP
OP::ReductionType transform_reduce(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, OP op)
pass vec_inout and vec_in2 data pointers to \ op, then execute via node parallel_reduce.
void binary_transform(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, OP op)
Transform values of vec_inout using vec_inout, vec_in2 and operator op.
Definition: Tpetra_RTI.hpp:167
adapter class between kernels for Tpetra::RTI::binary_transform and Tpetra::RTI::detail::binary_trans...
void unary_transform(Vector< S, LO, GO, Node > &vec, OP op)
pass vec data pointer to op, then execute via node parallel_for
adapter class between kernels for Tpetra::RTI::binary_pre_transform_reduce and Tpetra::RTI::detail::b...
size_t getLocalLength() const
Local number of rows on the calling process.
Glob::RedOP::result_type binary_pre_transform_reduce(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, Glob glob)
Transforms values of vec_inout while simultaneously performing a parallel reduction.
Definition: Tpetra_RTI.hpp:253
OP::ReductionType reduce(const Vector< S, LO, GO, Node > &vec_in, OP op)
pass vec_in data pointer to \ op, then execute via node parallel_reduce.
adapter class between kernels for Tpetra::RTI::unary_transform and Tpetra::RTI::detail::unary_transfo...
void binary_transform(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, OP op)
pass vec_inout and vec_in2 data pointers to op, then execute via node parallel_for ...
adapter class between kernels for Tpetra::RTI::binary_transform and Tpetra::RTI::detail::binary_trans...
void tertiary_transform(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, const Vector< S3, LO, GO, Node > &vec_in3, OP op)
pass vec_inout, vec_in2 and vec_in3 data pointers to op, then execute via node parallel_for ...
Glob::RedOP::result_type reduce(const Vector< S, LO, GO, Node > &vec_in, Glob glob)
Reduce values of vec_in using the operators instantiated in glob.
Definition: Tpetra_RTI.hpp:201
ReductionGlob< GOP, ROP, IOP > reductionGlob(GOP gop, ROP rop)
Non-member constructor to instantiate a type glob of a static identity functor and generation and red...
Definition: Tpetra_RTI.hpp:137
A type glob containing the types needed for calling Tpetra::RTI::binary_pre_transform_reduce() with i...
Definition: Tpetra_RTI.hpp:123
adapter class between kernels for Tpetra::RTI::tertiary_transform and Tpetra::RTI::detail::tertiary_t...
adapter class between kernels for Tpetra::RTI::binary_transform and Tpetra::RTI::detail::binary_trans...
adapter class between kernels for Tpetra::RTI::binary_transform and Tpetra::RTI::detail::binary_trans...
Glob::RedOP::result_type tertiary_pre_transform_reduce(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, const Vector< S3, LO, GO, Node > &vec_in3, Glob glob)
Transforms values of vec_inout while simultaneously performing a parallel reduction.
Definition: Tpetra_RTI.hpp:273
A type glob containing the types needed for calling Tpetra::RTI::reduce() with individual functors...
Definition: Tpetra_RTI.hpp:111
adapter class between kernels for Tpetra::RTI::binary_pre_transform_reduce and Tpetra::RTI::detail::b...
A distributed dense vector.
A static identity functor, providing a static method identity() that returns zero.
Definition: Tpetra_RTI.hpp:67
void unary_transform(Vector< S, LO, GO, Node > &vec_inout, OP op)
Transform values of vec_inout using via operator op.
Definition: Tpetra_RTI.hpp:155
void tertiary_transform(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, const Vector< S3, LO, GO, Node > &vec_in3, OP op)
Transform values of vec_inout using vec_inout, vec_in2, vec_in3 and operator op.
Definition: Tpetra_RTI.hpp:183
A static identity functor, providing a static method identity() that returns one. ...
Definition: Tpetra_RTI.hpp:79