/usr/include/trilinos/BelosTpetraAdapter.hpp is in libtrilinos-dev 10.4.0.dfsg-1ubuntu2.
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 | // @HEADER
// ***********************************************************************
//
// Belos: Block Linear Solvers Package
// Copyright (2004) 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.
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
// @HEADER
#ifndef BELOS_TPETRA_ADAPTER_HPP
#define BELOS_TPETRA_ADAPTER_HPP
#include <Kokkos_NodeTrace.hpp>
/*! \file BelosTpetraAdapter.hpp
\brief Provides several interfaces between Belos virtual classes and Tpetra concrete classes.
*/
// TODO: the assumption is made that the solver, multivector and operator are templated on the same scalar. this will need to be modified.
#include <Tpetra_MultiVector.hpp>
#include <Tpetra_Operator.hpp>
#include <Teuchos_TestForException.hpp>
#include <Teuchos_ScalarTraits.hpp>
#include <Teuchos_Array.hpp>
#include <Teuchos_DefaultSerialComm.hpp>
#include "BelosConfigDefs.hpp"
#include "BelosTypes.hpp"
#include "BelosMultiVecTraits.hpp"
#include "BelosOperatorTraits.hpp"
#include <Kokkos_NodeAPIConfigDefs.hpp>
namespace Belos {
////////////////////////////////////////////////////////////////////
//
// Implementation of the Belos::MultiVecTraits for Tpetra::MultiVector.
//
////////////////////////////////////////////////////////////////////
/*! \brief Template specialization of Belos::MultiVecTraits class using the Tpetra::MultiVector class.
This interface will ensure that any Tpetra::MultiVector will be accepted by the Belos
templated solvers. */
template<class Scalar, class LO, class GO, class Node>
class MultiVecTraits<Scalar, Tpetra::MultiVector<Scalar,LO,GO,Node> >
{
public:
#ifdef HAVE_BELOS_TPETRA_TIMERS
static Teuchos::RCP<Teuchos::Time> mvTimesMatAddMvTimer_, mvTransMvTimer_;
#endif
static Teuchos::RCP<Tpetra::MultiVector<Scalar,LO,GO,Node> > Clone( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, const int numvecs )
{
return Teuchos::rcp( new Tpetra::MultiVector<Scalar,LO,GO,Node>(mv.getMap(),numvecs));
}
static Teuchos::RCP<Tpetra::MultiVector<Scalar,LO,GO,Node> > CloneCopy( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
{
KOKKOS_NODE_TRACE("MVT::CloneCopy(MV)")
return Teuchos::rcp( new Tpetra::MultiVector<Scalar,LO,GO,Node>( mv ) );
}
static Teuchos::RCP<Tpetra::MultiVector<Scalar,LO,GO,Node> > CloneCopy( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, const std::vector<int>& index )
{
KOKKOS_NODE_TRACE("MVT::CloneCopy(MV,ind)")
TEST_FOR_EXCEPTION(index.size() == 0,std::runtime_error,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneCopy(mv,index): numvecs must be greater than zero.");
#ifdef HAVE_TPETRA_DEBUG
TEST_FOR_EXCEPTION( *std::min_element(index.begin(),index.end()) < 0, std::runtime_error,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneCopy(mv,index): indices must be >= zero.");
TEST_FOR_EXCEPTION( (size_t)*std::max_element(index.begin(),index.end()) >= mv.getNumVectors(), std::runtime_error,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneCopy(mv,index): indices must be < mv.getNumVectors().");
#endif
for (typename std::vector<int>::size_type j=1; j<index.size(); ++j) {
if (index[j] != index[j-1]+1) {
// not contiguous; short circuit
Teuchos::Array<size_t> stinds(index.begin(), index.end());
return mv.subCopy(stinds);
}
}
// contiguous
return mv.subCopy(Teuchos::Range1D(index.front(),index.back()));
}
static Teuchos::RCP<Tpetra::MultiVector<Scalar,LO,GO,Node> > CloneViewNonConst( Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, const std::vector<int>& index )
{
TEST_FOR_EXCEPTION(index.size() == 0,std::invalid_argument,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneView(mv,index): numvecs must be greater than zero.");
#ifdef HAVE_TPETRA_DEBUG
TEST_FOR_EXCEPTION( *std::min_element(index.begin(),index.end()) < 0, std::invalid_argument,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneView(mv,index): indices must be >= zero.");
TEST_FOR_EXCEPTION( (size_t)*std::max_element(index.begin(),index.end()) >= mv.getNumVectors(), std::invalid_argument,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneView(mv,index): indices must be < mv.getNumVectors().");
#endif
for (typename std::vector<int>::size_type j=1; j<index.size(); ++j) {
if (index[j] != index[j-1]+1) {
// not contiguous; short circuit
Teuchos::Array<size_t> stinds(index.begin(), index.end());
return mv.subViewNonConst(stinds);
}
}
// contiguous
return mv.subViewNonConst(Teuchos::Range1D(index.front(),index.back()));
}
static Teuchos::RCP<const Tpetra::MultiVector<Scalar,LO,GO,Node> > CloneView( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, const std::vector<int>& index )
{
TEST_FOR_EXCEPTION(index.size() == 0,std::invalid_argument,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneView(mv,index): numvecs must be greater than zero.");
#ifdef HAVE_TPETRA_DEBUG
TEST_FOR_EXCEPTION( *std::min_element(index.begin(),index.end()) < 0, std::invalid_argument,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneView(mv,index): indices must be >= zero.");
TEST_FOR_EXCEPTION( (size_t)*std::max_element(index.begin(),index.end()) >= mv.getNumVectors(), std::invalid_argument,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneView(mv,index): indices must be < mv.getNumVectors().");
#endif
for (typename std::vector<int>::size_type j=1; j<index.size(); ++j) {
if (index[j] != index[j-1]+1) {
// not contiguous; short circuit
Teuchos::Array<size_t> stinds(index.begin(), index.end());
return mv.subView(stinds);
}
}
// contiguous
return mv.subView(Teuchos::Range1D(index.front(),index.back()));
}
static int GetVecLength( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
{ return mv.getGlobalLength(); }
static int GetNumberVecs( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
{ return mv.getNumVectors(); }
static void MvTimesMatAddMv( Scalar alpha, const Tpetra::MultiVector<Scalar,LO,GO,Node>& A,
const Teuchos::SerialDenseMatrix<int,Scalar>& B,
Scalar beta, Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
{
KOKKOS_NODE_TRACE("MVT::MvTimesMatAddMv()")
#ifdef HAVE_BELOS_TPETRA_TIMERS
Teuchos::TimeMonitor lcltimer(*mvTimesMatAddMvTimer_);
#endif
// create local map
Teuchos::SerialComm<int> scomm;
Tpetra::Map<LO,GO,Node> LocalMap(B.numRows(), 0, Teuchos::rcpFromRef< const Teuchos::Comm<int> >(scomm), Tpetra::LocallyReplicated, A.getMap()->getNode());
// encapsulate Teuchos::SerialDenseMatrix data in ArrayView
Teuchos::ArrayView<const Scalar> Bvalues(B.values(),B.stride()*B.numCols());
// create locally replicated MultiVector with a copy of this data
Tpetra::MultiVector<Scalar,LO,GO,Node> B_mv(Teuchos::rcpFromRef(LocalMap),Bvalues,B.stride(),B.numCols());
// multiply
mv.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, alpha, A, B_mv, beta);
}
static void MvAddMv( Scalar alpha, const Tpetra::MultiVector<Scalar,LO,GO,Node>& A, Scalar beta, const Tpetra::MultiVector<Scalar,LO,GO,Node>& B, Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
{
mv.update(alpha,A,beta,B,Teuchos::ScalarTraits<Scalar>::zero());
}
static void MvScale ( Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, Scalar alpha )
{ mv.scale(alpha); }
static void MvScale ( Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, const std::vector<Scalar>& alphas )
{ mv.scale(alphas); }
static void MvTransMv( Scalar alpha, const Tpetra::MultiVector<Scalar,LO,GO,Node>& A, const Tpetra::MultiVector<Scalar,LO,GO,Node>& B, Teuchos::SerialDenseMatrix<int,Scalar>& C)
{
KOKKOS_NODE_TRACE("MVT::MvTransMv()")
#ifdef HAVE_BELOS_TPETRA_TIMERS
Teuchos::TimeMonitor lcltimer(*mvTransMvTimer_);
#endif
// form alpha * A^H * B, then copy into SDM
// we will create a multivector C_mv from a a local map
// this map has a serial comm, the purpose being to short-circuit the MultiVector::reduce() call at the end of MultiVector::multiply()
// otherwise, the reduced multivector data would be copied back to the GPU, only to turn around and have to get it back here.
// this saves us a round trip for this data.
const int numRowsC = C.numRows(),
numColsC = C.numCols(),
strideC = C.stride();
Teuchos::SerialComm<int> scomm;
// create local map with serial comm
Tpetra::Map<LO,GO,Node> LocalMap(numRowsC, 0, Teuchos::rcpFromRef< const Teuchos::Comm<int> >(scomm), Tpetra::LocallyReplicated, A.getMap()->getNode());
// create local multivector to hold the result
const bool INIT_TO_ZERO = true;
Tpetra::MultiVector<Scalar,LO,GO,Node> C_mv(Teuchos::rcpFromRef(LocalMap),numColsC, INIT_TO_ZERO);
// multiply result into local multivector
C_mv.multiply(Teuchos::CONJ_TRANS,Teuchos::NO_TRANS,alpha,A,B,Teuchos::ScalarTraits<Scalar>::zero());
// get comm
Teuchos::RCP< const Teuchos::Comm<int> > pcomm = A.getMap()->getComm();
// create arrayview encapsulating the Teuchos::SerialDenseMatrix
Teuchos::ArrayView<Scalar> C_view(C.values(),strideC*numColsC);
if (pcomm->getSize() == 1) {
// no accumulation to do; simply extract the multivector data into C
// extract a copy of the result into the array view (and therefore, the SerialDenseMatrix)
C_mv.get1dCopy(C_view,strideC);
}
else {
// get a const host view of the data in C_mv
Teuchos::ArrayRCP<const Scalar> C_mv_view = C_mv.get1dView();
if (strideC == numRowsC) {
// sumall into C
Teuchos::reduceAll<int,Scalar>(*pcomm,Teuchos::REDUCE_SUM,numColsC*numRowsC,C_mv_view.getRawPtr(),C_view.getRawPtr());
}
else {
// sumall into temp, copy into C
Teuchos::Array<Scalar> destBuff(numColsC*numRowsC);
Teuchos::reduceAll<int,Scalar>(*pcomm,Teuchos::REDUCE_SUM,numColsC*numRowsC,C_mv_view.getRawPtr(),destBuff.getRawPtr());
for (int j=0; j < numColsC; ++j) {
for (int i=0; i < numRowsC; ++i) {
C_view[strideC*j+i] = destBuff[numRowsC*j+i];
}
}
}
}
}
static void MvDot( const Tpetra::MultiVector<Scalar,LO,GO,Node>& A, const Tpetra::MultiVector<Scalar,LO,GO,Node>& B, std::vector<Scalar> &dots)
{
TEST_FOR_EXCEPTION(A.getNumVectors() != B.getNumVectors(),std::invalid_argument,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::MvDot(A,B,dots): A and B must have the same number of vectors.");
#ifdef HAVE_TPETRA_DEBUG
TEST_FOR_EXCEPTION(dots.size() < (typename std::vector<int>::size_type)A.getNumVectors(),std::invalid_argument,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::MvDot(A,B,dots): dots must have room for all dot products.");
#endif
Teuchos::ArrayView<Scalar> av(dots);
A.dot(B,av(0,A.getNumVectors()));
}
static void MvNorm(const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, std::vector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> &normvec, NormType type=TwoNorm)
{
#ifdef HAVE_TPETRA_DEBUG
TEST_FOR_EXCEPTION(normvec.size() < (typename std::vector<int>::size_type)mv.getNumVectors(),std::invalid_argument,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::MvNorm(mv,normvec): normvec must have room for all norms.");
#endif
Teuchos::ArrayView<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> av(normvec);
switch (type) {
case OneNorm:
mv.norm1(av(0,mv.getNumVectors()));
break;
case TwoNorm:
mv.norm2(av(0,mv.getNumVectors()));
break;
case InfNorm:
mv.normInf(av(0,mv.getNumVectors()));
break;
}
}
static void SetBlock( const Tpetra::MultiVector<Scalar,LO,GO,Node>& A, const std::vector<int>& index, Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
{
KOKKOS_NODE_TRACE("MVT::SetBlock()")
#ifdef HAVE_TPETRA_DEBUG
TEST_FOR_EXCEPTION((typename std::vector<int>::size_type)A.getNumVectors() < index.size(),std::invalid_argument,
"Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::SetBlock(A,index,mv): index must be the same size as A.");
#endif
Teuchos::RCP<Tpetra::MultiVector<Scalar,LO,GO,Node> > mvsub = CloneViewNonConst(mv,index);
if ((typename std::vector<int>::size_type)A.getNumVectors() > index.size()) {
Teuchos::RCP<const Tpetra::MultiVector<Scalar,LO,GO,Node> > Asub = A.subView(Teuchos::Range1D(0,index.size()-1));
(*mvsub) = (*Asub);
}
else {
(*mvsub) = A;
}
mvsub = Teuchos::null;
}
static void MvRandom( Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
{
KOKKOS_NODE_TRACE("MVT::randomize()")
mv.randomize();
}
static void MvInit( Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, Scalar alpha = Teuchos::ScalarTraits<Scalar>::zero() )
{ mv.putScalar(alpha); }
static void MvPrint( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, std::ostream& os )
{ mv.print(os); }
};
////////////////////////////////////////////////////////////////////
//
// Implementation of the Belos::OperatorTraits for Tpetra::Operator.
//
////////////////////////////////////////////////////////////////////
template <class Scalar, class LO, class GO, class Node>
class OperatorTraits < Scalar, Tpetra::MultiVector<Scalar,LO,GO,Node>, Tpetra::Operator<Scalar,LO,GO,Node> >
{
public:
static void Apply ( const Tpetra::Operator<Scalar,LO,GO,Node> & Op,
const Tpetra::MultiVector<Scalar,LO,GO,Node> & X,
Tpetra::MultiVector<Scalar,LO,GO,Node> & Y,
ETrans trans=NOTRANS )
{
switch (trans) {
case NOTRANS:
Op.apply(X,Y,Teuchos::NO_TRANS);
break;
case TRANS:
Op.apply(X,Y,Teuchos::TRANS);
break;
case CONJTRANS:
Op.apply(X,Y,Teuchos::CONJ_TRANS);
break;
}
}
};
} // end of Belos namespace
#endif
// end of file BELOS_TPETRA_ADAPTER_HPP
|