EpetraExt  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_BTF_CrsGraph.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 #include <EpetraExt_BTF_CrsGraph.h>
42 
43 #include <Epetra_Import.h>
44 #include <Epetra_CrsGraph.h>
45 #include <Epetra_Map.h>
46 
47 #include <vector>
48 
49 using std::vector;
50 
51 #define MATTRANS_F77 F77_FUNC(mattrans,MATTRANS)
52 #define GENBTF_F77 F77_FUNC(genbtf,GENBTF)
53 extern "C" {
54 extern void MATTRANS_F77( int*, int*, int*, int*, int*, int* );
55 extern void GENBTF_F77( int*, int*, int*, int*, int*, int*, int*, int*, int*,
56  int*, int*, int*, int*, int*, int*, int*, int*, int*,
57  int*, int*, int* );
58 }
59 
60 namespace EpetraExt {
61 
64 {
65  if( NewRowMap_ ) delete NewRowMap_;
66  if( NewDomainMap_ ) delete NewDomainMap_;
67 
68  if( NewGraph_ ) delete NewGraph_;
69 }
70 
74 {
75  origObj_ = &orig;
76 
77  if( orig.RowMap().DistributedGlobal() )
78  { cout << "FAIL for Global!\n"; abort(); }
79  if( orig.IndicesAreGlobal() )
80  { cout << "FAIL for Global Indices!\n"; abort(); }
81 
82  int n = orig.NumMyRows();
83  int nnz = orig.NumMyNonzeros();
84 
85  //create std CRS format
86  vector<int> ia(n+1,0);
87  vector<int> ja(nnz);
88  int cnt;
89  for( int i = 0; i < n; ++i )
90  {
91  int * tmpP = &ja[ia[i]];
92  orig.ExtractMyRowCopy( i, nnz-ia[i], cnt, tmpP );
93  ia[i+1] = ia[i] + cnt;
94  }
95 
96  //convert to Fortran indexing
97  for( int i = 0; i < n+1; ++i ) ++ia[i];
98  for( int i = 0; i < nnz; ++i ) ++ja[i];
99 
100 #ifdef BTF_VERBOSE
101  cout << "-----------------------------------------\n";
102  cout << "CRS Format Graph\n";
103  cout << "-----------------------------------------\n";
104  for( int i = 0; i < n; ++i )
105  {
106  cout << i << ": " << ia[i+1] << ": ";
107  for( int j = ia[i]-1; j<ia[i+1]-1; ++j )
108  cout << " " << ja[j];
109  cout << endl;
110  }
111  cout << "-----------------------------------------\n";
112 #endif
113 
114  vector<int> iat(n+1);
115  vector<int> jat(nnz);
116  int * jaf = &ja[0];
117  int * iaf = &ia[0];
118  int * jatf = &jat[0];
119  int * iatf = &iat[0];
120  MATTRANS_F77( &n, &n, jaf, iaf, jatf, iatf );
121 
122 #ifdef BTF_VERBOSE
123  cout << "-----------------------------------------\n";
124  cout << "CCS Format Graph\n";
125  cout << "-----------------------------------------\n";
126  for( int i = 0; i < n; ++i )
127  {
128  cout << i << ": " << iat[i+1] << ": ";
129  for( int j = iat[i]-1; j<iat[i+1]-1; ++j )
130  cout << " " << jat[j];
131  cout << endl;
132  }
133  cout << "-----------------------------------------\n";
134 #endif
135 
136  vector<int> w(10*n);
137 
138  vector<int> rowperm(n);
139  vector<int> colperm(n);
140 
141  //horizontal block
142  int nhrows, nhcols, hrzcmp;
143  //square block
144  int nsrows, sqcmpn;
145  //vertial block
146  int nvrows, nvcols, vrtcmp;
147 
148  vector<int> rcmstr(n+1);
149  vector<int> ccmstr(n+1);
150 
151  int msglvl = 0;
152  int output = 6;
153 
154  GENBTF_F77( &n, &n, &iat[0], &jat[0], &ia[0], &ja[0], &w[0],
155  &rowperm[0], &colperm[0], &nhrows, &nhcols,
156  &hrzcmp, &nsrows, &sqcmpn, &nvrows, &nvcols, &vrtcmp,
157  &rcmstr[0], &ccmstr[0], &msglvl, &output );
158 
159  //convert back to C indexing
160  for( int i = 0; i < n; ++i )
161  {
162  --rowperm[i];
163  --colperm[i];
164  }
165  for( int i = 0; (i<n+1) && (rcmstr[i]!=n+1); ++i )
166  {
167  --rcmstr[i];
168  --ccmstr[i];
169  }
170 
171 #ifdef BTF_VERBOSE
172  cout << "-----------------------------------------\n";
173  cout << "BTF Output\n";
174  cout << "-----------------------------------------\n";
175  cout << "RowPerm and ColPerm\n";
176  for( int i = 0; i<n; ++i )
177  cout << rowperm[i] << "\t" << colperm[i] << endl;
178  if( hrzcmp )
179  {
180  cout << "Num Horizontal: Rows, Cols, Comps\n";
181  cout << nhrows << "\t" << nhcols << "\t" << hrzcmp << endl;
182  }
183  cout << "Num Square: Rows, Comps\n";
184  cout << nsrows << "\t" << sqcmpn << endl;
185  if( vrtcmp )
186  {
187  cout << "Num Vertical: Rows, Cols, Comps\n";
188  cout << nvrows << "\t" << nvcols << "\t" << vrtcmp << endl;
189  }
190  cout << "Row, Col of upper left pt in blocks\n";
191  for( int i = 0; (i<n+1) && (rcmstr[i]!=n+1); ++i )
192  cout << i << " " << rcmstr[i] << " " << ccmstr[i] << endl;
193  cout << "-----------------------------------------\n";
194 #endif
195 
196  if( hrzcmp || vrtcmp )
197  { cout << "FAILED! hrz cmp's:" << hrzcmp << " vrtcmp: " << vrtcmp << endl;
198  exit(0); }
199 
200  //convert rowperm to OLD->NEW
201  //reverse ordering of permutation to get upper triangular
202  vector<int> rowperm_t( n );
203  vector<int> colperm_t( n );
204  for( int i = 0; i < n; ++i )
205  {
206 // rowperm_t[ rowperm[i] ] = n-i;
207 // colperm[i] = n-colperm[i];
208  rowperm_t[i] = rowperm[(n-1)-i];
209  colperm_t[i] = colperm[(n-1)-i];
210  }
211 
212  //Generate New Domain and Range Maps
213  //for now, assume they start out as identical
214  const Epetra_BlockMap & OldMap = orig.RowMap();
215  vector<int> myElements( n );
216  OldMap.MyGlobalElements( &myElements[0] );
217 
218  vector<int> newDomainElements( n );
219  vector<int> newRangeElements( n );
220  for( int i = 0; i < n; ++i )
221  {
222  newDomainElements[ i ] = myElements[ rowperm_t[i] ];
223  newRangeElements[ i ] = myElements[ colperm_t[i] ];
224 cout << i << "\t" << rowperm_t[i] << "\t" << colperm[i] << "\t" << myElements[i] << endl;
225  }
226 
227  NewRowMap_ = new Epetra_Map( n, n, &newDomainElements[0], OldMap.IndexBase(), OldMap.Comm() );
228  NewDomainMap_ = new Epetra_Map( n, n, &newRangeElements[0], OldMap.IndexBase(), OldMap.Comm() );
229 
230 #ifdef BTF_VERBOSE
231  cout << "New Row Map\n";
232  cout << *RowMap << endl;
233  cout << "New Domain Map\n";
234  cout << *DomainMap << endl;
235 #endif
236 
237  //Generate New Graph
238  NewGraph_ = new Epetra_CrsGraph( Copy, *NewRowMap_, 0 );
239  Epetra_Import Importer( *NewRowMap_, OldMap );
240  NewGraph_->Import( orig, Importer, Insert );
241  NewGraph_->FillComplete( *NewDomainMap_, *NewRowMap_ );
242 
243 #ifdef BTF_VERBOSE
244  cout << "New CrsGraph\n";
245  cout << *NewGraph_ << endl;
246 #endif
247 
248  newObj_ = NewGraph_;
249 
250  return *NewGraph_;
251 }
252 
253 } //namespace EpetraExt
NewTypeRef operator()(OriginalTypeRef orig)
Construction of BTF ordered Epetra_CrsGraph from orig object.
#define GENBTF_F77
#define MATTRANS_F77