42 #ifndef TPETRA_RTI_detail_HPP
43 #define TPETRA_RTI_detail_HPP
45 #include <Teuchos_Assert.hpp>
46 #include <Teuchos_CommHelpers.hpp>
47 #include <Kokkos_NodeHelpers.hpp>
49 #include "Tpetra_Vector.hpp"
67 inline StdOpKernel() : _alpha(ScalarTraits<S>::one()), _beta(ScalarTraits<S>::zero()) {}
68 inline void setData(S * vec_inout,
const S * vec_in2) { _vec_inout = vec_inout; _vec_in2 = vec_in2; }
69 inline void setAlphaBeta(
const S &alpha,
const S &beta) { _alpha = alpha; _beta = beta; }
73 template <
class OP,
class S>
80 inline void setData(S *vec) { _vec = vec; }
81 inline void execute(
int i) { _vec[i] = _op(_vec[i]); }
85 template <
class OP,
class S1,
class S2>
93 inline void setData(S1 *vec_inout,
const S2 *vec_in2) { _vec_inout = vec_inout; _vec_in2 = vec_in2; }
94 inline void execute(
int i) { _vec_inout[i] = _op(_vec_inout[i], _vec_in2[i]); }
98 template <
class OP,
class S>
106 inline void setData(S *vec_inout,
const S *vec_in2) { _vec_inout = vec_inout; _vec_in2 = vec_in2; }
107 inline void execute(
int i)
109 S res = _op(_vec_inout[i], _vec_in2[i]);
110 _vec_inout[i] = this->_alpha * res + this->_beta * _vec_inout[i];
115 template <
class OP,
class S1,
class S2,
class S3>
124 inline void setData(S1 *vec_inout,
const S2 *vec_in2,
const S3 *vec_in3) { _vec_inout = vec_inout; _vec_in2 = vec_in2; _vec_in3 = vec_in3; }
125 inline void execute(
int i) { _vec_inout[i] = _op(_vec_inout[i], _vec_in2[i], _vec_in3[i]); }
129 template <
class Glob,
class S>
132 typedef typename Glob::GenOP GenOP;
133 typedef typename Glob::RedOP RedOP;
134 typedef typename Glob::IdOP IdOP;
135 typedef typename RedOP::result_type ReductionType;
142 inline void setData(
const S *vec_in) { _vec_in = vec_in; }
143 static inline ReductionType identity() {
return IdOP::identity(); }
144 inline ReductionType generate(
int i) {
return _genop(_vec_in[i]); }
145 inline ReductionType reduce(ReductionType a, ReductionType b) {
return _redop(a, b); }
149 template <
class Glob,
class S1,
class S2>
152 typedef typename Glob::GenOP GenOP;
153 typedef typename Glob::RedOP RedOP;
154 typedef typename Glob::IdOP IdOP;
155 typedef typename RedOP::result_type ReductionType;
163 inline void setData(
const S1 *vec_in1,
const S2 *vec_in2) { _vec_in1 = vec_in1; _vec_in2 = vec_in2; }
164 static inline ReductionType identity() {
return IdOP::identity(); }
165 inline ReductionType generate(
int i) {
return _genop(_vec_in1[i], _vec_in2[i]); }
166 inline ReductionType reduce(ReductionType a, ReductionType b) {
return _redop(a, b); }
170 template <
class Glob,
class S1,
class S2,
class S3>
173 typedef typename Glob::GenOP GenOP;
174 typedef typename Glob::RedOP RedOP;
175 typedef typename Glob::IdOP IdOP;
176 typedef typename RedOP::result_type ReductionType;
185 inline void setData(
const S1 *vec_in1,
const S2 *vec_in2,
const S3 *vec_in3) { _vec_in1 = vec_in1; _vec_in2 = vec_in2; _vec_in3 = vec_in3; }
186 static inline ReductionType identity() {
return IdOP::identity(); }
187 inline ReductionType generate(
int i) {
return _genop(_vec_in1[i], _vec_in2[i], _vec_in3[i]); }
188 inline ReductionType reduce(ReductionType a, ReductionType b) {
return _redop(a, b); }
192 template <
class Glob,
class S1,
class S2>
195 typedef typename Glob::TOP TOP;
196 typedef typename Glob::GenOP GenOP;
197 typedef typename Glob::RedOP RedOP;
198 typedef typename Glob::IdOP IdOP;
199 typedef typename RedOP::result_type ReductionType;
208 inline void setData(S1 *vec_inout,
const S2 *vec_in2) { _vec_inout = vec_inout; _vec_in2 = vec_in2; }
209 static inline ReductionType identity() {
return IdOP::identity(); }
210 inline ReductionType reduce(ReductionType a, ReductionType b) {
return _redop(a, b); }
211 inline ReductionType generate(
int i)
213 _vec_inout[i] = _top( _vec_inout[i], _vec_in2[i] );
214 return _genop( _vec_inout[i], _vec_in2[i] );
219 template <
class Glob,
class S1,
class S2,
class S3>
222 typedef typename Glob::TOP TOP;
223 typedef typename Glob::GenOP GenOP;
224 typedef typename Glob::RedOP RedOP;
225 typedef typename Glob::IdOP IdOP;
226 typedef typename RedOP::result_type ReductionType;
236 inline void setData(S1 *vec_inout,
const S2 *vec_in2,
const S3 *vec_in3) { _vec_inout = vec_inout; _vec_in2 = vec_in2; _vec_in3 = vec_in3; }
237 static inline ReductionType identity() {
return IdOP::identity(); }
238 inline ReductionType reduce(ReductionType a, ReductionType b) {
return _redop(a, b); }
239 inline ReductionType generate(
int i)
241 _vec_inout[i] = _top( _vec_inout[i], _vec_in2[i], _vec_in3[i] );
242 return _genop( _vec_inout[i], _vec_in2[i], _vec_in3[i] );
253 typedef typename OP::ReductionType Packet;
255 void reduce(
const int count,
const Packet inBuffer[], Packet inoutBuffer [])
const
257 for (
int i=0; i != count; ++i) {
258 inoutBuffer[i] = _op.reduce( inoutBuffer[i], inBuffer[i] );
264 template <
class S,
class LO,
class GO,
class Node,
class OP>
268 const RCP<Node> node = mv.getNode();
270 Kokkos::ReadyBufferHelper<Node> rbh(node);
272 S * out_ptr = rbh.addNonConstBuffer(mv.getValuesNonConst());
275 const size_t N = mv.getNumRows();
276 node->template parallel_for(0, N, op);
280 template <
class S1,
class S2,
class LO,
class GO,
class Node,
class OP>
284 const Kokkos::MultiVector<S2,Node> &mv_in2 = vec_in2.
getLocalMV();
285 const RCP<Node> node = mv_inout.getNode();
287 Kokkos::ReadyBufferHelper<Node> rbh(node);
289 S1 * out_ptr = rbh.addNonConstBuffer(mv_inout.getValuesNonConst());
290 const S2 * in_ptr = rbh.addConstBuffer(mv_in2.getValues());
292 op.setData(out_ptr, in_ptr);
293 const size_t N = mv_inout.getNumRows();
294 #ifdef HAVE_TPETRA_DEBUG
295 TEUCHOS_TEST_FOR_EXCEPTION( mv_in2.getNode() != mv_inout.getNode(), std::runtime_error,
296 "Tpetra::RTI::detail::binary_transform(): multivectors must share the same node.");
298 node->template parallel_for(0, N, op);
302 template <
class S1,
class S2,
class S3,
class LO,
class GO,
class Node,
class OP>
303 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)
306 const Kokkos::MultiVector<S2,Node> &mv_in2 = vec_in2.
getLocalMV();
307 const Kokkos::MultiVector<S3,Node> &mv_in3 = vec_in3.
getLocalMV();
308 const RCP<Node> node = mv_inout.getNode();
310 Kokkos::ReadyBufferHelper<Node> rbh(node);
312 S1 * out_ptr = rbh.addNonConstBuffer(mv_inout.getValuesNonConst());
313 const S2 * in_ptr2 = rbh.addConstBuffer(mv_in2.getValues());
314 const S3 * in_ptr3 = rbh.addConstBuffer(mv_in3.getValues());
316 op.setData(out_ptr, in_ptr2, in_ptr3);
317 const size_t N = mv_inout.getNumRows();
318 #ifdef HAVE_TPETRA_DEBUG
319 TEUCHOS_TEST_FOR_EXCEPTION( mv_in2.getNode() != mv_inout.getNode() || mv_in3.getNode() != mv_in2.getNode(), std::runtime_error,
320 "Tpetra::RTI::detail::tertiary_transform(): multivectors must share the same node.");
322 node->template parallel_for(0, N, op);
326 template <
class S,
class LO,
class GO,
class Node,
class OP>
327 typename OP::ReductionType
330 const Kokkos::MultiVector<S,Node> &mv_in = vec_in.
getLocalMV();
331 const RCP<Node> node = mv_in.getNode();
332 const RCP<const Teuchos::Comm<int> > comm = vec_in.
getMap()->getComm();
334 Kokkos::ReadyBufferHelper<Node> rbh(node);
336 const S * in_ptr = rbh.addConstBuffer(mv_in.getValues());
338 op.setData( in_ptr );
339 const size_t N = mv_in.getNumRows();
341 typename OP::ReductionType gbl_res, lcl_res;
342 lcl_res = node->template parallel_reduce(0, N, op);
345 Teuchos::reduceAll(*comm, vtrop, 1, &lcl_res, &gbl_res);
350 template <
class S1,
class S2,
class LO,
class GO,
class Node,
class OP>
351 typename OP::ReductionType
354 const Kokkos::MultiVector<S1,Node> &mv_in1 = vec_in1.
getLocalMV(),
356 const RCP<Node> node = mv_in1.getNode();
357 const RCP<const Teuchos::Comm<int> > comm = vec_in1.
getMap()->getComm();
359 Kokkos::ReadyBufferHelper<Node> rbh(node);
361 const S1 * in_ptr1 = rbh.addConstBuffer(mv_in1.getValues());
362 const S2 * in_ptr2 = rbh.addConstBuffer(mv_in2.getValues());
364 op.setData( in_ptr1, in_ptr2 );
365 const size_t N = mv_in1.getNumRows();
366 #ifdef HAVE_TPETRA_DEBUG
367 TEUCHOS_TEST_FOR_EXCEPTION( mv_in1.getNode() != mv_in2.getNode(), std::runtime_error,
368 "Tpetra::RTI::detail::reduce(): multivectors must share the same node.");
371 typename OP::ReductionType gbl_res, lcl_res;
372 lcl_res = node->template parallel_reduce(0, N, op);
375 Teuchos::reduceAll(*comm, vtrop, 1, &lcl_res, &gbl_res);
380 template <
class S1,
class S2,
class S3,
class LO,
class GO,
class Node,
class OP>
381 typename OP::ReductionType
382 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, OP op)
384 const Kokkos::MultiVector<S1,Node> &mv_in1 = vec_in1.
getLocalMV();
385 const Kokkos::MultiVector<S2,Node> &mv_in2 = vec_in2.
getLocalMV();
386 const Kokkos::MultiVector<S3,Node> &mv_in3 = vec_in3.
getLocalMV();
387 const RCP<Node> node = mv_in1.getNode();
388 const RCP<const Teuchos::Comm<int> > comm = vec_in1.
getMap()->getComm();
390 Kokkos::ReadyBufferHelper<Node> rbh(node);
392 const S1 * in_ptr1 = rbh.addConstBuffer(mv_in1.getValues());
393 const S2 * in_ptr2 = rbh.addConstBuffer(mv_in2.getValues());
394 const S3 * in_ptr3 = rbh.addConstBuffer(mv_in3.getValues());
396 op.setData( in_ptr1, in_ptr2, in_ptr3 );
397 const size_t N = mv_in1.getNumRows();
398 #ifdef HAVE_TPETRA_DEBUG
399 TEUCHOS_TEST_FOR_EXCEPTION( mv_in1.getNode() != mv_in2.getNode() || mv_in2.getNode() != mv_in3.getNode(), std::runtime_error,
400 "Tpetra::RTI::detail::reduce(): multivectors must share the same node.");
403 typename OP::ReductionType gbl_res, lcl_res;
404 lcl_res = node->template parallel_reduce(0, N, op);
407 Teuchos::reduceAll(*comm, vtrop, 1, &lcl_res, &gbl_res);
412 template <
class S1,
class S2,
class LO,
class GO,
class Node,
class OP>
413 typename OP::ReductionType
417 const Kokkos::MultiVector<S2,Node> &mv_in2 = vec_in2.
getLocalMV();
418 const RCP<Node> node = mv_inout.getNode();
419 const RCP<const Teuchos::Comm<int> > comm = vec_inout.
getMap()->getComm();
421 Kokkos::ReadyBufferHelper<Node> rbh(node);
423 S1 * in_ptr1 = rbh.addNonConstBuffer(mv_inout.getValuesNonConst());
424 const S2 * in_ptr2 = rbh.addConstBuffer(mv_in2.getValues());
426 op.setData( in_ptr1, in_ptr2 );
427 const size_t N = mv_inout.getNumRows();
428 #ifdef HAVE_TPETRA_DEBUG
429 TEUCHOS_TEST_FOR_EXCEPTION( mv_inout.getNode() != mv_in2.getNode(), std::runtime_error,
430 "Tpetra::RTI::detail::transform_reduce(): multivectors must share the same node.");
433 typename OP::ReductionType gbl_res, lcl_res;
434 lcl_res = node->template parallel_reduce(0, N, op);
437 Teuchos::reduceAll(*comm, vtrop, 1, &lcl_res, &gbl_res);
442 template <
class S1,
class S2,
class S3,
class LO,
class GO,
class Node,
class OP>
443 typename OP::ReductionType
444 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, OP op)
447 const Kokkos::MultiVector<S2,Node> &mv_in2 = vec_in2.
getLocalMV();
448 const Kokkos::MultiVector<S3,Node> &mv_in3 = vec_in3.
getLocalMV();
449 const RCP<Node> node = mv_inout.getNode();
450 const RCP<const Teuchos::Comm<int> > comm = vec_inout.
getMap()->getComm();
452 Kokkos::ReadyBufferHelper<Node> rbh(node);
454 S1 * in_ptr1 = rbh.addNonConstBuffer(mv_inout.getValuesNonConst());
455 const S2 * in_ptr2 = rbh.addConstBuffer(mv_in2.getValues());
456 const S3 * in_ptr3 = rbh.addConstBuffer(mv_in3.getValues());
458 op.setData( in_ptr1, in_ptr2, in_ptr3 );
459 const size_t N = mv_inout.getNumRows();
460 #ifdef HAVE_TPETRA_DEBUG
461 TEUCHOS_TEST_FOR_EXCEPTION( mv_inout.getNode() != mv_in2.getNode() && mv_inout.getNode() != mv_in3.getNode(), std::runtime_error,
462 "Tpetra::RTI::detail::transform_transform(): multivectors must share the same node.");
465 typename OP::ReductionType gbl_res, lcl_res;
466 lcl_res = node->template parallel_reduce(0, N, op);
469 Teuchos::reduceAll(*comm, vtrop, 1, &lcl_res, &gbl_res);
479 #endif // TPETRA_RTI_detail_HPP
Utility base class for kernels used to define Tpetra::Operator objects.
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.
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
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...
decorator for Kokkos reduction kernels to satisfy requirements for Teuchos::ValueTypeReductionOp ...
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 ...
const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getMap() const
The Map describing the parallel distribution of this object.
adapter class between binary functors and BinaryOp
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...
const Kokkos::MultiVector< Scalar, Node > & getLocalMV() const
Return a const reference to the underlying Kokkos::MultiVector object (advanced use only) ...
A distributed dense vector.
Kokkos::MultiVector< Scalar, Node > & getLocalMVNonConst()
Return a non-const reference to the underlying Kokkos::MultiVector object (advanced use only) ...