Tpetra Matrix/Vector Services  Version of the Day
Tpetra_Power_Method_VbrMatrix.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #include <Teuchos_Array.hpp>
43 #include <Teuchos_ScalarTraits.hpp>
45 #include <Teuchos_RCP.hpp>
47 #include <Teuchos_oblackholestream.hpp>
48 
49 #include "Tpetra_DefaultPlatform.hpp"
50 #include "Tpetra_Version.hpp"
51 #include "Tpetra_BlockMap.hpp"
52 #include "Tpetra_BlockMultiVector.hpp"
53 #include "Tpetra_Vector.hpp"
54 #include "Tpetra_VbrMatrix.hpp"
55 
56 
57 // prototype
58 template <class Scalar, class Ordinal>
59 Scalar power_method(const Teuchos::RCP<const Tpetra::Operator<Scalar,Ordinal> > &A, size_t niters, typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance, bool verbose);
60 
61 
62 int main(int argc, char *argv[]) {
63  Teuchos::oblackholestream blackhole;
64  Teuchos::GlobalMPISession mpiSession(&argc,&argv,&blackhole);
65  typedef double Scalar;
66  typedef int Ordinal;
68 
70 
71  size_t myRank = comm->getRank();
72  size_t numProc = comm->getSize();
73  bool verbose = (myRank==0);
74 
75  if (verbose) {
76  std::cout << Tpetra::version() << std::endl << std::endl;
77  }
78  std::cout << *comm;
79 
80  // Get the number of global equations from the command line
81  if (argc != 2) {
82  if (verbose) {
83  std::cout << "Usage: " << argv[0] << " number_of_equations" << std::endl;
84  }
85  return -1;
86  }
87  const int blockSize = 3;
88  global_size_t numGlobalElements = std::atoi(argv[1]);
89 
90  //make sure numGlobalElements is an integer multiple of blockSize.
91  numGlobalElements += blockSize - numGlobalElements%blockSize;
92  global_size_t numGlobalBlocks = numGlobalElements/blockSize;
93 
94  if (numGlobalBlocks < numProc) {
95  if (verbose) {
96  std::cout << "numGlobalBlocks = " << numGlobalBlocks
97  << " cannot be less than the number of processors = " << numProc << std::endl;
98  }
99  return -1;
100  }
101 
102  if (verbose) {
103  std::cout << "numGlobalBlocks = " << numGlobalBlocks << ", numGlobalElements (point-rows): " << numGlobalElements << std::endl;
104  }
105 
106  // Construct a Map that puts approximately the same number of equations on each processor.
107 
108  Ordinal indexBase = 0;
109  Teuchos::RCP<const Tpetra::BlockMap<Ordinal> > blkmap = Teuchos::rcp(new Tpetra::BlockMap<Ordinal>(numGlobalBlocks, blockSize, indexBase, comm));
110 
111  // Get update list and number of local equations from newly created map.
112 
113  const size_t numMyBlocks = blkmap->getNodeNumBlocks();
114 
115  Teuchos::ArrayView<const Ordinal> myGlobalBlocks = blkmap->getNodeBlockIDs();
116 
117  // Create an OTeger vector NumNz that is used to build the Petra Matrix.
118  // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation
119  // on this processor
120 
121  // Create a Tpetra::VbrMatrix using the BlockMap.
123  A = Teuchos::rcp( new Tpetra::VbrMatrix<Scalar,Ordinal>(blkmap, 3) );
124 
125  // Add rows one-at-a-time.
126  // We will build a block-tridiagonal matrix where the diagonal block has 2
127  // on the diagonal and the off-diagonal blocks have -1 on the diagonal.
128  Teuchos::Array<Scalar> two(blockSize*blockSize, 0);
129  two[0] = 2.0; two[blockSize+1] = 2.0; two[blockSize*2+2] = 2.0;
130  Teuchos::Array<Scalar> negOne(blockSize*blockSize, 0);
131  negOne[0] = -1.0; negOne[blockSize+1] = -1.0; negOne[blockSize*2+2] = -1.0;
132 
133  for (size_t i=0; i<numMyBlocks; i++) {
134  if (myGlobalBlocks[i] != 0) {
135  A->setGlobalBlockEntry( myGlobalBlocks[i], myGlobalBlocks[i]-1,
136  blockSize, blockSize, blockSize, negOne() );
137  }
138 
139  //always set the diagonal block:
140  A->setGlobalBlockEntry( myGlobalBlocks[i], myGlobalBlocks[i],
141  blockSize, blockSize, blockSize, two() );
142 
143  if (static_cast<global_size_t>(myGlobalBlocks[i]) != numGlobalBlocks-1) {
144  A->setGlobalBlockEntry( myGlobalBlocks[i], myGlobalBlocks[i]+1,
145  blockSize, blockSize, blockSize, negOne() );
146  }
147  }
148 
149  // Finish up matrix fill
150  A->fillComplete();
151  if (verbose) std::cout << std::endl << A->description() << std::endl << std::endl;
152 
153  // variable needed for iteration
154  Scalar lambda; (void)lambda;
155  const size_t niters = static_cast<size_t>(numGlobalElements*10);
156  const Scalar tolerance = 1.0e-2;
157 
158  // Iterate
159  lambda = power_method<Scalar,Ordinal>(A, niters, tolerance, verbose);
160 
161  // Increase diagonal dominance
162  if (verbose) {
163  std::cout << "\nIncreasing magnitude of first diagonal term, solving again\n"
164  << std::endl;
165  }
166 
167  if (A->getBlockRowMap()->getPointMap()->isNodeGlobalElement(0)) {
168  // modify the diagonal entry of the row with global-id 0
169  const Ordinal ID = 0;
170  Ordinal numPtRows, numPtCols;
171  Teuchos::ArrayRCP<Scalar> blockEntry;
172  A->getGlobalBlockEntryViewNonConst(ID,ID, numPtRows,numPtCols, blockEntry);
173  blockEntry[0] *= 10.0;
174  }
175 
176  // Iterate (again)
177  lambda = power_method<Scalar,Ordinal>(A, niters, tolerance, verbose);
178 
179  /* end main
180  */
181  if (verbose) {
182  std::cout << "\nEnd Result: TEST PASSED" << std::endl;
183  }
184  return 0;
185 }
186 
187 
188 template <class Scalar, class Ordinal>
189 Scalar power_method(const Teuchos::RCP<const Tpetra::Operator<Scalar,Ordinal> > &A, size_t niters, typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance, bool verbose) {
190  typedef Scalar scalar_type;
192  typedef typename STS::magnitudeType magnitude_type;
194 
195  const bool NO_INITIALIZE_TO_ZERO = false;
196  // create three vectors; do not bother initializing q to zero, as we will fill it with random below
197  Tpetra::Vector<scalar_type, Ordinal> z (A->getRangeMap (), NO_INITIALIZE_TO_ZERO);
198 
199  // mfh 26 Nov 2012: For some reason, not initializing r to zero
200  // causes the norm of r to be NaN in the loop below. This fixes
201  // a test failure on my workstation.
202  Tpetra::Vector<scalar_type, Ordinal> q (A->getRangeMap ()); //, NO_INITIALIZE_TO_ZERO);
203  Tpetra::Vector<scalar_type, Ordinal> r (A->getRangeMap ()); //, NO_INITIALIZE_TO_ZERO);
204 
205  // Fill z with random numbers
206  z.randomize ();
207  // Variables needed for iteration
208  scalar_type lambda = STS::zero ();
209  magnitude_type normz, residual = STM::zero ();
210 
211  // power iteration
212  for (size_t iter = 0; iter < niters; ++iter) {
213  normz = z.norm2 (); // Compute 2-norm of z
214  q.scale (STS::one () / normz, z); // Compute q = z / normz
215  A->apply (q, z); // Compute z = A*q
216  lambda = q.dot (z); // Approximate maximum eigenvalue: lambda = dot(q, z)
217  if ( iter % 100 == 0 || iter + 1 == niters ) {
218  r.update (STS::one (), z, -lambda, q, STS::zero ()); // Compute A*q - lambda*q
219 
220  magnitude_type r_norm = r.norm2 ();
221  residual = STS::magnitude (r_norm / lambda);
222  if (verbose) {
223  std::cout << "Iter = " << iter << ": lambda = " << lambda
224  << ", ||A*q - lambda*q||_2 = " << r_norm
225  << ", ||A*q - lambda*q||_2 / lambda = " << residual
226  << std::endl;
227  }
228  }
229  if (residual < tolerance) {
230  break;
231  }
232  }
233  return lambda;
234 }
Block-entry counterpart to Tpetra::Map.
RCP< const Teuchos::Comm< int > > getComm() const
The Teuchos::Comm instance with which this object was created.
VbrMatrix: Variable block row matrix.
size_t global_size_t
Global size_t object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Abstract interface for linear operators accepting Tpetra MultiVector objects.
void randomize()
Set all values in the multivector to pseudorandom numbers.
A distributed dense vector.
static DefaultPlatformType & getDefaultPlatform()
Return the default platform.