Tpetra Matrix/Vector Services  Version of the Day
RTITutorialOperator.cpp
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 
45 #include <Teuchos_oblackholestream.hpp>
46 
47 #include <Kokkos_DefaultNode.hpp>
48 #include <Tpetra_DefaultPlatform.hpp>
49 #include <Tpetra_Version.hpp>
50 #include <Tpetra_Vector.hpp>
51 #include <Tpetra_RTIOp.hpp>
52 
53 #include <iostream>
54 #include <iomanip>
55 
57 template <class S>
59 {
60  protected:
61  int _myImageID, _numImages;
62  int _n;
63  S _sub, _diag, _super;
64  public:
65  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) {}
66  inline void execute(int i) const
67  {
68  S res = _diag * this->_vec_in2[i];
69  if (i > 0 || _myImageID != 0 ) res += _sub * this->_vec_in2[i-1];
70  if (i < _n || _myImageID != _numImages-1) res += _super * this->_vec_in2[i+1];
71  if (this->_alpha == Teuchos::ScalarTraits<S>::one() && this->_beta == Teuchos::ScalarTraits<S>::zero()) {
72  this->_vec_inout[i] = res;
73  }
74  else {
75  this->_vec_inout[i] = this->_alpha * res + this->_beta * this->_vec_inout[i];
76  }
77  }
78 };
79 
81 template <class S>
83 {
84  protected:
85  S _gamma;
86  public:
87  ScaleKernel(const S & gamma) : _gamma(gamma) {}
88  inline void execute(int i) {
89  if (this->_alpha == Teuchos::ScalarTraits<S>::one() && this->_beta == Teuchos::ScalarTraits<S>::zero()) {
90  this->_vec_inout[i] = _gamma * this->_vec_in2[i];
91  }
92  else {
93  this->_vec_inout[i] = this->_alpha * _gamma * this->_vec_in2[i] + this->_beta * this->_vec_inout[i];
94  }
95  }
96 };
97 
98 int main(int argc, char *argv[])
99 {
100  Teuchos::oblackholestream blackhole;
101  Teuchos::GlobalMPISession mpiSession(&argc,&argv,&blackhole);
102 
103  //
104  // Get the default communicator and node
105  //
107  auto comm = platform.getComm();
108  auto node = platform.getNode();
109  const int myImageID = comm->getRank();
110  const int numImages = comm->getSize();
111  const bool verbose = (myImageID==0);
112 
113  //
114  // Say hello, print some communicator info
115  //
116  if (verbose) {
117  std::cout << "\n" << Tpetra::version() << std::endl;
118  std::cout << "Comm info: " << *comm;
119  std::cout << "Node type: " << Teuchos::typeName(*node) << std::endl;
120  std::cout << std::endl;
121  }
122 
123  //
124  // Create a simple map for domain and range
125  //
126  Tpetra::global_size_t numGlobalRows = 1000*numImages;
127  auto map = Tpetra::createUniformContigMapWithNode<int,int>(numGlobalRows, comm, node);
128  auto x = Tpetra::createVector<double>(map),
129  y = Tpetra::createVector<double>(map);
130 
131  // Create a simple diagonal operator using lambda function
132  auto fTwoOp = Tpetra::RTI::binaryOp<double>( [](double /*y*/, double x) { return 2.0 * x; } , map );
133  // y = 3*fTwoOp*x + 2*y = 3*2*1 + 2*1 = 8
134  x->putScalar(1.0);
135  y->putScalar(1.0);
136  fTwoOp->apply( *x, *y, Teuchos::NO_TRANS, 3.0, 2.0 );
137  // check that y == eights
138  double norm = y->norm1();
139  if (verbose) {
140  std::cout << "Tpetra::RTI::binaryOp" << std::endl
141  << "norm(y) result == " << std::setprecision(2) << std::scientific << norm
142  << ", expected value is " << numGlobalRows * 8.0 << std::endl;
143  }
144 
145  //
146  // Create a finite-difference stencil using a Kokkos kernel and non-trivial maps
147  //
148  decltype(map) colmap;
149  if (numImages > 1) {
150  Teuchos::Array<int> colElements;
151  Teuchos::ArrayView<const int> rowElements = map->getNodeElementList();
152  // This isn't the most efficient Map/Import layout, but it makes for a very straight-forward kernel
153  if (myImageID != 0) colElements.push_back( map->getMinGlobalIndex() - 1 );
154  colElements.insert(colElements.end(), rowElements.begin(), rowElements.end());
155  if (myImageID != numImages-1) colElements.push_back( map->getMaxGlobalIndex() + 1 );
156  colmap = Tpetra::createNonContigMapWithNode<int,int>(colElements(), comm, node);
157  }
158  else {
159  colmap = map;
160  }
161  auto importer = createImport(map,colmap);
162  // Finite-difference kernel = tridiag(-1, 2, -1)
163  FDStencil<double> kern(myImageID, numImages, map->getNodeNumElements(), -1.0, 2.0, -1.0);
164  auto FDStencilOp = Tpetra::RTI::kernelOp<double>( kern, map, map, importer );
165  // x = ones(), FD(x) = [1 zeros() 1]
166  FDStencilOp->apply( *x, *y );
167  norm = y->norm1();
168  if (verbose) {
169  std::cout << std::endl
170  << "TpetraExamples::FDStencil" << std::endl
171  << "norm(y) result == " << std::setprecision(2) << std::scientific << norm
172  << ", expected value is " << 2.0 << std::endl;
173  }
174 
175  std::cout << "\nEnd Result: TEST PASSED" << std::endl;
176  return 0;
177 }
Utility base class for kernels used to define Tpetra::Operator objects.
iterator begin() const
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.
iterator end()
void push_back(const value_type &x)
iterator end() const
static DefaultPlatformType & getDefaultPlatform()
Return the default platform.
iterator insert(iterator position, const value_type &x)
std::string typeName(const T &t)