EpetraExt  Development
EpetraExt_BTF_CrsMatrix.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
42 
44 
45 #include <Epetra_Import.h>
46 #include <Epetra_CrsMatrix.h>
47 #include <Epetra_CrsGraph.h>
48 #include <Epetra_Map.h>
49 
50 #include <vector>
51 
52 using std::vector;
53 
54 #define MATTRANS_F77 F77_FUNC(mattrans,MATTRANS)
55 #define GENBTF_F77 F77_FUNC(genbtf,GENBTF)
56 
57 extern "C" {
58 extern void MATTRANS_F77( int*, int*, int*, int*, int*, int* );
59 extern void GENBTF_F77( int*, int*, int*, int*, int*, int*, int*, int*, int*,
60  int*, int*, int*, int*, int*, int*, int*, int*, int*,
61  int*, int*, int* );
62 }
63 
64 namespace EpetraExt {
65 
68 {
69  if( NewRowMap_ ) delete NewRowMap_;
70  if( NewColMap_ ) delete NewColMap_;
71 
72  if( Importer_ ) delete Importer_;
73 
74  if( NewMatrix_ ) delete NewMatrix_;
75  if( NewGraph_ ) delete NewGraph_;
76 }
77 
81 {
82  origObj_ = &orig;
83 
84  if( orig.RowMap().DistributedGlobal() )
85  { cout << "FAIL for Global!\n"; abort(); }
86  if( orig.IndicesAreGlobal() )
87  { cout << "FAIL for Global Indices!\n"; abort(); }
88 
89  int n = orig.NumMyRows();
90  int nnz = orig.NumMyNonzeros();
91 
92  if( verbose_ )
93  {
94  cout << "Orig Matrix:\n";
95  cout << orig << endl;
96  }
97 
98  //create std CRS format
99  //also create graph without zero elements
100  vector<int> ia(n+1,0);
101  int maxEntries = orig.MaxNumEntries();
102  vector<int> ja_tmp(maxEntries);
103  vector<double> jva_tmp(maxEntries);
104  vector<int> ja(nnz);
105  int cnt;
106 
107  const Epetra_BlockMap & OldRowMap = orig.RowMap();
108  const Epetra_BlockMap & OldColMap = orig.ColMap();
109  Epetra_CrsGraph strippedGraph( Copy, OldRowMap, OldColMap, 0 );
110 
111  for( int i = 0; i < n; ++i )
112  {
113  orig.ExtractMyRowCopy( i, maxEntries, cnt, &jva_tmp[0], &ja_tmp[0] );
114  ia[i+1] = ia[i];
115  for( int j = 0; j < cnt; ++j )
116  if( fabs(jva_tmp[j]) > threshold_ )
117  ja[ ia[i+1]++ ] = ja_tmp[j];
118 
119  int new_cnt = ia[i+1] - ia[i];
120  strippedGraph.InsertMyIndices( i, new_cnt, &ja[ ia[i] ] );
121  }
122  nnz = ia[n];
123  strippedGraph.FillComplete();
124 
125  if( verbose_ )
126  {
127  cout << "Stripped Graph\n";
128  cout << strippedGraph;
129  }
130 
131  vector<int> iat(n+1,0);
132  vector<int> jat(nnz);
133  for( int i = 0; i < n; ++i )
134  for( int j = ia[i]; j < ia[i+1]; ++j )
135  ++iat[ ja[j]+1 ];
136  for( int i = 0; i < n; ++i )
137  iat[i+1] += iat[i];
138  for( int i = 0; i < n; ++i )
139  for( int j = ia[i]; j < ia[i+1]; ++j )
140  jat[ iat[ ja[j] ]++ ] = i;
141  for( int i = 0; i < n; ++i )
142  iat[n-i] = iat[n-i-1];
143  iat[0] = 0;
144 
145  //convert to Fortran indexing
146  for( int i = 0; i < n+1; ++i ) ++ia[i];
147  for( int i = 0; i < nnz; ++i ) ++ja[i];
148  for( int i = 0; i < n+1; ++i ) ++iat[i];
149  for( int i = 0; i < nnz; ++i ) ++jat[i];
150 
151  if( verbose_ )
152  {
153  cout << "-----------------------------------------\n";
154  cout << "CRS Format Graph\n";
155  cout << "-----------------------------------------\n";
156  for( int i = 0; i < n; ++i )
157  {
158  cout << i+1 << ": " << ia[i+1] << ": ";
159  for( int j = ia[i]-1; j<ia[i+1]-1; ++j )
160  cout << " " << ja[j];
161  cout << endl;
162  }
163  cout << "-----------------------------------------\n";
164  }
165 
166 /*
167  vector<int> iat(n+1);
168  vector<int> jat(nnz);
169  int * jaf = &ja[0];
170  int * iaf = &ia[0];
171  int * jatf = &jat[0];
172  int * iatf = &iat[0];
173  MATTRANS_F77( &n, &n, &ja[0], &ia[0], &jat[0], &iat[0] );
174 */
175 
176  if( verbose_ )
177  {
178  cout << "-----------------------------------------\n";
179  cout << "CCS Format Graph\n";
180  cout << "-----------------------------------------\n";
181  for( int i = 0; i < n; ++i )
182  {
183  cout << i+1 << ": " << iat[i+1] << ": ";
184  for( int j = iat[i]-1; j<iat[i+1]-1; ++j )
185  cout << " " << jat[j];
186  cout << endl;
187  }
188  cout << "-----------------------------------------\n";
189  }
190 
191  vector<int> w(10*n);
192 
193  vector<int> rowperm(n);
194  vector<int> colperm(n);
195 
196  //horizontal block
197  int nhrows, nhcols, hrzcmp;
198  //square block
199  int nsrows, sqcmpn;
200  //vertial block
201  int nvrows, nvcols, vrtcmp;
202 
203  vector<int> rcmstr(n+1);
204  vector<int> ccmstr(n+1);
205 
206  int msglvl = 0;
207  int output = 6;
208 
209  GENBTF_F77( &n, &n, &iat[0], &jat[0], &ia[0], &ja[0], &w[0],
210  &rowperm[0], &colperm[0], &nhrows, &nhcols,
211  &hrzcmp, &nsrows, &sqcmpn, &nvrows, &nvcols, &vrtcmp,
212  &rcmstr[0], &ccmstr[0], &msglvl, &output );
213 
214  //convert back to C indexing
215  for( int i = 0; i < n; ++i )
216  {
217  --rowperm[i];
218  --colperm[i];
219  }
220  for( int i = 0; (i<n+1) && (rcmstr[i]!=n+1); ++i )
221  {
222  --rcmstr[i];
223  --ccmstr[i];
224  }
225 
226  if( verbose_ )
227  {
228  cout << "-----------------------------------------\n";
229  cout << "BTF Output\n";
230  cout << "-----------------------------------------\n";
231  cout << "RowPerm and ColPerm\n";
232  for( int i = 0; i<n; ++i )
233  cout << rowperm[i] << "\t" << colperm[i] << endl;
234  if( hrzcmp )
235  {
236  cout << "Num Horizontal: Rows, Cols, Comps\n";
237  cout << nhrows << "\t" << nhcols << "\t" << hrzcmp << endl;
238  }
239  cout << "Num Square: Rows, Comps\n";
240  cout << nsrows << "\t" << sqcmpn << endl;
241  if( vrtcmp )
242  {
243  cout << "Num Vertical: Rows, Cols, Comps\n";
244  cout << nvrows << "\t" << nvcols << "\t" << vrtcmp << endl;
245  }
246  cout << "Row, Col of upper left pt in blocks\n";
247  for( int i = 0; (i<n+1) && (rcmstr[i]!=n+1); ++i )
248  cout << i << " " << rcmstr[i] << " " << ccmstr[i] << endl;
249  cout << "-----------------------------------------\n";
250  }
251 
252  if( hrzcmp || vrtcmp )
253  {
254  cout << "FAILED! hrz cmp's:" << hrzcmp << " vrtcmp: " << vrtcmp << endl;
255  exit(0);
256  }
257 
258  //convert rowperm to OLD->NEW
259  //reverse ordering of permutation to get upper triangular
260  vector<int> rowperm_t( n );
261  vector<int> colperm_t( n );
262  for( int i = 0; i < n; ++i )
263  {
264 // rowperm_t[ rowperm[i] ] = i;
265  rowperm_t[i] = rowperm[i];
266  colperm_t[i] = colperm[i];
267  }
268 
269  //Generate New Domain and Range Maps
270  //for now, assume they start out as identical
271  vector<int> myElements( n );
272  OldRowMap.MyGlobalElements( &myElements[0] );
273 
274  vector<int> newDomainElements( n );
275  vector<int> newRangeElements( n );
276  for( int i = 0; i < n; ++i )
277  {
278  newDomainElements[ i ] = myElements[ rowperm_t[i] ];
279  newRangeElements[ i ] = myElements[ colperm_t[i] ];
280  }
281 
282  NewRowMap_ = new Epetra_Map( n, n, &newDomainElements[0], OldRowMap.IndexBase(), OldRowMap.Comm() );
283  NewColMap_ = new Epetra_Map( n, n, &newRangeElements[0], OldColMap.IndexBase(), OldColMap.Comm() );
284 
285  if( verbose_ )
286  {
287  cout << "New Row Map\n";
288  cout << *NewRowMap_ << endl;
289  cout << "New ColMap\n";
290  cout << *NewColMap_ << endl;
291  }
292 
293  //Generate New Graph
294  NewGraph_ = new Epetra_CrsGraph( Copy, *NewRowMap_, *NewColMap_, 0 );
295  Importer_ = new Epetra_Import( *NewRowMap_, OldRowMap );
296  NewGraph_->Import( strippedGraph, *Importer_, Insert );
297  NewGraph_->FillComplete();
298  if( verbose_ )
299  {
300  cout << "NewGraph\n";
301  cout << *NewGraph_;
302  }
303 
304  NewMatrix_ = new Epetra_CrsMatrix( Copy, *NewGraph_ );
305  NewMatrix_->Import( orig, *Importer_, Insert );
306  NewMatrix_->FillComplete();
307 
308  if( verbose_ )
309  {
310  cout << "New CrsMatrix\n";
311  cout << *NewMatrix_ << endl;
312  }
313 
314  newObj_ = NewMatrix_;
315 
316  return *NewMatrix_;
317 }
318 
319 bool
322 {
323  int ret = NewMatrix_->Import( *origObj_, *Importer_, Insert );
324  if (ret<0) return false;
325  return true;
326 }
327 
328 bool
331 {
332  int ret = origObj_->Export( *NewMatrix_, *Importer_, Insert );
333  if (ret<0) return false;
334  return true;
335 }
336 
337 } //namespace EpetraExt
#define MATTRANS_F77
NewTypeRef operator()(OriginalTypeRef orig)
Analysis of transform operation on original object and construction of new object.
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
bool fwd()
Forward transfer of data from orig object input in the operator() method call to the new object creat...
#define GENBTF_F77
bool rvs()
Reverse transfer of data from new object created in the operator() method call to the orig object inp...