Tpetra Matrix/Vector Services  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 
53  template <class S, class LO, class GO, class Node, class Kernel>
54  class KernelOp : public Tpetra::Operator<S,LO,GO,Node>
55  {
56  protected:
59  mutable RCP< MultiVector<S,LO,GO,Node> > _importMV, _exportMV;
60  RCP< const Map<LO,GO,Node> > _rangeMap, _domainMap;
61  mutable Kernel _kernel;
62  public:
63  KernelOp(Kernel kernel,
64  const RCP<const Map<LO,GO,Node> > & domainMap,
65  const RCP<const Map<LO,GO,Node> > & rangeMap,
66  const RCP<const Import<LO,GO,Node> > & importer,
67  const RCP<const Export<LO,GO,Node> > & exporter)
68  : _importer(importer), _exporter(exporter)
69  , _rangeMap(rangeMap), _domainMap(domainMap)
70  , _kernel(kernel)
71  {
72  std::string tfecfFuncName("KernelOp(kernel,domainMap,rangeMap,importer,exporter)");
73  if (_rangeMap == null) _rangeMap = _domainMap;
74  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( _domainMap == null || _rangeMap == null, std::runtime_error,
75  ":KernelOp(): neither domainMap nor rangeMap may be specified null:\ndomainMap: " << _domainMap << "\nrangeMap: " << _rangeMap << "\n");
76 #ifdef HAVE_TPETRA_DEBUG
77  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( _rangeMap->getNode() != _domainMap->getNode(), std::runtime_error, ": all specified maps must have the same node.");
78  if (_importer != null) {
79  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( !_importer->getSourceMap()->isSameAs(*_domainMap), std::runtime_error, ": domain map is not consistent with importer.");
80  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( _importer->getSourceMap()->getNode() != _domainMap->getNode(), std::runtime_error, ": all specified maps must have the same node.");
81  }
82  if (_exporter != null) {
83  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( !_exporter->getTargetMap()->isSameAs(*_rangeMap), std::runtime_error, ": range map is not consistent with importer.");
84  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( _exporter->getTargetMap()->getNode() != _domainMap->getNode(), std::runtime_error, ": all specified maps must have the same node.");
85  }
86 #endif
87  }
88  //
89  const RCP<const Map<LO,GO,Node> > & getDomainMap() const { return _domainMap; }
90  //
91  const RCP<const Map<LO,GO,Node> > & getRangeMap() const { return _rangeMap; }
92  //
96  S alpha = Teuchos::ScalarTraits<S>::one(),
97  S beta = Teuchos::ScalarTraits<S>::zero()) const
98  {
99  const size_t numVectors = X.getNumVectors();
100  RCP< MultiVector<S,LO,GO,Node> > mvec_inout;
102  //
103  if (_importer != null) {
104  if (_importMV != null && _importMV->getNumVectors() != numVectors) _importMV = null;
105  if (_importMV == null) _importMV = createMultiVector<S>(_importer->getTargetMap(), numVectors);
106  _importMV->doImport( X, *_importer, INSERT );
107  mvec_in2 = _importMV;
108  }
109  else {
110  mvec_in2 = rcpFromRef(X);
111  }
112  //
113  if (_exporter != null) {
114  if (_exportMV != null && _exportMV->getNumVectors() != numVectors) _exportMV = null;
115  if (_exportMV == null) _exportMV = createMultiVector<S>(_exporter->getSourceMap(), numVectors);
116  mvec_inout = _exportMV;
117  }
118  else {
119  mvec_inout = rcpFromRef(Y);
120  }
121  _kernel.setAlphaBeta(alpha,beta);
122  //
123  for (size_t j=0; j < numVectors; ++j)
124  {
125  RCP< Vector<S,LO,GO,Node> > vec_inout = mvec_inout->getVectorNonConst(j);
126  RCP< const Vector<S,LO,GO,Node> > vec_in2 = mvec_in2->getVector(j);
127  Tpetra::RTI::detail::binary_transform( *vec_inout, *vec_in2, _kernel );
128  }
129  // export
130  if (_exporter != null) {
131  Y.doExport(*_exportMV, *_exporter, ADD);
132  }
133  }
134  };
135 
137  template <class S, class LO, class GO, class Node, class Kernel>
139  kernelOp(Kernel kernel,
140  const RCP<const Map<LO,GO,Node> > & domainMap,
141  const RCP<const Map<LO,GO,Node> > & rangeMap = null,
142  const RCP<const Import<LO,GO,Node> > & importer = null,
143  const RCP<const Export<LO,GO,Node> > & exporter = null)
144  {
145  return Teuchos::rcp(new KernelOp<S,LO,GO,Node,Kernel>(kernel,domainMap,rangeMap,importer,exporter) );
146  }
147 
149  template <class S, class LO, class GO, class Node, class Op>
150  class BinaryOp : public KernelOp<S,LO,GO,Node,Tpetra::RTI::detail::BinaryFunctorAdapterWithAlphaBeta<Op,S> >
151  {
152  public:
153  BinaryOp(Op op,
154  const RCP<const Map<LO,GO,Node> > & domainMap,
155  const RCP<const Map<LO,GO,Node> > & rangeMap,
156  const RCP<const Import<LO,GO,Node> > & importer,
157  const RCP<const Export<LO,GO,Node> > & exporter)
159  };
160 
162  template <class S, class LO, class GO, class Node, class Op>
164  binaryOp(Op op,
165  const RCP<const Map<LO,GO,Node> > & domainMap,
166  const RCP<const Map<LO,GO,Node> > & rangeMap = null,
167  const RCP<const Import<LO,GO,Node> > & importer = null,
168  const RCP<const Export<LO,GO,Node> > & exporter = null)
169  {
170  return Teuchos::rcp(new BinaryOp<S,LO,GO,Node,Op>(op,domainMap,rangeMap,importer,exporter) );
171  }
172 
173  } // end of namespace RTI
174 
175 } // end of namespace Tpetra
176 
177 #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.
size_t getNumVectors() const
Number of columns in the multivector.
#define TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(throw_exception_test, Exception, msg)
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 ...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Tpetra::Operator wrapping a Kokkos kernel using the Tpetra Reduction/Transformation Interface...
Abstract interface for linear operators accepting Tpetra MultiVector objects.
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
const RCP< const Map< LO, GO, Node > > & getDomainMap() const
Returns the Map associated with the domain of this operator, which must be compatible with X...
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.
const RCP< const Map< LO, GO, Node > > & getRangeMap() const
Returns the Map associated with the range of this operator, which must be compatible with Y...
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.
Describes a parallel distribution of objects over processes.
Tpetra::Operator wrapping a binary functor using the Tpetra Reduction/Transformation Interface...
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.
void doExport(const SrcDistObject &source, const Export< LocalOrdinal, GlobalOrdinal, Node > &exporter, CombineMode CM)
Export data into this object using an Export object ("forward mode").