| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Belos: Block Linear Solvers Package |
| 4 |
// |
| 5 |
// Copyright 2004-2016 NTESS and the Belos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/*! \file BelosKokkosAdapter.hpp |
| 11 |
\brief Implementation of the interface between Belos virtual classes and Kokkos concrete classes. |
| 12 |
Allows the user to use a Kokkos::view as a Belos::MultiVector in the Belos solvers. |
| 13 |
\warning Note: This class does not currently allow the user to run Belos solvers which |
| 14 |
require accessing non-contiguous columns of data in memory. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include<Kokkos_Core.hpp> |
| 18 |
#include<Kokkos_Random.hpp> |
| 19 |
#include<KokkosBlas.hpp> |
| 20 |
#include<KokkosSparse_spmv.hpp> |
| 21 |
|
| 22 |
#include<BelosTypes.hpp> |
| 23 |
#include<BelosMultiVec.hpp> |
| 24 |
#include<BelosOperator.hpp> |
| 25 |
|
| 26 |
#include <Teuchos_SerialDenseMatrix.hpp> |
| 27 |
|
| 28 |
|
| 29 |
#ifndef BELOS_KOKKOS_ADAPTER_HPP |
| 30 |
#define BELOS_KOKKOS_ADAPTER_HPP |
| 31 |
namespace Belos { |
| 32 |
|
| 33 |
//Forward class declaration of KokkosCrsOperator: |
| 34 |
template<class ScalarType, class OrdinalType, class Device> |
| 35 |
class KokkosCrsOperator; |
| 36 |
|
| 37 |
/// \class KokkosMultiVec |
| 38 |
/// \brief Implementation of Belos::MultiVec using Kokkos::View. |
| 39 |
/// \author Jennifer Loe |
| 40 |
/// |
| 41 |
/// \tparam ScalarType The type of entries of the multivector. |
| 42 |
/// \tparam Device The Kokkos::ExecutionSpace where the multivector |
| 43 |
/// should reside. |
| 44 |
/// |
| 45 |
/// Belos::MultiVec offers a simple abstract interface for |
| 46 |
/// multivector operations in Belos solver algorithms. This class |
| 47 |
/// implements Belos::MultiVec using Kokkos::View. |
| 48 |
template<class ScalarType, class Device = Kokkos::DefaultExecutionSpace > |
| 49 |
class KokkosMultiVec : public MultiVec<ScalarType> { |
| 50 |
|
| 51 |
public: |
| 52 |
|
| 53 |
using ViewVectorType = Kokkos::View<ScalarType*,Kokkos::LayoutLeft, Device>; |
| 54 |
using ConstViewVectorType = Kokkos::View<const ScalarType*,Kokkos::LayoutLeft, Device>; |
| 55 |
using ViewMatrixType = Kokkos::View<ScalarType**,Kokkos::LayoutLeft, Device>; |
| 56 |
using ConstViewMatrixType = Kokkos::View<const ScalarType**,Kokkos::LayoutLeft, Device>; |
| 57 |
|
| 58 |
//Unmanaged view types: |
| 59 |
using UMHostViewVectorType = |
| 60 |
Kokkos::View<ScalarType*,Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>; |
| 61 |
using UMHostConstViewVectorType = |
| 62 |
Kokkos::View<const ScalarType*,Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>; |
| 63 |
using UMHostViewMatrixType = |
| 64 |
Kokkos::View<ScalarType**,Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>; |
| 65 |
using UMHostConstViewMatrixType = |
| 66 |
Kokkos::View<const ScalarType**,Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>; |
| 67 |
|
| 68 |
protected: |
| 69 |
ViewMatrixType myView; |
| 70 |
|
| 71 |
public: |
| 72 |
//! @name Constructors/Destructor |
| 73 |
//@{ |
| 74 |
|
| 75 |
/// \brief Returns a multivector with `numrows` rows and `numvecs` columns. |
| 76 |
/// |
| 77 |
/// The `label` string indicates the label for the internal `Kokkos::view`. |
| 78 |
/// If `zeroOut` is set to `true`, the multivector will be initialized to zeros. |
| 79 |
KokkosMultiVec<ScalarType, Device> (const std::string label, const int numrows, const int numvecs, const bool zeroOut = true) : |
| 80 |
myView (Kokkos::view_alloc(Kokkos::WithoutInitializing,label),numrows,numvecs) |
| 81 |
{ if (zeroOut) { Kokkos::deep_copy(myView,0); } } |
| 82 |
|
| 83 |
/// \brief Returns a multivector with `numrows` rows and `numvecs` columns. |
| 84 |
/// |
| 85 |
/// If `zeroOut` is set to `true`, the multivector will be initialized to zeros. |
| 86 |
KokkosMultiVec<ScalarType, Device> (const int numrows, const int numvecs, const bool zeroOut = true) : |
| 87 |
myView (Kokkos::view_alloc(Kokkos::WithoutInitializing,"MV"),numrows,numvecs) |
| 88 |
{ if (zeroOut) { Kokkos::deep_copy(myView,0); } } |
| 89 |
|
| 90 |
/// \brief Returns a single column multivector with `numrows` rows. |
| 91 |
/// |
| 92 |
/// If `zeroOut` is set to `true`, the multivector will be initialized to zeros. |
| 93 |
KokkosMultiVec<ScalarType, Device> (const int numrows, const bool zeroOut = true) : |
| 94 |
myView(Kokkos::view_alloc(Kokkos::WithoutInitializing,"MV"),numrows,1) |
| 95 |
{ if (zeroOut) { Kokkos::deep_copy(myView,0); } } |
| 96 |
|
| 97 |
//! Copy constructor (performs deep copy). |
| 98 |
|
| 99 |
/// This copy constructor returns a new KokksMultiVec containing a |
| 100 |
/// deep copy of the multivector given by the user. |
| 101 |
KokkosMultiVec<ScalarType, Device> (const KokkosMultiVec<ScalarType, Device> &sourceVec) : |
| 102 |
myView(Kokkos::view_alloc(Kokkos::WithoutInitializing,"MV"),(int)sourceVec.GetGlobalLength(),sourceVec.GetNumberVecs()) |
| 103 |
{ Kokkos::deep_copy(myView,sourceVec.GetInternalViewConst()); } |
| 104 |
|
| 105 |
//! Copy constructor for type conversion. (Performs deep copy.) |
| 106 |
|
| 107 |
/// This copy constructor returns a new KokksMultiVec containing a |
| 108 |
/// deep copy of the multivector given by the user. |
| 109 |
/// The internal data of the multivector is converted from |
| 110 |
/// `ScalarType` to `ScalarType2`. |
| 111 |
template < class ScalarType2 > |
| 112 |
KokkosMultiVec<ScalarType, Device> (const KokkosMultiVec<ScalarType2, Device> &sourceVec) : |
| 113 |
myView(Kokkos::view_alloc(Kokkos::WithoutInitializing, "MV"),(int)sourceVec.GetGlobalLength(),sourceVec.GetNumberVecs()) |
| 114 |
{ Kokkos::deep_copy(myView,sourceVec.GetInternalViewConst()); } |
| 115 |
|
| 116 |
//! Assignment operator (performs deep copy). |
| 117 |
|
| 118 |
/// This `=` operator performs a deep copy of |
| 119 |
/// the right-hand side KokkosMultiVec to the |
| 120 |
/// left-hand side KokkosMultiVec. The left-hand |
| 121 |
/// side MultiVec will be resized if necessary. |
| 122 |
KokkosMultiVec<ScalarType, Device> & operator=(const KokkosMultiVec<ScalarType, Device> & sourceVec) { |
| 123 |
int len = sourceVec.GetGlobalLength(); |
| 124 |
int cols = sourceVec.GetNumberVecs(); |
| 125 |
if( len != (int)myView.extent(0) || cols != (int)myView.extent(1) ){ |
| 126 |
Kokkos::resize(myView, len, cols); |
| 127 |
} |
| 128 |
Kokkos::deep_copy(myView,sourceVec.GetInternalViewConst()); |
| 129 |
return *this; |
| 130 |
} |
| 131 |
|
| 132 |
//! Assignment operator for type conversion. (Performs deep copy.) |
| 133 |
|
| 134 |
/// This `=` operator performs a deep copy of |
| 135 |
/// the right-hand side KokkosMultiVec to the |
| 136 |
/// left-hand side KokkosMultiVec. The left-hand |
| 137 |
/// side MultiVec will be resized if necessary. |
| 138 |
/// The internal data of the right-hand side multivec |
| 139 |
/// is converted from `ScalarType` to `ScalarType2`. |
| 140 |
template < class ScalarType2 > |
| 141 |
KokkosMultiVec<ScalarType, Device> & operator=(const KokkosMultiVec<ScalarType2, Device> & sourceVec) { |
| 142 |
int len = sourceVec.GetGlobalLength(); |
| 143 |
int cols = sourceVec.GetNumberVecs(); |
| 144 |
if( len != (int)myView.extent(0) || cols != (int)myView.extent(1) ){ |
| 145 |
Kokkos::resize(myView, len, cols); |
| 146 |
} |
| 147 |
Kokkos::deep_copy(myView,sourceVec.GetInternalViewConst()); |
| 148 |
return *this; |
| 149 |
} |
| 150 |
|
| 151 |
//! Create a KokkosMultiVec from a given `Kokkos::view`. |
| 152 |
|
| 153 |
/// Returns a KokkosMultiVec that internally stores the |
| 154 |
/// data given in `sourceView`. If `makeCopy` has value |
| 155 |
/// `true`, then this function makes a deep copy of the |
| 156 |
/// `Kokkos::view`. If `false`, then the KokkosMultiVec stores a |
| 157 |
/// shallow copy of the given `Kokkos::view`. (This option assumes that |
| 158 |
/// the user will make no changes to that view outside of |
| 159 |
/// the KokkosMultiVec interface.) |
| 160 |
KokkosMultiVec<ScalarType, Device> (const ViewMatrixType & sourceView, bool makeCopy = true) { |
| 161 |
if( makeCopy ){ |
| 162 |
if( sourceView.extent(0) != myView.extent(0) || sourceView.extent(1) != myView.extent(1) ){ |
| 163 |
Kokkos::resize(myView, sourceView.extent(0), sourceView.extent(1)); |
| 164 |
} |
| 165 |
Kokkos::deep_copy(myView, sourceView); |
| 166 |
} |
| 167 |
else{ |
| 168 |
myView = sourceView; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
//! Create a KokkosMultiVec with ScalarType2 from a given `Kokkos::view` with ScalarType1. |
| 173 |
|
| 174 |
/// Returns a KokkosMultiVec that internally stores the |
| 175 |
/// data given in `sourceView`. This function always makes |
| 176 |
/// a deep copy of the sourceView in order to change scalar |
| 177 |
/// types. |
| 178 |
template < class ScalarType2 > //TODO: Fix this so that passing in a view without device specified actually compiles... |
| 179 |
KokkosMultiVec<ScalarType, Device> (const Kokkos::View<ScalarType2**,Kokkos::LayoutLeft, Device> & sourceView) : |
| 180 |
myView(Kokkos::view_alloc(Kokkos::WithoutInitializing, "MV"),sourceView.extent(0),sourceView.extent(1)) |
| 181 |
{ Kokkos::deep_copy(myView,sourceView); } |
| 182 |
|
| 183 |
//! Destructor (default) |
| 184 |
~KokkosMultiVec<ScalarType, Device>(){} |
| 185 |
|
| 186 |
//@} |
| 187 |
|
| 188 |
//! @name Data Access Methods |
| 189 |
//@{ |
| 190 |
|
| 191 |
//! Reader (const) method for internal view. |
| 192 |
|
| 193 |
/// Returns the (const) view that stores internal data |
| 194 |
/// for the KokkosMultiVec. |
| 195 |
ConstViewMatrixType GetInternalViewConst() const { |
| 196 |
return myView; |
| 197 |
} |
| 198 |
|
| 199 |
//! Reader/Writer method for internal view. |
| 200 |
|
| 201 |
/// Returns the view that stores internal data |
| 202 |
/// for the KokkosMultiVec. |
| 203 |
/// \warning ** Be careful!! ** The KokkosMultiVec does |
| 204 |
/// NOT expect users to use this function to |
| 205 |
/// make changes to its internal data. Most |
| 206 |
/// necessary changes can be made via other functions. |
| 207 |
/// Make sure you know what you are doing! |
| 208 |
ViewMatrixType GetInternalViewNonConst(){ |
| 209 |
return myView; |
| 210 |
} |
| 211 |
//@} |
| 212 |
|
| 213 |
//! @name Copy/View functions inherited from Belos::MultiVec |
| 214 |
//@{ |
| 215 |
|
| 216 |
//! Creates a MultiVec with same data distribution as `this`. |
| 217 |
|
| 218 |
/// A virtual "copy constructor" that returns a pointer to a new |
| 219 |
/// Belos::MultiVec. (Underneath, the vector is a KokkosMultiVec, |
| 220 |
/// and its properties can be accessed via `dynamic_cast`.) |
| 221 |
/// This vector's entries are |
| 222 |
/// not copied; instead, a new MultiVec is created with the same |
| 223 |
/// data distribution, but with numvecs columns (numvecs > 0). |
| 224 |
/// Multivector entries are not initialized. |
| 225 |
/// |
| 226 |
/// \param numvecs [in] The number of columns in the output |
| 227 |
/// multivector. Must be positive. |
| 228 |
MultiVec<ScalarType> * Clone ( const int numvecs ) const{ |
| 229 |
KokkosMultiVec<ScalarType, Device> * ptr = new KokkosMultiVec<ScalarType, Device>(myView.extent(0),numvecs, false); |
| 230 |
return ptr; |
| 231 |
} |
| 232 |
|
| 233 |
//! \brief Creates a MultiVec which is a (deep) copy of `this`. |
| 234 |
/// |
| 235 |
/// A virtual "copy constructor" returning a pointer to a new |
| 236 |
/// object of Belos::MultiVec. (KokkosMultiVec underneath.) |
| 237 |
/// All of this vector's entries are |
| 238 |
/// copied and a new stand-alone multivector is created. (deep |
| 239 |
/// copy). |
| 240 |
MultiVec<ScalarType> * CloneCopy () const{ |
| 241 |
KokkosMultiVec<ScalarType, Device> * ptr = new KokkosMultiVec<ScalarType, Device>(myView.extent(0),myView.extent(1), false); |
| 242 |
Kokkos::deep_copy(ptr->GetInternalViewNonConst(),myView); |
| 243 |
return ptr; |
| 244 |
} |
| 245 |
|
| 246 |
//! \brief Creates a MultiVec which is a (deep) copy of selected columns of `this`. |
| 247 |
/// |
| 248 |
/// A virtual "copy constructor" returning a pointer to a new Belos::MultiVec. |
| 249 |
/// This vector's entries are copied and a new |
| 250 |
/// stand-alone MultiVector is created where only selected columns |
| 251 |
/// are chosen. (deep copy). |
| 252 |
/// Indexing is zero-based, so an `std::vector index` with values 0, 3, 5 would |
| 253 |
/// indicate copying the first, 4th, and 6th columns of the original multivector. |
| 254 |
/// Indices need not be contiguous or ordered. |
| 255 |
/// Result is `output[:,i] = (*this)[:,index[i]]`. |
| 256 |
MultiVec<ScalarType> * CloneCopy ( const std::vector<int>& index ) const{ |
| 257 |
// JAL- If debug options needed, could add validity checks of index. |
| 258 |
// See debug code in belos/src/tpetra/BelosMultiVecTraits_Tpetra.hpp. |
| 259 |
int numvecs = index.size(); |
| 260 |
KokkosMultiVec<ScalarType, Device> * B = new KokkosMultiVec<ScalarType, Device>("B",myView.extent(0),numvecs, false); |
| 261 |
bool isContigAscending = true; |
| 262 |
|
| 263 |
//Check whether the given indices are contiguous and ascending. |
| 264 |
for(unsigned int i=0; i< (index.size()-1); i++){ |
| 265 |
if( index[i+1] != index[i]+1 ){ |
| 266 |
isContigAscending = false; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
//Copy the vectors: (Case depends on indices.) |
| 271 |
if(isContigAscending && index.size()==(unsigned)this->GetNumberVecs()){ //Copy entire multivec. |
| 272 |
Kokkos::deep_copy(B->GetInternalViewNonConst(),myView); |
| 273 |
} |
| 274 |
else if (isContigAscending){ //Copy contiguous subset |
| 275 |
ViewMatrixType ThisSub = Kokkos::subview(myView, Kokkos::ALL, std::make_pair(index.front(), index.back()+1)); |
| 276 |
Kokkos::deep_copy(B->GetInternalViewNonConst(),ThisSub); |
| 277 |
} |
| 278 |
else{ //Copy columns one by one |
| 279 |
for(unsigned int i=0; i<index.size(); i++){ |
| 280 |
auto Bsub = Kokkos::subview(B->GetInternalViewNonConst(), Kokkos::ALL, i); |
| 281 |
auto ThisSub = Kokkos::subview(myView, Kokkos::ALL, index[i]); |
| 282 |
Kokkos::deep_copy(Bsub, ThisSub); |
| 283 |
} |
| 284 |
} |
| 285 |
return B; |
| 286 |
} |
| 287 |
|
| 288 |
//! \brief Creates a (read-only) MultiVec which is a shallow copy of selected columns of `this`. |
| 289 |
/// |
| 290 |
/// A virtual view constructor returning a pointer to a new Belos::MultiVec with |
| 291 |
/// selected columns. (Column indexing is zero-based.) The view is read-only. |
| 292 |
/// This vector's entries are shared and hence no |
| 293 |
/// memory is allocated for the columns. (Internally, we create |
| 294 |
/// a `Kokkos::subview`.) |
| 295 |
/// \warning At this time, the Kokkos-Belos adapter only supports |
| 296 |
/// viewing column indices that form a contiguous subset in memory. |
| 297 |
/// Thus, the values in `index` must be contiguous and ascending (e.g. 0,1,2,3). |
| 298 |
const MultiVec<ScalarType> * CloneView ( const std::vector<int>& index ) const { //TODO This isn't const!! |
| 299 |
bool isContigAscending = true; |
| 300 |
//Check whether the given indices are contiguous and ascending. |
| 301 |
for(unsigned int i=0; i< (index.size()-1); i++){ |
| 302 |
if( index[i+1] != index[i]+1 ){ |
| 303 |
isContigAscending = false; |
| 304 |
} |
| 305 |
} |
| 306 |
if(isContigAscending ){ |
| 307 |
const KokkosMultiVec<ScalarType, Device> * B = |
| 308 |
new KokkosMultiVec<ScalarType, Device>(Kokkos::subview(myView, Kokkos::ALL, std::make_pair(index.front(), index.back()+1)),false); |
| 309 |
return B; |
| 310 |
} |
| 311 |
else{ |
| 312 |
throw std::runtime_error("CloneView asked for non-contiguous subset. \n This feature is not yet supported in Belos for Kokkos."); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
//! \brief Creates a nonconst MultiVec which is a shallow copy of selected columns of `this`. |
| 318 |
/// |
| 319 |
/// A virtual view constructor returning a pointer to a new Belos::MultiVec with |
| 320 |
/// selected columns. (Column indexing is zero-based.) |
| 321 |
/// This vector's entries are shared and hence no |
| 322 |
/// memory is allocated for the columns. (Internally, we create |
| 323 |
/// a `Kokkos::subview`.) Writing to this view will change the |
| 324 |
/// entries of the original multivector. |
| 325 |
/// \warning At this time, the Kokkos-Belos adapter only supports |
| 326 |
/// viewing column indices that form a contiguous subset in memory. |
| 327 |
/// Thus, the values in `index` must be contiguous and ascending (e.g. 0,1,2,3). |
| 328 |
MultiVec<ScalarType> * CloneViewNonConst ( const std::vector<int>& index ){ |
| 329 |
bool isContigAscending = true; |
| 330 |
//Check whether the given indices are contiguous and ascending. |
| 331 |
for(unsigned int i=0; i< (index.size()-1); i++){ |
| 332 |
if( index[i+1] != index[i]+1 ){ |
| 333 |
isContigAscending = false; |
| 334 |
} |
| 335 |
} |
| 336 |
if(isContigAscending ){ |
| 337 |
KokkosMultiVec<ScalarType, Device> * B = |
| 338 |
new KokkosMultiVec<ScalarType, Device>(Kokkos::subview(myView, Kokkos::ALL, std::make_pair(index.front(), index.back()+1)),false); |
| 339 |
return B; |
| 340 |
} |
| 341 |
else{ |
| 342 |
throw std::runtime_error("CloneViewNonConst asked for non-contiguous subset. \n This feature is not yet supported in Belos for Kokkos."); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
//! Copy the vectors in A to the vectors in (*this) specified by index. (Deep copy.) |
| 347 |
|
| 348 |
/// Copies vectors of A to a sub-block of vectors of this multivector. (Deep copy.) |
| 349 |
/// The sub-block to be overwritten is given by the indices and need not be contiguous. |
| 350 |
/// Result is (*this)[:,index[i]] = A[:,i]. |
| 351 |
/// Column indexing is zero-based. |
| 352 |
/// |
| 353 |
void SetBlock ( const MultiVec<ScalarType>& A, const std::vector<int>& index ){ |
| 354 |
KokkosMultiVec<ScalarType, Device> *A_vec = dynamic_cast<KokkosMultiVec *>(&const_cast<MultiVec<ScalarType> &>(A)); |
| 355 |
|
| 356 |
if( index.size() > myView.extent(1) ){ |
| 357 |
throw std::runtime_error("Error in KokkosMultiVec::SetBlock. A cannot have more vectors than (*this)."); |
| 358 |
} |
| 359 |
bool isContigAscending = true; |
| 360 |
//Check whether the given indices are contiguous and ascending. |
| 361 |
for(unsigned int i=0; i< (index.size()-1); i++){ |
| 362 |
if( index[i+1] != index[i]+1 ){ |
| 363 |
isContigAscending = false; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
//Perform deep copy of sub block: |
| 368 |
if(isContigAscending && index.size()==(unsigned)this->GetNumberVecs()){ //Copy entire multivec. |
| 369 |
Kokkos::deep_copy(myView,A_vec->GetInternalViewConst()); |
| 370 |
} |
| 371 |
else if (isContigAscending){ //Copy contiguous subset |
| 372 |
ConstViewMatrixType Asub = Kokkos::subview(A_vec->GetInternalViewConst(), Kokkos::ALL, std::make_pair(0,(int)index.size())); |
| 373 |
ViewMatrixType ThisSub = Kokkos::subview(myView, Kokkos::ALL, std::make_pair(index.front(), index.back()+1)); |
| 374 |
Kokkos::deep_copy(ThisSub, Asub); |
| 375 |
} |
| 376 |
else{ //Copy columns one by one |
| 377 |
for(unsigned int i=0; i<index.size(); i++){ |
| 378 |
ConstViewVectorType Asub = Kokkos::subview(A_vec->GetInternalViewConst(), Kokkos::ALL, i); |
| 379 |
ViewVectorType ThisSub = Kokkos::subview(myView, Kokkos::ALL, index[i]); |
| 380 |
Kokkos::deep_copy(ThisSub, Asub); |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
//@} |
| 385 |
|
| 386 |
//! @name Attribue functions inherited from Belos::MultiVec |
| 387 |
//@{ |
| 388 |
//! Returns the number of rows in the multivector. |
| 389 |
ptrdiff_t GetGlobalLength () const { |
| 390 |
return static_cast<ptrdiff_t>(myView.extent(0)); |
| 391 |
} |
| 392 |
|
| 393 |
//! Returns the number of columns in the multivector. |
| 394 |
int GetNumberVecs () const { return myView.extent(1); } |
| 395 |
//@} |
| 396 |
|
| 397 |
//! @name Mathematical functions inherited from Belos::MultiVec |
| 398 |
//@{ |
| 399 |
//! \brief `*this <- alpha * A * B + beta * (*this)` |
| 400 |
/// |
| 401 |
/// where alpha and beta are scalars and the dimensions of `A*B` match |
| 402 |
/// the dimensions of `(*this)`. |
| 403 |
void MvTimesMatAddMv ( const ScalarType alpha, const MultiVec<ScalarType>& A, |
| 404 |
const Teuchos::SerialDenseMatrix<int,ScalarType>& B, const ScalarType beta ){ |
| 405 |
KokkosMultiVec<ScalarType, Device> *A_vec = dynamic_cast<KokkosMultiVec *>(&const_cast<MultiVec<ScalarType> &>(A)); |
| 406 |
if( myView.extent(1) == 1 && A_vec->GetInternalViewConst().extent(1) == 1){ //B is a scalar. |
| 407 |
ScalarType scal1 = alpha*B(0,0); |
| 408 |
ViewVectorType mysub = Kokkos::subview(myView, Kokkos::ALL, 0); |
| 409 |
ConstViewVectorType Asub = Kokkos::subview(A_vec->GetInternalViewConst(), Kokkos::ALL, 0); |
| 410 |
KokkosBlas::axpby(scal1, Asub, beta, mysub); |
| 411 |
} |
| 412 |
else{ |
| 413 |
UMHostConstViewMatrixType mat_h(B.values(), A_vec->GetInternalViewConst().extent(1), myView.extent(1)); |
| 414 |
ViewMatrixType mat_d(Kokkos::view_alloc(Kokkos::WithoutInitializing,"mat"), A_vec->GetInternalViewConst().extent(1), myView.extent(1)); |
| 415 |
Kokkos::deep_copy(mat_d, mat_h); |
| 416 |
if( myView.extent(1) == 1 ){ // B has only 1 col |
| 417 |
ConstViewVectorType Bsub = Kokkos::subview(mat_d, Kokkos::ALL, 0); |
| 418 |
ViewVectorType mysub = Kokkos::subview(myView, Kokkos::ALL, 0); |
| 419 |
KokkosBlas::gemv("N", alpha, A_vec->GetInternalViewConst(), Bsub, beta, mysub); |
| 420 |
} |
| 421 |
else{ |
| 422 |
KokkosBlas::gemm("N", "N", alpha, A_vec->GetInternalViewConst(), mat_d, beta, myView); |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
//! `*this <- alpha * A + beta * B` |
| 428 |
/// |
| 429 |
/// Scale and add two vectors. Store the result in `*this`. |
| 430 |
void MvAddMv ( const ScalarType alpha, const MultiVec<ScalarType>& A, const ScalarType beta, |
| 431 |
const MultiVec<ScalarType>& B){ |
| 432 |
KokkosMultiVec<ScalarType, Device> *A_vec = dynamic_cast<KokkosMultiVec *>(&const_cast<MultiVec<ScalarType> &>(A)); |
| 433 |
KokkosMultiVec<ScalarType, Device> *B_vec = dynamic_cast<KokkosMultiVec *>(&const_cast<MultiVec<ScalarType> &>(B)); |
| 434 |
|
| 435 |
KokkosBlas::update(alpha, A_vec->GetInternalViewConst(), beta, B_vec->GetInternalViewConst(), (ScalarType) 0.0, myView); |
| 436 |
} |
| 437 |
|
| 438 |
/// `*this <- alpha * this` |
| 439 |
/// |
| 440 |
//! Scale (multiply) each element of the vectors in \c *this with \c alpha. |
| 441 |
void MvScale ( const ScalarType alpha ) { |
| 442 |
KokkosBlas::scal(myView, alpha, myView); |
| 443 |
} |
| 444 |
|
| 445 |
/// `*this[:,i] <- alpha[i] * this[:,i]` |
| 446 |
/// |
| 447 |
//! Scale (multiply) each element of the \c i-th vector in \c *this with \c alpha[i]. |
| 448 |
void MvScale ( const std::vector<ScalarType>& alpha ){ |
| 449 |
|
| 450 |
//Move scalar values to a Kokkos View: |
| 451 |
UMHostConstViewVectorType scalars_h(alpha.data(), alpha.size()); |
| 452 |
ViewVectorType scalars_d(Kokkos::view_alloc(Kokkos::WithoutInitializing,"scalars_d"), alpha.size()); |
| 453 |
Kokkos::deep_copy(scalars_d, scalars_h); |
| 454 |
|
| 455 |
KokkosBlas::scal(myView, scalars_d, myView); |
| 456 |
} |
| 457 |
|
| 458 |
//! B <- alpha * A^T * (*this) |
| 459 |
/// |
| 460 |
/// Computes matrix product with transpose. Result is a dense matrix. |
| 461 |
/// Conjugate transpose is used as appropriate. |
| 462 |
void MvTransMv ( const ScalarType alpha, const MultiVec<ScalarType>& A, Teuchos::SerialDenseMatrix<int,ScalarType>& B ) const{ |
| 463 |
KokkosMultiVec<ScalarType, Device> *A_vec = dynamic_cast<KokkosMultiVec *>(&const_cast<MultiVec<ScalarType> &>(A)); |
| 464 |
if(A_vec->myView.extent(1) == 1 && myView.extent(1) == 1){ |
| 465 |
ConstViewVectorType Asub = Kokkos::subview(A_vec->GetInternalViewConst(), Kokkos::ALL, 0); |
| 466 |
ViewVectorType mysub = Kokkos::subview(myView, Kokkos::ALL, 0); |
| 467 |
ScalarType soln = KokkosBlas::dot(Asub, mysub); |
| 468 |
soln = alpha*soln; |
| 469 |
B(0,0) = soln; |
| 470 |
} |
| 471 |
// *** |
| 472 |
// For MvTransMv, this option runs slower than GEMM on NVIDIA V100. |
| 473 |
// Do not enable for now. |
| 474 |
// **** |
| 475 |
// else if( myView.extent(1) == 1 ){ // Only 1 col in soln vec |
| 476 |
// ViewVectorType soln(Kokkos::view_alloc(Kokkos::WithoutInitializing,"soln"), A_vec->GetInternalViewConst().extent(1)); |
| 477 |
// ViewVectorType mysub = Kokkos::subview(myView, Kokkos::ALL, 0); |
| 478 |
// KokkosBlas::gemv("C", alpha, A_vec->GetInternalViewConst(), mysub, ScalarType(0.0), soln); |
| 479 |
// for( unsigned int i = 0; i < soln.extent(0); i++){ |
| 480 |
// B(i,0) = soln(i); |
| 481 |
// } |
| 482 |
// } |
| 483 |
else{ |
| 484 |
UMHostViewMatrixType soln_h(B.values(), A_vec->GetInternalViewConst().extent(1), myView.extent(1)); |
| 485 |
ViewMatrixType soln_d(Kokkos::view_alloc(Kokkos::WithoutInitializing,"mat"), A_vec->GetInternalViewConst().extent(1), myView.extent(1)); |
| 486 |
KokkosBlas::gemm("C", "N", alpha, A_vec->GetInternalViewConst(), myView, ScalarType(0.0), soln_d); |
| 487 |
Kokkos::deep_copy(soln_h, soln_d); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
//! b[i] = A[i]^T * this[i] |
| 493 |
|
| 494 |
/// Performs a dot product between A and (*this). |
| 495 |
/// Uses conjugate transpose when appropriate. |
| 496 |
/// Output is a vector. |
| 497 |
void MvDot ( const MultiVec<ScalarType>& A, std::vector<ScalarType>& b ) const{ |
| 498 |
//Put output vector in unmanaged Kokkos view: |
| 499 |
UMHostViewVectorType dotView_h(b.data(),myView.extent(1)); |
| 500 |
ViewVectorType dotView_d(Kokkos::view_alloc(Kokkos::WithoutInitializing,"Dot"),myView.extent(1)); |
| 501 |
|
| 502 |
KokkosMultiVec<ScalarType, Device> *A_vec = dynamic_cast<KokkosMultiVec *>(&const_cast<MultiVec<ScalarType> &>(A)); |
| 503 |
|
| 504 |
KokkosBlas::dot(dotView_d, A_vec->GetInternalViewConst(), myView); |
| 505 |
Kokkos::deep_copy(dotView_h, dotView_d); |
| 506 |
} |
| 507 |
|
| 508 |
//! alpha[i] = norm of i-th column of (*this) |
| 509 |
|
| 510 |
/// Valid norm types are Belos::TwoNorm, Belos::OneNorm, |
| 511 |
/// and Belos::InfNorm. |
| 512 |
void MvNorm ( std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>& normvec, NormType norm_type = TwoNorm ) const{ |
| 513 |
|
| 514 |
//Put output vector in unmanaged Kokkos view: |
| 515 |
using magnitudeType = typename Teuchos::ScalarTraits<ScalarType>::magnitudeType; |
| 516 |
Kokkos::View<magnitudeType*,Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>> normView_h(normvec.data(),myView.extent(1)); |
| 517 |
Kokkos::View<magnitudeType*,Kokkos::LayoutLeft, Device> normView_d(Kokkos::view_alloc(Kokkos::WithoutInitializing,"Norm"),myView.extent(1)); |
| 518 |
|
| 519 |
switch( norm_type ) { |
| 520 |
case ( OneNorm ) : |
| 521 |
KokkosBlas::nrm1(normView_d, myView); |
| 522 |
break; |
| 523 |
case ( TwoNorm ) : |
| 524 |
KokkosBlas::nrm2(normView_d, myView); |
| 525 |
break; |
| 526 |
case ( InfNorm ) : |
| 527 |
KokkosBlas::nrminf(normView_d, myView); |
| 528 |
break; |
| 529 |
default: |
| 530 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, |
| 531 |
"Belos::KokkosMultiVec::MvNorm: Invalid norm_type " |
| 532 |
<< norm_type << ". The current list of valid norm " |
| 533 |
"types is {OneNorm, TwoNorm, InfNorm}."); |
| 534 |
} |
| 535 |
Kokkos::deep_copy(normView_h, normView_d); |
| 536 |
} |
| 537 |
//@} |
| 538 |
|
| 539 |
//! @name Initialization functions inherited from Belos::MultiVec |
| 540 |
//@{ |
| 541 |
//! Fill all columns of *this with random values. |
| 542 |
void MvRandom() { |
| 543 |
int rand_seed = std::rand(); |
| 544 |
Kokkos::Random_XorShift64_Pool<> pool(rand_seed); |
| 545 |
Kokkos::fill_random(myView, pool, -1,1); |
| 546 |
} |
| 547 |
|
| 548 |
//! Initialize each element of (*this) to the scalar value alpha. |
| 549 |
void MvInit ( const ScalarType alpha ) { |
| 550 |
Kokkos::deep_copy(myView,alpha); |
| 551 |
} |
| 552 |
//@} |
| 553 |
|
| 554 |
//! @name Print function inherited from Belos::MultiVec |
| 555 |
//@{ |
| 556 |
//! Print (*this) to the given output stream. |
| 557 |
|
| 558 |
/// (This function will first copy the multivector to host space |
| 559 |
/// if needed.) |
| 560 |
void MvPrint( std::ostream& os ) const { |
| 561 |
typename ViewMatrixType::HostMirror hostView("myViewMirror", myView.extent(0), myView.extent(1)); |
| 562 |
Kokkos::deep_copy(hostView, myView); |
| 563 |
for(unsigned int i = 0; i < (hostView.extent(0)); i++){ |
| 564 |
for (unsigned int j = 0; j < (hostView.extent(1)); j++){ |
| 565 |
os << hostView(i , j) << " "; |
| 566 |
} |
| 567 |
os << std::endl; |
| 568 |
} |
| 569 |
os << std::endl; |
| 570 |
} |
| 571 |
//@} |
| 572 |
|
| 573 |
}; |
| 574 |
|
| 575 |
/// \class KokkosCrsOperator |
| 576 |
/// \brief Implementation of Belos::Operator using KokkosSparse::CrsMatrix. |
| 577 |
template<class ScalarType, class OrdinalType=int, class Device=Kokkos::DefaultExecutionSpace> |
| 578 |
class KokkosCrsOperator : public Operator<ScalarType> { |
| 579 |
|
| 580 |
private: |
| 581 |
// Shallow copy of the CrsMatrix used for SpMV. |
| 582 |
KokkosSparse::CrsMatrix<ScalarType, OrdinalType, Device> myMatrix; |
| 583 |
|
| 584 |
public: |
| 585 |
//! @name Constructor/Destructor |
| 586 |
//@{ |
| 587 |
//! Constructor obtains a shallow copy of the given CrsMatrix. |
| 588 |
KokkosCrsOperator<ScalarType, OrdinalType, Device> (const KokkosSparse::CrsMatrix<ScalarType, OrdinalType, Device> mat) |
| 589 |
: myMatrix(mat) {} |
| 590 |
|
| 591 |
//! Destructor. |
| 592 |
~KokkosCrsOperator<ScalarType, OrdinalType, Device>(){} |
| 593 |
//@} |
| 594 |
|
| 595 |
//! @name Methods relating to applying the operator |
| 596 |
//@{ |
| 597 |
|
| 598 |
/// \brief Apply the operator to x, putting the result in y. |
| 599 |
/// |
| 600 |
/// Take the KokkosMultiVec \c x and apply the operator (or its |
| 601 |
/// transpose or Hermitian transpose) to it, writing the result |
| 602 |
/// into the KokkosMultiVec \c y. (This performs a sparse matrix- |
| 603 |
/// vector product.) |
| 604 |
/// |
| 605 |
/// \param x [in] The input multivector. |
| 606 |
/// |
| 607 |
/// \param y [out] The output multivector. x and y may not alias |
| 608 |
/// (i.e., be views of) one another. |
| 609 |
/// |
| 610 |
/// \param trans [in] Whether to apply the operator (Belos::NOTRANS), its |
| 611 |
/// transpose (Belos::TRANS), or its Hermitian transpose (Belos::CONJTRANS). |
| 612 |
/// The default is Belos::NOTRANS. (Defined in BelosTypes.hpp.) |
| 613 |
/// |
| 614 |
void Apply (const MultiVec<ScalarType>& x, MultiVec<ScalarType>& y, ETrans trans=NOTRANS) const{ |
| 615 |
|
| 616 |
// Determine transpose mode: |
| 617 |
char mode[] = "X"; |
| 618 |
switch(trans){ |
| 619 |
case NOTRANS: |
| 620 |
mode[0]='N'; |
| 621 |
break; |
| 622 |
case TRANS: |
| 623 |
mode[0]='T'; |
| 624 |
break; |
| 625 |
case CONJTRANS: |
| 626 |
mode[0]='C'; |
| 627 |
break; |
| 628 |
default: |
| 629 |
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, |
| 630 |
"Belos::KokkosCrsOperator::Apply: Invalid ETrans type "); |
| 631 |
} |
| 632 |
|
| 633 |
//Use dynamic_cast to tell the compiler these are Kokkos Multivecs. |
| 634 |
KokkosMultiVec<ScalarType, Device> *x_vec = |
| 635 |
dynamic_cast<KokkosMultiVec<ScalarType, Device> *>(&const_cast<MultiVec<ScalarType> &>(x)); |
| 636 |
KokkosMultiVec<ScalarType, Device> *y_vec = dynamic_cast<KokkosMultiVec<ScalarType, Device> *>(&y); |
| 637 |
|
| 638 |
//KokkosSparse::spmv computes y = beta*y + alpha*Op(A)*x |
| 639 |
ScalarType alpha = 1.0; |
| 640 |
ScalarType beta = 0; |
| 641 |
KokkosSparse::spmv(mode, alpha, myMatrix, x_vec->GetInternalViewConst(), beta, y_vec->GetInternalViewNonConst()); |
| 642 |
} |
| 643 |
|
| 644 |
/// \brief Whether this operator implements applying the transpose. |
| 645 |
/// |
| 646 |
/// This function returns true since we can always apply the transpose |
| 647 |
/// of a Kokkos::CrsMatrix. |
| 648 |
/// |
| 649 |
bool HasApplyTranspose () const { |
| 650 |
return true; |
| 651 |
} |
| 652 |
//@} |
| 653 |
}; |
| 654 |
}// end namespace Belos |
| 655 |
#endif |