EpetraExt  Development
EpetraExt_BTF_LinearProblem.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_CrsMatrix.h>
45 #include <Epetra_VbrMatrix.h>
46 #include <Epetra_CrsGraph.h>
47 #include <Epetra_Map.h>
48 #include <Epetra_BlockMap.h>
49 #include <Epetra_MultiVector.h>
50 #include <Epetra_LinearProblem.h>
51 
52 #include <set>
53 
54 using std::vector;
55 using std::map;
56 using std::set;
57 
58 #define MATTRANS_F77 F77_FUNC(mattrans,MATTRANS)
59 #define GENBTF_F77 F77_FUNC(genbtf,GENBTF)
60 
61 extern "C" {
62 extern void MATTRANS_F77( int*, int*, int*, int*, int*, int* );
63 extern void GENBTF_F77( int*, int*, int*, int*, int*, int*, int*, int*, int*,
64  int*, int*, int*, int*, int*, int*, int*, int*, int*,
65  int*, int*, int* );
66 }
67 
68 namespace EpetraExt {
69 
72 {
73  deleteNewObjs_();
74 }
75 
76 void
77 LinearProblem_BTF::
78 deleteNewObjs_()
79 {
80  if( NewProblem_ ) delete NewProblem_;
81 
82  if( NewMatrix_ ) delete NewMatrix_;
83 
84  if( NewLHS_ ) delete NewLHS_;
85  if( NewRHS_ ) delete NewRHS_;
86 
87  if( NewMap_ ) delete NewMap_;
88 
89  for( int i = 0; i < Blocks_.size(); ++i )
90  for( int j = 0; j < Blocks_[i].size(); ++j )
91  delete Blocks_[i][j];
92 }
93 
97 {
98  changedLP_ = false;
99 
100  //check if there is a previous analysis and if it is valid
101  if( &orig == origObj_ && NewProblem_ )
102  {
103  int * indices;
104  double * values;
105  int currCnt;
106  int numRows = OrigMatrix_->NumMyRows();
107 
108  for( int i = 0; i < numRows; ++i )
109  if( ZeroElements_[i].size() )
110  {
111  int loc = 0;
112  OrigMatrix_->ExtractMyRowView( i, currCnt, values, indices );
113  for( int j = 0; j < currCnt; ++j )
114  if( ZeroElements_[i].count( indices[j] ) )
115  {
116  if( values[j] > threshold_ ) changedLP_ = true;
117  ++loc;
118  }
119  }
120 
121 // changedLP_ = true;
122 
123  //if changed, dump all the old stuff and start over
124  if( changedLP_ )
125  deleteNewObjs_();
126  else
127  return *newObj_;
128  }
129 
130  origObj_ = &orig;
131 
132  OrigMatrix_ = dynamic_cast<Epetra_CrsMatrix*>(orig.GetMatrix());
133  OrigLHS_ = orig.GetLHS();
134  OrigRHS_ = orig.GetRHS();
135 
136  if( OrigMatrix_->RowMap().DistributedGlobal() )
137  { cout << "FAIL for Global!\n"; abort(); }
138  if( OrigMatrix_->IndicesAreGlobal() )
139  { cout << "FAIL for Global Indices!\n"; abort(); }
140 
141  int n = OrigMatrix_->NumMyRows();
142  int nnz = OrigMatrix_->NumMyNonzeros();
143 
144 // cout << "Orig Matrix:\n";
145 // cout << *OrigMatrix_ << endl;
146 
147  //create std CRS format
148  //also create graph without zero elements
149  vector<int> ia(n+1,0);
150  int maxEntries = OrigMatrix_->MaxNumEntries();
151  vector<int> ja_tmp(maxEntries);
152  vector<double> jva_tmp(maxEntries);
153  vector<int> ja(nnz);
154  int cnt;
155 
156  const Epetra_BlockMap & OldRowMap = OrigMatrix_->RowMap();
157  const Epetra_BlockMap & OldColMap = OrigMatrix_->ColMap();
158  Epetra_CrsGraph strippedGraph( Copy, OldRowMap, OldColMap, 0 );
159  ZeroElements_.resize(n);
160 
161  for( int i = 0; i < n; ++i )
162  {
163  ZeroElements_[i].clear();
164  OrigMatrix_->ExtractMyRowCopy( i, maxEntries, cnt, &jva_tmp[0], &ja_tmp[0] );
165  ia[i+1] = ia[i];
166  for( int j = 0; j < cnt; ++j )
167  {
168  if( fabs(jva_tmp[j]) > threshold_ )
169  ja[ ia[i+1]++ ] = ja_tmp[j];
170  else
171  ZeroElements_[i].insert( ja_tmp[j] );
172  }
173 
174  int new_cnt = ia[i+1] - ia[i];
175  strippedGraph.InsertMyIndices( i, new_cnt, &ja[ ia[i] ] );
176  }
177  nnz = ia[n];
178  strippedGraph.FillComplete();
179 
180  if( verbose_ > 2 )
181  {
182  cout << "Stripped Graph\n";
183  cout << strippedGraph;
184  }
185 
186  vector<int> iat(n+1,0);
187  vector<int> jat(nnz);
188  for( int i = 0; i < n; ++i )
189  for( int j = ia[i]; j < ia[i+1]; ++j )
190  ++iat[ ja[j]+1 ];
191  for( int i = 0; i < n; ++i )
192  iat[i+1] += iat[i];
193  for( int i = 0; i < n; ++i )
194  for( int j = ia[i]; j < ia[i+1]; ++j )
195  jat[ iat[ ja[j] ]++ ] = i;
196  for( int i = 0; i < n; ++i )
197  iat[n-i] = iat[n-i-1];
198  iat[0] = 0;
199 
200  //convert to Fortran indexing
201  for( int i = 0; i < n+1; ++i ) ++ia[i];
202  for( int i = 0; i < nnz; ++i ) ++ja[i];
203  for( int i = 0; i < n+1; ++i ) ++iat[i];
204  for( int i = 0; i < nnz; ++i ) ++jat[i];
205 
206  if( verbose_ > 2 )
207  {
208  cout << "-----------------------------------------\n";
209  cout << "CRS Format Graph\n";
210  cout << "-----------------------------------------\n";
211  for( int i = 0; i < n; ++i )
212  {
213  cout << i+1 << ": " << ia[i+1] << ": ";
214  for( int j = ia[i]-1; j<ia[i+1]-1; ++j )
215  cout << " " << ja[j];
216  cout << endl;
217  }
218  cout << "-----------------------------------------\n";
219  }
220 
221  if( verbose_ > 2 )
222  {
223  cout << "-----------------------------------------\n";
224  cout << "CCS Format Graph\n";
225  cout << "-----------------------------------------\n";
226  for( int i = 0; i < n; ++i )
227  {
228  cout << i+1 << ": " << iat[i+1] << ": ";
229  for( int j = iat[i]-1; j<iat[i+1]-1; ++j )
230  cout << " " << jat[j];
231  cout << endl;
232  }
233  cout << "-----------------------------------------\n";
234  }
235 
236  vector<int> w(10*n);
237 
238  vector<int> rowperm(n);
239  vector<int> colperm(n);
240 
241  //horizontal block
242  int nhrows, nhcols, hrzcmp;
243  //square block
244  int nsrows, sqcmpn;
245  //vertial block
246  int nvrows, nvcols, vrtcmp;
247 
248  vector<int> rcmstr(n+1);
249  vector<int> ccmstr(n+1);
250 
251  int msglvl = 0;
252  int output = 6;
253 
254  GENBTF_F77( &n, &n, &iat[0], &jat[0], &ia[0], &ja[0], &w[0],
255  &rowperm[0], &colperm[0], &nhrows, &nhcols,
256  &hrzcmp, &nsrows, &sqcmpn, &nvrows, &nvcols, &vrtcmp,
257  &rcmstr[0], &ccmstr[0], &msglvl, &output );
258 
259  //convert back to C indexing
260  for( int i = 0; i < n; ++i )
261  {
262  --rowperm[i];
263  --colperm[i];
264  }
265  for( int i = 0; (i<n+1) && (rcmstr[i]!=n+1); ++i )
266  {
267  --rcmstr[i];
268  --ccmstr[i];
269  }
270 
271  if( verbose_ > 0 )
272  {
273  cout << "-----------------------------------------\n";
274  cout << "BTF Output\n";
275  cout << "-----------------------------------------\n";
276 // cout << "RowPerm and ColPerm\n";
277 // for( int i = 0; i<n; ++i )
278 // cout << rowperm[i] << "\t" << colperm[i] << endl;
279  if( hrzcmp )
280  {
281  cout << "Num Horizontal: Rows, Cols, Comps\n";
282  cout << nhrows << "\t" << nhcols << "\t" << hrzcmp << endl;
283  }
284  cout << "Num Square: Rows, Comps\n";
285  cout << nsrows << "\t" << sqcmpn << endl;
286  if( vrtcmp )
287  {
288  cout << "Num Vertical: Rows, Cols, Comps\n";
289  cout << nvrows << "\t" << nvcols << "\t" << vrtcmp << endl;
290  }
291 // cout << "Row, Col of upper left pt in blocks\n";
292 // for( int i = 0; (i<n+1) && (rcmstr[i]!=n+1); ++i )
293 // cout << i << " " << rcmstr[i] << " " << ccmstr[i] << endl;
294 // cout << "-----------------------------------------\n";
295  }
296 
297  if( hrzcmp || vrtcmp )
298  {
299  cout << "FAILED! hrz cmp's:" << hrzcmp << " vrtcmp: " << vrtcmp << endl;
300  exit(0);
301  }
302 
303  rcmstr[sqcmpn] = n;
304 
305  //convert rowperm to OLD->NEW
306  //reverse ordering of permutation to get upper triangular
307  vector<int> rowperm_t( n );
308  vector<int> colperm_t( n );
309  for( int i = 0; i < n; ++i )
310  {
311  rowperm_t[i] = rowperm[i];
312  colperm_t[i] = colperm[i];
313  }
314 
315  //Generate New Domain and Range Maps
316  //for now, assume they start out as identical
317  OldGlobalElements_.resize(n);
318  OldRowMap.MyGlobalElements( &OldGlobalElements_[0] );
319 
320  vector<int> newDomainElements( n );
321  vector<int> newRangeElements( n );
322  for( int i = 0; i < n; ++i )
323  {
324  newDomainElements[ i ] = OldGlobalElements_[ rowperm_t[i] ];
325  newRangeElements[ i ] = OldGlobalElements_[ colperm_t[i] ];
326  }
327 
328  //Setup New Block Map Info
329  Blocks_.resize( sqcmpn );
330  BlockDim_.resize( sqcmpn );
331  for( int i = 0; i < sqcmpn; ++i )
332  {
333  BlockDim_[i] = rcmstr[i+1]-rcmstr[i];
334  for( int j = rcmstr[i]; j < rcmstr[i+1]; ++j )
335  {
336  BlockRowMap_[ newDomainElements[j] ] = i;
337  SubBlockRowMap_[ newDomainElements[j] ] = j-rcmstr[i];
338  BlockColMap_[ newRangeElements[j] ] = i;
339  SubBlockColMap_[ newRangeElements[j] ] = j-rcmstr[i];
340  }
341  }
342 
343  if( verbose_ > 2 )
344  {
345 /*
346  cout << "Block Mapping!\n";
347  cout << "--------------------------\n";
348  for( int i = 0; i < n; ++i )
349  {
350  cout << "Row: " << newDomainElements[i] << " " << BlockRowMap_[newDomainElements[i]] << " " <<
351  SubBlockRowMap_[newDomainElements[i]] << "\t" << "Col: " << newRangeElements[i] << " " <<
352  BlockColMap_[newRangeElements[i]] << " " << SubBlockColMap_[newRangeElements[i]] << endl;
353  }
354  for( int i = 0; i < sqcmpn; ++i )
355  cout << "BlockDim: " << i << " " << BlockDim_[i] << endl;
356  cout << "--------------------------\n";
357 */
358  int MinSize = 1000000000;
359  int MaxSize = 0;
360  for( int i = 0; i < sqcmpn; ++i )
361  {
362  if( MinSize > BlockDim_[i] ) MinSize = BlockDim_[i];
363  if( MaxSize < BlockDim_[i] ) MaxSize = BlockDim_[i];
364  }
365  cout << "Min Block Size: " << MinSize << " " << "Max Block Size: " << MaxSize << endl;
366  }
367 
368  vector<int> myBlockElements( sqcmpn );
369  for( int i = 0; i < sqcmpn; ++i ) myBlockElements[i] = i;
370  NewMap_ = new Epetra_BlockMap( sqcmpn, sqcmpn, &myBlockElements[0], &BlockDim_[0], 0, OldRowMap.Comm() );
371 
372  if( verbose_ > 2 )
373  {
374  cout << "New Block Map!\n";
375  cout << *NewMap_;
376  }
377 
378  //setup new graph
379  vector< set<int> > crsBlocks( sqcmpn );
380  BlockCnt_.resize( sqcmpn );
381  int maxLength = strippedGraph.MaxNumIndices();
382  vector<int> sIndices( maxLength );
383  int currLength;
384  for( int i = 0; i < n; ++i )
385  {
386  strippedGraph.ExtractGlobalRowCopy( OldGlobalElements_[i], maxLength, currLength, &sIndices[0] );
387  for( int j = 0; j < currLength; ++j )
388  crsBlocks[ BlockRowMap_[ OldGlobalElements_[i] ] ].insert( BlockColMap_[ sIndices[j] ] );
389  }
390 
391  for( int i = 0; i < sqcmpn; ++i )
392  {
393  BlockCnt_[i] = crsBlocks[i].size();
394  Blocks_[i].resize( BlockCnt_[i] );
395  }
396 
397  NewBlockRows_.resize( sqcmpn );
398  for( int i = 0; i < sqcmpn; ++i )
399  {
400  NewBlockRows_[i] = vector<int>( crsBlocks[i].begin(), crsBlocks[i].end() );
401  for( int j = 0; j < BlockCnt_[i]; ++j )
402  {
403  Blocks_[i][j] = new Epetra_SerialDenseMatrix();
404  Blocks_[i][j]->Shape( BlockDim_[i], BlockDim_[ NewBlockRows_[i][j] ] );
405  }
406  }
407 
408  //put data in Blocks_ and new LHS and RHS
409  NewLHS_ = new Epetra_MultiVector( *NewMap_, 1 );
410  NewRHS_ = new Epetra_MultiVector( *NewMap_, 1 );
411 
412  maxLength = OrigMatrix_->MaxNumEntries();
413  vector<int> indices( maxLength );
414  vector<double> values( maxLength );
415  for( int i = 0; i < n; ++i )
416  {
417  int BlockRow = BlockRowMap_[ OldGlobalElements_[i] ];
418  int SubBlockRow = SubBlockRowMap_[ OldGlobalElements_[i] ];
419  OrigMatrix_->ExtractGlobalRowCopy( OldGlobalElements_[i], maxLength, currLength, &values[0], &indices[0] );
420  for( int j = 0; j < currLength; ++j )
421  {
422  int BlockCol = BlockColMap_[ indices[j] ];
423  int SubBlockCol = SubBlockColMap_[ indices[j] ];
424  for( int k = 0; k < BlockCnt_[BlockRow]; ++k )
425  if( BlockCol == NewBlockRows_[BlockRow][k] )
426  {
427  if( values[j] > threshold_ )
428  {
429 // cout << BlockRow << " " << SubBlockRow << " " << BlockCol << " " << SubBlockCol << endl;
430 // cout << k << endl;
431  (*(Blocks_[BlockRow][k]))(SubBlockRow,SubBlockCol) = values[j];
432  break;
433  }
434  else
435  ZeroElements_[i].erase( OrigMatrix_->RowMap().LID( indices[j] ) );
436  }
437  }
438 
439 // NewLHS_->ReplaceGlobalValue( BlockCol, SubBlockCol, 0, (*OrigLHS_)[0][i] );
440  NewRHS_->ReplaceGlobalValue( BlockRow, SubBlockRow, 0, (*OrigRHS_)[0][i] );
441  }
442 
443  if( verbose_ > 2 )
444  {
445  cout << "Zero Elements: \n";
446  cout << "--------------\n";
447  int cnt = 0;
448  for( int i = 0; i < n; ++i )
449  {
450  set<int>::iterator iterSI = ZeroElements_[i].begin();
451  set<int>::iterator endSI = ZeroElements_[i].end();
452  for( ; iterSI != endSI; ++iterSI )
453  {
454  cout << " " << *iterSI;
455  ++cnt;
456  }
457  cout << endl;
458  }
459  cout << "ZE Cnt: " << cnt << endl;
460  cout << "--------------\n";
461  }
462 
463  //setup new matrix
464  NewMatrix_ = new Epetra_VbrMatrix( View, *NewMap_, &BlockCnt_[0] );
465  for( int i = 0; i < sqcmpn; ++i )
466  {
467  NewMatrix_->BeginInsertGlobalValues( i, BlockCnt_[i], &(NewBlockRows_[i])[0] );
468  for( int j = 0; j < BlockCnt_[i]; ++j )
469  NewMatrix_->SubmitBlockEntry( *(Blocks_[i][j]) );
470  NewMatrix_->EndSubmitEntries();
471  }
472  NewMatrix_->FillComplete();
473 
474  if( verbose_ > 2 )
475  {
476  cout << "New Block Matrix!\n";
477  cout << *NewMatrix_;
478  cout << "New Block LHS!\n";
479  cout << *NewLHS_;
480  cout << "New Block RHS!\n";
481  cout << *NewRHS_;
482  }
483 
484  //create new LP
485  NewProblem_ = new Epetra_LinearProblem( NewMatrix_, NewLHS_, NewRHS_ );
486  newObj_ = NewProblem_;
487 
488  if( verbose_ ) cout << "-----------------------------------------\n";
489 
490  return *newObj_;
491 }
492 
493 bool
496 {
497  //zero out matrix
498  int NumBlockRows = BlockDim_.size();
499  for( int i = 0; i < NumBlockRows; ++i )
500  {
501  int NumBlocks = BlockCnt_[i];
502  for( int j = 0; j < NumBlocks; ++j )
503  {
504  int Size = BlockDim_[i] * BlockDim_[ NewBlockRows_[i][j] ];
505  double * p = Blocks_[i][j]->A();
506  for( int k = 0; k < Size; ++k ) *p++ = 0.0;
507  }
508  }
509 
510  int maxLength = OrigMatrix_->MaxNumEntries();
511  int n = OldGlobalElements_.size();
512  int currLength;
513  vector<int> indices( maxLength );
514  vector<double> values( maxLength );
515  for( int i = 0; i < n; ++i )
516  {
517  int BlockRow = BlockRowMap_[ OldGlobalElements_[i] ];
518  int SubBlockRow = SubBlockRowMap_[ OldGlobalElements_[i] ];
519  OrigMatrix_->ExtractGlobalRowCopy( OldGlobalElements_[i], maxLength, currLength, &values[0], &indices[0] );
520  for( int j = 0; j < currLength; ++j )
521  {
522  if( fabs(values[j]) > threshold_ )
523  {
524  int BlockCol = BlockColMap_[ indices[j] ];
525  int SubBlockCol = SubBlockColMap_[ indices[j] ];
526  for( int k = 0; k < BlockCnt_[BlockRow]; ++k )
527  if( BlockCol == NewBlockRows_[BlockRow][k] )
528  {
529  (*(Blocks_[BlockRow][k]))(SubBlockRow,SubBlockCol) = values[j];
530  break;
531  }
532  }
533  }
534 
535  NewRHS_->ReplaceGlobalValue( BlockRow, SubBlockRow, 0, (*OrigRHS_)[0][i] );
536  }
537 
538 /*
539  //fill matrix
540  int sqcmpn = BlockDim_.size();
541  for( int i = 0; i < sqcmpn; ++i )
542  {
543  NewMatrix_->BeginReplaceGlobalValues( i, NewBlockRows_[i].size(), &(NewBlockRows_[i])[0] );
544  for( int j = 0; j < NewBlockRows_[i].size(); ++j )
545  NewMatrix_->SubmitBlockEntry( Blocks_[i][j]->A(), Blocks_[i][j]->LDA(), Blocks_[i][j]->M(), Blocks_[i][j]->N() );
546  NewMatrix_->EndSubmitEntries();
547  }
548 */
549 
550  return true;
551 }
552 
553 bool
556 {
557  //copy data from NewLHS_ to OldLHS_;
558  int rowCnt = OrigLHS_->MyLength();
559  for( int i = 0; i < rowCnt; ++i )
560  {
561  int BlockCol = BlockColMap_[ OldGlobalElements_[i] ];
562  int SubBlockCol = SubBlockColMap_[ OldGlobalElements_[i] ];
563  (*OrigLHS_)[0][i] = (*NewLHS_)[0][ NewMap_->FirstPointInElement(BlockCol) + SubBlockCol ];
564  }
565 
566  return true;
567 }
568 
569 } //namespace EpetraExt
bool fwd()
Forward transfer of data from orig object input in the operator() method call to the new object creat...
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
#define GENBTF_F77
NewTypeRef operator()(OriginalTypeRef orig)
Analysis of transform operation on original object and construction of new object.
bool rvs()
Reverse transfer of data from new object created in the operator() method call to the orig object inp...
#define MATTRANS_F77