IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_SparseContainer.h
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) 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 IFPACK_SPARSECONTAINER_H
44 #define IFPACK_SPARSECONTAINER_H
45 
46 #include "Ifpack_Container.h"
47 #include "Epetra_IntSerialDenseVector.h"
48 #include "Epetra_MultiVector.h"
49 #include "Epetra_Vector.h"
50 #include "Epetra_Map.h"
51 #include "Epetra_RowMatrix.h"
52 #include "Epetra_CrsMatrix.h"
53 #include "Epetra_LinearProblem.h"
54 #include "Epetra_IntSerialDenseVector.h"
55 #include "Teuchos_ParameterList.hpp"
56 #include "Teuchos_RefCountPtr.hpp"
57 #ifdef HAVE_MPI
58 #include "Epetra_MpiComm.h"
59 #else
60 #include "Epetra_SerialComm.h"
61 #endif
62 
92 template<typename T>
94 
95 public:
96 
98  Ifpack_SparseContainer(const int NumRows, const int NumVectors = 1);
100 
103 
105  virtual ~Ifpack_SparseContainer();
107 
109 
113 
115  virtual int NumRows() const;
117 
119  virtual int NumVectors() const
120  {
121  return(NumVectors_);
122  }
123 
125  virtual int SetNumVectors(const int NumVectors_in)
126  {
127  if (NumVectors_ != NumVectors_in)
128  {
129  NumVectors_=NumVectors_in;
130  LHS_=Teuchos::rcp(new Epetra_MultiVector(*Map_,NumVectors_));
131  RHS_=Teuchos::rcp(new Epetra_MultiVector(*Map_,NumVectors_));
132  }
133  return(0);
134  }
135 
137  virtual double& LHS(const int i, const int Vector = 0);
138 
140  virtual double& RHS(const int i, const int Vector = 0);
141 
143 
152  virtual int& ID(const int i);
153 
155  virtual int SetMatrixElement(const int row, const int col,
156  const double value);
157 
158 
160  virtual bool IsInitialized() const
161  {
162  return(IsInitialized_);
163  }
164 
166  virtual bool IsComputed() const
167  {
168  return(IsComputed_);
169  }
170 
172  virtual int SetParameters(Teuchos::ParameterList& List);
173 
175  virtual const char* Label() const
176  {
177  return(Label_.c_str());
178  }
179 
181  Teuchos::RCP<const Epetra_Map> Map() const
182  {
183  return(Map_);
184  }
185 
187  Teuchos::RCP<const Epetra_MultiVector> LHS() const
188  {
189  return(LHS_);
190  }
191 
193  Teuchos::RCP<const Epetra_MultiVector> RHS() const
194  {
195  return(RHS_);
196  }
197 
199  Teuchos::RCP<const Epetra_CrsMatrix> Matrix() const
200  {
201  return(Matrix_);
202  }
203 
205  const Epetra_IntSerialDenseVector& ID() const
206  {
207  return(GID_);
208  }
209 
211  Teuchos::RCP<const T> Inverse() const
212  {
213  return(Inverse_);
214  }
216 
218 
225  virtual int Initialize();
227  virtual int Compute(const Epetra_RowMatrix& Matrix_in);
229  virtual int Apply();
230 
232  virtual int ApplyInverse();
233 
235 
237  virtual int Destroy();
240 
242  virtual double InitializeFlops() const
243  {
244  if (Inverse_ == Teuchos::null)
245  return (0.0);
246  else
247  return(Inverse_->InitializeFlops());
248  }
249 
251  virtual double ComputeFlops() const
252  {
253  if (Inverse_ == Teuchos::null)
254  return (0.0);
255  else
256  return(Inverse_->ComputeFlops());
257  }
258 
260  virtual double ApplyFlops() const
261  {
262  return(ApplyFlops_);
263  }
264 
266  virtual double ApplyInverseFlops() const
267  {
268  if (Inverse_ == Teuchos::null)
269  return (0.0);
270  else
271  return(Inverse_->ApplyInverseFlops());
272  }
273 
275  virtual ostream& Print(std::ostream& os) const;
276 
277 private:
278 
280  virtual int Extract(const Epetra_RowMatrix& Matrix_in);
281 
283  int NumRows_;
285  int NumVectors_;
287  Teuchos::RefCountPtr<Epetra_Map> Map_;
289  Teuchos::RefCountPtr<Epetra_CrsMatrix> Matrix_;
291  Teuchos::RefCountPtr<Epetra_MultiVector> LHS_;
293  Teuchos::RefCountPtr<Epetra_MultiVector> RHS_;
295  Epetra_IntSerialDenseVector GID_;
297  bool IsInitialized_;
299  bool IsComputed_;
301  Teuchos::RefCountPtr<Epetra_Comm> SerialComm_;
303  Teuchos::RefCountPtr<T> Inverse_;
305  string Label_;
306  Teuchos::ParameterList List_;
307  double ApplyFlops_;
308 
309 };
310 
311 //==============================================================================
312 template<typename T>
314 Ifpack_SparseContainer(const int NumRows_in, const int NumVectors_in) :
315  NumRows_(NumRows_in),
316  NumVectors_(NumVectors_in),
317  IsInitialized_(false),
318  IsComputed_(false),
319  ApplyFlops_(0.0)
320 {
321 
322 #ifdef HAVE_MPI
323  SerialComm_ = Teuchos::rcp( new Epetra_MpiComm(MPI_COMM_SELF) );
324 #else
325  SerialComm_ = Teuchos::rcp( new Epetra_SerialComm );
326 #endif
327 
328 }
329 
330 //==============================================================================
331 template<typename T>
334  NumRows_(rhs.NumRows()),
335  NumVectors_(rhs.NumVectors()),
336  IsInitialized_(rhs.IsInitialized()),
337  IsComputed_(rhs.IsComputed())
338 {
339 
340 #ifdef HAVE_MPI
341  SerialComm_ = Teuchos::rcp( new Epetra_MpiComm(MPI_COMM_SELF) );
342 #else
343  SerialComm_ = Teuchos::rcp( new Epetra_SerialComm );
344 #endif
345 
346  if (rhs.Map())
347  Map_ = Teuchos::rcp( new Epetra_Map(*rhs.Map()) );
348 
349  if (rhs.Matrix())
350  Matrix_ = Teuchos::rcp( new Epetra_CrsMatrix(*rhs.Matrix()) );
351 
352  if (rhs.LHS())
353  LHS_ = Teuchos::rcp( new Epetra_MultiVector(*rhs.LHS()) );
354 
355  if (rhs.RHS())
356  RHS_ = Teuchos::rcp( new Epetra_MultiVector(*rhs.RHS()) );
357 
358 }
359 //==============================================================================
360 template<typename T>
362 {
363  Destroy();
364 }
365 
366 //==============================================================================
367 template<typename T>
369 {
370  if (IsInitialized() == false)
371  return(0);
372  else
373  return(NumRows_);
374 }
375 
376 //==============================================================================
377 template<typename T>
379 {
380 
381  if (IsInitialized_ == true)
382  Destroy();
383 
384  IsInitialized_ = false;
385 
386  Map_ = Teuchos::rcp( new Epetra_Map(NumRows_,0,*SerialComm_) );
387 
388  LHS_ = Teuchos::rcp( new Epetra_MultiVector(*Map_,NumVectors_) );
389  RHS_ = Teuchos::rcp( new Epetra_MultiVector(*Map_,NumVectors_) );
390  GID_.Reshape(NumRows_,1);
391 
392  Matrix_ = Teuchos::rcp( new Epetra_CrsMatrix(Copy,*Map_,0) );
393 
394  // create the inverse
395  Inverse_ = Teuchos::rcp( new T(Matrix_.get()) );
396 
397  if (Inverse_ == Teuchos::null)
398  IFPACK_CHK_ERR(-5);
399 
400  IFPACK_CHK_ERR(Inverse_->SetParameters(List_));
401 
402  // Call Inverse_->Initialize() in Compute(). This saves
403  // some time, because I can extract the diagonal blocks faster,
404  // and only once.
405 
406  Label_ = "Ifpack_SparseContainer";
407 
408  IsInitialized_ = true;
409  return(0);
410 
411 }
412 
413 //==============================================================================
414 template<typename T>
415 double& Ifpack_SparseContainer<T>::LHS(const int i, const int Vector)
416 {
417  return(((*LHS_)(Vector))->Values()[i]);
418 }
419 
420 //==============================================================================
421 template<typename T>
422 double& Ifpack_SparseContainer<T>::RHS(const int i, const int Vector)
423 {
424  return(((*RHS_)(Vector))->Values()[i]);
425 }
426 
427 //==============================================================================
428 template<typename T>
430 SetMatrixElement(const int row, const int col, const double value)
431 {
432  if (!IsInitialized())
433  IFPACK_CHK_ERR(-3); // problem not shaped yet
434 
435  if ((row < 0) || (row >= NumRows())) {
436  IFPACK_CHK_ERR(-2); // not in range
437  }
438 
439  if ((col < 0) || (col >= NumRows())) {
440  IFPACK_CHK_ERR(-2); // not in range
441  }
442 
443  int ierr = Matrix_->InsertGlobalValues((int)row,1,(double*)&value,(int*)&col);
444  if (ierr < 0) {
445  ierr = Matrix_->SumIntoGlobalValues((int)row,1,(double*)&value,(int*)&col);
446  if (ierr < 0)
447  IFPACK_CHK_ERR(-1);
448  }
449 
450  return(0);
451 
452 }
453 
454 //==============================================================================
455 template<typename T>
456 int Ifpack_SparseContainer<T>::Compute(const Epetra_RowMatrix& Matrix_in)
457 {
458 
459  IsComputed_ = false;
460  if (!IsInitialized()) {
461  IFPACK_CHK_ERR(Initialize());
462  }
463 
464  // extract the submatrices
465  IFPACK_CHK_ERR(Extract(Matrix_in));
466 
467  // initialize the inverse operator
468  IFPACK_CHK_ERR(Inverse_->Initialize());
469 
470  // compute the inverse operator
471  IFPACK_CHK_ERR(Inverse_->Compute());
472 
473  Label_ = "Ifpack_SparseContainer";
474 
475  IsComputed_ = true;
476 
477  return(0);
478 }
479 
480 //==============================================================================
481 template<typename T>
483 {
484  if (IsComputed() == false) {
485  IFPACK_CHK_ERR(-3); // not yet computed
486  }
487 
488  IFPACK_CHK_ERR(Matrix_->Apply(*RHS_, *LHS_));
489 
490  ApplyFlops_ += 2 * Matrix_->NumGlobalNonzeros();
491  return(0);
492 }
493 
494 //==============================================================================
495 template<typename T>
497 {
498  if (!IsComputed())
499  IFPACK_CHK_ERR(-1);
500 
501  IFPACK_CHK_ERR(Inverse_->ApplyInverse(*RHS_, *LHS_));
502 
503  return(0);
504 }
505 
506 
507 //==============================================================================
508 template<typename T>
510 {
511  IsInitialized_ = false;
512  IsComputed_ = false;
513  return(0);
514 }
515 
516 //==============================================================================
517 template<typename T>
519 {
520  return(GID_[i]);
521 }
522 
523 //==============================================================================
524 template<typename T>
526 SetParameters(Teuchos::ParameterList& List)
527 {
528  List_ = List;
529  return(0);
530 }
531 
532 //==============================================================================
533 // FIXME: optimize performances of this guy...
534 template<typename T>
535 int Ifpack_SparseContainer<T>::Extract(const Epetra_RowMatrix& Matrix_in)
536 {
537 
538  for (int j = 0 ; j < NumRows_ ; ++j) {
539  // be sure that the user has set all the ID's
540  if (ID(j) == -1)
541  IFPACK_CHK_ERR(-1);
542  // be sure that all are local indices
543  if (ID(j) > Matrix_in.NumMyRows())
544  IFPACK_CHK_ERR(-1);
545  }
546 
547  int Length = Matrix_in.MaxNumEntries();
548  std::vector<double> Values;
549  Values.resize(Length);
550  std::vector<int> Indices;
551  Indices.resize(Length);
552 
553  for (int j = 0 ; j < NumRows_ ; ++j) {
554 
555  int LRID = ID(j);
556 
557  int NumEntries;
558 
559  int ierr =
560  Matrix_in.ExtractMyRowCopy(LRID, Length, NumEntries,
561  &Values[0], &Indices[0]);
562  IFPACK_CHK_ERR(ierr);
563 
564  for (int k = 0 ; k < NumEntries ; ++k) {
565 
566  int LCID = Indices[k];
567 
568  // skip off-processor elements
569  if (LCID >= Matrix_in.NumMyRows())
570  continue;
571 
572  // for local column IDs, look for each ID in the list
573  // of columns hosted by this object
574  // FIXME: use STL
575  int jj = -1;
576  for (int kk = 0 ; kk < NumRows_ ; ++kk)
577  if (ID(kk) == LCID)
578  jj = kk;
579 
580  if (jj != -1)
581  SetMatrixElement(j,jj,Values[k]);
582 
583  }
584  }
585 
586  IFPACK_CHK_ERR(Matrix_->FillComplete());
587 
588  return(0);
589 }
590 
591 //==============================================================================
592 template<typename T>
593 ostream& Ifpack_SparseContainer<T>::Print(ostream & os) const
594 {
595  os << "================================================================================" << endl;
596  os << "Ifpack_SparseContainer" << endl;
597  os << "Number of rows = " << NumRows() << endl;
598  os << "Number of vectors = " << NumVectors() << endl;
599  os << "IsInitialized() = " << IsInitialized() << endl;
600  os << "IsComputed() = " << IsComputed() << endl;
601  os << "Flops in Initialize() = " << InitializeFlops() << endl;
602  os << "Flops in Compute() = " << ComputeFlops() << endl;
603  os << "Flops in ApplyInverse() = " << ApplyInverseFlops() << endl;
604  os << "================================================================================" << endl;
605  os << endl;
606 
607  return(os);
608 }
609 #endif // IFPACK_SPARSECONTAINER_H
virtual double ComputeFlops() const
Returns the flops in Compute().
virtual double InitializeFlops() const
Returns the flops in Compute().
Teuchos::RCP< const Epetra_MultiVector > LHS() const
Returns a pointer to the internally stored solution multi-vector.
virtual int NumRows() const
Returns the number of rows of the matrix and LHS/RHS.
Ifpack_SparseContainer & operator=(const Ifpack_SparseContainer< T > &rhs)
Operator =.
Teuchos::RCP< const Epetra_MultiVector > RHS() const
Returns a pointer to the internally stored rhs multi-vector.
virtual int SetParameters(Teuchos::ParameterList &List)
Sets all necessary parameters.
virtual bool IsInitialized() const
Returns true is the container has been successfully initialized.
Teuchos::RCP< const Epetra_CrsMatrix > Matrix() const
Returns a pointer to the internally stored matrix.
virtual double & LHS(const int i, const int Vector=0)
Returns the i-th component of the vector Vector of LHS.
virtual int Initialize()
Initializes the container, by completing all the operations based on matrix structure.
virtual int SetMatrixElement(const int row, const int col, const double value)
Set the matrix element (row,col) to value.
virtual const char * Label() const
Returns the label of this container.
virtual double & RHS(const int i, const int Vector=0)
Returns the i-th component of the vector Vector of RHS.
Teuchos::RCP< const Epetra_Map > Map() const
Returns a pointer to the internally stored map.
virtual int NumVectors() const
Returns the number of vectors in LHS/RHS.
Ifpack_SparseContainer: a class for storing and solving linear systems using sparse matrices...
virtual int SetNumVectors(const int NumVectors_in)
Sets the number of vectors for LHS/RHS.
virtual int ApplyInverse()
Apply the inverse of the matrix to RHS, result is stored in LHS.
Ifpack_SparseContainer(const int NumRows, const int NumVectors=1)
Constructor.
virtual double ApplyFlops() const
Returns the flops in Apply().
virtual ~Ifpack_SparseContainer()
Destructor.
Teuchos::RCP< const T > Inverse() const
Returns a pointer to the internally stored inverse operator.
virtual ostream & Print(std::ostream &os) const
Prints basic information on iostream. This function is used by operator&lt;&lt;.
const Epetra_IntSerialDenseVector & ID() const
Returns a pointer to the internally stored ID&#39;s.
virtual int Destroy()
Destroys all data.
Ifpack_Container: a pure virtual class for creating and solving local linear problems.
virtual bool IsComputed() const
Returns true is the container has been successfully computed.
virtual int Compute(const Epetra_RowMatrix &Matrix_in)
Finalizes the linear system matrix and prepares for the application of the inverse.
virtual double ApplyInverseFlops() const
Returns the flops in ApplyInverse().
virtual int Apply()
Apply the matrix to RHS, result is stored in LHS.