/usr/include/trilinos/ROL_TpetraMultiVector.hpp is in libtrilinos-rol-dev 12.10.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | // @HEADER
// ************************************************************************
//
// Rapid Optimization Library (ROL) Package
// Copyright (2014) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact lead developers:
// Drew Kouri (dpkouri@sandia.gov) and
// Denis Ridzal (dridzal@sandia.gov)
//
// ************************************************************************
// @HEADER
#ifndef ROL_TPETRAMULTIVECTOR_HPP
#define ROL_TPETRAMULTIVECTOR_HPP
/** \class ROL::TpetraMultiVector
\brief Implements the ROL::Vector interface for a Tpetra_MultiVector.
\author Created by Greg von Winckel
*/
#include "ROL_Vector.hpp"
#include "Tpetra_MultiVector_def.hpp"
namespace ROL {
namespace TPMultiVector {
// Locally define a Kokkos wrapper functor for UnaryFunction
template <class Real,
class LO=Tpetra::Map<>::local_ordinal_type,
class GO=Tpetra::Map<>::global_ordinal_type,
class Node=Tpetra::Map<>::node_type >
struct unaryFunc {
typedef typename Tpetra::MultiVector<Real,LO,GO,Node>::dual_view_type::t_dev ViewType;
typedef typename ViewType::execution_space execution_space;
ViewType X_;
const Elementwise::UnaryFunction<Real>* const f_;
unaryFunc(ViewType& X, const Elementwise::UnaryFunction<Real>* const f)
: X_(X), f_(f) {}
KOKKOS_INLINE_FUNCTION
void operator()(const int i) const {
const int M = X_.dimension_1();
for(int j=0;j<M;++j) {
X_(i,j) = f_->apply(X_(i,j));
}
}
};
// Locally define a Kokkos wrapper functor for BinaryFunction
template <class Real,
class LO=Tpetra::Map<>::local_ordinal_type,
class GO=Tpetra::Map<>::global_ordinal_type,
class Node=Tpetra::Map<>::node_type >
struct binaryFunc {
typedef typename Tpetra::MultiVector<Real,LO,GO,Node>::dual_view_type::t_dev ViewType;
typedef typename ViewType::execution_space execution_space;
ViewType X_;
ViewType Y_;
const Elementwise::BinaryFunction<Real>* const f_;
binaryFunc(ViewType& X, const ViewType& Y, const Elementwise::BinaryFunction<Real>* const f)
: X_(X), Y_(Y), f_(f) {}
KOKKOS_INLINE_FUNCTION
void operator()(const int i) const {
const int M = X_.dimension_1();
for(int j=0;j<M;++j) {
X_(i,j) = f_->apply(X_(i,j),Y_(i,j));
}
}
};
// Locally define a Kokkos wrapper functor for ReductionOp
template <class Real,
class LO=Tpetra::Map<>::local_ordinal_type,
class GO=Tpetra::Map<>::global_ordinal_type,
class Node=Tpetra::Map<>::node_type >
struct reduceFunc {
typedef typename Tpetra::MultiVector<Real,LO,GO,Node>::dual_view_type::t_dev ViewType;
typedef typename ViewType::execution_space execution_space;
ViewType X_;
const Elementwise::ReductionOp<Real>* const r_;
reduceFunc(const ViewType& X, const Elementwise::ReductionOp<Real>* const r)
: X_(X), r_(r) {}
KOKKOS_INLINE_FUNCTION
void operator()(const int i, Real &rval) const {
const int M = X_.dimension_1();
for(int j=0;j<M;++j) {
r_->reduce(X_(i,j),rval);
}
}
KOKKOS_INLINE_FUNCTION
void init(Real &rval) const {
rval = r_->initialValue();
}
KOKKOS_INLINE_FUNCTION
void join(volatile Real &globalVal, const volatile Real &localVal) const {
r_->reduce(localVal,globalVal);
}
};
}
// Template on the Real/Scalar type, Local Ordinal, Global Ordinal, and Node
template <class Real,
class LO=Tpetra::Map<>::local_ordinal_type,
class GO=Tpetra::Map<>::global_ordinal_type,
class Node=Tpetra::Map<>::node_type >
class TpetraMultiVector : public Vector<Real> {
private:
const Teuchos::RCP<Tpetra::MultiVector<Real,LO,GO,Node> > tpetra_vec_;
const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> > map_;
const Teuchos::RCP<const Teuchos::Comm<int> > comm_;
protected:
const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> > getMap(void) const {
return map_;
}
public:
virtual ~TpetraMultiVector() {}
TpetraMultiVector(const Teuchos::RCP<Tpetra::MultiVector<Real,LO,GO,Node> > &tpetra_vec)
: tpetra_vec_(tpetra_vec), map_(tpetra_vec_->getMap()), comm_(map_->getComm()) {}
/** \brief Assign \f$y \leftarrow x \f$ where \f$y = \mbox{*this}\f$.
*/
void set(const Vector<Real> &x) {
TEUCHOS_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
std::invalid_argument,
"Error: Vectors must have the same dimension." );
const TpetraMultiVector &ex = Teuchos::dyn_cast<const TpetraMultiVector>(x);
tpetra_vec_->assign(*ex.getVector());
}
/** \brief Compute \f$y \leftarrow x + y\f$ where \f$y = \mbox{*this}\f$.
*/
void plus(const Vector<Real> &x) {
TEUCHOS_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
std::invalid_argument,
"Error: Vectors must have the same dimension." );
Real one(1);
const TpetraMultiVector &ex = Teuchos::dyn_cast<const TpetraMultiVector>(x);
tpetra_vec_->update(one,*ex.getVector(),one);
}
void axpy( const Real alpha, const Vector<Real> &x ) {
TEUCHOS_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
std::invalid_argument,
"Error: Vectors must have the same dimension." );
Real one(1);
const TpetraMultiVector &ex = Teuchos::dyn_cast<const TpetraMultiVector>(x);
tpetra_vec_->update(alpha,*ex.getVector(),one);
}
/** \brief Compute \f$y \leftarrow \alpha y\f$ where \f$y = \mbox{*this}\f$.
*/
void scale( const Real alpha ) {
tpetra_vec_->scale( alpha );
}
/** \brief Returns \f$ \langle y,x \rangle \f$ where \f$y = \mbox{*this}\f$.
*/
virtual Real dot( const Vector<Real> &x ) const {
TEUCHOS_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
std::invalid_argument,
"Error: Vectors must have the same dimension." );
const TpetraMultiVector &ex = Teuchos::dyn_cast<const TpetraMultiVector>(x);
size_t n = tpetra_vec_->getNumVectors();
// Perform Euclidean dot between *this and x for each vector
Teuchos::Array<Real> val(n,0);
tpetra_vec_->dot( *ex.getVector(), val.view(0,n) );
// Combine dots for each vector to get a scalar
Real xy(0);
for (size_t i = 0; i < n; ++i) {
xy += val[i];
}
return xy;
}
/** \brief Returns \f$ \| y \| \f$ where \f$y = \mbox{*this}\f$.
*/
Real norm() const {
return std::sqrt(dot(*this));
}
/** \brief Clone to make a new (uninitialized) vector.
*/
virtual Teuchos::RCP<Vector<Real> > clone() const {
size_t n = tpetra_vec_->getNumVectors();
return Teuchos::rcp( new TpetraMultiVector(
Teuchos::rcp( new Tpetra::MultiVector<Real,LO,GO,Node>(map_,n))));
}
/** \brief Set to zero vector.
*/
void zero() {
Real zero(0);
tpetra_vec_->putScalar(zero);
}
Teuchos::RCP<const Tpetra::MultiVector<Real,LO,GO,Node> > getVector() const {
return tpetra_vec_;
}
Teuchos::RCP<Tpetra::MultiVector<Real,LO,GO,Node> > getVector() {
return tpetra_vec_;
}
Teuchos::RCP<Vector<Real> > basis( const int i ) const {
TEUCHOS_TEST_FOR_EXCEPTION( i >= dimension() || i<0,
std::invalid_argument,
"Error: Basis index must be between 0 and vector dimension." );
const size_t n = tpetra_vec_->getNumVectors();
Teuchos::RCP<Tpetra::MultiVector<Real,LO,GO,Node> > e
= Teuchos::rcp(new Tpetra::MultiVector<Real,LO,GO,Node>(map_,n));
if (!map_.is_null() && map_->isNodeGlobalElement(static_cast<GO>(i))) {
for (size_t j = 0; j < n; ++j) {
e->replaceGlobalValue (i, j, Teuchos::ScalarTraits<Real>::one());
}
}
return Teuchos::rcp(new TpetraMultiVector(e) );
}
int dimension() const {
int nVecs = static_cast<int>(tpetra_vec_->getNumVectors());
int dim = static_cast<int>(tpetra_vec_->getGlobalLength());
return nVecs*dim;
}
/*****************************************************************************/
/************** ELEMENTWISE DEFINITIONS AND IMPLEMENTATIONS ******************/
/*****************************************************************************/
private:
typedef typename Tpetra::MultiVector<Real,LO,GO,Node>::dual_view_type::t_dev ViewType;
public:
void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
ViewType v_lcl = tpetra_vec_->getDualView().d_view;
int lclDim = tpetra_vec_->getLocalLength();
TPMultiVector::unaryFunc<Real,LO,GO,Node> func(v_lcl, &f);
Kokkos::parallel_for(lclDim,func);
}
void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
TEUCHOS_TEST_FOR_EXCEPTION( dimension() != x.dimension(),
std::invalid_argument,
"Error: Vectors must have the same dimension." );
const TpetraMultiVector &ex = Teuchos::dyn_cast<const TpetraMultiVector>(x);
Teuchos::RCP<const Tpetra::MultiVector<Real,LO,GO,Node> > xp = ex.getVector();
ViewType v_lcl = tpetra_vec_->getDualView().d_view;
ViewType x_lcl = xp->getDualView().d_view;
int lclDim = tpetra_vec_->getLocalLength();
TPMultiVector::binaryFunc<Real,LO,GO,Node> func(v_lcl,x_lcl,&f);
Kokkos::parallel_for(lclDim,func);
}
Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
ViewType v_lcl = tpetra_vec_->getDualView().d_view;
int lclDim = tpetra_vec_->getLocalLength();
TPMultiVector::reduceFunc<Real,LO,GO,Node> func(v_lcl, &r);
Real lclValue;
// Reduce for this MPI process
Kokkos::parallel_reduce(lclDim,func,lclValue);
Real gblValue;
// Reduce over MPI processes
Teuchos::reduceAll<int,Real>(*comm_,r.reductionType(),lclValue,Teuchos::outArg(gblValue));
return gblValue;
}
}; // class TpetraMultiVector
} // namespace ROL
#endif
|