| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// NOX: An Object-Oriented Nonlinear Solver Package |
| 4 |
// |
| 5 |
// Copyright 2002 NTESS and the NOX contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
#ifndef NOX_TPETRA_1DFEM_FUNCTORS_HPP |
| 10 |
#define NOX_TPETRA_1DFEM_FUNCTORS_HPP |
| 11 |
|
| 12 |
#include "Kokkos_Core.hpp" |
| 13 |
#include "Kokkos_ArithTraits.hpp" |
| 14 |
#include "Tpetra_Details_OrdinalTraits.hpp" |
| 15 |
|
| 16 |
template <class ViewType, class GO> |
| 17 |
struct IntegralOperatorReductionFunctor |
| 18 |
{ |
| 19 |
typedef typename ViewType::traits::non_const_value_type Scalar; |
| 20 |
|
| 21 |
const ViewType u_; |
| 22 |
const GO myMinGID_; |
| 23 |
const GO globalRow_; |
| 24 |
|
| 25 |
KOKKOS_INLINE_FUNCTION |
| 26 |
IntegralOperatorReductionFunctor(const ViewType& u, |
| 27 |
const GO& myMinGID, |
| 28 |
const std::size_t& globalRow) : |
| 29 |
u_(u), |
| 30 |
myMinGID_(myMinGID), |
| 31 |
globalRow_(globalRow) |
| 32 |
{} |
| 33 |
|
| 34 |
KOKKOS_INLINE_FUNCTION |
| 35 |
void operator() (const std::size_t i, Scalar& localSum) const |
| 36 |
{ |
| 37 |
GO myGlobalRow = myMinGID_ + i; |
| 38 |
localSum += (globalRow_ + 0.5)*u_(i, 0)/static_cast<Scalar>(globalRow_ + myGlobalRow + 1); |
| 39 |
} |
| 40 |
|
| 41 |
}; |
| 42 |
|
| 43 |
//================================================================== |
| 44 |
|
| 45 |
template<class TpetraVectorType> |
| 46 |
struct IntegralOperatorFunctor |
| 47 |
{ |
| 48 |
typedef typename TpetraVectorType::impl_scalar_type Scalar; |
| 49 |
typedef typename TpetraVectorType::local_ordinal_type LO; |
| 50 |
typedef typename TpetraVectorType::global_ordinal_type GO; |
| 51 |
typedef typename TpetraVectorType::dual_view_type::t_dev ViewType; |
| 52 |
typedef typename TpetraVectorType::dual_view_type::t_dev::const_type ConstViewType; |
| 53 |
typedef typename TpetraVectorType::execution_space ExecSpace; |
| 54 |
typedef Kokkos::TeamPolicy<ExecSpace> TeamPolicy; |
| 55 |
typedef typename TeamPolicy::member_type MemberType; |
| 56 |
|
| 57 |
ConstViewType uView_; |
| 58 |
ViewType resultView_; |
| 59 |
const GO myMinGID_; |
| 60 |
const GO procMinGID_; |
| 61 |
const std::size_t myLength_; |
| 62 |
|
| 63 |
IntegralOperatorFunctor(const TpetraVectorType& u, |
| 64 |
TpetraVectorType& result, |
| 65 |
const GO& myMinGID, |
| 66 |
const GO& procMinGID) : |
| 67 |
uView_(u.template getLocalView<ExecSpace>(Tpetra::Access::ReadOnly)), |
| 68 |
resultView_(result.template getLocalView<ExecSpace>(Tpetra::Access::ReadWrite)), |
| 69 |
myMinGID_(myMinGID), |
| 70 |
procMinGID_(procMinGID), |
| 71 |
myLength_(u.getLocalLength()) |
| 72 |
{} |
| 73 |
|
| 74 |
KOKKOS_INLINE_FUNCTION |
| 75 |
void operator() (const MemberType& member) const |
| 76 |
{ |
| 77 |
int localRow = member.league_rank(); |
| 78 |
const GO globalRow = procMinGID_ + localRow; |
| 79 |
|
| 80 |
Scalar localSum = 0; |
| 81 |
IntegralOperatorReductionFunctor<ConstViewType, GO> functor(uView_, myMinGID_, globalRow); |
| 82 |
Kokkos::parallel_reduce(Kokkos::TeamThreadRange(member, myLength_), functor, localSum); |
| 83 |
|
| 84 |
if (member.team_rank() == 0) |
| 85 |
resultView_(localRow, 0) = localSum; |
| 86 |
} |
| 87 |
|
| 88 |
}; |
| 89 |
|
| 90 |
//================================================================== |
| 91 |
|
| 92 |
template <class TpetraVectorType> |
| 93 |
struct ResidualEvaluatorFunctor |
| 94 |
{ |
| 95 |
typedef typename TpetraVectorType::impl_scalar_type Scalar; |
| 96 |
typedef typename TpetraVectorType::execution_space ExecSpace; |
| 97 |
typedef typename TpetraVectorType::dual_view_type::t_dev ViewType; |
| 98 |
typedef typename TpetraVectorType::dual_view_type::t_dev::const_type ConstViewType; |
| 99 |
|
| 100 |
ViewType fView_; |
| 101 |
ConstViewType uView_; |
| 102 |
ConstViewType integralOpView_; |
| 103 |
|
| 104 |
ResidualEvaluatorFunctor(TpetraVectorType& f, |
| 105 |
const TpetraVectorType& u, |
| 106 |
const TpetraVectorType& integralOp) : |
| 107 |
fView_(f.template getLocalView<ExecSpace>(Tpetra::Access::ReadWrite)), |
| 108 |
uView_(u.template getLocalView<ExecSpace>(Tpetra::Access::ReadOnly)), |
| 109 |
integralOpView_(integralOp.template getLocalView<ExecSpace>(Tpetra::Access::ReadOnly)) |
| 110 |
{} |
| 111 |
|
| 112 |
KOKKOS_INLINE_FUNCTION |
| 113 |
void operator() (const std::size_t row) const |
| 114 |
{ |
| 115 |
Scalar one = Kokkos::ArithTraits<Scalar>::one(); |
| 116 |
fView_(row, 0) = one/(one - integralOpView_(row,0)) - uView_(row, 0); |
| 117 |
} |
| 118 |
|
| 119 |
}; |
| 120 |
|
| 121 |
//================================================================== |
| 122 |
|
| 123 |
template <class TpetraVectorType> |
| 124 |
struct JacobianEvaluatorFunctor |
| 125 |
{ |
| 126 |
typedef typename TpetraVectorType::impl_scalar_type Scalar; |
| 127 |
typedef typename TpetraVectorType::dual_view_type::t_dev ViewType; |
| 128 |
typedef typename TpetraVectorType::dual_view_type::t_dev::const_type ConstViewType; |
| 129 |
typedef typename TpetraVectorType::execution_space ExecSpace; |
| 130 |
|
| 131 |
ViewType yView_; |
| 132 |
ConstViewType xView_; |
| 133 |
ConstViewType integralOpView_; |
| 134 |
ConstViewType integralOpXView_; |
| 135 |
const Scalar alpha_; |
| 136 |
const Scalar beta_; |
| 137 |
const Scalar omega_; |
| 138 |
const Tpetra::global_size_t globalLength_; |
| 139 |
|
| 140 |
JacobianEvaluatorFunctor(TpetraVectorType& y, |
| 141 |
const TpetraVectorType& x, |
| 142 |
const TpetraVectorType& integralOp, |
| 143 |
const TpetraVectorType& integralOpX, |
| 144 |
const Scalar& alpha, |
| 145 |
const Scalar& beta, |
| 146 |
const Scalar& omega) : |
| 147 |
yView_(y.template getLocalView<ExecSpace>(Tpetra::Access::ReadWrite)), |
| 148 |
xView_(x.template getLocalView<ExecSpace>(Tpetra::Access::ReadOnly)), |
| 149 |
integralOpView_(integralOp.template getLocalView<ExecSpace>(Tpetra::Access::ReadOnly)), |
| 150 |
integralOpXView_(integralOpX.template getLocalView<ExecSpace>(Tpetra::Access::ReadOnly)), |
| 151 |
alpha_(alpha), |
| 152 |
beta_(beta), |
| 153 |
omega_(omega), |
| 154 |
globalLength_(x.getGlobalLength()) |
| 155 |
{} |
| 156 |
|
| 157 |
KOKKOS_INLINE_FUNCTION |
| 158 |
void operator() (const std::size_t row) const |
| 159 |
{ |
| 160 |
Scalar zero = Kokkos::ArithTraits<Scalar>::zero(); |
| 161 |
Scalar one = Kokkos::ArithTraits<Scalar>::one(); |
| 162 |
Scalar value = omega_*integralOpXView_(row,0); |
| 163 |
value /= (2*globalLength_*(one-integralOpView_(row,0))*(one-integralOpView_(row,0))); |
| 164 |
if (beta_ == zero) |
| 165 |
yView_(row,0) = alpha_*(value - xView_(row,0)); |
| 166 |
else |
| 167 |
yView_(row,0) = alpha_*(value - xView_(row,0)) + beta_*yView_(row,0); |
| 168 |
} |
| 169 |
|
| 170 |
}; |
| 171 |
|
| 172 |
//================================================================== |
| 173 |
|
| 174 |
template <class TpetraVectorType, class TpetraMatrixType> |
| 175 |
struct PreconditionerEvaluatorFunctor |
| 176 |
{ |
| 177 |
typedef typename TpetraVectorType::impl_scalar_type Scalar; |
| 178 |
typedef typename TpetraVectorType::local_ordinal_type LO; |
| 179 |
typedef typename TpetraVectorType::global_ordinal_type GO; |
| 180 |
typedef typename TpetraVectorType::execution_space ExecSpace; |
| 181 |
typedef typename TpetraVectorType::dual_view_type::t_dev::const_type ConstViewType; |
| 182 |
typedef typename TpetraMatrixType::local_matrix_device_type LocalMatrixType; |
| 183 |
|
| 184 |
const LocalMatrixType precLocal_; |
| 185 |
const ConstViewType integralOpView_; |
| 186 |
const Scalar omega_; |
| 187 |
const GO numGlobalElements_; |
| 188 |
|
| 189 |
PreconditionerEvaluatorFunctor(const TpetraMatrixType& prec, |
| 190 |
const TpetraVectorType& integralOp, |
| 191 |
const Scalar& omega, |
| 192 |
const GO& numGlobalElements) : |
| 193 |
precLocal_(prec.getLocalMatrixDevice()), |
| 194 |
integralOpView_(integralOp.template getLocalView<ExecSpace>(Tpetra::Access::ReadOnly)), |
| 195 |
omega_(omega), |
| 196 |
numGlobalElements_(numGlobalElements) |
| 197 |
{} |
| 198 |
|
| 199 |
KOKKOS_INLINE_FUNCTION |
| 200 |
void operator() (const LO localRow) const |
| 201 |
{ |
| 202 |
Scalar one = Kokkos::ArithTraits<Scalar>::one(); |
| 203 |
Scalar value = one - integralOpView_(localRow, 0); |
| 204 |
value = omega_/(4*numGlobalElements_*value*value) - one; |
| 205 |
value = 1.0/value; |
| 206 |
precLocal_.replaceValues(localRow, &localRow, 1, &value); |
| 207 |
} |
| 208 |
|
| 209 |
}; |
| 210 |
|
| 211 |
//================================================================== |
| 212 |
|
| 213 |
template <class TpetraGraphType> |
| 214 |
struct GraphSetupFunctor |
| 215 |
{ |
| 216 |
typedef typename TpetraGraphType::local_graph_device_type::row_map_type::non_const_type OffsetsViewType; |
| 217 |
typedef typename TpetraGraphType::local_graph_device_type::entries_type::non_const_type IndicesViewType; |
| 218 |
|
| 219 |
const OffsetsViewType offsetsView_; |
| 220 |
const IndicesViewType indicesView_; |
| 221 |
const std::size_t numLocalElements_; |
| 222 |
|
| 223 |
GraphSetupFunctor(const OffsetsViewType& offsetsView, |
| 224 |
const IndicesViewType& indicesView, |
| 225 |
const std::size_t& numLocalElements) : |
| 226 |
offsetsView_(offsetsView), |
| 227 |
indicesView_(indicesView), |
| 228 |
numLocalElements_(numLocalElements) |
| 229 |
{} |
| 230 |
|
| 231 |
KOKKOS_INLINE_FUNCTION |
| 232 |
void operator() (const std::size_t localRow) const |
| 233 |
{ |
| 234 |
offsetsView_(localRow) = localRow; |
| 235 |
if (localRow < numLocalElements_) |
| 236 |
indicesView_(localRow) = localRow; |
| 237 |
} |
| 238 |
|
| 239 |
}; |
| 240 |
|
| 241 |
#endif |