Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_BlockRelaxation_def.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Tempated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
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 
43 #ifndef IFPACK2_BLOCKRELAXATION_DEF_HPP
44 #define IFPACK2_BLOCKRELAXATION_DEF_HPP
45 
46 #include "Ifpack2_BlockRelaxation_decl.hpp"
47 #include "Ifpack2_LinearPartitioner_decl.hpp"
48 
49 namespace Ifpack2 {
50 
51 //==========================================================================
52 template<class MatrixType,class ContainerType>
53 BlockRelaxation<MatrixType,ContainerType>::BlockRelaxation(const Teuchos::RCP<const Tpetra::RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >& A)
54 : A_(A),
55  Time_( Teuchos::rcp( new Teuchos::Time("Ifpack2::BlockRelaxation") ) ),
56  OverlapLevel_(0),
57  PartitionerType_("linear"),
58  NumSweeps_(1),
59  PrecType_(Ifpack2::JACOBI),
60  MinDiagonalValue_(0.0),
61  DampingFactor_(1.0),
62  IsParallel_(false),
63  ZeroStartingSolution_(true),
64  DoBackwardGS_(false),
65  Condest_(-1.0),
66  IsInitialized_(false),
67  IsComputed_(false),
68  NumInitialize_(0),
69  NumCompute_(0),
70  NumApply_(0),
71  InitializeTime_(0.0),
72  ComputeTime_(0.0),
73  ApplyTime_(0.0),
74  ComputeFlops_(0.0),
75  ApplyFlops_(0.0),
76  NumMyRows_(0),
77  NumGlobalRows_(0),
78  NumGlobalNonzeros_(0)
79 {
80  TEUCHOS_TEST_FOR_EXCEPTION(A_ == Teuchos::null, std::runtime_error,
81  Teuchos::typeName(*this) << "::BlockRelaxation(): input matrix reference was null.");
82 }
83 
84 //==========================================================================
85 template<class MatrixType,class ContainerType>
87 }
88 
89 //==========================================================================
90 template<class MatrixType,class ContainerType>
92 {
93  Teuchos::ParameterList validparams;
94  Ifpack2::getValidParameters(validparams);
95  List.validateParameters(validparams);
96 
97  std::string PT;
98  if (PrecType_ == Ifpack2::JACOBI)
99  PT = "Jacobi";
100  else if (PrecType_ == Ifpack2::GS)
101  PT = "Gauss-Seidel";
102  else if (PrecType_ == Ifpack2::SGS)
103  PT = "Symmetric Gauss-Seidel";
104 
105  Ifpack2::getParameter(List, "relaxation: type", PT);
106 
107  if (PT == "Jacobi")
108  PrecType_ = Ifpack2::JACOBI;
109  else if (PT == "Gauss-Seidel")
110  PrecType_ = Ifpack2::GS;
111  else if (PT == "Symmetric Gauss-Seidel")
112  PrecType_ = Ifpack2::SGS;
113  else {
114  std::ostringstream osstr;
115  osstr << "Ifpack2::BlockRelaxation::setParameters: unsupported parameter-value for 'relaxation: type' (" << PT << ")";
116  std::string str = osstr.str();
117  throw std::runtime_error(str);
118  }
119 
120  Ifpack2::getParameter(List, "relaxation: sweeps",NumSweeps_);
121  Ifpack2::getParameter(List, "relaxation: damping factor", DampingFactor_);
122  Ifpack2::getParameter(List, "relaxation: min diagonal value", MinDiagonalValue_);
123  Ifpack2::getParameter(List, "relaxation: zero starting solution", ZeroStartingSolution_);
124  Ifpack2::getParameter(List, "relaxation: backward mode",DoBackwardGS_);
125  Ifpack2::getParameter(List, "partitioner: type",PartitionerType_);
126  Ifpack2::getParameter(List, "partitioner: local parts",NumLocalBlocks_);
127  Ifpack2::getParameter(List, "partitioner: overlap",OverlapLevel_);
128 
129  // check parameters
130  if (PrecType_ != Ifpack2::JACOBI)
131  OverlapLevel_ = 0;
132  if (NumLocalBlocks_ < 0)
133  NumLocalBlocks_ = A_->getNodeNumRows() / (-NumLocalBlocks_);
134  // other checks are performed in Partitioner_
135 
136 
137  // NTS: Sanity check to be removed at a later date when Backward mode is enabled
138  TEUCHOS_TEST_FOR_EXCEPTION(DoBackwardGS_ == true, std::runtime_error,
139  "Ifpack2::BlockRelaxation:setParameters ERROR: 'relaxation: backward mode' == true is not supported yet.");
140 
141 
142  // copy the list as each subblock's constructor will
143  // require it later
144  List_ = List;
145 }
146 
147 //==========================================================================
148 template<class MatrixType,class ContainerType>
151  return A_->getComm();
152 }
153 
154 //==========================================================================
155 template<class MatrixType,class ContainerType>
156 Teuchos::RCP<const Tpetra::RowMatrix<typename MatrixType::scalar_type,
157  typename MatrixType::local_ordinal_type,
158  typename MatrixType::global_ordinal_type,
159  typename MatrixType::node_type> >
161  return(A_);
162 }
163 
164 //==========================================================================
165 template<class MatrixType,class ContainerType>
166 const Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
167  typename MatrixType::global_ordinal_type,
168  typename MatrixType::node_type> >&
170  return A_->getDomainMap();
171 }
172 
173 //==========================================================================
174 template<class MatrixType,class ContainerType>
175 const Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
176  typename MatrixType::global_ordinal_type,
177  typename MatrixType::node_type> >&
179  return A_->getRangeMap();
180 }
181 
182 //==========================================================================
183 template<class MatrixType,class ContainerType>
185  return true;
186 }
187 
188 //==========================================================================
189 template<class MatrixType,class ContainerType>
191  return(NumInitialize_);
192 }
193 
194 //==========================================================================
195 template<class MatrixType,class ContainerType>
197  return(NumCompute_);
198 }
199 
200 //==========================================================================
201 template<class MatrixType,class ContainerType>
203  return(NumApply_);
204 }
205 
206 //==========================================================================
207 template<class MatrixType,class ContainerType>
209  return(InitializeTime_);
210 }
211 
212 //==========================================================================
213 template<class MatrixType,class ContainerType>
215  return(ComputeTime_);
216 }
217 
218 //==========================================================================
219 template<class MatrixType,class ContainerType>
221  return(ApplyTime_);
222 }
223 
224 //==========================================================================
225 template<class MatrixType,class ContainerType>
227  return(ComputeFlops_);
228 }
229 
230 //==========================================================================
231 template<class MatrixType,class ContainerType>
233  return(ApplyFlops_);
234 }
235 
236 //==========================================================================
237 template<class MatrixType,class ContainerType>
240 {
241  return(Condest_);
242 }
243 
244 //==========================================================================
245 template<class MatrixType,class ContainerType>
248  CondestType CT,
249  typename MatrixType::local_ordinal_type MaxIters,
250  magnitudeType Tol,
251  const Teuchos::Ptr<const Tpetra::RowMatrix<typename MatrixType::scalar_type,
252  typename MatrixType::local_ordinal_type,
253  typename MatrixType::global_ordinal_type,
254  typename MatrixType::node_type> > &matrix)
255 {
256  if (!isComputed()) // cannot compute right now
257  return(-1.0);
258 
259  // always compute it. Call Condest() with no parameters to get
260  // the previous estimate.
261  Condest_ = Ifpack2::Condest(*this, CT, MaxIters, Tol, matrix);
262 
263  return(Condest_);
264 }
265 
266 //==========================================================================
267 template<class MatrixType,class ContainerType>
269  const Tpetra::MultiVector<typename MatrixType::scalar_type,
270  typename MatrixType::local_ordinal_type,
271  typename MatrixType::global_ordinal_type,
272  typename MatrixType::node_type>& X,
273  Tpetra::MultiVector<typename MatrixType::scalar_type,
274  typename MatrixType::local_ordinal_type,
275  typename MatrixType::global_ordinal_type,
276  typename MatrixType::node_type>& Y,
277  Teuchos::ETransp mode,
278  Scalar alpha,
279  Scalar beta) const
280 {
281  TEUCHOS_TEST_FOR_EXCEPTION(isComputed() == false, std::runtime_error,
282  "Ifpack2::BlockRelaxation::apply ERROR: isComputed() must be true prior to calling apply.");
283 
284  TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
285  "Ifpack2::BlockRelaxation::apply ERROR: X.getNumVectors() != Y.getNumVectors().");
286 
287  TEUCHOS_TEST_FOR_EXCEPTION(mode != Teuchos::NO_TRANS, std::runtime_error,
288  "Ifpack2::BlockRelaxation::apply ERORR: transpose modes not supported.");
289 
290  Time_->start(true);
291 
292  // If X and Y are pointing to the same memory location,
293  // we need to create an auxiliary vector, Xcopy
295  if (X.getLocalMV().getValues() == Y.getLocalMV().getValues())
296  Xcopy = Teuchos::rcp( new Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(X) );
297  else
298  Xcopy = Teuchos::rcp( &X, false );
299 
300  if (ZeroStartingSolution_)
301  Y.putScalar(0.0);
302 
303  // Flops are updated in each of the following.
304  switch (PrecType_) {
305  case Ifpack2::JACOBI:
306  ApplyInverseJacobi(*Xcopy,Y);
307  break;
308  case Ifpack2::GS:
309  ApplyInverseGS(*Xcopy,Y);
310  break;
311  case Ifpack2::SGS:
312  ApplyInverseSGS(*Xcopy,Y);
313  break;
314  default:
315  throw std::runtime_error("Ifpack2::BlockRelaxation::apply internal logic error.");
316  }
317 
318  ++NumApply_;
319  Time_->stop();
320  ApplyTime_ += Time_->totalElapsedTime();
321 }
322 
323 //==========================================================================
324 template<class MatrixType,class ContainerType>
326  const Tpetra::MultiVector<typename MatrixType::scalar_type,
327  typename MatrixType::local_ordinal_type,
328  typename MatrixType::global_ordinal_type,
329  typename MatrixType::node_type>& X,
330  Tpetra::MultiVector<typename MatrixType::scalar_type,
331  typename MatrixType::local_ordinal_type,
332  typename MatrixType::global_ordinal_type,
333  typename MatrixType::node_type>& Y,
334  Teuchos::ETransp mode) const
335 {
336  TEUCHOS_TEST_FOR_EXCEPTION(isComputed() == false, std::runtime_error,
337  "Ifpack2::BlockRelaxation::applyMat() ERROR: isComputed() must be true prior to calling applyMat().");
338  TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
339  "Ifpack2::BlockRelaxation::applyMat() ERROR: X.getNumVectors() != Y.getNumVectors().");
340  A_->apply(X, Y, mode);
341 }
342 
343 //==========================================================================
344 template<class MatrixType,class ContainerType>
346  IsInitialized_ = false;
347 
348  TEUCHOS_TEST_FOR_EXCEPTION(A_ == Teuchos::null, std::runtime_error,
349  "Ifpack2::BlockRelaxation::Initialize ERROR, Matrix is NULL");
350 
351  Time_->start(true);
352 
353  NumMyRows_ = A_->getNodeNumRows();
354  NumGlobalRows_ = A_->getGlobalNumRows();
355  NumGlobalNonzeros_ = A_->getGlobalNumEntries();
356 
357  // NTS: Will need to add support for Zoltan2 partitions later
358  // Also, will need a better way of handling the Graph typing issue. Especially with ordinal types w.r.t the container
359 
360  if (PartitionerType_ == "linear")
361  Partitioner_ = Teuchos::rcp( new Ifpack2::LinearPartitioner<Tpetra::RowGraph<LocalOrdinal,GlobalOrdinal,Node> >(A_->getGraph()) );
362  else
363  TEUCHOS_TEST_FOR_EXCEPTION(0, std::runtime_error,"Ifpack2::BlockRelaxation::initialize, invalid partitioner type.");
364 
365  // need to partition the graph of A
366  Partitioner_->setParameters(List_);
367  Partitioner_->compute();
368 
369  // get actual number of partitions
370  NumLocalBlocks_ = Partitioner_->numLocalParts();
371 
372  // Note: Unlike Ifpack, we'll punt on computing W_ until compute(), which is where
373  // we assume that the type of relaxation has been chosen.
374 
375  if (A_->getComm()->getSize() != 1)
376  IsParallel_ = true;
377  else
378  IsParallel_ = false;
379 
380  ++NumInitialize_;
381  Time_->stop();
382  InitializeTime_ += Time_->totalElapsedTime();
383  IsInitialized_ = true;
384 }
385 
386 //==========================================================================
387 template<class MatrixType,class ContainerType>
389 {
390  if (!isInitialized()) {
391  initialize();
392  }
393 
394  Time_->start(true);
395 
396  // reset values
397  IsComputed_ = false;
398  Condest_ = -1.0;
399 
400  TEUCHOS_TEST_FOR_EXCEPTION(NumSweeps_ < 0, std::runtime_error,
401  "Ifpack2::BlockRelaxation::compute, NumSweeps_ must be >= 0");
402 
403  // Extract the submatrices
404  ExtractSubmatrices();
405 
406  // Compute the weight vector if we're doing overlapped Jacobi (and only if we're doing overlapped Jacobi).
407  if (PrecType_ == Ifpack2::JACOBI && OverlapLevel_ > 0) {
408  // weight of each vertex
409  W_ = Teuchos::rcp( new Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(A_->getRowMap()) );
410  W_->putScalar(Teuchos::ScalarTraits<Scalar>::zero());
411  Teuchos::ArrayRCP<Scalar > w_ptr = W_->getDataNonConst(0);
412 
413  for (LocalOrdinal i = 0 ; i < NumLocalBlocks_ ; ++i) {
414  for (size_t j = 0 ; j < Partitioner_->numRowsInPart(i) ; ++j) {
415  int LID = (*Partitioner_)(i,j);
416  w_ptr[LID]+= Teuchos::ScalarTraits<Scalar>::one();
417  }
418  }
419  W_->reciprocal(*W_);
420  }
421 
422  // We need to import data from external processors. Here I create a
423  // Tpetra::Import object if needed (stealing from A_ if possible)
424  // Marzio's comment:
425  // Note that I am doing some strange stuff to set the components of Y
426  // from Y2 (to save some time).
427  //
428  if (IsParallel_ && ((PrecType_ == Ifpack2::GS) || (PrecType_ == Ifpack2::SGS))) {
429  Importer_=A_->getGraph()->getImporter();
430  if(Importer_==Teuchos::null)
431  Importer_ = Teuchos::rcp( new Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node>(A_->getDomainMap(),
432  A_->getColMap()) );
433 
434  TEUCHOS_TEST_FOR_EXCEPTION(Importer_ == Teuchos::null, std::runtime_error,
435  "Ifpack2::BlockRelaxation::compute ERROR failed to create Importer_");
436  }
437 
438  ++NumCompute_;
439  Time_->stop();
440  ComputeTime_ += Time_->totalElapsedTime();
441  IsComputed_ = true;
442 }
443 
444 //==============================================================================
445 template<class MatrixType,class ContainerType>
447 {
448  TEUCHOS_TEST_FOR_EXCEPTION(Partitioner_==Teuchos::null, std::runtime_error,
449  "Ifpack2::BlockRelaxation::ExtractSubmatrices, partitioner is null.");
450 
451  NumLocalBlocks_ = Partitioner_->numLocalParts();
452 
453  Containers_.resize(NumLocalBlocks_);
454 
455  for (LocalOrdinal i = 0 ; i < NumLocalBlocks_ ; ++i) {
456  size_t rows = Partitioner_->numRowsInPart(i);
457  Containers_[i] = Teuchos::rcp( new ContainerType(rows) );
458  TEUCHOS_TEST_FOR_EXCEPTION(Containers_[i]==Teuchos::null, std::runtime_error,
459  "Ifpack2::BlockRelaxation::ExtractSubmatrices, container consturctor failed.");
460 
461  Containers_[i]->setParameters(List_);
462  Containers_[i]->initialize();
463  // flops in initialize() will be computed on-the-fly in method initializeFlops().
464 
465  // set "global" ID of each partitioner row
466  for (size_t j = 0 ; j < rows ; ++j) {
467  Containers_[i]->ID(j) = (*Partitioner_)(i,j);
468  }
469 
470  Containers_[i]->compute(A_);
471  // flops in compute() will be computed on-the-fly in method computeFlops().
472  }
473 }
474 
475 
476 
477 //==========================================================================
478 template<class MatrixType,class ContainerType>
479 void BlockRelaxation<MatrixType,ContainerType>::ApplyInverseJacobi(
480  const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X,
481  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y) const
482 {
483  size_t NumVectors = X.getNumVectors();
484  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> AY( Y.getMap(),NumVectors );
485 
486  // Initial matvec not needed
487  int starting_iteration=0;
488  if(ZeroStartingSolution_) {
489  DoJacobi(X,Y);
490  starting_iteration=1;
491  }
492 
493  for (int j = starting_iteration; j < NumSweeps_ ; j++) {
494  applyMat(Y,AY);
495  AY.update(1.0,X,-1.0);
496  DoJacobi(AY,Y);
497 
498  // Flops for matrix apply & update
499  ApplyFlops_ += NumVectors * (2 * NumGlobalNonzeros_ + 2 * NumGlobalRows_);
500  }
501 
502 }
503 
504 
505 //==========================================================================
506 template<class MatrixType,class ContainerType>
507 void BlockRelaxation<MatrixType,ContainerType>::DoJacobi(const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X, Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y) const
508 {
509  size_t NumVectors = X.getNumVectors();
511  // Note: Flop counts copied naively from Ifpack.
512 
513  if (OverlapLevel_ == 0) {
514  // Non-overlapping Jacobi
515  for (LocalOrdinal i = 0 ; i < NumLocalBlocks_ ; i++) {
516  // may happen that a partition is empty
517  if (Containers_[i]->getNumRows() == 0) continue;
518  Containers_[i]->apply(X,Y,Teuchos::NO_TRANS,DampingFactor_,one);
519  ApplyFlops_ += NumVectors * 2 * NumGlobalRows_;
520  }
521  }
522  else {
523  // Overlapping Jacobi
524  for (LocalOrdinal i = 0 ; i < NumLocalBlocks_ ; i++) {
525  // may happen that a partition is empty
526  if (Containers_[i]->getNumRows() == 0) continue;
527  Containers_[i]->weightedApply(X,Y,*W_,Teuchos::NO_TRANS,DampingFactor_,one);
528  // NOTE: do not count (for simplicity) the flops due to overlapping rows
529  ApplyFlops_ += NumVectors * 4 * NumGlobalRows_;
530  }
531  }
532 }
533 
534 //==========================================================================
535 template<class MatrixType,class ContainerType>
536 void BlockRelaxation<MatrixType,ContainerType>::ApplyInverseGS(const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X,
537  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y) const
538 {
539 
540  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> Xcopy(X);
541  for (int j = 0; j < NumSweeps_ ; j++) {
542  DoGaussSeidel(Xcopy,Y);
543  if(j != NumSweeps_-1)
544  Xcopy=X;
545  }
546 }
547 
548 
549 //==============================================================================
550 template<class MatrixType,class ContainerType>
551 void BlockRelaxation<MatrixType,ContainerType>::DoGaussSeidel(Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X,
552  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y) const
553 {
554  // Note: Flop counts copied naively from Ifpack.
555 
556  // allocations
558  int Length = A_->getNodeMaxNumRowEntries();
559  int NumVectors = X.getNumVectors();
560  Teuchos::Array<Scalar> Values;
562  Values.resize(Length);
563  Indices.resize(Length);
564 
565  // an additonal vector is needed by parallel computations
566  // (note that applications through Ifpack2_AdditiveSchwarz
567  // are always seen are serial)
569  if (IsParallel_)
570  Y2 = Teuchos::rcp( new Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(Importer_->getTargetMap(), NumVectors) );
571  else
572  Y2 = Teuchos::rcp( &Y, false );
573 
574 
575  Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> > x_ptr = X.get2dViewNonConst();
576  Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> > y_ptr = Y.get2dViewNonConst();
577  Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> > y2_ptr = Y2->get2dViewNonConst();
578 
579  // data exchange is here, once per sweep
580  if (IsParallel_) Y2->doImport(Y,*Importer_,Tpetra::INSERT);
581 
582  for (LocalOrdinal i = 0 ; i < NumLocalBlocks_ ; i++) {
583  // may happen that a partition is empty
584  if (Containers_[i]->getNumRows() == 0) continue;
585  LocalOrdinal LID;
586 
587  // update from previous block
588  for (size_t j = 0 ; j < Containers_[i]->getNumRows(); j++) {
589  LID = Containers_[i]->ID(j);
590  size_t NumEntries;
591  A_->getLocalRowCopy(LID,Indices(),Values(),NumEntries);
592 
593  for (size_t k = 0 ; k < NumEntries ; k++) {
594  LocalOrdinal col = Indices[k];
595  for (int kk = 0 ; kk < NumVectors ; kk++)
596  x_ptr[kk][LID] -= Values[k] * y2_ptr[kk][col];
597  }
598  }
599  // solve with this block
600  // Note: I'm abusing the ordering information, knowing that X/Y and Y2 have the same ordering for on-proc unknowns.
601  // Note: Add flop counts for inverse apply
602  Containers_[i]->apply(X,*Y2,Teuchos::NO_TRANS,DampingFactor_,one);
603 
604  // operations for all getrow's
605  ApplyFlops_ += NumVectors * (2 * NumGlobalNonzeros_ + 2 * NumGlobalRows_);
606  } // end for NumLocalBlocks_
607 
608  // Attention: this is delicate... Not all combinations
609  // of Y2 and Y will always work (tough for ML it should be ok)
610  if (IsParallel_)
611  for (int m = 0 ; m < NumVectors ; ++m)
612  for (size_t i = 0 ; i < NumMyRows_ ; ++i)
613  y_ptr[m][i] = y2_ptr[m][i];
614 
615 }
616 
617 //==========================================================================
618 template<class MatrixType,class ContainerType>
619 void BlockRelaxation<MatrixType,ContainerType>::ApplyInverseSGS(const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X,
620  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y) const
621 {
622 
623  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> Xcopy(X);
624  for (int j = 0; j < NumSweeps_ ; j++) {
625  DoSGS(X,Xcopy,Y);
626  if(j != NumSweeps_-1)
627  Xcopy=X;
628  }
629 }
630 
631 //==========================================================================
632 template<class MatrixType,class ContainerType>
633 void BlockRelaxation<MatrixType,ContainerType>::DoSGS(const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X,
634  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Xcopy,
635  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y) const
636 {
638  int Length = A_->getNodeMaxNumRowEntries();
639  int NumVectors = X.getNumVectors();
640  Teuchos::Array<Scalar> Values;
642  Values.resize(Length);
643  Indices.resize(Length);
644 
645  // an additonal vector is needed by parallel computations
646  // (note that applications through Ifpack2_AdditiveSchwarz
647  // are always seen are serial)
649  if (IsParallel_)
650  Y2 = Teuchos::rcp( new Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(Importer_->getTargetMap(), NumVectors) );
651  else
652  Y2 = Teuchos::rcp( &Y, false );
653 
655  Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> > xcopy_ptr = Xcopy.get2dViewNonConst();
656  Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> > y_ptr = Y.get2dViewNonConst();
657  Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> > y2_ptr = Y2->get2dViewNonConst();
658 
659  // data exchange is here, once per sweep
660  if (IsParallel_) Y2->doImport(Y,*Importer_,Tpetra::INSERT);
661 
662  // Forward Sweep
663  for(LocalOrdinal i = 0 ; i < NumLocalBlocks_ ; i++) {
664  // may happen that a partition is empty
665  if (Containers_[i]->getNumRows() == 0) continue;
666  LocalOrdinal LID;
667 
668  // update from previous block
669  for(size_t j = 0 ; j < Containers_[i]->getNumRows(); j++) {
670  LID = Containers_[i]->ID(j);
671  size_t NumEntries;
672  A_->getLocalRowCopy(LID,Indices(),Values(),NumEntries);
673 
674  for (size_t k = 0 ; k < NumEntries ; k++) {
675  LocalOrdinal col = Indices[k];
676  for (int kk = 0 ; kk < NumVectors ; kk++)
677  xcopy_ptr[kk][LID] -= Values[k] * y2_ptr[kk][col];
678  }
679  }
680  // solve with this block
681  // Note: I'm abusing the ordering information, knowing that X/Y and Y2 have the same ordering for on-proc unknowns.
682  // Note: Add flop counts for inverse apply
683  Containers_[i]->apply(Xcopy,*Y2,Teuchos::NO_TRANS,DampingFactor_,one);
684 
685  // operations for all getrow's
686  ApplyFlops_ += NumVectors * (2 * NumGlobalNonzeros_ + 2 * NumGlobalRows_);
687  }// end forward sweep
688 
689  // Reverse Sweep
690  Xcopy = X;
691  for(LocalOrdinal i = NumLocalBlocks_-1; i >=0 ; i--) {
692  // may happen that a partition is empty
693  if (Containers_[i]->getNumRows() == 0) continue;
694  LocalOrdinal LID;
695 
696  // update from previous block
697  for(size_t j = 0 ; j < Containers_[i]->getNumRows(); j++) {
698  LID = Containers_[i]->ID(j);
699  size_t NumEntries;
700  A_->getLocalRowCopy(LID,Indices(),Values(),NumEntries);
701 
702  for (size_t k = 0 ; k < NumEntries ; k++) {
703  LocalOrdinal col = Indices[k];
704  for (int kk = 0 ; kk < NumVectors ; kk++)
705  xcopy_ptr[kk][LID] -= Values[k] * y2_ptr[kk][col];
706  }
707  }
708 
709  // solve with this block
710  // Note: I'm abusing the ordering information, knowing that X/Y and Y2 have the same ordering for on-proc unknowns.
711  // Note: Add flop counts for inverse apply
712  Containers_[i]->apply(Xcopy,*Y2,Teuchos::NO_TRANS,DampingFactor_,one);
713 
714  // operations for all getrow's
715  ApplyFlops_ += NumVectors * (2 * NumGlobalNonzeros_ + 2 * NumGlobalRows_);
716  } //end reverse sweep
717 
718 
719  // Attention: this is delicate... Not all combinations
720  // of Y2 and Y will always work (tough for ML it should be ok)
721  if (IsParallel_)
722  for (int m = 0 ; m < NumVectors ; ++m)
723  for (size_t i = 0 ; i < NumMyRows_ ; ++i)
724  y_ptr[m][i] = y2_ptr[m][i];
725 }
726 
727 //==========================================================================
728 template<class MatrixType,class ContainerType>
730  std::ostringstream oss;
732  if (isInitialized()) {
733  if (isComputed()) {
734  oss << "{status = initialized, computed";
735  }
736  else {
737  oss << "{status = initialized, not computed";
738  }
739  }
740  else {
741  oss << "{status = not initialized, not computed";
742  }
743  //
744  if (PrecType_ == Ifpack2::JACOBI) oss << "Type = Block Jacobi, " << std::endl;
745  else if (PrecType_ == Ifpack2::GS) oss << "Type = Block Gauss-Seidel, " << std::endl;
746  else if (PrecType_ == Ifpack2::SGS) oss << "Type = Block Sym. Gauss-Seidel, " << std::endl;
747  //
748  oss << ", global rows = " << A_->getGlobalNumRows()
749  << ", global cols = " << A_->getGlobalNumCols();
750 
751  oss << "}";
752  return oss.str();
753 }
754 
755 //==========================================================================
756 template<class MatrixType,class ContainerType>
758  using std::endl;
759  using std::setw;
760  using Teuchos::VERB_DEFAULT;
761  using Teuchos::VERB_NONE;
762  using Teuchos::VERB_LOW;
763  using Teuchos::VERB_MEDIUM;
764  using Teuchos::VERB_HIGH;
765  using Teuchos::VERB_EXTREME;
766  Teuchos::EVerbosityLevel vl = verbLevel;
767  if (vl == VERB_DEFAULT) vl = VERB_LOW;
768  const int myImageID = A_->getComm()->getRank();
769  Teuchos::OSTab tab(out);
770 
771  // none: print nothing
772  // low: print O(1) info from node 0
773  // medium:
774  // high:
775  // extreme:
776  if (vl != VERB_NONE && myImageID == 0) {
777  out << this->description() << endl;
778  out << endl;
779  out << "===============================================================================" << endl;
780  out << "Sweeps = " << NumSweeps_ << endl;
781  out << "damping factor = " << DampingFactor_ << endl;
782  if (PrecType_ == Ifpack2::GS && DoBackwardGS_) {
783  out << "Using backward mode (BGS only)" << endl;
784  }
785  if (ZeroStartingSolution_) { out << "Using zero starting solution" << endl; }
786  else { out << "Using input starting solution" << endl; }
787  if (Condest_ == -1.0) { out << "Condition number estimate = N/A" << endl; }
788  else { out << "Condition number estimate = " << Condest_ << endl; }
789  out << endl;
790  out << "Phase # calls Total Time (s) Total MFlops MFlops/s " << endl;
791  out << "------------ ------- --------------- --------------- ---------------" << endl;
792  out << setw(12) << "initialize()" << setw(5) << getNumInitialize() << " " << setw(15) << getInitializeTime() << endl;
793  out << setw(12) << "compute()" << setw(5) << getNumCompute() << " " << setw(15) << getComputeTime() << " "
794  << setw(15) << getComputeFlops() << " "
795  << setw(15) << (getComputeTime() != 0.0 ? getComputeFlops() / getComputeTime() * 1.0e-6 : 0.0) << endl;
796  out << setw(12) << "apply()" << setw(5) << getNumApply() << " " << setw(15) << getApplyTime() << " "
797  << setw(15) << getApplyFlops() << " "
798  << setw(15) << (getApplyTime() != 0.0 ? getApplyFlops() / getApplyTime() * 1.0e-6 : 0.0) << endl;
799  out << "===============================================================================" << endl;
800  out << endl;
801  }
802 }
803 
804 }//namespace Ifpack2
805 
806 #endif // IFPACK2_BLOCKRELAXATION_DEF_HPP
807 
magnitudeType computeCondEst(CondestType CT=Cheap, LocalOrdinal MaxIters=1550, magnitudeType Tol=1e-9, const Teuchos::Ptr< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &matrix=Teuchos::null)
Computes the estimated condition number and returns the value.
Definition: Ifpack2_BlockRelaxation_def.hpp:247
double getApplyFlops() const
Returns the number of flops for the application of the preconditioner.
Definition: Ifpack2_BlockRelaxation_def.hpp:232
CondestType
Ifpack2::CondestType: enum to define the type of condition number estimate.
Definition: Ifpack2_CondestType.hpp:50
const Teuchos::RCP< const Teuchos::Comm< int > > & getComm() const
Returns the Tpetra::BlockMap object associated with the range of this matrix operator.
Definition: Ifpack2_BlockRelaxation_def.hpp:150
BlockRelaxation(const Teuchos::RCP< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &Matrix)
BlockRelaxation constructor with given Tpetra::RowMatrix input.
Definition: Ifpack2_BlockRelaxation_def.hpp:53
std::string description() const
Return a simple one-line description of this object.
Definition: Ifpack2_BlockRelaxation_def.hpp:729
double getApplyTime() const
Returns the time spent in apply().
Definition: Ifpack2_BlockRelaxation_def.hpp:220
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
int getNumApply() const
Returns the number of calls to apply().
Definition: Ifpack2_BlockRelaxation_def.hpp:202
void getParameter(const Teuchos::ParameterList &params, const std::string &name, T &value)
Set a value from a ParameterList if a parameter with the specified name exists.
Definition: Ifpack2_Parameters.hpp:59
int getNumCompute() const
Returns the number of calls to compute().
Definition: Ifpack2_BlockRelaxation_def.hpp:196
void setParameters(const Teuchos::ParameterList &params)
Sets all the parameters for the preconditioner.
Definition: Ifpack2_BlockRelaxation_def.hpp:91
void compute()
compute the preconditioner for the specified matrix, diagonal perturbation thresholds and relaxation ...
Definition: Ifpack2_BlockRelaxation_def.hpp:388
Ifpack2::BlockRelaxation: defines relaxation preconditioners for Tpetra::RowMatrix objects...
Definition: Ifpack2_BlockRelaxation_decl.hpp:137
double getInitializeTime() const
Returns the time spent in initialize().
Definition: Ifpack2_BlockRelaxation_def.hpp:208
void apply(const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const
Applies the preconditioner to X, returns the result in Y.
Definition: Ifpack2_BlockRelaxation_def.hpp:268
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
double getComputeFlops() const
Returns the number of flops in the computation phase.
Definition: Ifpack2_BlockRelaxation_def.hpp:226
double getComputeTime() const
Returns the time spent in compute().
Definition: Ifpack2_BlockRelaxation_def.hpp:214
virtual std::string description() const
virtual ~BlockRelaxation()
BlockRelaxation Destructor.
Definition: Ifpack2_BlockRelaxation_def.hpp:86
void resize(size_type new_size, const value_type &x=value_type())
int getNumInitialize() const
Returns the number of calls to initialize().
Definition: Ifpack2_BlockRelaxation_def.hpp:190
void initialize()
Initialize.
Definition: Ifpack2_BlockRelaxation_def.hpp:345
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to an FancyOStream object.
Definition: Ifpack2_BlockRelaxation_def.hpp:757
const Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > & getRangeMap() const
Returns the Tpetra::Map object associated with the range of this operator.
Definition: Ifpack2_BlockRelaxation_def.hpp:178
const Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > & getDomainMap() const
Returns the Tpetra::Map object associated with the domain of this operator.
Definition: Ifpack2_BlockRelaxation_def.hpp:169
magnitudeType getCondEst() const
Returns the computed estimated condition number, or -1.0 if no computed.
Definition: Ifpack2_BlockRelaxation_def.hpp:239
void applyMat(const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS) const
Applies the matrix to a Tpetra::MultiVector.
Definition: Ifpack2_BlockRelaxation_def.hpp:325
Teuchos::RCP< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getMatrix() const
Returns a reference to the matrix to be preconditioned.
Definition: Ifpack2_BlockRelaxation_def.hpp:160
void getValidParameters(Teuchos::ParameterList &params)
Fills a list which contains all the parameters possibly used by Ifpack2.
Definition: Ifpack2_Parameters.cpp:47
Ifpack2::LinearPartitioner: A class to define linear partitions.
Definition: Ifpack2_LinearPartitioner_decl.hpp:54
std::string typeName(const T &t)