44 #include <Epetra_CrsMatrix.h> 45 #include <Epetra_CrsGraph.h> 46 #include <Epetra_Map.h> 47 #include <Epetra_Comm.h> 56 return(*((
int *) a) - *((
int *) b));
62 while ((n > m) && (l < 31)) {
89 Teuchos::RCP<Epetra_CrsGraph>
BlockAdjacencyGraph::compute( Epetra_CrsGraph& B,
int nbrr, std::vector<int>&r, std::vector<double>& weights,
bool verbose)
92 int myMatProc = -1, matProc = -1;
93 int myPID = B.Comm().MyPID();
94 for (
int proc=0; proc<B.Comm().NumProc(); proc++)
96 if (B.NumGlobalEntries() == B.NumMyEntries())
99 B.Comm().MaxAll( &myMatProc, &matProc, 1 );
102 { cout <<
"FAIL for Global! All CrsGraph entries must be on one processor!\n"; abort(); }
104 int i= 0, j = 0, k, l = 0, p, pm, q = -1, ns;
112 std::vector<int> Mi, Mj, Mnum(nbrr+1,0);
114 if ( matProc == myPID && verbose )
115 std::printf(
" Matrix Size = %d Number of Blocks = %d\n",nrr, nbrr);
118 bstree = csr_bst(nbrr);
123 for( i = 0; i < nrr; i++ ){
126 m = EPETRA_MAX(m,j) ;
127 j = B.NumGlobalIndices(i);
129 j += B.NumGlobalIndices(i);
133 m = EPETRA_MAX(m,j) ;
135 colstack = (
int*) malloc( EPETRA_MAX(m,1) *
sizeof(int) );
141 nzM = 0; q = -1; l = 0;
144 for( i = 0; i <= nrr; i++ ){
146 if( q > 0 ) std::qsort(colstack,q+1,
sizeof(
int),
compare_ints);
148 for( j=1; j<=q ; j++ ){
149 if( colstack[j] > colstack[j-1] ) ++ns;
156 B.ExtractMyRowView( i, numEntries, indices );
157 for( k = 0; k < numEntries; k++){
158 j = indices[k]; ns = 0; p = 0;
159 while( (r[bstree[p]] > j) || (j >= r[bstree[p]+1]) ){
160 if( r[bstree[p]] > j){
163 if( r[bstree[p]+1] <= j) p = 2*p+2;
166 if( p > nbrr || ns > tree_height ) {
168 std::printf(
"error: p %d nbrr %d ns %d %d\n",p,nbrr,ns,j);
break;
171 colstack[++q] = bstree[p];
178 if ( matProc == myPID && verbose )
179 std::printf(
"nzM = %d \n", nzM );
182 q = -1; l = 0; pm = -1;
183 for( i = 0; i <= nrr; i++ ){
185 if( q > 0 ) std::qsort(colstack,q+1,
sizeof(colstack[0]),
compare_ints);
188 Mj[pm] = colstack[0];
190 for( j=1; j<=q ; j++ ){
191 if( colstack[j] > colstack[j-1] ){
193 Mj[pm] = colstack[j];
203 B.ExtractMyRowView( i, numEntries, indices );
204 for( k = 0; k < numEntries; k++){
205 j = indices[k]; ns = 0; p = 0;
206 while( (r[bstree[p]] > j) || (j >= r[bstree[p]+1]) ){
207 if( r[bstree[p]] > j){
210 if( r[bstree[p]+1] <= j) p = 2*p+2;
214 colstack[++q] = bstree[p];
218 if ( bstree ) free ( bstree );
219 if ( colstack ) free( colstack );
222 weights.resize( nbrr );
223 for( l=0; l<nbrr; l++) weights[l] = r[l+1] - r[l];
226 Teuchos::RCP<Epetra_Map> newMap;
227 if ( matProc == myPID )
228 newMap = Teuchos::rcp(
new Epetra_Map(nbrr, nbrr, 0, B.Comm() ) );
230 newMap = Teuchos::rcp(
new Epetra_Map( nbrr, 0, 0, B.Comm() ) );
231 Teuchos::RCP<Epetra_CrsGraph> newGraph = Teuchos::rcp(
new Epetra_CrsGraph( Copy, *newMap, 0 ) );
232 for( l=0; l<newGraph->NumMyRows(); l++) {
233 newGraph->InsertGlobalIndices( l, Mnum[l+1]-Mnum[l], &Mj[Mnum[l]] );
235 newGraph->FillComplete();
249 int* BlockAdjacencyGraph::csr_bst(
int n )
251 int i, l=1, m, nstack = 0, nexp=0, os, *array, *stack;
253 if( n == 0 )
return(NULL);
258 array = (
int *) malloc( n *
sizeof(
int) );
259 stack = (
int *) malloc(3*nexp *
sizeof(
int) );
260 stack[3*nstack] = 0; stack[3*nstack+1] = 0; stack[3*nstack+2] = n;
265 i = stack[3*nstack]; os = stack[3*nstack+1]; m = stack[3*nstack+2];
266 array[i] = csr_bstrootindex(m) + os;
268 stack[3*nstack] = 2*i+2; stack[3*nstack+1] = array[i] + 1 ; stack[3*nstack+2] = m-array[i]-1+os;
272 stack[3*nstack] = 2*i+1; stack[3*nstack+1] = os; stack[3*nstack+2] = array[i] - os;
275 if( nstack > max_nstack ) max_nstack = nstack;
286 int BlockAdjacencyGraph::csr_bstrootindex(
int n )
288 int l = 1, nexp = 0, i;
289 if( n == 0)
return(-1);
Teuchos::RCP< Epetra_CrsGraph > compute(Epetra_CrsGraph &B, int nbrr, std::vector< int > &r, std::vector< double > &weights, bool verbose=false)
Constructs an adjacency graph representing the block connectivity of the input graph, where nbrr is the number of block rows in B and r contains the row index where each block begins.
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
int compare_ints(const void *a, const void *b)
Given an Epetra_CrsGraph that has block structure, an adjacency graph is constructed representing the...