| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Thyra: Interfaces and Support for Abstract Numerical Algorithms |
| 4 |
// |
| 5 |
// Copyright 2004 NTESS and the Thyra contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef Thyra_TpetraExplicitAdjointModelEvaluator_hpp |
| 11 |
#define Thyra_TpetraExplicitAdjointModelEvaluator_hpp |
| 12 |
|
| 13 |
#include "Thyra_ModelEvaluatorDelegatorBase.hpp" |
| 14 |
#include "Thyra_TpetraLinearOp.hpp" |
| 15 |
#include "Tpetra_RowMatrixTransposer.hpp" |
| 16 |
|
| 17 |
namespace Thyra { |
| 18 |
|
| 19 |
/** \brief A model evaluator decorator for computing an explicit adjoint. */ |
| 20 |
/** |
| 21 |
* This ModelEvaluator only supports computing W_op and forms the adjoint by |
| 22 |
* computing W_op from the underlying ModelEvaluator and then explicitly |
| 23 |
* transposes it. |
| 24 |
* |
| 25 |
* Since it derives from ModelEvaluatorDelegatorBase, it forwards all |
| 26 |
* ModelEvaluator methods to the supplied underlying ModelEvaluator except |
| 27 |
* those overloaded here. |
| 28 |
*/ |
| 29 |
template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal=LocalOrdinal, typename Node=Tpetra::KokkosClassic::DefaultNode::DefaultNodeType> |
| 30 |
class TpetraExplicitAdjointModelEvaluator : |
| 31 |
public ModelEvaluatorDelegatorBase<Scalar>{ |
| 32 |
public: |
| 33 |
|
| 34 |
//! Constructor |
| 35 |
TpetraExplicitAdjointModelEvaluator( |
| 36 |
const RCP<const ModelEvaluator<Scalar> >& model) : |
| 37 |
ModelEvaluatorDelegatorBase<Scalar>(model) {} |
| 38 |
|
| 39 |
//! Constructor |
| 40 |
TpetraExplicitAdjointModelEvaluator( |
| 41 |
const RCP<ModelEvaluator<Scalar> >& model) : |
| 42 |
ModelEvaluatorDelegatorBase<Scalar>(model) {} |
| 43 |
|
| 44 |
//! Destructor |
| 45 |
virtual ~TpetraExplicitAdjointModelEvaluator() = default; |
| 46 |
|
| 47 |
ModelEvaluatorBase::InArgs<Scalar> createInArgs() const |
| 48 |
{ |
| 49 |
// This ME should use the same InArgs as the underlying model. However |
| 50 |
// we can't just use it's InArgs directly because the description won't |
| 51 |
// match (which is checked in debug builds). Instead create a new |
| 52 |
// InArgsSetup initialized by the underlying model's createInArgs() and |
| 53 |
// set the description appropriately. |
| 54 |
ModelEvaluatorBase::InArgsSetup<Scalar> inArgs = |
| 55 |
this->getUnderlyingModel()->createInArgs(); |
| 56 |
inArgs.setModelEvalDescription(this->description()); |
| 57 |
return inArgs; |
| 58 |
} |
| 59 |
|
| 60 |
RCP<LinearOpBase<Scalar> > create_W_op() const { |
| 61 |
typedef TpetraLinearOp<Scalar,LocalOrdinal,GlobalOrdinal,Node> TLO; |
| 62 |
typedef Tpetra::Operator<Scalar,LocalOrdinal,GlobalOrdinal,Node> TO; |
| 63 |
typedef Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> TCM; |
| 64 |
typedef Tpetra::RowMatrixTransposer<Scalar,LocalOrdinal,GlobalOrdinal,Node> TRMT; |
| 65 |
if (thyra_fwd_op == Teuchos::null) |
| 66 |
thyra_fwd_op = this->getUnderlyingModel()->create_W_op(); |
| 67 |
RCP<TLO> thyra_tpetra_fwd_op = |
| 68 |
Teuchos::rcp_dynamic_cast<TLO>(thyra_fwd_op,true); |
| 69 |
RCP<TO> tpetra_fwd_op = thyra_tpetra_fwd_op->getTpetraOperator(); |
| 70 |
RCP<TCM> tpetra_fwd_mat = |
| 71 |
Teuchos::rcp_dynamic_cast<TCM>(tpetra_fwd_op,true); |
| 72 |
TRMT transposer(tpetra_fwd_mat); |
| 73 |
RCP<TCM> tpetra_trans_mat = transposer.createTranspose(); |
| 74 |
return tpetraLinearOp(thyra_op->range(), thyra_op->domain(), |
| 75 |
tpetra_trans_mat); |
| 76 |
} |
| 77 |
|
| 78 |
private: |
| 79 |
|
| 80 |
mutable RCP< Thyra::LinearOpBase<Scalar> > thyra_fwd_op; |
| 81 |
|
| 82 |
ModelEvaluatorBase::OutArgs<Scalar> createOutArgsImpl() const |
| 83 |
{ |
| 84 |
typedef ModelEvaluatorBase MEB; |
| 85 |
MEB::OutArgs<Scalar> model_outArgs = |
| 86 |
this->getUnderlyingModel()->createOutArgs(); |
| 87 |
MEB::OutArgsSetup<Scalar> outArgs; |
| 88 |
outArgs.setModelEvalDescription(this->description()); |
| 89 |
outArgs.setSupports(MEB::OUT_ARG_f); //All models must support f, apparently |
| 90 |
outArgs.setSupports(MEB::OUT_ARG_W_op); |
| 91 |
TEUCHOS_ASSERT(model_outArgs.supports(MEB::OUT_ARG_W_op)); |
| 92 |
return outArgs; |
| 93 |
} |
| 94 |
|
| 95 |
void evalModelImpl( |
| 96 |
const ModelEvaluatorBase::InArgs<Scalar> &inArgs, |
| 97 |
const ModelEvaluatorBase::OutArgs<Scalar> &outArgs) const |
| 98 |
{ |
| 99 |
typedef ModelEvaluatorBase MEB; |
| 100 |
typedef TpetraLinearOp<Scalar,LocalOrdinal,GlobalOrdinal,Node> TLO; |
| 101 |
typedef Tpetra::Operator<Scalar,LocalOrdinal,GlobalOrdinal,Node> TO; |
| 102 |
typedef Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> TCM; |
| 103 |
typedef Tpetra::RowMatrixTransposer<Scalar,LocalOrdinal,GlobalOrdinal,Node> TRMT; |
| 104 |
|
| 105 |
MEB::OutArgs<Scalar> model_outArgs = |
| 106 |
this->getUnderlyingModel()->createOutArgs(); |
| 107 |
|
| 108 |
if (model_outArgs.supports(MEB::OUT_ARG_W_op) && |
| 109 |
outArgs.get_W_op() != Teuchos::null) { |
| 110 |
// Compute underlying W_op. |
| 111 |
if (thyra_fwd_op == Teuchos::null) |
| 112 |
thyra_fwd_op = this->getUnderlyingModel()->create_W_op(); |
| 113 |
model_outArgs->set_W_op(thyra_fwd_op); |
| 114 |
this->getUnderlyingModel()->evalModel(inArgs, model_outArgs); |
| 115 |
|
| 116 |
// Transpose W_op |
| 117 |
// Unfortunately, because of the horrendous design of the Tpetra |
| 118 |
// RowMatrixTransposer, this creates a new transposed matrix each time |
| 119 |
RCP<TLO> thyra_tpetra_fwd_op = |
| 120 |
Teuchos::rcp_dynamic_cast<TLO>(thyra_fwd_op,true); |
| 121 |
RCP<TO> tpetra_fwd_op = thyra_tpetra_fwd_op->getTpetraOperator(); |
| 122 |
RCP<TCM> tpetra_fwd_mat = |
| 123 |
Teuchos::rcp_dynamic_cast<TCM>(tpetra_fwd_op,true); |
| 124 |
TRMT transposer(tpetra_fwd_mat); |
| 125 |
RCP<TCM> tpetra_trans_mat = transposer.createTranspose(); |
| 126 |
|
| 127 |
// Copy transposed matrix into our outArg |
| 128 |
RCP<LOB> thyra_adj_op = outArgs.get_W_op(); |
| 129 |
RCP<TLO> thyra_tpetra_adj_op = |
| 130 |
Teuchos::rcp_dynamic_cast<TLO>(thyra_adj_op,true); |
| 131 |
RCP<TO> tpetra_adj_op = thyra_tpetra_adj_op->getTpetraOperator(); |
| 132 |
RCP<TCM> tpetra_adj_mat = |
| 133 |
Teuchos::rcp_dynamic_cast<TCM>(tpetra_adj_op,true); |
| 134 |
*tpetra_adj_mat = *tpetra_trans_mat; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
}; |
| 139 |
|
| 140 |
template <typename Scalar> |
| 141 |
RCP<TpetraExplicitAdjointModelEvaluator<Scalar> > |
| 142 |
tpetraExplicitAdjointModelEvaluator( |
| 143 |
const RCP<const ModelEvaluator<Scalar> >& model) |
| 144 |
{ |
| 145 |
return Teuchos::rcp(new TpetraExplicitAdjointModelEvaluator<Scalar>(model)); |
| 146 |
} |
| 147 |
|
| 148 |
template <typename Scalar> |
| 149 |
RCP<TpetraExplicitAdjointModelEvaluator<Scalar> > |
| 150 |
tpetraExplicitAdjointModelEvaluator( |
| 151 |
const RCP<ModelEvaluator<Scalar> >& model) |
| 152 |
{ |
| 153 |
return Teuchos::rcp(new TpetraExplicitAdjointModelEvaluator<Scalar>(model)); |
| 154 |
} |
| 155 |
|
| 156 |
} // namespace Thyra |
| 157 |
|
| 158 |
#endif |