Demonstrate building a simple sparse matrix and computing its leading eigenvalue using the TpetraExamples::powerMethod() function.
#include <Teuchos_GlobalMPISession.hpp>
#include <Teuchos_oblackholestream.hpp>
#include <Teuchos_CommandLineProcessor.hpp>
#include <Teuchos_Array.hpp>
#include "Tpetra_Power_Method.hpp"
#include "Tpetra_DefaultPlatform.hpp"
#include "Tpetra_Version.hpp"
#include "Tpetra_Map.hpp"
#include "Tpetra_MultiVector.hpp"
#include "Tpetra_Vector.hpp"
#include "Tpetra_CrsMatrix.hpp"
#include <algorithm>
#include <functional>
int main(int argc, char *argv[]) {
Teuchos::oblackholestream blackhole;
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &blackhole);
typedef double Scalar;
typedef Teuchos::ScalarTraits<Scalar>::magnitudeType Magnitude;
typedef int Ordinal;
using Teuchos::Array;
using Teuchos::ArrayView;
using Teuchos::as;
using Teuchos::getFancyOStream;
using Teuchos::RCP;
using Teuchos::rcpFromRef;
using Teuchos::tuple;
using std::cout;
using std::endl;
typedef Array<Scalar>::size_type size_type;
RCP<const Teuchos::Comm<int> > comm = platform.getComm();
RCP<Node> node = platform.getNode();
const int myRank = comm->getRank();
bool printMatrix = false;
bool verbose = (myRank==0);
int niters = 100;
int numGlobalElements = 100;
Magnitude tolerance = 1.0e-2;
Teuchos::CommandLineProcessor cmdp (false, true);
cmdp.setOption ("verbose", "quiet", &verbose, "Print messages and results.");
cmdp.setOption ("numGlobalElements", &numGlobalElements,
"Global problem size.");
cmdp.setOption ("tolerance", &tolerance,
"Relative residual tolerance used for solver.");
cmdp.setOption ("iterations", &niters, "Maximum number of iterations.");
cmdp.setOption ("printMatrix", "noPrintMatrix", &printMatrix,
"Print the full matrix after reading it.");
if (cmdp.parse (argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
return -1;
}
if (verbose) {
cout << endl << Tpetra::version() << endl << endl;
}
cout << "Comm info: " << *comm;
RCP<const Map> map = Tpetra::createUniformContigMap<Ordinal,Ordinal> (numGlobalElements, comm);
const size_type numMyElements = as<size_type> (map->getNodeNumElements ());
ArrayView<const Ordinal> myGlobalElements = map->getNodeElementList ();
RCP<CrsMatrix> A = Tpetra::createCrsMatrix<Scalar> (map, 3);
for (size_type i = 0; i < numMyElements; ++i) {
if (myGlobalElements[i] == 0) {
A->insertGlobalValues (myGlobalElements[i],
tuple<Ordinal> (myGlobalElements[i], myGlobalElements[i]+1),
tuple<Scalar> (2.0, -1.0));
}
else if (myGlobalElements[i] == numGlobalElements-1) {
A->insertGlobalValues (myGlobalElements[i],
tuple<Ordinal> (myGlobalElements[i]-1, myGlobalElements[i]),
tuple<Scalar> (-1.0, 2.0));
}
else {
A->insertGlobalValues (myGlobalElements[i],
tuple<Ordinal> (myGlobalElements[i]-1,
myGlobalElements[i],
myGlobalElements[i]+1),
tuple<Scalar> (-1.0, 2.0, -1.0));
}
}
A->fillComplete ();
if (printMatrix) {
RCP<Teuchos::FancyOStream> fos = fancyOStream (rcpFromRef (cout));
A->describe (*fos, Teuchos::VERB_EXTREME);
}
else if (verbose) {
cout << endl << A->description() << endl << endl;
}
TpetraExamples::powerMethod<Scalar,Ordinal> (A, niters, tolerance, verbose);
if (verbose) {
cout << endl << "Increasing magnitude of first diagonal term, solving again"
<< endl << endl;
}
A->resumeFill();
if (A->getRowMap ()->isNodeGlobalElement (0)) {
size_t numVals = A->getNumEntriesInGlobalRow (0);
Array<Scalar> rowVals (as<size_type> (numVals));
Array<Ordinal> rowInds (as<size_type> (numVals));
A->getGlobalRowCopy (0, rowInds (), rowVals (), numVals);
for (size_type k = 0; k < as<size_type> (numVals); ++k) {
if (rowInds[k] == 0) {
rowVals[k] = as<Scalar> (10.0);
}
}
A->replaceGlobalValues (0, rowInds (), rowVals ());
}
A->fillComplete ();
TpetraExamples::powerMethod<Scalar,Ordinal> (A, niters, tolerance, verbose);
if (verbose) {
cout << endl << "End Result: TEST PASSED" << endl;
}
return 0;
}