Debian Codegraph @

packages/trilinoscouplings/src/epetraext/EpetraExt_AmesosBTF_CrsGraph.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Trilinos: An Object-Oriented Solver Framework
4 //
5 // Copyright 2001-2024 NTESS and the Trilinos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include <EpetraExt_AmesosBTF_CrsGraph.h>
11
12 #include <Epetra_Import.h>
13 #include <Epetra_CrsGraph.h>
14 #include <Epetra_Map.h>
15
16 #include <trilinos_btf_decl.h>
17
18 using std::vector;
19
20 namespace EpetraExt {
21
22 AmesosBTF_CrsGraph::
23 ~AmesosBTF_CrsGraph()
24 {
25 }
26
27 AmesosBTF_CrsGraph::NewTypeRef
28 AmesosBTF_CrsGraph::
29 operator()( OriginalTypeRef orig )
30 {
31 origObj_ = &orig;
32
33 if( orig.RowMap().DistributedGlobal() )
34 { std::cout << "FAIL for Global!\n"; abort(); }
35 if( orig.IndicesAreGlobal() )
36 { std::cout << "FAIL for Global Indices!\n"; abort(); }
37
38 // Extract the CCS information
39 int n = orig.NumMyRows();
40 int nnz = orig.NumMyNonzeros();
41 vector<int> Ai_tmp(nnz);
42
43 vector<int> Ap(n+1,0); // column pointers
44 vector<int> Ai(nnz); // row indices
45 int cnt;
46 for( int i = 0; i < n; ++i )
47 {
48 int * tmpP = &Ai[Ap[i]];
49 orig.ExtractMyRowCopy( i, nnz-Ap[i], cnt, tmpP );
50 Ap[i+1] = Ap[i] + cnt;
51 }
52
53 if (verbose_)
54 {
55 std::cout << "-----------------------------------------\n";
56 std::cout << "CRS Format Graph\n";
57 std::cout << "-----------------------------------------\n";
58 for( int i = 0; i < n; ++i )
59 {
60 std::cout << Ap[i] << " - " << Ap[i+1] << " : ";
61 for( int j = Ap[i]; j<Ap[i+1]; ++j )
62 std::cout << " " << Ai[j];
63 std::cout << std::endl;
64 }
65 std::cout << "-----------------------------------------\n";
66 }
67
68 // Transpose the graph, not the values
69 int j=0, next=0;
70 vector<int> Ap_tmp(n+1,0);
71
72 // Compute row lengths
73 for (int i = 0; i < n; i++)
74 for (int k = Ap[i]; k < Ap[i+1]; k++)
75 ++Ap_tmp[ Ai[k]+1 ];
76
77 // Compute pointers from row lengths
78 Ap_tmp[0] = 0;
79 for (int i = 0; i < n; i++)
80 Ap_tmp[i+1] += Ap_tmp[i];
81
82 // Copy over indices
83 for (int i = 0; i < n; i++) {
84 for (int k = Ap[i]; k < Ap[i+1]; k++) {
85 j = Ai[k];
86 next = Ap_tmp[j];
87 Ai_tmp[next] = i;
88 Ap_tmp[j] = next + 1;
89 }
90 }
91
92 // Reshift Ap_tmp
93 for (int i=n-1; i >= 0; i--) Ap_tmp[i+1] = Ap_tmp[i];
94 Ap_tmp[0] = 0;
95
96 // Transformation information
97 int numMatch = 0; // number of nonzeros on diagonal after permutation.
98 double maxWork = 0.0; // no limit on how much work to perform in max-trans.
99 double workPerf = 0.0; // how much work was performed in max-trans.
100
101 // Create a work vector for the BTF code.
102 vector<int> work(5*n);
103
104 // Storage for the row and column permutations.
105 vector<int> rowperm(n);
106 vector<int> colperm(n);
107 vector<int> blkPtr(n+1);
108
109 // NOTE: The permutations are sent in backwards since the matrix is transposed.
110 // On output, rowperm and colperm are the row and column permutations of A, where
111 // i = TRILINOS_BTF_UNFLIP(rowperm[k]) if row i of A is the kth row of P*A*Q, and j = colperm[k]
112 // if column j of A is the kth column of P*A*Q. If rowperm[k] < 0, then the
113 // (k,k)th entry in P*A*Q is structurally zero.
114
115 numBlocks_ = trilinos_btf_order( n, &Ap_tmp[0], &Ai_tmp[0], maxWork, &workPerf,
116 &rowperm[0], &colperm[0], &blkPtr[0],
117 &numMatch, &work[0] );
118
119 // Reverse ordering of permutation to get upper triangular form, if necessary
120 rowPerm_.resize( n );
121 colPerm_.resize( n );
122 blkPtr_.resize( numBlocks_+1 );
123 if (!upperTri_) {
124 for( int i = 0; i < n; ++i )
125 {
126 rowPerm_[i] = TRILINOS_BTF_UNFLIP(rowperm[(n-1)-i]);
127 colPerm_[i] = colperm[(n-1)-i];
128 }
129 for( int i = 0; i < numBlocks_+1; ++i )
130 blkPtr_[i] = n - blkPtr[numBlocks_-i];
131 }
132 else {
133 colPerm_ = colperm;
134 blkPtr_ = blkPtr;
135 for( int i = 0; i < n; ++i )
136 rowPerm_[i] = TRILINOS_BTF_UNFLIP(rowperm[i]);
137 }
138
139 if (verbose_)
140 {
141 std::cout << "-----------------------------------------\n";
142 std::cout << "BTF Output (n = " << n << ")\n";
143 std::cout << "-----------------------------------------\n";
144 std::cout << "Num Blocks: " << numBlocks_ << std::endl;
145 std::cout << "Num NNZ Diags: " << numMatch << std::endl;
146 std::cout << "RowPerm and ColPerm \n";
147 for( int i = 0; i<n; ++i )
148 std::cout << rowPerm_[i] << "\t" << colPerm_[i] << std::endl;
149 std::cout << "-----------------------------------------\n";
150 }
151
152 //Generate New Domain and Range Maps
153 //for now, assume they start out as identical
154 const Epetra_BlockMap & OldMap = orig.RowMap();
155 vector<int> newDomainElements( n );
156 vector<int> newRangeElements( n );
157 for( int i = 0; i < n; ++i )
158 {
159 newRangeElements[ i ] = rowPerm_[i];
160 newDomainElements[ i ] = colPerm_[i];
161 }
162
163 NewRowMap_ = Teuchos::rcp( new Epetra_Map( n, n, &newRangeElements[0], OldMap.IndexBase(), OldMap.Comm() ) );
164 NewColMap_ = Teuchos::rcp( new Epetra_Map( n, n, &newDomainElements[0], OldMap.IndexBase(), OldMap.Comm() ) );
165
166 if (verbose_)
167 {
168 std::cout << "New Row Map\n";
169 std::cout << *NewRowMap_ << std::endl;
170 std::cout << "New Col Map\n";
171 std::cout << *NewColMap_ << std::endl;
172 }
173
174 //Generate New Graph
175 NewGraph_ = Teuchos::rcp( new Epetra_CrsGraph( Copy, *NewRowMap_, *NewColMap_, 0 ) );
176 Epetra_Import Importer( *NewRowMap_, OldMap );
177 NewGraph_->Import( orig, Importer, Insert );
178 NewGraph_->FillComplete();
179
180 if (verbose_)
181 {
182 std::cout << "New CrsGraph\n";
183 std::cout << *NewGraph_ << std::endl;
184 }
185
186 newObj_ = &*NewGraph_;
187
188 return *NewGraph_;
189 }
190
191 } //namespace EpetraExt