| 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 |
// This driver reads a matrix from a file, uses the corresponding map to |
| 11 |
// create a set of random multivectors, and then orthogonalizes the |
| 12 |
// multivectors. The orthogonalization function at the end of this file |
| 13 |
// can be included in other codes to orthogonalize an arbitrary |
| 14 |
// Tpetra::MultiVector. |
| 15 |
// |
| 16 |
// |
| 17 |
#include "BelosConfigDefs.hpp" |
| 18 |
#include "BelosOutputManager.hpp" |
| 19 |
|
| 20 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 21 |
#include "Teuchos_ParameterList.hpp" |
| 22 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 23 |
#include "Teuchos_StackedTimer.hpp" |
| 24 |
|
| 25 |
#include "BelosTpetraAdapter.hpp" |
| 26 |
#include "BelosMultiVecTraits_Tpetra.hpp" |
| 27 |
#include "BelosTpetraOperator.hpp" |
| 28 |
#include "Tpetra_Core.hpp" |
| 29 |
#include "Tpetra_CrsMatrix.hpp" |
| 30 |
#include "MatrixMarket_Tpetra.hpp" |
| 31 |
#include "BelosOrthoManagerFactory.hpp" |
| 32 |
#include "BelosOrthoManager.hpp" |
| 33 |
|
| 34 |
using Teuchos::RCP; |
| 35 |
using Teuchos::rcp; |
| 36 |
using Teuchos::Array; |
| 37 |
|
| 38 |
//Function Forward Declaration: |
| 39 |
template <class ScalarType> |
| 40 |
int orthogTpMVecs(Tpetra::MultiVector<ScalarType> & inputVecs, RCP<Teuchos::SerialDenseMatrix<int,ScalarType>> & coeffs, std::string orthogType, int blkSize); |
| 41 |
|
| 42 |
int main(int argc, char *argv[]) { |
| 43 |
Tpetra::ScopeGuard tpetraScope(&argc,&argv); |
| 44 |
{ |
| 45 |
typedef double ScalarType; |
| 46 |
typedef int OT; |
| 47 |
typedef Tpetra::MultiVector<ScalarType> MV; |
| 48 |
typedef Tpetra::MultiVector<ScalarType>::mag_type MT; |
| 49 |
typedef Belos::MultiVecTraits<ScalarType,MV> MVT; |
| 50 |
typedef Teuchos::SerialDenseMatrix<OT,ScalarType> MAT; |
| 51 |
|
| 52 |
RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 53 |
int MyPID = Teuchos::rank(*comm); |
| 54 |
|
| 55 |
bool verbose = true; |
| 56 |
int blockSize = 4; |
| 57 |
int numVecs = 10; |
| 58 |
int vecLength = 100; |
| 59 |
std::string orthoType("ICGS"); |
| 60 |
bool use_stacked_timer = false; |
| 61 |
|
| 62 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 63 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 64 |
cmdp.setOption("ortho", &orthoType, "Type of orthogonalization: ICGS, IMGS, DGKS."); |
| 65 |
cmdp.setOption("blkSize",&blockSize,"Number of vectors to orthogonalize at each step. "); |
| 66 |
cmdp.setOption("numVecs",&numVecs,"Total number of vectors for tester to orthogonalize."); |
| 67 |
cmdp.setOption("vecLength",&vecLength,"Length of vectors to be orthogonalized (num elements)."); |
| 68 |
cmdp.setOption("stacked-timer", "no-stacked-timer", &use_stacked_timer, "Run with or without stacked timer output"); |
| 69 |
|
| 70 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 71 |
return -1; |
| 72 |
} |
| 73 |
bool proc_verbose = ( verbose && (MyPID==0) ); /* Only print on the zero processor */ |
| 74 |
|
| 75 |
// Create the timer. |
| 76 |
RCP<std::ostream> outputStream = rcp(&std::cout,false); |
| 77 |
RCP<Belos::OutputManager<ScalarType> > printer_ = rcp( new Belos::OutputManager<ScalarType>(Belos::TimingDetails,outputStream) ); |
| 78 |
std::string OrthoLabel = "Total Orthog time:"; |
| 79 |
#ifdef BELOS_TEUCHOS_TIME_MONITOR |
| 80 |
RCP<Teuchos::Time> timerOrtho_ = Teuchos::TimeMonitor::getNewCounter(OrthoLabel); |
| 81 |
#endif |
| 82 |
|
| 83 |
// Set output stream and stacked timer: |
| 84 |
RCP<Teuchos::FancyOStream> fancy = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); |
| 85 |
Teuchos::FancyOStream& out = *fancy; |
| 86 |
out.setOutputToRootOnly(0); |
| 87 |
// Set up timers |
| 88 |
Teuchos::RCP<Teuchos::StackedTimer> stacked_timer; |
| 89 |
if (use_stacked_timer){ |
| 90 |
stacked_timer = rcp(new Teuchos::StackedTimer("Main")); |
| 91 |
} |
| 92 |
Teuchos::TimeMonitor::setStackedTimer(stacked_timer); |
| 93 |
|
| 94 |
// Create map and random multivec to orthogonalize. |
| 95 |
RCP<const Tpetra::Map<> > map = rcp (new Tpetra::Map<> (vecLength,0,comm)); |
| 96 |
|
| 97 |
RCP<Tpetra::MultiVector<ScalarType>> X1 = rcp( new Tpetra::MultiVector<ScalarType>(map, numVecs) ); |
| 98 |
X1->randomize(); |
| 99 |
RCP<Tpetra::MultiVector<ScalarType>> XCopy = rcp(new Tpetra::MultiVector<ScalarType>(*X1, Teuchos::Copy)); //Deep copy of X1. |
| 100 |
|
| 101 |
// Orthogonalize multivec. |
| 102 |
RCP<MAT> coeffMat = rcp(new MAT()); |
| 103 |
int rank = -1; |
| 104 |
{ //scope guard for timer |
| 105 |
#ifdef BELOS_TEUCHOS_TIME_MONITOR |
| 106 |
Teuchos::TimeMonitor orthotimer(*timerOrtho_); |
| 107 |
#endif |
| 108 |
rank = orthogTpMVecs(*X1, coeffMat, orthoType, blockSize); |
| 109 |
} |
| 110 |
if(proc_verbose){ |
| 111 |
std::cout << std::endl << "We have orthogonalized " << numVecs << " vectors. Numerical rank is " << rank << "." << std::endl; |
| 112 |
} |
| 113 |
|
| 114 |
//Verify Orthogonality: |
| 115 |
RCP<MAT> bigDotAns = rcp( new MAT(numVecs, numVecs)); |
| 116 |
MVT::MvTransMv(1.0, *X1, *X1, *bigDotAns); |
| 117 |
if(proc_verbose){ |
| 118 |
std::cout << std::endl << "Printed dot prod matrix for verification: " << std::endl; |
| 119 |
std::cout << "Should be ones on diagonal and zeros elsewhere." << std::endl; |
| 120 |
bigDotAns->print(std::cout); |
| 121 |
std::cout << std::endl << std::endl; |
| 122 |
} |
| 123 |
|
| 124 |
//Verify coefficients: |
| 125 |
MV coeffs_mv = impl::makeStaticLocalMultiVector (*X1, numVecs, numVecs); |
| 126 |
Tpetra::deep_copy(coeffs_mv, *coeffMat); |
| 127 |
XCopy->multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, *X1, coeffs_mv, -1.0); |
| 128 |
std::vector<MT> norms(numVecs); |
| 129 |
Teuchos::ArrayView<MT> normView(norms); |
| 130 |
XCopy->norm2(normView); |
| 131 |
if(proc_verbose){ |
| 132 |
std::cout << "Here are the QR minus OrigMatrix norms. Should be zero." << std::endl; |
| 133 |
std::cout << normView << std::endl << std::endl; |
| 134 |
} |
| 135 |
|
| 136 |
if(proc_verbose){ |
| 137 |
//Print final timing details: |
| 138 |
Teuchos::TimeMonitor::summarize( printer_->stream(Belos::TimingDetails) ); |
| 139 |
|
| 140 |
if (use_stacked_timer) { |
| 141 |
stacked_timer->stop("Main"); |
| 142 |
Teuchos::StackedTimer::OutputOptions options; |
| 143 |
options.output_fraction = options.output_histogram = options.output_minmax = true; |
| 144 |
stacked_timer->report(out, comm, options); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
}// End Tpetra Scope Guard |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
// This function orthogonalizes the given MultiVector using |
| 154 |
// orthogonalization type 'orthogType' and blocks of size |
| 155 |
// blkSize. It returns the corresponding upper-triangular |
| 156 |
// matrix of orthogonalization coefficients. |
| 157 |
template <class ScalarType> |
| 158 |
int orthogTpMVecs(Tpetra::MultiVector<ScalarType> & inputVecs, RCP<Teuchos::SerialDenseMatrix<int,ScalarType>> & coeffs, std::string orthogType, int blkSize){ |
| 159 |
typedef int OT; |
| 160 |
typedef typename Teuchos::SerialDenseMatrix<OT,ScalarType> MAT; |
| 161 |
typedef Tpetra::MultiVector<ScalarType> MV; |
| 162 |
typedef Tpetra::Operator<ScalarType> OP; |
| 163 |
int numVecs = inputVecs.getNumVectors(); |
| 164 |
|
| 165 |
//Default OutputManager is std::cout. |
| 166 |
Teuchos::RCP<Belos::OutputManager<ScalarType> > myOutputMgr = Teuchos::rcp( new Belos::OutputManager<ScalarType>() ); |
| 167 |
|
| 168 |
//Check that block size is not bigger than total num vecs. |
| 169 |
if(blkSize > numVecs){ |
| 170 |
blkSize = numVecs; |
| 171 |
} |
| 172 |
int numLoops = numVecs/blkSize; |
| 173 |
int remainder = numVecs % blkSize; |
| 174 |
|
| 175 |
coeffs->shape(numVecs,numVecs); |
| 176 |
RCP<MAT> coeffDot; |
| 177 |
std::vector<RCP<const MV>> pastVecArray; // Stores vectors after orthogonalized |
| 178 |
Teuchos::ArrayView<RCP<const MV>> pastVecArrayView; // To hold the above |
| 179 |
|
| 180 |
// Create the orthogonalization manager: |
| 181 |
Belos::OrthoManagerFactory<ScalarType, MV, OP> factory; |
| 182 |
Teuchos::RCP<Teuchos::ParameterList> paramsOrtho; // can be null |
| 183 |
const Teuchos::RCP<Belos::OrthoManager<ScalarType,MV>> orthoMgr = |
| 184 |
factory.makeOrthoManager (orthogType, Teuchos::null, myOutputMgr, "Tpetra OrthoMgr", paramsOrtho); |
| 185 |
|
| 186 |
// Get a view of the first block and normalize: (also orthogonalizes these vecs wrt themselves) |
| 187 |
RCP<MV> vecBlock = inputVecs.subViewNonConst(Teuchos::Range1D(0,blkSize-1)); |
| 188 |
RCP<MAT> coeffNorm = Teuchos::rcp( new MAT(Teuchos::View, *coeffs, blkSize, blkSize)); |
| 189 |
RCP<MV> pastVecBlock; |
| 190 |
int rank = orthoMgr->normalize(*vecBlock, coeffNorm); |
| 191 |
pastVecArray.push_back(vecBlock); |
| 192 |
|
| 193 |
// Loop over remaining blocks: |
| 194 |
for(int k=1; k<numLoops; k++){ |
| 195 |
pastVecArrayView = arrayViewFromVector(pastVecArray); |
| 196 |
vecBlock = inputVecs.subViewNonConst(Teuchos::Range1D(k*blkSize,k*blkSize + blkSize - 1)); |
| 197 |
coeffNorm = Teuchos::rcp( new MAT(Teuchos::View, *coeffs, blkSize, blkSize, k*blkSize, k*blkSize)); |
| 198 |
coeffDot = Teuchos::rcp(new MAT(Teuchos::View, *coeffs, k*blkSize, blkSize, 0, k*blkSize)); |
| 199 |
Array<RCP<MAT>> dotArray; //Tuechos::Array of Matrices for coeffs from dot products |
| 200 |
dotArray.append(coeffDot); |
| 201 |
rank += orthoMgr->projectAndNormalize(*vecBlock, dotArray, coeffNorm, pastVecArrayView); |
| 202 |
pastVecBlock = inputVecs.subViewNonConst(Teuchos::Range1D(0,k*blkSize + blkSize - 1)); |
| 203 |
pastVecArray.front() = pastVecBlock; |
| 204 |
} |
| 205 |
if( remainder > 0){ |
| 206 |
pastVecArrayView = arrayViewFromVector(pastVecArray); |
| 207 |
vecBlock = inputVecs.subViewNonConst(Teuchos::Range1D(numVecs-remainder, numVecs-1)); |
| 208 |
coeffNorm = Teuchos::rcp( new MAT(Teuchos::View, *coeffs, remainder, remainder, numLoops*blkSize, numLoops*blkSize)); |
| 209 |
coeffDot = Teuchos::rcp(new MAT(Teuchos::View, *coeffs, numLoops*blkSize, remainder, 0, numLoops*blkSize)); |
| 210 |
Array<RCP<MAT>> dotArray; //Tuechos::Array of Matrices for coeffs from dot products |
| 211 |
dotArray.append(coeffDot); |
| 212 |
rank += orthoMgr->projectAndNormalize(*vecBlock, dotArray, coeffNorm, pastVecArrayView); |
| 213 |
} |
| 214 |
|
| 215 |
return rank; |
| 216 |
} |