| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Anasazi: Block Eigensolvers Package |
| 4 |
// |
| 5 |
// Copyright 2004 NTESS and the Anasazi contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
#ifndef MY_MULTIVECTOR_HPP |
| 10 |
#define MY_MULTIVECTOR_HPP |
| 11 |
|
| 12 |
#include "AnasaziConfigDefs.hpp" |
| 13 |
#include "AnasaziMultiVec.hpp" |
| 14 |
|
| 15 |
#include "Teuchos_SerialDenseMatrix.hpp" |
| 16 |
#include "Teuchos_SerialDenseHelpers.hpp" |
| 17 |
|
| 18 |
//! Simple example of a user's defined Anasazi::MultiVec class. |
| 19 |
/*! |
| 20 |
* This is a simple, single processor example of user's defined |
| 21 |
* MultiVec-derived class. The class is templated with ScalarType; |
| 22 |
* possible choices are, for example, "float", "double", or |
| 23 |
* "complex<double>". |
| 24 |
* |
| 25 |
* \author Oscar Chinallato (ETHZ/ICOS) and Marzio Sala (ETHZ/COLAB) |
| 26 |
* |
| 27 |
* \date Last modified on 01-Nov-05 |
| 28 |
*/ |
| 29 |
template <class ScalarType> |
| 30 |
class MyMultiVec : public Anasazi::MultiVec<ScalarType> |
| 31 |
{ |
| 32 |
public: |
| 33 |
|
| 34 |
//! Constructor for a \c NumberVecs vectors of length \c Length. |
| 35 |
MyMultiVec(const int Length, const int NumberVecs) : |
| 36 |
Length_(Length), |
| 37 |
NumberVecs_(NumberVecs) |
| 38 |
{ |
| 39 |
Check(); |
| 40 |
|
| 41 |
data_.resize(NumberVecs); |
| 42 |
ownership_.resize(NumberVecs); |
| 43 |
|
| 44 |
// Allocates memory to store the vectors. |
| 45 |
for (int v = 0 ; v < NumberVecs_ ; ++v) |
| 46 |
{ |
| 47 |
data_[v] = new ScalarType[Length]; |
| 48 |
ownership_[v] = true; |
| 49 |
} |
| 50 |
|
| 51 |
// Initializes all elements to zero. |
| 52 |
MvInit(0.0); |
| 53 |
} |
| 54 |
|
| 55 |
//! Constructor with already allocated memory |
| 56 |
MyMultiVec(const int Length, const std::vector<ScalarType*>& rhs) : |
| 57 |
Length_(Length), |
| 58 |
NumberVecs_(rhs.size()) |
| 59 |
{ |
| 60 |
Check(); |
| 61 |
|
| 62 |
data_.resize(NumberVecs_); |
| 63 |
ownership_.resize(NumberVecs_); |
| 64 |
|
| 65 |
// Copies pointers from input array, set ownership so that |
| 66 |
// this memory is not free'd in the destructor |
| 67 |
for (int v = 0 ; v < NumberVecs_ ; ++v) |
| 68 |
{ |
| 69 |
data_[v] = rhs[v]; |
| 70 |
ownership_[v] = false; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
//! Copy constructor, performs a deep copy. |
| 75 |
MyMultiVec(const MyMultiVec& rhs) : |
| 76 |
Length_(rhs.GetGlobalLength()), |
| 77 |
NumberVecs_(rhs.NumberVecs_) |
| 78 |
{ |
| 79 |
Check(); |
| 80 |
|
| 81 |
data_.resize(NumberVecs_); |
| 82 |
ownership_.resize(NumberVecs_); |
| 83 |
|
| 84 |
for (int v = 0 ; v < NumberVecs_ ; ++v) |
| 85 |
{ |
| 86 |
data_[v] = new ScalarType[Length_]; |
| 87 |
ownership_[v] = true; |
| 88 |
} |
| 89 |
|
| 90 |
for (int v = 0 ; v < NumberVecs_ ; ++v) |
| 91 |
{ |
| 92 |
for (int i = 0 ; i < Length_ ; ++i) |
| 93 |
(*this)(i, v) = rhs(i, v); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
//! Destructor |
| 98 |
~MyMultiVec() |
| 99 |
{ |
| 100 |
for (int v = 0 ; v < NumberVecs_ ; ++v) |
| 101 |
if (ownership_[v]) |
| 102 |
delete[] data_[v]; |
| 103 |
} |
| 104 |
|
| 105 |
//! Returns a clone of the current vector. |
| 106 |
MyMultiVec* Clone(const int NumberVecs) const |
| 107 |
{ |
| 108 |
// FIXME |
| 109 |
MyMultiVec* tmp = new MyMultiVec(Length_, NumberVecs); |
| 110 |
|
| 111 |
// for (int v = 0 ; v < NumberVecs ; ++v) |
| 112 |
// for (int i = 0 ; i < Length_ ; ++i) |
| 113 |
// (*tmp)(i, v) = (*this)(i, v); |
| 114 |
|
| 115 |
return(tmp); |
| 116 |
} |
| 117 |
|
| 118 |
// Returns a clone of the corrent multi-vector. |
| 119 |
MyMultiVec* CloneCopy() const |
| 120 |
{ |
| 121 |
return(new MyMultiVec(*this)); |
| 122 |
} |
| 123 |
|
| 124 |
//! Returns a clone copy of specified vectors. |
| 125 |
MyMultiVec* CloneCopy(const std::vector< int > &index) const |
| 126 |
{ |
| 127 |
int size = index.size(); |
| 128 |
MyMultiVec* tmp = new MyMultiVec(Length_, size); |
| 129 |
|
| 130 |
for (unsigned int v = 0 ; v < index.size() ; ++v) { |
| 131 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 132 |
(*tmp)(i, v) = (*this)(i, index[v]); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return(tmp); |
| 137 |
} |
| 138 |
|
| 139 |
//! Returns a view of current vector (shallow copy) |
| 140 |
MyMultiVec* CloneViewNonConst(const std::vector< int > &index) |
| 141 |
{ |
| 142 |
int size = index.size(); |
| 143 |
std::vector<ScalarType*> values(size); |
| 144 |
|
| 145 |
for (unsigned int v = 0 ; v < index.size() ; ++v) |
| 146 |
values[v] = data_[index[v]]; |
| 147 |
|
| 148 |
return(new MyMultiVec(Length_, values)); |
| 149 |
} |
| 150 |
|
| 151 |
//! Returns a view of current vector (shallow copy), const version. |
| 152 |
const MyMultiVec* CloneView(const std::vector< int > &index) const |
| 153 |
{ |
| 154 |
int size = index.size(); |
| 155 |
std::vector<ScalarType*> values(size); |
| 156 |
|
| 157 |
for (unsigned int v = 0 ; v < index.size() ; ++v) |
| 158 |
values[v] = data_[index[v]]; |
| 159 |
|
| 160 |
return(new MyMultiVec(Length_, values)); |
| 161 |
} |
| 162 |
|
| 163 |
ptrdiff_t GetGlobalLength () const |
| 164 |
{ |
| 165 |
return(Length_); |
| 166 |
} |
| 167 |
|
| 168 |
int GetNumberVecs () const |
| 169 |
{ |
| 170 |
return(NumberVecs_); |
| 171 |
} |
| 172 |
|
| 173 |
// Update *this with alpha * A * B + beta * (*this). |
| 174 |
void MvTimesMatAddMv (ScalarType alpha, const Anasazi::MultiVec<ScalarType> &A, |
| 175 |
const Teuchos::SerialDenseMatrix<int, ScalarType> &B, |
| 176 |
ScalarType beta) |
| 177 |
{ |
| 178 |
|
| 179 |
assert (Length_ == A.GetGlobalLength()); |
| 180 |
assert (B.numRows() == A.GetNumberVecs()); |
| 181 |
assert (B.numCols() <= NumberVecs_); |
| 182 |
|
| 183 |
MyMultiVec* MyA; |
| 184 |
MyA = dynamic_cast<MyMultiVec*>(&const_cast<Anasazi::MultiVec<ScalarType> &>(A)); |
| 185 |
assert(MyA!=NULL); |
| 186 |
|
| 187 |
if ((*this)[0] == (*MyA)[0]) { |
| 188 |
// If this == A, then need additional storage ... |
| 189 |
// This situation is a bit peculiar but it may be required by |
| 190 |
// certain algorithms. |
| 191 |
|
| 192 |
std::vector<ScalarType> tmp(NumberVecs_); |
| 193 |
|
| 194 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 195 |
for (int v = 0; v < A.GetNumberVecs() ; ++v) { |
| 196 |
tmp[v] = (*MyA)(i, v); |
| 197 |
} |
| 198 |
|
| 199 |
for (int v = 0 ; v < B.numCols() ; ++v) { |
| 200 |
(*this)(i, v) *= beta; |
| 201 |
ScalarType res = Teuchos::ScalarTraits<ScalarType>::zero(); |
| 202 |
|
| 203 |
for (int j = 0 ; j < A.GetNumberVecs() ; ++j) { |
| 204 |
res += tmp[j] * B(j, v); |
| 205 |
} |
| 206 |
|
| 207 |
(*this)(i, v) += alpha * res; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
else { |
| 212 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 213 |
for (int v = 0 ; v < B.numCols() ; ++v) { |
| 214 |
(*this)(i, v) *= beta; |
| 215 |
ScalarType res = 0.0; |
| 216 |
for (int j = 0 ; j < A.GetNumberVecs() ; ++j) { |
| 217 |
res += (*MyA)(i, j) * B(j, v); |
| 218 |
} |
| 219 |
|
| 220 |
(*this)(i, v) += alpha * res; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// Replace *this with alpha * A + beta * B. |
| 227 |
void MvAddMv (ScalarType alpha, const Anasazi::MultiVec<ScalarType>& A, |
| 228 |
ScalarType beta, const Anasazi::MultiVec<ScalarType>& B) |
| 229 |
{ |
| 230 |
MyMultiVec* MyA; |
| 231 |
MyA = dynamic_cast<MyMultiVec*>(&const_cast<Anasazi::MultiVec<ScalarType> &>(A)); |
| 232 |
assert (MyA != 0); |
| 233 |
|
| 234 |
MyMultiVec* MyB; |
| 235 |
MyB = dynamic_cast<MyMultiVec*>(&const_cast<Anasazi::MultiVec<ScalarType> &>(B)); |
| 236 |
assert (MyB != 0); |
| 237 |
|
| 238 |
assert (NumberVecs_ == A.GetNumberVecs()); |
| 239 |
assert (NumberVecs_ == B.GetNumberVecs()); |
| 240 |
|
| 241 |
assert (Length_ == A.GetGlobalLength()); |
| 242 |
assert (Length_ == B.GetGlobalLength()); |
| 243 |
|
| 244 |
for (int v = 0 ; v < NumberVecs_ ; ++v) { |
| 245 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 246 |
(*this)(i, v) = alpha * (*MyA)(i, v) + beta * (*MyB)(i, v); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
// Compute a dense matrix B through the matrix-matrix multiply alpha * A^H * (*this). |
| 252 |
void MvTransMv (ScalarType alpha, const Anasazi::MultiVec<ScalarType>& A, |
| 253 |
Teuchos::SerialDenseMatrix< int, ScalarType >& B |
| 254 |
#ifdef HAVE_ANASAZI_EXPERIMENTAL |
| 255 |
, Anasazi::ConjType conj |
| 256 |
#endif |
| 257 |
) const |
| 258 |
{ |
| 259 |
MyMultiVec* MyA; |
| 260 |
MyA = dynamic_cast<MyMultiVec*>(&const_cast<Anasazi::MultiVec<ScalarType> &>(A)); |
| 261 |
assert (MyA != 0); |
| 262 |
|
| 263 |
assert (A.GetGlobalLength() == Length_); |
| 264 |
assert (NumberVecs_ <= B.numCols()); |
| 265 |
assert (A.GetNumberVecs() <= B.numRows()); |
| 266 |
|
| 267 |
#ifdef HAVE_ANASAZI_EXPERIMENTAL |
| 268 |
if (conj == Anasazi::CONJ) { |
| 269 |
#endif |
| 270 |
for (int v = 0 ; v < A.GetNumberVecs() ; ++v) { |
| 271 |
for (int w = 0 ; w < NumberVecs_ ; ++w) { |
| 272 |
ScalarType value = 0.0; |
| 273 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 274 |
value += Teuchos::ScalarTraits<ScalarType>::conjugate((*MyA)(i, v)) * (*this)(i, w); |
| 275 |
} |
| 276 |
B(v, w) = alpha * value; |
| 277 |
} |
| 278 |
} |
| 279 |
#ifdef HAVE_ANASAZI_EXPERIMENTAL |
| 280 |
} else { |
| 281 |
for (int v = 0 ; v < A.GetNumberVecs() ; ++v) { |
| 282 |
for (int w = 0 ; w < NumberVecs_ ; ++w) { |
| 283 |
ScalarType value = 0.0; |
| 284 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 285 |
value += (*MyA)(i, v) * (*this)(i, w); |
| 286 |
} |
| 287 |
B(v, w) = alpha * value; |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
#endif |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
// Compute a vector b where the components are the individual dot-products, i.e.b[i] = A[i]^H*this[i] where A[i] is the i-th column of A. |
| 296 |
void MvDot (const Anasazi::MultiVec<ScalarType>& A, std::vector<ScalarType> &b |
| 297 |
#ifdef HAVE_ANASAZI_EXPERIMENTAL |
| 298 |
, Anasazi::ConjType conj |
| 299 |
#endif |
| 300 |
) const |
| 301 |
{ |
| 302 |
MyMultiVec* MyA; |
| 303 |
MyA = dynamic_cast<MyMultiVec*>(&const_cast<Anasazi::MultiVec<ScalarType> &>(A)); |
| 304 |
assert (MyA != 0); |
| 305 |
|
| 306 |
assert (NumberVecs_ <= (int)b.size()); |
| 307 |
assert (NumberVecs_ == A.GetNumberVecs()); |
| 308 |
assert (Length_ == A.GetGlobalLength()); |
| 309 |
|
| 310 |
#ifdef HAVE_ANASAZI_EXPERIMENTAL |
| 311 |
if (conj == Anasazi::CONJ) { |
| 312 |
#endif |
| 313 |
for (int v = 0 ; v < NumberVecs_ ; ++v) { |
| 314 |
ScalarType value = 0.0; |
| 315 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 316 |
value += (*this)(i, v) * Teuchos::ScalarTraits<ScalarType>::conjugate((*MyA)(i, v)); |
| 317 |
} |
| 318 |
b[v] = value; |
| 319 |
} |
| 320 |
#ifdef HAVE_ANASAZI_EXPERIMENTAL |
| 321 |
} else { |
| 322 |
for (int v = 0 ; v < NumberVecs_ ; ++v) { |
| 323 |
ScalarType value = 0.0; |
| 324 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 325 |
value += (*this)(i, v) * (*MyA)(i, v); |
| 326 |
} |
| 327 |
b[v] = value; |
| 328 |
} |
| 329 |
} |
| 330 |
#endif |
| 331 |
} |
| 332 |
|
| 333 |
void MvNorm ( std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> &normvec ) const |
| 334 |
{ |
| 335 |
assert (NumberVecs_ <= (int)normvec.size()); |
| 336 |
|
| 337 |
typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType; |
| 338 |
|
| 339 |
for (int v = 0 ; v < NumberVecs_ ; ++v) { |
| 340 |
MagnitudeType value = Teuchos::ScalarTraits<MagnitudeType>::zero(); |
| 341 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 342 |
MagnitudeType val = Teuchos::ScalarTraits<ScalarType>::magnitude((*this)(i, v)); |
| 343 |
value += val * val; |
| 344 |
} |
| 345 |
normvec[v] = Teuchos::ScalarTraits<MagnitudeType>::squareroot(value); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
// Copy the vectors in A to a set of vectors in *this. The numvecs vectors in |
| 350 |
// A are copied to a subset of vectors in *this indicated by the indices given |
| 351 |
// in index. |
| 352 |
// FIXME: not so clear what the size of A and index.size() are... |
| 353 |
void SetBlock (const Anasazi::MultiVec<ScalarType>& A, |
| 354 |
const std::vector<int> &index) |
| 355 |
{ |
| 356 |
MyMultiVec* MyA; |
| 357 |
MyA = dynamic_cast<MyMultiVec*>(&const_cast<Anasazi::MultiVec<ScalarType> &>(A)); |
| 358 |
assert (MyA != 0); |
| 359 |
|
| 360 |
assert (A.GetNumberVecs() >= (int)index.size()); |
| 361 |
assert (A.GetGlobalLength() == Length_); |
| 362 |
|
| 363 |
for (unsigned int v = 0 ; v < index.size() ; ++v) { |
| 364 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 365 |
(*this)(i, index[v]) = (*MyA)(i, v); |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
// Scale the vectors by alpha |
| 371 |
void MvScale( ScalarType alpha ) |
| 372 |
{ |
| 373 |
for (int v = 0 ; v < NumberVecs_ ; ++v) { |
| 374 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 375 |
(*this)(i, v) *= alpha; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
// Scale the i-th vector by alpha[i] |
| 381 |
void MvScale( const std::vector<ScalarType>& alpha ) |
| 382 |
{ |
| 383 |
for (int v = 0 ; v < NumberVecs_ ; ++v) { |
| 384 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 385 |
(*this)(i, v) *= alpha[v]; |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
// Fill the vectors in *this with random numbers. |
| 391 |
void MvRandom () |
| 392 |
{ |
| 393 |
Teuchos::SerialDenseMatrix<int,ScalarType> R( Length_, NumberVecs_ ); |
| 394 |
Teuchos::randomSyncedMatrix( R ); |
| 395 |
for (int v = 0 ; v < NumberVecs_ ; ++v) { |
| 396 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 397 |
(*this)(i, v) = R(i, v); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
// Replace each element of the vectors in *this with alpha. |
| 403 |
void MvInit (ScalarType alpha) |
| 404 |
{ |
| 405 |
for (int v = 0 ; v < NumberVecs_ ; ++v) { |
| 406 |
for (int i = 0 ; i < Length_ ; ++i) { |
| 407 |
(*this)(i, v) = alpha; |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
void MvPrint (std::ostream &os) const |
| 413 |
{ |
| 414 |
os << "Object MyMultiVec" << std::endl; |
| 415 |
os << "Number of rows = " << Length_ << std::endl; |
| 416 |
os << "Number of vecs = " << NumberVecs_ << std::endl; |
| 417 |
|
| 418 |
for (int i = 0 ; i < Length_ ; ++i) |
| 419 |
{ |
| 420 |
for (int v = 0 ; v < NumberVecs_ ; ++v) |
| 421 |
os << (*this)(i, v) << " "; |
| 422 |
os << std::endl; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
inline ScalarType& operator()(const int i, const int j) |
| 427 |
{ |
| 428 |
if (j < 0 || j >= NumberVecs_) throw(-1); |
| 429 |
if (i < 0 || i >= Length_) throw(-2); |
| 430 |
// |
| 431 |
return(data_[j][i]); |
| 432 |
} |
| 433 |
|
| 434 |
inline const ScalarType& operator()(const int i, const int j) const |
| 435 |
{ |
| 436 |
if (j < 0 || j >= NumberVecs_) throw(-1); |
| 437 |
if (i < 0 || i >= Length_) throw(-2); |
| 438 |
// |
| 439 |
return(data_[j][i]); |
| 440 |
} |
| 441 |
|
| 442 |
ScalarType* operator[](int v) |
| 443 |
{ |
| 444 |
if (v < 0 || v >= NumberVecs_) throw(-1); |
| 445 |
return(data_[v]); |
| 446 |
} |
| 447 |
|
| 448 |
ScalarType* operator[](int v) const |
| 449 |
{ |
| 450 |
return(data_[v]); |
| 451 |
} |
| 452 |
|
| 453 |
private: |
| 454 |
void Check() |
| 455 |
{ |
| 456 |
if (Length_ <= 0) |
| 457 |
throw("Length must be positive"); |
| 458 |
|
| 459 |
if (NumberVecs_ <= 0) |
| 460 |
throw("Number of vectors must be positive"); |
| 461 |
} |
| 462 |
|
| 463 |
//! Length of the vectors |
| 464 |
const int Length_; |
| 465 |
//! Number of multi-vectors |
| 466 |
const int NumberVecs_; |
| 467 |
//! Pointers to the storage of the vectors. |
| 468 |
std::vector<ScalarType*> data_; |
| 469 |
//! If \c true, then this object owns the vectors and must free them in dtor. |
| 470 |
std::vector<bool> ownership_; |
| 471 |
|
| 472 |
}; |
| 473 |
|
| 474 |
/*! \relates MyMultiVec |
| 475 |
Output stream operator for handling the printing of MyMultiVec. |
| 476 |
*/ |
| 477 |
template <typename ScalarType> |
| 478 |
std::ostream& operator<<(std::ostream& os, const MyMultiVec<ScalarType>& Obj) |
| 479 |
{ |
| 480 |
Obj.MvPrint(os); |
| 481 |
return os; |
| 482 |
} |
| 483 |
|
| 484 |
#endif // MY_MULTIVECTOR_HPP |