Tpetra Matrix/Vector Services  Version of the Day
Tpetra_Power_Method.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>
47 #include <Teuchos_Array.hpp>
48 
49 #include "Tpetra_Power_Method.hpp"
50 
51 #include "Tpetra_DefaultPlatform.hpp"
52 #include "Tpetra_Version.hpp"
53 #include "Tpetra_Map.hpp"
54 #include "Tpetra_MultiVector.hpp"
55 #include "Tpetra_Vector.hpp"
56 #include "Tpetra_CrsMatrix.hpp"
57 
58 #include <algorithm>
59 #include <functional>
60 
61 int main(int argc, char *argv[]) {
62  Teuchos::oblackholestream blackhole;
63  Teuchos::GlobalMPISession mpiSession (&argc, &argv, &blackhole);
64 
65  //
66  // Specify types used in this example
67  //
68  typedef double Scalar;
70  typedef int Ordinal;
75  using Teuchos::Array;
76  using Teuchos::ArrayView;
77  using Teuchos::as;
78  using Teuchos::getFancyOStream;
79  using Teuchos::RCP;
80  using Teuchos::rcpFromRef;
81  using Teuchos::tuple;
82  using std::cout;
83  using std::endl;
84  typedef Array<Scalar>::size_type size_type;
85 
86  //
87  // Get the default communicator and node
88  //
89  Platform &platform = Tpetra::DefaultPlatform::getDefaultPlatform();
90  RCP<const Teuchos::Comm<int> > comm = platform.getComm();
91  RCP<Node> node = platform.getNode();
92  const int myRank = comm->getRank();
93 
94  //
95  // Get example parameters from command-line processor
96  //
97  bool printMatrix = false;
98  bool verbose = (myRank==0);
99  int niters = 100;
100  int numGlobalElements = 100;
101  Magnitude tolerance = 1.0e-2;
102  Teuchos::CommandLineProcessor cmdp (false, true);
103  cmdp.setOption ("verbose", "quiet", &verbose, "Print messages and results.");
104  cmdp.setOption ("numGlobalElements", &numGlobalElements,
105  "Global problem size.");
106  cmdp.setOption ("tolerance", &tolerance,
107  "Relative residual tolerance used for solver.");
108  cmdp.setOption ("iterations", &niters, "Maximum number of iterations.");
109  cmdp.setOption ("printMatrix", "noPrintMatrix", &printMatrix,
110  "Print the full matrix after reading it.");
111  if (cmdp.parse (argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
112  return -1;
113  }
114 
115  //
116  // Say hello, print some communicator info
117  //
118  if (verbose) {
119  cout << endl << Tpetra::version() << endl << endl;
120  }
121  cout << "Comm info: " << *comm;
122 
123  //
124  // Construct the problem
125  //
126  // Construct a Map that puts approximately the same number of equations on each processor.
127  RCP<const Map> map = Tpetra::createUniformContigMap<Ordinal,Ordinal> (numGlobalElements, comm);
128  // Get update list and number of local equations from newly created map.
129  const size_type numMyElements = as<size_type> (map->getNodeNumElements ());
130  ArrayView<const Ordinal> myGlobalElements = map->getNodeElementList ();
131  // Create a CrsMatrix using the map, with a dynamic allocation of 3 entries per row.
132  RCP<CrsMatrix> A = Tpetra::createCrsMatrix<Scalar> (map, 3);
133  // Add rows one at a time.
134  for (size_type i = 0; i < numMyElements; ++i) {
135  // Teuchos::tuple<T> constructs an in-line fixed-length array with
136  // entries of type T. It's convenient for examples like this.
137  if (myGlobalElements[i] == 0) {
138  A->insertGlobalValues (myGlobalElements[i],
139  tuple<Ordinal> (myGlobalElements[i], myGlobalElements[i]+1),
140  tuple<Scalar> (2.0, -1.0));
141  }
142  else if (myGlobalElements[i] == numGlobalElements-1) {
143  A->insertGlobalValues (myGlobalElements[i],
144  tuple<Ordinal> (myGlobalElements[i]-1, myGlobalElements[i]),
145  tuple<Scalar> (-1.0, 2.0));
146  }
147  else {
148  A->insertGlobalValues (myGlobalElements[i],
149  tuple<Ordinal> (myGlobalElements[i]-1,
150  myGlobalElements[i],
151  myGlobalElements[i]+1),
152  tuple<Scalar> (-1.0, 2.0, -1.0));
153  }
154  }
155  // Complete the fill. Note that if the row Map, domain Map, and
156  // range Map are not all the same, you will need to supply the
157  // domain and range Maps to fillComplete().
158  A->fillComplete ();
159  if (printMatrix) {
160  RCP<Teuchos::FancyOStream> fos = fancyOStream (rcpFromRef (cout));
161  A->describe (*fos, Teuchos::VERB_EXTREME);
162  }
163  else if (verbose) {
164  cout << endl << A->description() << endl << endl;
165  }
166 
167  //
168  // Iterate
169  //
170  TpetraExamples::powerMethod<Scalar,Ordinal> (A, niters, tolerance, verbose);
171 
172  // Increase diagonal dominance
173  if (verbose) {
174  cout << endl << "Increasing magnitude of first diagonal term, solving again"
175  << endl << endl;
176  }
177 
178  A->resumeFill();
179 
180  // Get a copy of the row with with global index 0. Modify the
181  // diagonal entry of that row, and submit the modified values to the
182  // matrix. This code assumes that row 0 is uniquely owned.
183  if (A->getRowMap ()->isNodeGlobalElement (0)) {
184  // Row 0 belongs to the calling process. Get the number of
185  // entries in row 0 on my process. Teuchos::as casts between
186  // types like static_cast, but also checks in a debug build
187  // whether the cast will overflow.
188  size_t numVals = A->getNumEntriesInGlobalRow (0);
189  Array<Scalar> rowVals (as<size_type> (numVals));
190  Array<Ordinal> rowInds (as<size_type> (numVals));
191  // The parentheses indicate that we are passing in a view of the
192  // array. The callee can't modify the length of the view. If we
193  // didn't supply enough space, the method will throw an exception.
194  // That's why we ask above for the number of entries in the row.
195  // numVals on output is set to the number of entries in the row,
196  // which is why it was declared nonconst above.
197  A->getGlobalRowCopy (0, rowInds (), rowVals (), numVals);
198  // Replace the diagonal entry with 10.0.
199  for (size_type k = 0; k < as<size_type> (numVals); ++k) {
200  if (rowInds[k] == 0) {
201  rowVals[k] = as<Scalar> (10.0);
202  }
203  }
204  A->replaceGlobalValues (0, rowInds (), rowVals ());
205  }
206  // This call assumes that the row Map, domain Map, and range Map are
207  // all the same. If this is not the case, you should supply the
208  // domain Map and range Map to fillComplete().
209  A->fillComplete ();
210 
211  // Iterate again
212  TpetraExamples::powerMethod<Scalar,Ordinal> (A, niters, tolerance, verbose);
213 
214  if (verbose) {
215  cout << endl << "End Result: TEST PASSED" << endl;
216  }
217  return 0;
218 }
Sparse matrix that presents a compressed sparse row interface.
Kokkos::DefaultNode::DefaultNodeType NodeType
Kokkos Node type; the template parameter of this class.
SerialPlatform specialization for Kokkos::DefaultNode::DefaultNodeType.
Describes a parallel distribution of objects over processes.
TypeTo as(const TypeFrom &t)
static DefaultPlatformType & getDefaultPlatform()
Return the default platform.