| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Rapid Optimization Library (ROL) Package |
| 4 |
// |
| 5 |
// Copyright 2014 NTESS and the ROL contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef ROL_TPETRAMULTIVECTOR_HPP |
| 11 |
#define ROL_TPETRAMULTIVECTOR_HPP |
| 12 |
|
| 13 |
/** \class ROL::TpetraMultiVector |
| 14 |
\brief Implements the ROL::Vector interface for a Tpetra_MultiVector. |
| 15 |
\author Created by Greg von Winckel |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "ROL_Vector.hpp" |
| 21 |
#include "Tpetra_MultiVector.hpp" |
| 22 |
#include "Tpetra_Vector.hpp" |
| 23 |
#include "MatrixMarket_Tpetra.hpp" |
| 24 |
|
| 25 |
namespace ROL { |
| 26 |
|
| 27 |
|
| 28 |
namespace TPMultiVector { |
| 29 |
|
| 30 |
|
| 31 |
// Locally define a Kokkos wrapper functor for UnaryFunction |
| 32 |
template <class Real, |
| 33 |
class LO=Tpetra::Map<>::local_ordinal_type, |
| 34 |
class GO=Tpetra::Map<>::global_ordinal_type, |
| 35 |
class Node=Tpetra::Map<>::node_type > |
| 36 |
struct unaryFunc { |
| 37 |
typedef typename Tpetra::MultiVector<Real,LO,GO,Node>::dual_view_type::t_dev ViewType; |
| 38 |
typedef typename ViewType::execution_space execution_space; |
| 39 |
ViewType X_; |
| 40 |
const Elementwise::UnaryFunction<Real>* const f_; |
| 41 |
|
| 42 |
unaryFunc(ViewType& X, const Elementwise::UnaryFunction<Real>* const f) |
| 43 |
: X_(X), f_(f) {} |
| 44 |
|
| 45 |
KOKKOS_INLINE_FUNCTION |
| 46 |
void operator()(const int i) const { |
| 47 |
const int M = X_.extent(1); |
| 48 |
for(int j=0;j<M;++j) { |
| 49 |
X_(i,j) = f_->apply(X_(i,j)); |
| 50 |
} |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
// Locally define a Kokkos wrapper functor for BinaryFunction |
| 55 |
template <class Real, |
| 56 |
class LO=Tpetra::Map<>::local_ordinal_type, |
| 57 |
class GO=Tpetra::Map<>::global_ordinal_type, |
| 58 |
class Node=Tpetra::Map<>::node_type > |
| 59 |
struct binaryFunc { |
| 60 |
typedef typename Tpetra::MultiVector<Real,LO,GO,Node>::dual_view_type::t_dev ViewType; |
| 61 |
typedef typename Tpetra::MultiVector<const Real,LO,GO,Node>::dual_view_type::t_dev ConstViewType; |
| 62 |
typedef typename ViewType::execution_space execution_space; |
| 63 |
ViewType X_; |
| 64 |
ConstViewType Y_; |
| 65 |
const Elementwise::BinaryFunction<Real>* const f_; |
| 66 |
|
| 67 |
binaryFunc(ViewType& X, const ConstViewType& Y, const Elementwise::BinaryFunction<Real>* const f) |
| 68 |
: X_(X), Y_(Y), f_(f) {} |
| 69 |
|
| 70 |
KOKKOS_INLINE_FUNCTION |
| 71 |
void operator()(const int i) const { |
| 72 |
const int M = X_.extent(1); |
| 73 |
for(int j=0;j<M;++j) { |
| 74 |
X_(i,j) = f_->apply(X_(i,j),Y_(i,j)); |
| 75 |
} |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
// Locally define a Kokkos wrapper functor for ReductionOp |
| 80 |
template <class Real, |
| 81 |
class LO=Tpetra::Map<>::local_ordinal_type, |
| 82 |
class GO=Tpetra::Map<>::global_ordinal_type, |
| 83 |
class Node=Tpetra::Map<>::node_type > |
| 84 |
struct reduceFunc { |
| 85 |
typedef typename Tpetra::MultiVector<const Real,LO,GO,Node>::dual_view_type::t_dev ConstViewType; |
| 86 |
typedef typename ConstViewType::execution_space execution_space; |
| 87 |
ConstViewType X_; |
| 88 |
const Elementwise::ReductionOp<Real>* const r_; |
| 89 |
|
| 90 |
reduceFunc(const ConstViewType& X, const Elementwise::ReductionOp<Real>* const r) |
| 91 |
: X_(X), r_(r) {} |
| 92 |
|
| 93 |
KOKKOS_INLINE_FUNCTION |
| 94 |
void operator()(const int i, Real &rval) const { |
| 95 |
const int M = X_.extent(1); |
| 96 |
|
| 97 |
for(int j=0;j<M;++j) { |
| 98 |
r_->reduce(X_(i,j),rval); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
KOKKOS_INLINE_FUNCTION |
| 103 |
void init(Real &rval) const { |
| 104 |
rval = r_->initialValue(); |
| 105 |
} |
| 106 |
|
| 107 |
KOKKOS_INLINE_FUNCTION |
| 108 |
void join(Real &globalVal, const Real &localVal) const { |
| 109 |
r_->reduce(localVal,globalVal); |
| 110 |
} |
| 111 |
|
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
} // end namespace TPMultiVector |
| 116 |
|
| 117 |
|
| 118 |
// Template on the Real/Scalar type, Local Ordinal, Global Ordinal, and Node |
| 119 |
template <class Real, |
| 120 |
class LO=Tpetra::Map<>::local_ordinal_type, |
| 121 |
class GO=Tpetra::Map<>::global_ordinal_type, |
| 122 |
class Node=Tpetra::Map<>::node_type > |
| 123 |
class TpetraMultiVector : public Vector<Real> { |
| 124 |
private: |
| 125 |
const Ptr<Tpetra::MultiVector<Real,LO,GO,Node> > tpetra_vec_; |
| 126 |
const Ptr<const Tpetra::Map<LO,GO,Node> > map_; |
| 127 |
const Ptr<const Teuchos::Comm<int> > comm_; |
| 128 |
|
| 129 |
protected: |
| 130 |
const Ptr<const Tpetra::Map<LO,GO,Node> > getMap(void) const { |
| 131 |
return map_; |
| 132 |
} |
| 133 |
|
| 134 |
public: |
| 135 |
virtual ~TpetraMultiVector() {} |
| 136 |
|
| 137 |
TpetraMultiVector(const Ptr<Tpetra::MultiVector<Real,LO,GO,Node> > &tpetra_vec) |
| 138 |
: tpetra_vec_(tpetra_vec), map_(tpetra_vec_->getMap()), comm_(map_->getComm()) {} |
| 139 |
|
| 140 |
/** \brief Assign \f$y \leftarrow x \f$ where \f$y = \mbox{*this}\f$. |
| 141 |
*/ |
| 142 |
void set(const Vector<Real> &x) { |
| 143 |
ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(), |
| 144 |
std::invalid_argument, |
| 145 |
"Error: Vectors must have the same dimension." ); |
| 146 |
const TpetraMultiVector &ex = dynamic_cast<const TpetraMultiVector&>(x); |
| 147 |
tpetra_vec_->assign(*ex.getVector()); |
| 148 |
} |
| 149 |
|
| 150 |
/** \brief Compute \f$y \leftarrow x + y\f$ where \f$y = \mbox{*this}\f$. |
| 151 |
*/ |
| 152 |
void plus(const Vector<Real> &x) { |
| 153 |
ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(), |
| 154 |
std::invalid_argument, |
| 155 |
"Error: Vectors must have the same dimension." ); |
| 156 |
Real one(1); |
| 157 |
const TpetraMultiVector &ex = dynamic_cast<const TpetraMultiVector&>(x); |
| 158 |
tpetra_vec_->update(one,*ex.getVector(),one); |
| 159 |
} |
| 160 |
|
| 161 |
void axpy( const Real alpha, const Vector<Real> &x ) { |
| 162 |
ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(), |
| 163 |
std::invalid_argument, |
| 164 |
"Error: Vectors must have the same dimension." ); |
| 165 |
Real one(1); |
| 166 |
const TpetraMultiVector &ex = dynamic_cast<const TpetraMultiVector&>(x); |
| 167 |
tpetra_vec_->update(alpha,*ex.getVector(),one); |
| 168 |
} |
| 169 |
|
| 170 |
/** \brief Compute \f$y \leftarrow \alpha y\f$ where \f$y = \mbox{*this}\f$. |
| 171 |
*/ |
| 172 |
void scale( const Real alpha ) { |
| 173 |
tpetra_vec_->scale( alpha ); |
| 174 |
} |
| 175 |
|
| 176 |
/** \brief Returns \f$ \langle y,x \rangle \f$ where \f$y = \mbox{*this}\f$. |
| 177 |
*/ |
| 178 |
virtual Real dot( const Vector<Real> &x ) const { |
| 179 |
ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(), |
| 180 |
std::invalid_argument, |
| 181 |
"Error: Vectors must have the same dimension." ); |
| 182 |
const TpetraMultiVector &ex = dynamic_cast<const TpetraMultiVector&>(x); |
| 183 |
size_t n = tpetra_vec_->getNumVectors(); |
| 184 |
// Perform Euclidean dot between *this and x for each vector |
| 185 |
Teuchos::Array<Real> val(n,0); |
| 186 |
tpetra_vec_->dot( *ex.getVector(), val.view(0,n) ); |
| 187 |
// Combine dots for each vector to get a scalar |
| 188 |
Real xy(0); |
| 189 |
for (size_t i = 0; i < n; ++i) { |
| 190 |
xy += val[i]; |
| 191 |
} |
| 192 |
return xy; |
| 193 |
} |
| 194 |
|
| 195 |
/** \brief Returns \f$ \| y \| \f$ where \f$y = \mbox{*this}\f$. |
| 196 |
*/ |
| 197 |
Real norm() const { |
| 198 |
return std::sqrt(dot(*this)); |
| 199 |
} |
| 200 |
|
| 201 |
/** \brief Clone to make a new (uninitialized) vector. |
| 202 |
*/ |
| 203 |
virtual Ptr<Vector<Real> > clone() const { |
| 204 |
size_t n = tpetra_vec_->getNumVectors(); |
| 205 |
return makePtr<TpetraMultiVector>( |
| 206 |
makePtr<Tpetra::MultiVector<Real,LO,GO,Node>>(map_,n)); |
| 207 |
} |
| 208 |
|
| 209 |
/** \brief Set to zero vector. |
| 210 |
*/ |
| 211 |
void zero() { |
| 212 |
Real zero(0); |
| 213 |
tpetra_vec_->putScalar(zero); |
| 214 |
} |
| 215 |
|
| 216 |
void setScalar(const Real C) { |
| 217 |
tpetra_vec_->putScalar(static_cast<double>(C)); |
| 218 |
} |
| 219 |
|
| 220 |
void randomize(const Real l=0.0, const Real u = 1.0) { |
| 221 |
tpetra_vec_->randomize(static_cast<double>(l),static_cast<double>(u)); |
| 222 |
} |
| 223 |
|
| 224 |
Ptr<const Tpetra::MultiVector<Real,LO,GO,Node> > getVector() const { |
| 225 |
return tpetra_vec_; |
| 226 |
} |
| 227 |
|
| 228 |
Ptr<Tpetra::MultiVector<Real,LO,GO,Node> > getVector() { |
| 229 |
return tpetra_vec_; |
| 230 |
} |
| 231 |
|
| 232 |
Ptr<Vector<Real> > basis( const int i ) const { |
| 233 |
ROL_TEST_FOR_EXCEPTION( i >= dimension() || i<0, |
| 234 |
std::invalid_argument, |
| 235 |
"Error: Basis index must be between 0 and vector dimension." ); |
| 236 |
Ptr<Vector<Real> > e = clone(); |
| 237 |
const size_t n = tpetra_vec_->getNumVectors(); |
| 238 |
if ( (map_ != nullPtr) && map_->isNodeGlobalElement(static_cast<GO>(i))) { |
| 239 |
for (size_t j = 0; j < n; ++j) { |
| 240 |
(staticPtrCast<TpetraMultiVector<Real,LO,GO,Node> >(e)->getVector())->replaceGlobalValue(i, j, ScalarTraits<Real>::one()); |
| 241 |
} |
| 242 |
} |
| 243 |
return e; |
| 244 |
} |
| 245 |
|
| 246 |
int dimension() const { |
| 247 |
int nVecs = static_cast<int>(tpetra_vec_->getNumVectors()); |
| 248 |
int dim = static_cast<int>(tpetra_vec_->getGlobalLength()); |
| 249 |
return nVecs*dim; |
| 250 |
} |
| 251 |
|
| 252 |
/*****************************************************************************/ |
| 253 |
/************** ELEMENTWISE DEFINITIONS AND IMPLEMENTATIONS ******************/ |
| 254 |
/*****************************************************************************/ |
| 255 |
private: |
| 256 |
typedef typename Tpetra::MultiVector<Real,LO,GO,Node>::dual_view_type::t_dev ViewType; |
| 257 |
typedef typename Tpetra::MultiVector<const Real,LO,GO,Node>::dual_view_type::t_dev ConstViewType; |
| 258 |
public: |
| 259 |
void applyUnary( const Elementwise::UnaryFunction<Real> &f ) { |
| 260 |
ViewType v_lcl = tpetra_vec_->getLocalViewDevice(Tpetra::Access::ReadWrite); |
| 261 |
|
| 262 |
int lclDim = tpetra_vec_->getLocalLength(); |
| 263 |
TPMultiVector::unaryFunc<Real,LO,GO,Node> func(v_lcl, &f); |
| 264 |
|
| 265 |
Kokkos::parallel_for(lclDim,func); |
| 266 |
} |
| 267 |
|
| 268 |
void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) { |
| 269 |
|
| 270 |
ROL_TEST_FOR_EXCEPTION( dimension() != x.dimension(), |
| 271 |
std::invalid_argument, |
| 272 |
"Error: Vectors must have the same dimension." ); |
| 273 |
|
| 274 |
const TpetraMultiVector &ex = dynamic_cast<const TpetraMultiVector&>(x); |
| 275 |
Ptr<const Tpetra::MultiVector<Real,LO,GO,Node> > xp = ex.getVector(); |
| 276 |
|
| 277 |
ViewType v_lcl = tpetra_vec_->getLocalViewDevice(Tpetra::Access::ReadWrite); |
| 278 |
ConstViewType x_lcl = xp->getLocalViewDevice(Tpetra::Access::ReadOnly); |
| 279 |
|
| 280 |
int lclDim = tpetra_vec_->getLocalLength(); |
| 281 |
|
| 282 |
TPMultiVector::binaryFunc<Real,LO,GO,Node> func(v_lcl,x_lcl,&f); |
| 283 |
|
| 284 |
Kokkos::parallel_for(lclDim,func); |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
Real reduce( const Elementwise::ReductionOp<Real> &r ) const { |
| 289 |
ConstViewType v_lcl = tpetra_vec_->getLocalViewDevice(Tpetra::Access::ReadOnly); |
| 290 |
|
| 291 |
int lclDim = tpetra_vec_->getLocalLength(); |
| 292 |
TPMultiVector::reduceFunc<Real,LO,GO,Node> func(v_lcl, &r); |
| 293 |
|
| 294 |
Real lclValue; |
| 295 |
|
| 296 |
// Reduce for this MPI process |
| 297 |
Kokkos::parallel_reduce(lclDim,func,lclValue); |
| 298 |
|
| 299 |
Real gblValue; |
| 300 |
|
| 301 |
auto reductionType = static_cast<Teuchos::EReductionType>(r.reductionType()); |
| 302 |
|
| 303 |
// Reduce over MPI processes |
| 304 |
Teuchos::reduceAll<int,Real>(*comm_,reductionType,lclValue,Teuchos::outArg(gblValue)); |
| 305 |
|
| 306 |
return gblValue; |
| 307 |
} |
| 308 |
|
| 309 |
void print( std::ostream &outStream ) const { |
| 310 |
//tpetra_vec_->print(outStream); |
| 311 |
Tpetra::MatrixMarket::Writer< Tpetra::CrsMatrix<>> vecWriter; |
| 312 |
vecWriter.writeDense(outStream, *tpetra_vec_); |
| 313 |
vecWriter.writeMap(outStream, *tpetra_vec_->getMap()); |
| 314 |
} |
| 315 |
|
| 316 |
}; // class TpetraMultiVector |
| 317 |
|
| 318 |
|
| 319 |
} // namespace ROL |
| 320 |
|
| 321 |
#endif |