Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
RTIOperatorExample.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Tpetra: Templated Linear Algebra Services Package
6 // Copyright (2008) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 // @HEADER
42 */
43 
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>
49 
50 #include <Kokkos_DefaultNode.hpp>
51 
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"
57 
58 #include <iostream>
59 #include <iomanip>
60 #include <functional>
61 
67 namespace TpetraExamples {
68 
70  template <class S>
72  {
73  protected:
74  int _myImageID, _numImages;
75  int _n;
76  S _sub, _diag, _super;
77  public:
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
80  {
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;
86  }
87  else {
88  this->_vec_inout[i] = this->_alpha * res + this->_beta * this->_vec_inout[i];
89  }
90  }
91  };
92 
94  template <class S>
96  {
97  protected:
98  S _gamma;
99  public:
100  ScaleKernel(const S & gamma) : _gamma(gamma) {}
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];
104  }
105  else {
106  this->_vec_inout[i] = this->_alpha * _gamma * this->_vec_in2[i] + this->_beta * this->_vec_inout[i];
107  }
108  }
109  };
110 
111 }
112 
113 int main(int argc, char *argv[])
114 {
115  Teuchos::oblackholestream blackhole;
116  Teuchos::GlobalMPISession mpiSession(&argc,&argv,&blackhole);
117 
118  using Teuchos::TimeMonitor;
121 
122  //
123  // Get the default communicator and node
124  //
126  auto comm = platform.getComm();
127  auto node = platform.getNode();
128  const int myImageID = comm->getRank();
129  const int numImages = comm->getSize();
130 
131  //
132  // Get example parameters from command-line processor
133  //
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) {
142  return -1;
143  }
144 
145  //
146  // Say hello, print some communicator info
147  //
148  if (verbose) {
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;
153  }
154 
155  //
156  // Create a simple map for domain and range
157  //
158  Tpetra::global_size_t numGlobalRows = numGlobal_user;
159  auto map = Tpetra::createUniformContigMapWithNode<int,int>(numGlobalRows, comm, node);
160  // const size_t numLocalRows = map->getNodeNumElements();
161  auto x = Tpetra::createVector<double>(map),
162  y = Tpetra::createVector<double>(map);
163 
164  // Create a simple diagonal operator using lambda function
165  auto fTwoOp = Tpetra::RTI::binaryOp<double>( [](double /*y*/, double x) { return 2.0 * x; } , map );
166  // y = 3*fTwoOp*x + 2*y = 3*2*1 + 2*1 = 8
167  x->putScalar(1.0);
168  y->putScalar(1.0);
169  fTwoOp->apply( *x, *y, Teuchos::NO_TRANS, 3.0, 2.0 );
170  // check that y == eights
171  double norm = y->norm1();
172  if (verbose) {
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;
176  }
177 
178  // Create the same diagonal operator using a Kokkos kernel
179  auto kTwoOp = Tpetra::RTI::kernelOp<double>( ScaleKernel<double>(2.0), map );
180  // y = 0.5*kTwop*x + 0.75*y = 0.5*2*1 + 0.75*8 = 7
181  kTwoOp->apply( *x, *y, Teuchos::NO_TRANS, 0.5, 0.75 );
182  // check that y == sevens
183  norm = y->norm1();
184  if (verbose) {
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;
188  }
189 
190  //
191  // Create a finite-difference stencil using a Kokkos kernel and non-trivial maps
192  //
193  decltype(map) colmap;
194  if (numImages > 1) {
195  Teuchos::Array<int> colElements;
196  Teuchos::ArrayView<const int> rowElements = map->getNodeElementList();
197  // This isn't the most efficient Map/Import layout, but it makes for a very straight-forward kernel
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);
202  }
203  else {
204  colmap = map;
205  }
206  auto importer = createImport(map,colmap);
207  // Finite-difference kernel = tridiag(-1, 2, -1)
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 );
210  // x = ones(), FD(x) = [1 zeros() 1]
211  auto timeFDStencil = TimeMonitor::getNewTimer("FD RTI Stencil");
212  {
213  TimeMonitor lcltimer(*timeFDStencil);
214  for (int l=0; l != numTimeTrials; ++l) {
215  FDStencilOp->apply( *x, *y );
216  }
217  }
218  norm = y->norm1();
219  if (verbose) {
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;
224  }
225 
226  //
227  // Create a finite-difference stencil using a CrsMatrix
228  //
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));
233  }
234  else if (r == map->getMaxAllGlobalIndex()) {
235  FDMatrix->insertGlobalValues(r, Teuchos::tuple<int>(r-1,r), Teuchos::tuple<double>(-1.0,2.0));
236  }
237  else {
238  FDMatrix->insertGlobalValues(r, Teuchos::tuple<int>(r-1,r,r+1), Teuchos::tuple<double>(-1.0,2.0,-1.0));
239  }
240  }
241  FDMatrix->fillComplete();
242  auto timeFDMatrix = TimeMonitor::getNewTimer("FD CrsMatrix");
243  {
244  TimeMonitor lcltimer(*timeFDMatrix);
245  for (int l=0; l != numTimeTrials; ++l) {
246  FDMatrix->apply(*x, *y);
247  }
248  }
249 
250  //
251  // Print timings
252  //
253  if (verbose) {
254  std::cout << std::endl;
255  TimeMonitor::summarize( std::cout );
256  }
257 
258  if (verbose) {
259  std::cout << "\nEnd Result: TEST PASSED" << std::endl;
260  }
261  return 0;
262 }
263 
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.
static DefaultPlatformType & getDefaultPlatform()
Return the default platform.
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.