Xpetra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Xpetra_TpetraHalfPrecisionOperator.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef XPETRA_TPETRAHALFPRECISIONOPEARTOR_HPP
47 #define XPETRA_TPETRAHALFPRECISIONOPEARTOR_HPP
48 
49 #include "Xpetra_ConfigDefs.hpp"
50 
51 #include <Teuchos_ScalarTraits.hpp>
52 
53 #include <Tpetra_CrsMatrix.hpp>
55 #include <Xpetra_MultiVector.hpp>
56 #include <Xpetra_CrsMatrixWrap.hpp>
57 #include <Xpetra_MultiVectorFactory.hpp>
58 
59 namespace Xpetra {
60 
61 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
62 RCP<Xpetra::Matrix<typename Teuchos::ScalarTraits<Scalar>::halfPrecision, LocalOrdinal, GlobalOrdinal, Node>>
64 #if defined(HAVE_TPETRA_INST_DOUBLE) && defined(HAVE_TPETRA_INST_FLOAT)
65  using HalfScalar = typename Teuchos::ScalarTraits<Scalar>::halfPrecision;
67 
68  RCP<XpCrs> xpCrs = Teuchos::rcp_dynamic_cast<Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>>(A, true)->getCrsMatrix();
69  auto tpCrs = Teuchos::rcp_dynamic_cast<Xpetra::TpetraCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>(xpCrs, true)->getTpetra_CrsMatrix();
70  auto newTpCrs = tpCrs->template convert<HalfScalar>();
71  auto newXpCrs = Teuchos::rcp(new Xpetra::TpetraCrsMatrix<HalfScalar, LocalOrdinal, GlobalOrdinal, Node>(newTpCrs));
73 
74  return newA;
75 #else
76  TEUCHOS_TEST_FOR_EXCEPTION(true, Xpetra::Exceptions::RuntimeError,
77  "Xpetra::convertToHalfPrecision only available for Tpetra with SC=double and SC=float enabled");
78  TEUCHOS_UNREACHABLE_RETURN(false);
79 #endif
80 }
81 
82 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
83 RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::halfPrecision, LocalOrdinal, GlobalOrdinal, Node>>
85 #if defined(HAVE_TPETRA_INST_DOUBLE) && defined(HAVE_TPETRA_INST_FLOAT)
86  using HalfScalar = typename Teuchos::ScalarTraits<Scalar>::halfPrecision;
87 
88  auto tpX = toTpetra(*X);
89  auto newTpX = tpX.template convert<HalfScalar>();
91 
92  return newX;
93 #else
94  TEUCHOS_TEST_FOR_EXCEPTION(true, Xpetra::Exceptions::RuntimeError,
95  "Xpetra::convertToHalfPrecision only available for Tpetra with SC=double and SC=float enabled");
96  TEUCHOS_UNREACHABLE_RETURN(false);
97 #endif
98 }
99 
102 template <class Scalar,
103  class LocalOrdinal,
104  class GlobalOrdinal,
105  class Node = Tpetra::KokkosClassic::DefaultNode::DefaultNodeType>
106 class TpetraHalfPrecisionOperator : public Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node> {
107  public:
108  typedef typename Teuchos::ScalarTraits<Scalar>::halfPrecision HalfScalar;
109 
111 
112 
115  : Op_(op) {
116  Allocate(1);
117  }
118 
119  void Allocate(int numVecs) {
122  }
123 
126 
128 
130  const Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> getDomainMap() const {
131  return Op_->getDomainMap();
132  }
133 
135  const Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> getRangeMap() const {
136  return Op_->getRangeMap();
137  }
138 
140 
146  Teuchos::ETransp mode = Teuchos::NO_TRANS,
147  Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
148  Scalar beta = Teuchos::ScalarTraits<Scalar>::one()) const {
150  Tpetra::deep_copy(*Teuchos::rcp_dynamic_cast<tMVHalf>(X_)->getTpetra_MultiVector(),
151  toTpetra(X));
152  Op_->apply(*X_, *Y_, mode, Teuchos::as<HalfScalar>(alpha), Teuchos::as<HalfScalar>(beta));
153  Tpetra::deep_copy(toTpetra(Y),
154  *Teuchos::rcp_dynamic_cast<tMVHalf>(Y_)->getTpetra_MultiVector());
155  }
156 
158  bool hasTransposeApply() const { return false; }
159 
164  using STS = Teuchos::ScalarTraits<Scalar>;
165  R.update(STS::one(), B, STS::zero());
166  this->apply(X, R, Teuchos::NO_TRANS, -STS::one(), STS::one());
167  }
168 
170 
171 
173  RCP<Xpetra::Operator<HalfScalar, LocalOrdinal, GlobalOrdinal, Node>> GetHalfPrecisionOperator() const { return Op_; }
174 
176 
178 
179  private:
180  RCP<Xpetra::Operator<HalfScalar, LocalOrdinal, GlobalOrdinal, Node>> Op_;
181  RCP<Xpetra::MultiVector<HalfScalar, LocalOrdinal, GlobalOrdinal, Node>> X_, Y_;
182 };
183 
184 } // namespace Xpetra
185 
186 #endif // XPETRA_TPETRAHALFPRECISIONOPEARTOR_HPP
static Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Build(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node >> &map, size_t NumVectors, bool zeroOut=true)
Constructor specifying the number of non-zeros for all rows.
Teuchos::ScalarTraits< Scalar >::halfPrecision HalfScalar
void SetHalfPrecisionOperator(const RCP< Xpetra::Operator< HalfScalar, LocalOrdinal, GlobalOrdinal, Node >> &op)
RCP< Xpetra::Matrix< typename Teuchos::ScalarTraits< Scalar >::halfPrecision, LocalOrdinal, GlobalOrdinal, Node > > convertToHalfPrecision(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> &A)
Wraps an existing halfer precision Xpetra::Operator as a Xpetra::Operator.
RCP< Xpetra::Operator< HalfScalar, LocalOrdinal, GlobalOrdinal, Node > > GetHalfPrecisionOperator() const
Direct access to the underlying TpetraOperator.
Exception throws to report errors in the internal logical of the program.
RCP< Xpetra::MultiVector< HalfScalar, LocalOrdinal, GlobalOrdinal, Node > > X_
void apply(const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::one()) const
Returns in Y the result of a Xpetra::TpetraOperator applied to a Xpetra::MultiVector X...
virtual void update(const Scalar &alpha, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Scalar &beta)=0
Update multi-vector values with scaled values of A, this = beta*this + alpha*A.
TpetraHalfPrecisionOperator(const RCP< Xpetra::Operator< HalfScalar, LocalOrdinal, GlobalOrdinal, Node >> &op)
Constructor.
void residual(const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &B, Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &R) const
Compute a residual R = B - (*this) * X.
RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > toTpetra(const RCP< const CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > &graph)
const Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const
Returns the Tpetra::Map object associated with the range of this TpetraOperator.
RCP< Xpetra::MultiVector< HalfScalar, LocalOrdinal, GlobalOrdinal, Node > > Y_
const Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const
Returns the Tpetra::Map object associated with the domain of this TpetraOperator. ...
Concrete implementation of Xpetra::Matrix.
bool hasTransposeApply() const
Indicates whether this TpetraOperator supports applying the adjoint TpetraOperator.
RCP< Xpetra::Operator< HalfScalar, LocalOrdinal, GlobalOrdinal, Node > > Op_
Xpetra-specific matrix class.