EpetraExt  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_Transpose_RowMatrix.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) 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 
43 
44 #include <Epetra_Export.h>
45 #include <Epetra_CrsGraph.h>
46 #include <Epetra_CrsMatrix.h>
47 #include <Epetra_Map.h>
48 #include <Epetra_Import.h>
49 #include <Epetra_Export.h>
50 
51 #include <Teuchos_TimeMonitor.hpp>
52 #include <vector>
53 
54 //#define ENABLE_TRANSPOSE_TIMINGS
55 
56 namespace EpetraExt {
57 
58 // Provide a "resize" operation for double*'s.
59 inline void resize_doubles(int nold,int nnew,double*& d){
60  if(nnew > nold){
61  double *tmp = new double[nnew];
62  for(int i=0; i<nold; i++)
63  tmp[i]=d[i];
64  delete [] d;
65  d=tmp;
66  }
67 }
68 
69 
72 {
73  if( TransposeMatrix_ ) delete TransposeMatrix_;
74 
75  if( !OrigMatrixIsCrsMatrix_ )
76  {
77  delete [] Indices_;
78  delete [] Values_;
79  }
80 }
81 
82 //=========================================================================
83  Epetra_CrsMatrix* EpetraExt::RowMatrix_Transpose::BuildTempTrans()
84 {
85 #ifdef ENABLE_TRANSPOSE_TIMINGS
86  Teuchos::Time myTime("global");
87  Teuchos::TimeMonitor MM(myTime);
88  Teuchos::RCP<Teuchos::Time> mtime;
89  mtime=MM.getNewTimer("Transpose: BuildTempTrans 1");
90  mtime->start();
91 #endif
92 
93  int i,j,err;
94  const Epetra_RowMatrix & orig = *origObj_;
95  const Epetra_CrsMatrix * OrigCrsMatrix = dynamic_cast<const Epetra_CrsMatrix*>(&orig);
96  const Epetra_Map & TransMap = orig.RowMatrixColMap();
97  int TransNnz = orig.NumMyNonzeros();
98  int NumIndices;
99 
100  Epetra_CrsMatrix *TempTransA1 = new Epetra_CrsMatrix(Copy, TransMap,orig.RowMatrixRowMap(),0);
101  Epetra_IntSerialDenseVector & TransRowptr = TempTransA1->ExpertExtractIndexOffset();
102  Epetra_IntSerialDenseVector & TransColind = TempTransA1->ExpertExtractIndices();
103  double *& TransVals = TempTransA1->ExpertExtractValues();
104 
105  TransRowptr.Resize(NumMyCols_+1);
106  TransColind.Resize(TransNnz);
107  resize_doubles(0,TransNnz,TransVals);
108  std::vector<int> CurrentStart(NumMyCols_,0);
109 
110  // Count up nnz using the Rowptr to count the number of non-nonzeros.
111  if (OrigMatrixIsCrsMatrix_)
112  {
113  const Epetra_CrsGraph & OrigGraph = OrigCrsMatrix->Graph(); // Get matrix graph
114 
115  for (i=0; i<NumMyRows_; i++)
116  {
117  err = OrigGraph.ExtractMyRowView(i, NumIndices, Indices_); // Get view of ith row
118  if (err != 0) throw OrigGraph.ReportError("ExtractMyRowView failed",err);
119  for (j=0; j<NumIndices; j++) ++CurrentStart[Indices_[j]];
120  }
121  }
122  else // Original is not a CrsMatrix
123  {
124  MaxNumEntries_ = 0;
125  MaxNumEntries_ = orig.MaxNumEntries();
126  delete [] Indices_; delete [] Values_;
127  Indices_ = new int[MaxNumEntries_];
128  Values_ = new double[MaxNumEntries_];
129 
130  for (i=0; i<NumMyRows_; i++)
131  {
132  err = orig.ExtractMyRowCopy(i, MaxNumEntries_, NumIndices, Values_, Indices_);
133  if (err != 0) {
134  std::cerr << "ExtractMyRowCopy failed."<<std::endl;
135  throw err;
136  }
137  for (j=0; j<NumIndices; j++) ++CurrentStart[Indices_[j]];
138  }
139  }
140 
141  // Scansum the rowptr; reset currentstart
142  TransRowptr[0] = 0;
143  for (i=1;i<NumMyCols_+1; i++) TransRowptr[i] = CurrentStart[i-1] + TransRowptr[i-1];
144  for (i=0;i<NumMyCols_; i++) CurrentStart[i] = TransRowptr[i];
145 
146  // Now copy values and global indices into newly create transpose storage
147  for (i=0; i<NumMyRows_; i++)
148  {
149  if (OrigMatrixIsCrsMatrix_)
150  err = OrigCrsMatrix->ExtractMyRowView(i, NumIndices, Values_, Indices_);
151  else
152  err = orig.ExtractMyRowCopy(i, MaxNumEntries_, NumIndices, Values_, Indices_);
153  if (err != 0) {
154  std::cerr << "ExtractMyRowCopy failed."<<std::endl;
155  throw err;
156  }
157 
158  for (j=0; j<NumIndices; j++)
159  {
160  int idx = CurrentStart[Indices_[j]];
161  TransColind[idx] = i;
162  TransVals[idx] = Values_[j];
163  ++CurrentStart[Indices_[j]];// increment the counter into the local row
164  }
165  }
166 
167 #ifdef ENABLE_TRANSPOSE_TIMINGS
168  mtime->stop();
169  mtime=MM.getNewTimer("Transpose: BuildTempTrans 2");
170  mtime->start();
171 #endif
172 
173  // Prebuild the importers and exporters the no-communication way, flipping the importers
174  // and exporters around.
175  Epetra_Import * myimport = 0;
176  Epetra_Export * myexport = 0;
177  if(OrigMatrixIsCrsMatrix_ && OrigCrsMatrix->Importer())
178  myexport = new Epetra_Export(*OrigCrsMatrix->Importer());
179  if(OrigMatrixIsCrsMatrix_ && OrigCrsMatrix->Exporter())
180  myimport = new Epetra_Import(*OrigCrsMatrix->Exporter());
181 
182 #ifdef ENABLE_TRANSPOSE_TIMINGS
183  mtime->stop();
184  mtime=MM.getNewTimer("Transpose: BuildTempTrans 3");
185  mtime->start();
186 #endif
187 
188  // Call ExpertStaticFillComplete
189  err = TempTransA1->ExpertStaticFillComplete(orig.OperatorRangeMap(),*TransposeRowMap_,myimport,myexport);
190  if (err != 0) {
191  throw TempTransA1->ReportError("ExpertStaticFillComplete failed.",err);
192  }
193 
194 #ifdef ENABLE_TRANSPOSE_TIMINGS
195  mtime->stop();
196 #endif
197 
198  return TempTransA1;
199 }
200 
201 
202 //=========================================================================
206 {
207  origObj_ = &orig;
208 
209  if( !TransposeRowMap_ )
210  {
211  if( IgnoreNonLocalCols_ )
212  TransposeRowMap_ = (Epetra_Map *) &(orig.OperatorRangeMap()); // Should be replaced with refcount =
213  else
214  TransposeRowMap_ = (Epetra_Map *) &(orig.OperatorDomainMap()); // Should be replaced with refcount =
215  }
216 
217  NumMyRows_ = orig.NumMyRows();
218  NumMyCols_ = orig.NumMyCols();
219 
220  // This routine will work for any RowMatrix object, but will attempt cast the matrix to a CrsMatrix if
221  // possible (because we can then use a View of the matrix and graph, which is much cheaper).
222 
223  // First get the local indices to count how many nonzeros will be in the
224  // transpose graph on each processor
225  Epetra_CrsMatrix * TempTransA1 = BuildTempTrans();
226 
227  if(!TempTransA1->Exporter()) {
228  // Short Circuit: There is no need to make another matrix since TransposeRowMap_== TransMap
229  newObj_ = TransposeMatrix_ = TempTransA1;
230  return *newObj_;
231  }
232 
233 #ifdef ENABLE_TRANSPOSE_TIMINGS
234  Teuchos::Time myTime("global");
235  Teuchos::TimeMonitor MM(myTime);
236  Teuchos::RCP<Teuchos::Time> mtime;
237  mtime=MM.getNewTimer("Transpose: Final FusedExport");
238  mtime->start();
239 #endif
240 
241  // Now that transpose matrix with shared rows is entered, create a new matrix that will
242  // get the transpose with uniquely owned rows (using the same row distribution as A).
243  TransposeMatrix_ = new Epetra_CrsMatrix(*TempTransA1,*TempTransA1->Exporter(),0,TransposeRowMap_);
244 
245 #ifdef ENABLE_TRANSPOSE_TIMINGS
246  mtime->stop();
247 #endif
248 
249  newObj_ = TransposeMatrix_;
250  delete TempTransA1;
251  return *newObj_;
252 }
253 
254 //=========================================================================
256 {
257  Epetra_CrsMatrix * TempTransA1 = BuildTempTrans();
258  const Epetra_Export * TransposeExporter=0;
259  bool DeleteExporter = false;
260 
261  if(TempTransA1->Exporter()) TransposeExporter = TempTransA1->Exporter();
262  else {
263  DeleteExporter=true;
264  TransposeExporter = new Epetra_Export(TransposeMatrix_->DomainMap(),TransposeMatrix_->RowMap());
265  }
266 
267  TransposeMatrix_->PutScalar(0.0); // Zero out all values of the matrix
268 
269  EPETRA_CHK_ERR(TransposeMatrix_->Export(*TempTransA1, *TransposeExporter, Add));
270 
271  if(DeleteExporter) delete TransposeExporter;
272  delete TempTransA1;
273  return 0;
274 }
275 
276 //=========================================================================
278 {
279  EPETRA_CHK_ERR(-1); //Not Implemented Yet
280  return false;
281 }
282 
283 } // namespace EpetraExt
Transform< T, T >::NewTypeRef Transform_Composite< T >typename Transform< T, T >::OriginalTypeRef orig this origObj_
void resize_doubles(int nold, int nnew, double *&d)
NewTypeRef operator()(OriginalTypeRef orig)
Transpose Tranform Operator.