Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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  if (_importer != null) {
90  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
91  ! _importer->getSourceMap ()->isSameAs (*_domainMap),
92  std::runtime_error,
93  ": domain Map is not consistent with importer.");
94  }
95  if (_exporter != null) {
96  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
97  ! _exporter->getTargetMap ()->isSameAs (*_rangeMap),
98  std::runtime_error, ": range Map is not consistent with importer.");
99  }
100 #endif // HAVE_TPETRA_DEBUG
101  }
102 
103  RCP<const Map<LO,GO,Node> > getDomainMap () const { return _domainMap; }
104  RCP<const Map<LO,GO,Node> > getRangeMap () const { return _rangeMap; }
105 
106  void
109  Teuchos::ETransp mode = Teuchos::NO_TRANS,
110  S alpha = Teuchos::ScalarTraits<S>::one (),
111  S beta = Teuchos::ScalarTraits<S>::zero ()) const
112  {
113  const size_t numVectors = X.getNumVectors ();
114  RCP<MultiVector<S,LO,GO,Node> > mvec_inout;
115  RCP<const MultiVector<S,LO,GO,Node> > mvec_in2;
116 
117  if (_importer != null) {
118  if (_importMV != null && _importMV->getNumVectors () != numVectors) {
119  _importMV = null;
120  }
121  if (_importMV == null) {
122  _importMV = createMultiVector<S> (_importer->getTargetMap (), numVectors);
123  }
124  _importMV->doImport (X, *_importer, INSERT);
125  mvec_in2 = _importMV;
126  }
127  else {
128  mvec_in2 = rcpFromRef(X);
129  }
130 
131  if (_exporter != null) {
132  if (_exportMV != null && _exportMV->getNumVectors () != numVectors) {
133  _exportMV = null;
134  }
135  if (_exportMV == null) {
136  _exportMV = createMultiVector<S> (_exporter->getSourceMap (), numVectors);
137  }
138  mvec_inout = _exportMV;
139  }
140  else {
141  mvec_inout = rcpFromRef (Y);
142  }
143  _kernel.setAlphaBeta (alpha, beta);
144  //
145  for (size_t j=0; j < numVectors; ++j) {
146  RCP< Vector<S,LO,GO,Node> > vec_inout = mvec_inout->getVectorNonConst(j);
147  RCP< const Vector<S,LO,GO,Node> > vec_in2 = mvec_in2->getVector(j);
148  Tpetra::RTI::detail::binary_transform( *vec_inout, *vec_in2, _kernel );
149  }
150  // export
151  if (_exporter != null) {
152  Y.doExport (*_exportMV, *_exporter, ADD);
153  }
154  }
155  };
156 
158  template <class S, class LO, class GO, class Node, class Kernel>
159  RCP<const KernelOp<S,LO,GO,Node,Kernel> >
160  kernelOp (Kernel kernel,
161  const RCP<const Map<LO,GO,Node> > & domainMap,
162  const RCP<const Map<LO,GO,Node> > & rangeMap = null,
163  const RCP<const Import<LO,GO,Node> > & importer = null,
164  const RCP<const Export<LO,GO,Node> > & exporter = null)
165  {
166  return Teuchos::rcp (new KernelOp<S,LO,GO,Node,Kernel> (kernel, domainMap, rangeMap,
167  importer, exporter) );
168  }
169 
171  template <class S, class LO, class GO, class Node, class Op>
172  class BinaryOp :
173  public KernelOp<S,LO,GO,Node,Tpetra::RTI::detail::BinaryFunctorAdapterWithAlphaBeta<Op,S> >
174  {
175  public:
176  BinaryOp (Op op,
177  const RCP<const Map<LO,GO,Node> > & domainMap,
178  const RCP<const Map<LO,GO,Node> > & rangeMap,
179  const RCP<const Import<LO,GO,Node> > & importer,
180  const RCP<const Export<LO,GO,Node> > & exporter)
182  };
183 
185  template <class S, class LO, class GO, class Node, class Op>
186  RCP<const BinaryOp<S,LO,GO,Node,Op> >
187  binaryOp (Op op,
188  const RCP<const Map<LO,GO,Node> > & domainMap,
189  const RCP<const Map<LO,GO,Node> > & rangeMap = null,
190  const RCP<const Import<LO,GO,Node> > & importer = null,
191  const RCP<const Export<LO,GO,Node> > & exporter = null)
192  {
193  return Teuchos::rcp (new BinaryOp<S,LO,GO,Node,Op> (op, domainMap, rangeMap,
194  importer, exporter) );
195  }
196  } // namespace RTI
197 } // namespace Tpetra
198 
199 #endif // TPETRA_RTIOP_HPP
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
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.
size_t getNumVectors() const
Number of columns in the multivector.
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 ...
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.
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.
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, const CombineMode CM, const bool restrictedMode=false)
Export data into this object using an Export object (&quot;forward mode&quot;).
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...