Tpetra parallel linear algebra  Version of the Day
Tpetra_RTIOp.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_RTIOP_HPP
43 #define TPETRA_RTIOP_HPP
44 
45 #include "Tpetra_RTI.hpp"
46 #include "Tpetra_RTI_detail.hpp"
47 
48 namespace Tpetra {
49 
50  namespace RTI {
51 
60  template <class S, class LO, class GO, class Node, class Kernel>
61  class KernelOp : public Tpetra::Operator<S,LO,GO,Node> {
62  protected:
63  RCP<const Import<LO,GO,Node> > _importer;
64  RCP<const Export<LO,GO,Node> > _exporter;
65  mutable RCP<MultiVector<S,LO,GO,Node> > _importMV, _exportMV;
66  RCP<const Map<LO,GO,Node> > _rangeMap, _domainMap;
67  mutable Kernel _kernel;
68 
69  public:
71  KernelOp (Kernel kernel,
72  const RCP<const Map<LO,GO,Node> > & domainMap,
73  const RCP<const Map<LO,GO,Node> > & rangeMap,
74  const RCP<const Import<LO,GO,Node> > & importer,
75  const RCP<const Export<LO,GO,Node> > & exporter)
76  : _importer (importer), _exporter (exporter)
77  , _rangeMap (rangeMap), _domainMap (domainMap)
78  , _kernel (kernel)
79  {
80  const char tfecfFuncName[] = "KernelOp(kernel,domainMap,rangeMap,importer,exporter)";
81  if (_rangeMap == null) {
82  _rangeMap = _domainMap;
83  }
84  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
85  _domainMap == null || _rangeMap == null, std::runtime_error,
86  ": neither domainMap nor rangeMap may be specified null:\ndomainMap: "
87  << _domainMap << "\nrangeMap: " << _rangeMap << "\n");
88 #ifdef HAVE_TPETRA_DEBUG
89  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
90  _rangeMap->getNode() != _domainMap->getNode(), std::runtime_error,
91  ": all specified maps must have the same Node instance.");
92  if (_importer != null) {
93  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
94  ! _importer->getSourceMap ()->isSameAs (*_domainMap),
95  std::runtime_error,
96  ": domain Map is not consistent with importer.");
97  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
98  _importer->getSourceMap ()->getNode () != _domainMap->getNode (),
99  std::runtime_error,
100  ": all specified Maps must have the same Node instance.");
101  }
102  if (_exporter != null) {
103  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
104  ! _exporter->getTargetMap ()->isSameAs (*_rangeMap),
105  std::runtime_error, ": range Map is not consistent with importer.");
106  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
107  _exporter->getTargetMap ()->getNode () != _domainMap->getNode (),
108  std::runtime_error,
109  ": all specified Maps must have the same Node instance.");
110  }
111 #endif // HAVE_TPETRA_DEBUG
112  }
113 
114  RCP<const Map<LO,GO,Node> > getDomainMap () const { return _domainMap; }
115  RCP<const Map<LO,GO,Node> > getRangeMap () const { return _rangeMap; }
116 
117  void
120  Teuchos::ETransp mode = Teuchos::NO_TRANS,
121  S alpha = Teuchos::ScalarTraits<S>::one (),
122  S beta = Teuchos::ScalarTraits<S>::zero ()) const
123  {
124  const size_t numVectors = X.getNumVectors ();
125  RCP<MultiVector<S,LO,GO,Node> > mvec_inout;
126  RCP<const MultiVector<S,LO,GO,Node> > mvec_in2;
127 
128  if (_importer != null) {
129  if (_importMV != null && _importMV->getNumVectors () != numVectors) {
130  _importMV = null;
131  }
132  if (_importMV == null) {
133  _importMV = createMultiVector<S> (_importer->getTargetMap (), numVectors);
134  }
135  _importMV->doImport (X, *_importer, INSERT);
136  mvec_in2 = _importMV;
137  }
138  else {
139  mvec_in2 = rcpFromRef(X);
140  }
141 
142  if (_exporter != null) {
143  if (_exportMV != null && _exportMV->getNumVectors () != numVectors) {
144  _exportMV = null;
145  }
146  if (_exportMV == null) {
147  _exportMV = createMultiVector<S> (_exporter->getSourceMap (), numVectors);
148  }
149  mvec_inout = _exportMV;
150  }
151  else {
152  mvec_inout = rcpFromRef (Y);
153  }
154  _kernel.setAlphaBeta (alpha, beta);
155  //
156  for (size_t j=0; j < numVectors; ++j) {
157  RCP< Vector<S,LO,GO,Node> > vec_inout = mvec_inout->getVectorNonConst(j);
158  RCP< const Vector<S,LO,GO,Node> > vec_in2 = mvec_in2->getVector(j);
159  Tpetra::RTI::detail::binary_transform( *vec_inout, *vec_in2, _kernel );
160  }
161  // export
162  if (_exporter != null) {
163  Y.doExport (*_exportMV, *_exporter, ADD);
164  }
165  }
166  };
167 
169  template <class S, class LO, class GO, class Node, class Kernel>
170  RCP<const KernelOp<S,LO,GO,Node,Kernel> >
171  kernelOp (Kernel kernel,
172  const RCP<const Map<LO,GO,Node> > & domainMap,
173  const RCP<const Map<LO,GO,Node> > & rangeMap = null,
174  const RCP<const Import<LO,GO,Node> > & importer = null,
175  const RCP<const Export<LO,GO,Node> > & exporter = null)
176  {
177  return Teuchos::rcp (new KernelOp<S,LO,GO,Node,Kernel> (kernel, domainMap, rangeMap,
178  importer, exporter) );
179  }
180 
182  template <class S, class LO, class GO, class Node, class Op>
183  class BinaryOp :
184  public KernelOp<S,LO,GO,Node,Tpetra::RTI::detail::BinaryFunctorAdapterWithAlphaBeta<Op,S> >
185  {
186  public:
187  BinaryOp (Op op,
188  const RCP<const Map<LO,GO,Node> > & domainMap,
189  const RCP<const Map<LO,GO,Node> > & rangeMap,
190  const RCP<const Import<LO,GO,Node> > & importer,
191  const RCP<const Export<LO,GO,Node> > & exporter)
193  };
194 
196  template <class S, class LO, class GO, class Node, class Op>
197  RCP<const BinaryOp<S,LO,GO,Node,Op> >
198  binaryOp (Op op,
199  const RCP<const Map<LO,GO,Node> > & domainMap,
200  const RCP<const Map<LO,GO,Node> > & rangeMap = null,
201  const RCP<const Import<LO,GO,Node> > & importer = null,
202  const RCP<const Export<LO,GO,Node> > & exporter = null)
203  {
204  return Teuchos::rcp (new BinaryOp<S,LO,GO,Node,Op> (op, domainMap, rangeMap,
205  importer, exporter) );
206  }
207  } // namespace RTI
208 } // namespace Tpetra
209 
210 #endif // TPETRA_RTIOP_HPP
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
Namespace Tpetra contains the class and methods constituting the Tpetra library.
RCP< const BinaryOp< S, LO, GO, Node, Op > > binaryOp(Op op, const RCP< const Map< LO, GO, Node > > &domainMap, const RCP< const Map< LO, GO, Node > > &rangeMap=null, const RCP< const Import< LO, GO, Node > > &importer=null, const RCP< const Export< LO, GO, Node > > &exporter=null)
Non-member constructor for a Tpetra::RTI::BinaryOp object.
KernelOp(Kernel kernel, const RCP< const Map< LO, GO, Node > > &domainMap, const RCP< const Map< LO, GO, Node > > &rangeMap, const RCP< const Import< LO, GO, Node > > &importer, const RCP< const Export< LO, GO, Node > > &exporter)
Constructor.
RCP< const Map< LO, GO, Node > > getDomainMap() const
The Map associated with the domain of this operator, which must be compatible with X...
One or more distributed dense vectors.
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 ...
void apply(const MultiVector< S, LO, GO, Node > &X, MultiVector< S, LO, GO, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, S alpha=Teuchos::ScalarTraits< S >::one(), S beta=Teuchos::ScalarTraits< S >::zero()) const
Computes the operator-multivector application.
Insert new values that don&#39;t currently exist.
Operator wrapping a Kokkos (Classic) kernel using RTI.
Abstract interface for operators (e.g., matrices and preconditioners).
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
Sum new values into existing values.
size_t getNumVectors() const
Number of columns in the multivector.
RCP< const KernelOp< S, LO, GO, Node, Kernel > > kernelOp(Kernel kernel, const RCP< const Map< LO, GO, Node > > &domainMap, const RCP< const Map< LO, GO, Node > > &rangeMap=null, const RCP< const Import< LO, GO, Node > > &importer=null, const RCP< const Export< LO, GO, Node > > &exporter=null)
Non-member constructor for a Tpetra::RTI::KernelOp object.
Tpetra::Operator wrapping a binary functor using the Tpetra Reduction/Transformation Interface...
RCP< const Map< LO, GO, Node > > getRangeMap() const
The Map associated with the range of this operator, which must be compatible with Y...
void doExport(const SrcDistObject &source, const Export< LocalOrdinal, GlobalOrdinal, Node > &exporter, CombineMode CM)
Export data into this object using an Export object ("forward mode").