| 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 |
|
| 10 |
#ifndef __TSQR_Trilinos_Randomizer_hpp |
| 11 |
#define __TSQR_Trilinos_Randomizer_hpp |
| 12 |
|
| 13 |
#include "AnasaziConfigDefs.hpp" |
| 14 |
#include "TsqrTypeAdaptor.hpp" |
| 15 |
#include "TsqrCommFactory.hpp" |
| 16 |
|
| 17 |
#include "Tsqr_ScalarTraits.hpp" |
| 18 |
#include "Tsqr_Random_GlobalMatrix.hpp" |
| 19 |
|
| 20 |
#include <string> |
| 21 |
|
| 22 |
//////////////////////////////////////////////////////////////////////////////// |
| 23 |
//////////////////////////////////////////////////////////////////////////////// |
| 24 |
|
| 25 |
namespace TSQR { |
| 26 |
namespace Trilinos { |
| 27 |
/// \class Randomizer |
| 28 |
/// \brief Generates random test problems for TSQR |
| 29 |
/// |
| 30 |
/// Randomizer knows how to fill in an instance of the given |
| 31 |
/// MultiVector class MV with a (pseudo)random test problem, |
| 32 |
/// using a generator of type Gen. |
| 33 |
/// \li S type of the matrix entries |
| 34 |
/// \li LO local ordinal type |
| 35 |
/// \li GO global ordinal type |
| 36 |
/// \li MV MultiVector type |
| 37 |
/// \li Gen normal(0,1) pseudorandom generator type |
| 38 |
template< class S, class LO, class GO, class MV, class Gen > |
| 39 |
class Randomizer { |
| 40 |
public: |
| 41 |
typedef S scalar_type; |
| 42 |
typedef LO local_ordinal_type; |
| 43 |
typedef GO global_ordinal_type; |
| 44 |
typedef MV multivector_type; |
| 45 |
typedef Gen normalgen_type; |
| 46 |
typedef Teuchos::RCP< Gen > normalgen_ptr; |
| 47 |
typedef TSQR::Random::MatrixGenerator< S, LO, Gen > matgen_type; |
| 48 |
|
| 49 |
typedef typename TSQR::ScalarTraits< S >::magnitude_type magnitude_type; |
| 50 |
|
| 51 |
typedef TsqrTypeAdaptor< S, LO, GO, MV > type_adaptor; |
| 52 |
typedef typename type_adaptor::comm_type comm_type; |
| 53 |
typedef typename type_adaptor::comm_ptr comm_ptr; |
| 54 |
typedef Teuchos::RCP< MessengerBase< LO > > ordinal_messenger_ptr; |
| 55 |
typedef Teuchos::RCP< MessengerBase< S > > scalar_messenger_ptr; |
| 56 |
|
| 57 |
virtual ~Randomizer() {} |
| 58 |
|
| 59 |
/// \brief Fill A with a (pseudo)random (distributed) matrix |
| 60 |
/// |
| 61 |
/// Fill the MultiVector A with a (pseudo)random (distributed) |
| 62 |
/// matrix with the given singular values. Given the same |
| 63 |
/// singular values and the same pseudorandom number sequence |
| 64 |
/// (produced by the Gen object), this function will always |
| 65 |
/// produce the same matrix, no matter the number of processors. |
| 66 |
/// It achieves this at the cost of scalability; only Proc 0 |
| 67 |
/// invokes the pseudorandom number generator. |
| 68 |
virtual void |
| 69 |
randomMultiVector (multivector_type& A, |
| 70 |
const magnitude_type singularValues[]) |
| 71 |
{ |
| 72 |
using TSQR::Random::randomGlobalMatrix; |
| 73 |
using Teuchos::ArrayRCP; |
| 74 |
typedef MatView< local_ordinal_type, scalar_type > matview_type; |
| 75 |
|
| 76 |
local_ordinal_type nrowsLocal, ncols, LDA; |
| 77 |
fetchDims (A, nrowsLocal, ncols, LDA); |
| 78 |
ArrayRCP< scalar_type > A_ptr = fetchNonConstView (A); |
| 79 |
matview_type A_view (nrowsLocal, ncols, A_ptr.get(), LDA); |
| 80 |
|
| 81 |
randomGlobalMatrix (pGen_.get(), A_view, singularValues, |
| 82 |
pOrdinalMessenger_.get(), pScalarMessenger_.get()); |
| 83 |
} |
| 84 |
|
| 85 |
protected: |
| 86 |
/// Like the constructor, except you're not supposed to call the |
| 87 |
/// constructor of a pure virtual class. |
| 88 |
/// |
| 89 |
/// \param mv [in] Only used to extract the underlying |
| 90 |
/// communication object (e.g., Epetra_Comm or |
| 91 |
/// Teuchos::Comm<int>). |
| 92 |
/// \param pGen [in/out] Pointer to generator of pseudorandom |
| 93 |
/// normal(0,1) sequence. |
| 94 |
void |
| 95 |
init (const multivector_type& mv, |
| 96 |
const normalgen_ptr& pGen) |
| 97 |
{ |
| 98 |
pGen_ = pGen; |
| 99 |
// This is done in a multivector type - dependent way. |
| 100 |
fetchMessengers (mv, pScalarMessenger_, pOrdinalMessenger_); |
| 101 |
} |
| 102 |
|
| 103 |
private: |
| 104 |
/// \brief Return dimensions of a multivector object |
| 105 |
/// |
| 106 |
/// For a given multivector A, return the number of rows stored |
| 107 |
/// locally on this process, the number of columns (multivectors |
| 108 |
/// are stored in a block row layout, so all columns of this |
| 109 |
/// process' row block are stored on this process), and the |
| 110 |
/// leading dimension of this process' row block (>= # rows on |
| 111 |
/// this process). |
| 112 |
/// |
| 113 |
/// \param A [in] The multivector object |
| 114 |
/// \param nrowsLocal [out] Number of rows of A stored locally |
| 115 |
/// on this process |
| 116 |
/// \param ncols [out] Number of columns of A |
| 117 |
/// \param LDA [out] Leading dimension of this process' row |
| 118 |
/// block of A |
| 119 |
virtual void |
| 120 |
fetchDims (const multivector_type& A, |
| 121 |
local_ordinal_type& nrowsLocal, |
| 122 |
local_ordinal_type& ncols, |
| 123 |
local_ordinal_type& LDA) const = 0; |
| 124 |
|
| 125 |
/// \return Non-const smart pointer to the node-local data in A |
| 126 |
/// |
| 127 |
/// \note Child classes should implement this in such a way as |
| 128 |
/// to make the above public methods always correct (though not |
| 129 |
/// necessarily efficient) for all multivector types. (It may |
| 130 |
/// not be efficient if the ArrayRCP copies between different |
| 131 |
/// memory spaces.) |
| 132 |
virtual Teuchos::ArrayRCP< scalar_type > |
| 133 |
fetchNonConstView (multivector_type& A) const = 0; |
| 134 |
|
| 135 |
/// Maps from multivector_type object to (scalar_messenger_ptr, |
| 136 |
/// ordinal_messenger_ptr). |
| 137 |
virtual void |
| 138 |
fetchMessengers (const multivector_type& mv, |
| 139 |
scalar_messenger_ptr& pScalarMessenger, |
| 140 |
ordinal_messenger_ptr& pOrdinalMessenger) const = 0; |
| 141 |
|
| 142 |
normalgen_ptr pGen_; |
| 143 |
ordinal_messenger_ptr pOrdinalMessenger_; |
| 144 |
scalar_messenger_ptr pScalarMessenger_; |
| 145 |
}; |
| 146 |
|
| 147 |
} // namespace Trilinos |
| 148 |
} // namespace TSQR |
| 149 |
|
| 150 |
#endif // __TSQR_Trilinos_Randomizer_hpp |