44 #include <Teuchos_GlobalMPISession.hpp>
45 #include <Teuchos_oblackholestream.hpp>
46 #include <Teuchos_CommandLineProcessor.hpp>
47 #include <Teuchos_Tuple.hpp>
48 #include <Teuchos_TimeMonitor.hpp>
50 #include <Kokkos_DefaultNode.hpp>
52 #include "Tpetra_Version.hpp"
53 #include "Tpetra_DefaultPlatform.hpp"
54 #include "Tpetra_MultiVector.hpp"
55 #include "Tpetra_CrsMatrix.hpp"
56 #include "Tpetra_RTIOp.hpp"
67 namespace TpetraExamples {
74 int _myImageID, _numImages;
76 S _sub, _diag, _super;
78 FDStencil(
int myImageID,
int numImages,
int n,
const S &sub,
const S &diag,
const S &super) : _myImageID(myImageID), _numImages(numImages), _n(n), _sub(sub), _diag(diag), _super(super) {}
79 inline void execute(
int i)
const
81 S res = _diag * this->_vec_in2[i];
82 if (i > 0 || _myImageID != 0 ) res += _sub * this->_vec_in2[i-1];
83 if (i < _n || _myImageID != _numImages-1) res += _super * this->_vec_in2[i+1];
84 if (this->_alpha == Teuchos::ScalarTraits<S>::one() && this->_beta == Teuchos::ScalarTraits<S>::zero()) {
85 this->_vec_inout[i] = res;
88 this->_vec_inout[i] = this->_alpha * res + this->_beta * this->_vec_inout[i];
101 inline void execute(
int i) {
102 if (this->_alpha == Teuchos::ScalarTraits<S>::one() && this->_beta == Teuchos::ScalarTraits<S>::zero()) {
103 this->_vec_inout[i] = _gamma * this->_vec_in2[i];
106 this->_vec_inout[i] = this->_alpha * _gamma * this->_vec_in2[i] + this->_beta * this->_vec_inout[i];
113 int main(
int argc,
char *argv[])
115 Teuchos::oblackholestream blackhole;
116 Teuchos::GlobalMPISession mpiSession(&argc,&argv,&blackhole);
118 using Teuchos::TimeMonitor;
126 auto comm = platform.getComm();
127 auto node = platform.getNode();
128 const int myImageID = comm->getRank();
129 const int numImages = comm->getSize();
134 bool verbose = (myImageID==0);
135 int numGlobal_user = 100*comm->getSize();
136 int numTimeTrials = 3;
137 Teuchos::CommandLineProcessor cmdp(
false,
true);
138 cmdp.setOption(
"verbose",
"quiet",&verbose,
"Print messages and results.");
139 cmdp.setOption(
"global-size",&numGlobal_user,
"Global test size.");
140 cmdp.setOption(
"num-time-trials",&numTimeTrials,
"Number of trials in timing loops.");
141 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
149 std::cout <<
"\n" << Tpetra::version() << std::endl;
150 std::cout <<
"Comm info: " << *comm;
151 std::cout <<
"Node type: " << Teuchos::typeName(*node) << std::endl;
152 std::cout << std::endl;
159 auto map = Tpetra::createUniformContigMapWithNode<int,int>(numGlobalRows, comm, node);
161 auto x = Tpetra::createVector<double>(map),
162 y = Tpetra::createVector<double>(map);
165 auto fTwoOp = Tpetra::RTI::binaryOp<double>( [](
double ,
double x) {
return 2.0 * x; } , map );
169 fTwoOp->apply( *x, *y, Teuchos::NO_TRANS, 3.0, 2.0 );
171 double norm = y->norm1();
173 std::cout <<
"Tpetra::RTI::binaryOp" << std::endl
174 <<
"norm(y) result == " << std::setprecision(2) << std::scientific << norm
175 <<
", expected value is " << numGlobalRows * 8.0 << std::endl;
181 kTwoOp->apply( *x, *y, Teuchos::NO_TRANS, 0.5, 0.75 );
185 std::cout <<
"Tpetra::RTI::kernelOp" << std::endl
186 <<
"norm(y) result == " << std::setprecision(2) << std::scientific << norm
187 <<
", expected value is " << numGlobalRows * 7.0 << std::endl;
193 decltype(map) colmap;
195 Teuchos::Array<int> colElements;
196 Teuchos::ArrayView<const int> rowElements = map->getNodeElementList();
198 if (myImageID != 0) colElements.push_back( map->getMinGlobalIndex() - 1 );
199 colElements.insert(colElements.end(), rowElements.begin(), rowElements.end());
200 if (myImageID != numImages-1) colElements.push_back( map->getMaxGlobalIndex() + 1 );
201 colmap = Tpetra::createNonContigMapWithNode<int,int>(colElements(), comm, node);
208 FDStencil<double> kern(myImageID, numImages, map->getNodeNumElements(), -1.0, 2.0, -1.0);
209 auto FDStencilOp = Tpetra::RTI::kernelOp<double>( kern, map, map, importer );
211 auto timeFDStencil = TimeMonitor::getNewTimer(
"FD RTI Stencil");
213 TimeMonitor lcltimer(*timeFDStencil);
214 for (
int l=0; l != numTimeTrials; ++l) {
215 FDStencilOp->apply( *x, *y );
220 std::cout << std::endl
221 <<
"TpetraExamples::FDStencil" << std::endl
222 <<
"norm(y) result == " << std::setprecision(2) << std::scientific << norm
223 <<
", expected value is " << 2.0 << std::endl;
229 auto FDMatrix = Tpetra::createCrsMatrix<double>(map);
230 for (
int r=map->getMinGlobalIndex(); r <= map->getMaxGlobalIndex(); ++r) {
231 if (r == map->getMinAllGlobalIndex()) {
232 FDMatrix->insertGlobalValues(r, Teuchos::tuple<int>(r,r+1), Teuchos::tuple<double>(2.0,-1.0));
234 else if (r == map->getMaxAllGlobalIndex()) {
235 FDMatrix->insertGlobalValues(r, Teuchos::tuple<int>(r-1,r), Teuchos::tuple<double>(-1.0,2.0));
238 FDMatrix->insertGlobalValues(r, Teuchos::tuple<int>(r-1,r,r+1), Teuchos::tuple<double>(-1.0,2.0,-1.0));
241 FDMatrix->fillComplete();
242 auto timeFDMatrix = TimeMonitor::getNewTimer(
"FD CrsMatrix");
244 TimeMonitor lcltimer(*timeFDMatrix);
245 for (
int l=0; l != numTimeTrials; ++l) {
246 FDMatrix->apply(*x, *y);
254 std::cout << std::endl;
255 TimeMonitor::summarize( std::cout );
259 std::cout <<
"\nEnd Result: TEST PASSED" << std::endl;
Utility base class for kernels used to define Tpetra::Operator objects.
Class to demonstrate a simple, finite-different type tridiagonal stencil.
size_t global_size_t
Global size_t object.
Class to demonstrate a simple diagonal matrix.
Class to demonstrate a simple diagonal matrix.
Class to demonstrate a simple, finite-different type tridiagonal stencil.
Teuchos::RCP< const Import< LocalOrdinal, GlobalOrdinal, Node > > createImport(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &src, const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &tgt)
Nonmember constructor for Import.