1 /* 2 GAMG geometric-algebric multiogrid PC - Mark Adams 2011 3 */ 4 #include <../src/ksp/pc/impls/gamg/gamg.h> 5 6 #if defined PETSC_USE_LOG 7 PetscLogEvent gamg_setup_events[NUM_SET]; 8 #endif 9 10 #define GAMG_MAXLEVELS 30 11 12 /*#define GAMG_STAGES*/ 13 #if (defined PETSC_USE_LOG && defined GAMG_STAGES) 14 static PetscLogStage gamg_stages[GAMG_MAXLEVELS]; 15 #endif 16 17 /* Private context for the GAMG preconditioner */ 18 typedef struct gamg_TAG { 19 PetscInt m_dim; 20 PetscInt m_Nlevels; 21 PetscInt m_data_sz; 22 PetscInt m_data_rows; 23 PetscInt m_data_cols; 24 PetscBool m_useSA; 25 PetscReal *m_data; /* blocked vector of vertex data on fine grid (coordinates) */ 26 } PC_GAMG; 27 28 /* -------------------------------------------------------------------------- */ 29 /* 30 PCSetCoordinates_GAMG 31 32 Input Parameter: 33 . pc - the preconditioner context 34 */ 35 EXTERN_C_BEGIN 36 #undef __FUNCT__ 37 #define __FUNCT__ "PCSetCoordinates_GAMG" 38 PetscErrorCode PCSetCoordinates_GAMG( PC a_pc, PetscInt a_ndm, PetscReal *a_coords ) 39 { 40 PC_MG *mg = (PC_MG*)a_pc->data; 41 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 42 PetscErrorCode ierr; 43 PetscInt arrsz,bs,my0,kk,ii,jj,nloc,Iend; 44 Mat Amat = a_pc->pmat; 45 PetscBool flag; 46 char str[64]; 47 48 PetscFunctionBegin; 49 ierr = MatGetBlockSize( Amat, &bs ); CHKERRQ( ierr ); 50 ierr = MatGetOwnershipRange( Amat, &my0, &Iend ); CHKERRQ(ierr); 51 nloc = (Iend-my0)/bs; 52 if((Iend-my0)%bs!=0) SETERRQ1(((PetscObject)Amat)->comm,PETSC_ERR_ARG_WRONG, "Bad local size %d.",nloc); 53 54 ierr = PetscOptionsGetString(PETSC_NULL,"-pc_gamg_type",str,64,&flag); CHKERRQ( ierr ); 55 pc_gamg->m_useSA = (PetscBool)(flag && strcmp(str,"sa") == 0); 56 57 pc_gamg->m_data_rows = 1; 58 if(a_coords == 0) pc_gamg->m_useSA = PETSC_TRUE; /* use SA if no data */ 59 if( !pc_gamg->m_useSA ) pc_gamg->m_data_cols = a_ndm; /* coordinates */ 60 else{ /* SA: null space vectors */ 61 if(a_coords != 0 && bs==1 ) pc_gamg->m_data_cols = 1; /* scalar w/ coords and SA (not needed) */ 62 else if(a_coords != 0) pc_gamg->m_data_cols = (a_ndm==2 ? 3 : 6); /* elasticity */ 63 else pc_gamg->m_data_cols = bs; /* no data, force SA with constant null space vectors */ 64 pc_gamg->m_data_rows = bs; 65 } 66 arrsz = nloc*pc_gamg->m_data_rows*pc_gamg->m_data_cols; 67 68 /* create data - syntactic sugar that should be refactored at some point */ 69 if (!pc_gamg->m_data || (pc_gamg->m_data_sz != arrsz)) { 70 ierr = PetscFree( pc_gamg->m_data ); CHKERRQ(ierr); 71 ierr = PetscMalloc((arrsz+1)*sizeof(double), &pc_gamg->m_data ); CHKERRQ(ierr); 72 } 73 for(kk=0;kk<arrsz;kk++)pc_gamg->m_data[kk] = -999.; 74 pc_gamg->m_data[arrsz] = -99.; 75 /* copy data in - column oriented */ 76 if( pc_gamg->m_useSA ) { 77 const PetscInt M = Iend - my0; 78 for(kk=0;kk<nloc;kk++){ 79 PetscReal *data = &pc_gamg->m_data[kk*bs]; 80 if( pc_gamg->m_data_cols==1 ) *data = 1.0; 81 else { 82 for(ii=0;ii<bs;ii++) 83 for(jj=0;jj<bs;jj++) 84 if(ii==jj)data[ii*M + jj] = 1.0; /* translational modes */ 85 else data[ii*M + jj] = 0.0; 86 if( a_coords != 0 ) { 87 if( a_ndm == 2 ){ /* rotational modes */ 88 data += 2*M; 89 data[0] = -a_coords[2*kk+1]; 90 data[1] = a_coords[2*kk]; 91 } 92 else { 93 data += 3*M; 94 data[0] = 0.0; data[M+0] = a_coords[3*kk+2]; data[2*M+0] = -a_coords[3*kk+1]; 95 data[1] = -a_coords[3*kk+2]; data[M+1] = 0.0; data[2*M+1] = a_coords[3*kk]; 96 data[2] = a_coords[3*kk+1]; data[M+2] = -a_coords[3*kk]; data[2*M+2] = 0.0; 97 } 98 } 99 } 100 } 101 } 102 else { 103 for( kk = 0 ; kk < nloc ; kk++ ){ 104 for( ii = 0 ; ii < a_ndm ; ii++ ) { 105 pc_gamg->m_data[ii*nloc + kk] = a_coords[kk*a_ndm + ii]; 106 } 107 } 108 } 109 assert(pc_gamg->m_data[arrsz] == -99.); 110 111 pc_gamg->m_data_sz = arrsz; 112 pc_gamg->m_dim = a_ndm; 113 114 PetscFunctionReturn(0); 115 } 116 EXTERN_C_END 117 118 119 /* -----------------------------------------------------------------------------*/ 120 #undef __FUNCT__ 121 #define __FUNCT__ "PCReset_GAMG" 122 PetscErrorCode PCReset_GAMG(PC pc) 123 { 124 PetscErrorCode ierr; 125 PC_MG *mg = (PC_MG*)pc->data; 126 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 127 128 PetscFunctionBegin; 129 ierr = PetscFree(pc_gamg->m_data);CHKERRQ(ierr); 130 pc_gamg->m_data = 0; pc_gamg->m_data_sz = 0; 131 PetscFunctionReturn(0); 132 } 133 134 /* -------------------------------------------------------------------------- */ 135 /* 136 partitionLevel 137 138 Input Parameter: 139 . a_Amat_fine - matrix on this fine (k) level 140 . a_ndata_rows - size of data to move (coarse grid) 141 . a_ndata_cols - size of data to move (coarse grid) 142 In/Output Parameter: 143 . a_P_inout - prolongation operator to the next level (k-1) 144 . a_coarse_data - data that need to be moved 145 . a_nactive_proc - number of active procs 146 Output Parameter: 147 . a_Amat_crs - coarse matrix that is created (k-1) 148 */ 149 150 #define MIN_EQ_PROC 600 151 #define TOP_GRID_LIM 1000 152 153 #undef __FUNCT__ 154 #define __FUNCT__ "partitionLevel" 155 PetscErrorCode partitionLevel( Mat a_Amat_fine, 156 PetscInt a_ndata_rows, 157 PetscInt a_ndata_cols, 158 PetscInt a_cbs, 159 Mat *a_P_inout, 160 PetscReal **a_coarse_data, 161 PetscMPIInt *a_nactive_proc, 162 Mat *a_Amat_crs 163 ) 164 { 165 PetscErrorCode ierr; 166 Mat Cmat,Pnew,Pold=*a_P_inout; 167 IS new_indices,isnum; 168 MPI_Comm wcomm = ((PetscObject)a_Amat_fine)->comm; 169 PetscMPIInt mype,npe,new_npe,nactive; 170 PetscInt neq,NN,Istart,Iend,Istart0,Iend0,ncrs_new,ncrs0; 171 PetscBool flag = PETSC_FALSE; 172 173 PetscFunctionBegin; 174 ierr = MPI_Comm_rank( wcomm, &mype ); CHKERRQ(ierr); 175 ierr = MPI_Comm_size( wcomm, &npe ); CHKERRQ(ierr); 176 /* RAP */ 177 ierr = MatPtAP( a_Amat_fine, Pold, MAT_INITIAL_MATRIX, 2.0, &Cmat ); CHKERRQ(ierr); 178 179 ierr = MatSetBlockSize( Cmat, a_cbs ); CHKERRQ(ierr); 180 ierr = MatGetOwnershipRange( Cmat, &Istart0, &Iend0 ); CHKERRQ(ierr); 181 ncrs0 = (Iend0-Istart0)/a_cbs; assert((Iend0-Istart0)%a_cbs == 0); 182 183 ierr = PetscOptionsHasName(PETSC_NULL,"-pc_gamg_avoid_repartitioning",&flag); 184 CHKERRQ( ierr ); 185 if( flag ) { 186 *a_Amat_crs = Cmat; /* output */ 187 } 188 else { 189 /* Repartition Cmat_{k} and move colums of P^{k}_{k-1} and coordinates accordingly */ 190 Mat adj; 191 const PetscInt *idx,data_sz=a_ndata_rows*a_ndata_cols; 192 const PetscInt stride0=ncrs0*a_ndata_rows; 193 PetscInt is_sz,*isnewproc_idx,ii,jj,kk,strideNew,*tidx; 194 /* create sub communicator */ 195 MPI_Comm cm,new_comm; 196 MPI_Group wg, g2; 197 PetscInt *counts,inpe; 198 PetscMPIInt *ranks; 199 IS isscat; 200 PetscScalar *array; 201 Vec src_crd, dest_crd; 202 PetscReal *data = *a_coarse_data; 203 VecScatter vecscat; 204 IS isnewproc; 205 206 /* get number of PEs to make active, reduce */ 207 ierr = MatGetSize( Cmat, &neq, &NN );CHKERRQ(ierr); 208 new_npe = neq/MIN_EQ_PROC; /* hardwire min. number of eq/proc */ 209 if( new_npe == 0 || neq < TOP_GRID_LIM ) new_npe = 1; 210 else if (new_npe >= *a_nactive_proc ) new_npe = *a_nactive_proc; /* no change, rare */ 211 212 ierr = PetscMalloc( npe*sizeof(PetscMPIInt), &ranks ); CHKERRQ(ierr); 213 ierr = PetscMalloc( npe*sizeof(PetscInt), &counts ); CHKERRQ(ierr); 214 215 ierr = MPI_Allgather( &ncrs0, 1, MPIU_INT, counts, 1, MPIU_INT, wcomm ); CHKERRQ(ierr); 216 assert(counts[mype]==ncrs0); 217 /* count real active pes */ 218 for( nactive = jj = 0 ; jj < npe ; jj++) { 219 if( counts[jj] != 0 ) { 220 ranks[nactive++] = jj; 221 } 222 } 223 assert(nactive>=new_npe); 224 225 PetscPrintf(PETSC_COMM_WORLD,"\t[%d]%s npe (active): %d --> %d. new npe = %d, neq = %d\n",mype,__FUNCT__,*a_nactive_proc,nactive,new_npe,neq); 226 *a_nactive_proc = new_npe; /* output */ 227 228 ierr = MPI_Comm_group( wcomm, &wg ); CHKERRQ(ierr); 229 ierr = MPI_Group_incl( wg, nactive, ranks, &g2 ); CHKERRQ(ierr); 230 ierr = MPI_Comm_create( wcomm, g2, &cm ); CHKERRQ(ierr); 231 232 if( cm != MPI_COMM_NULL ) { 233 assert(ncrs0 != 0); 234 ierr = PetscCommDuplicate( cm, &new_comm, PETSC_NULL ); CHKERRQ(ierr); 235 ierr = MPI_Comm_free( &cm ); CHKERRQ(ierr); 236 } 237 else assert(ncrs0 == 0); 238 239 ierr = MPI_Group_free( &wg ); CHKERRQ(ierr); 240 ierr = MPI_Group_free( &g2 ); CHKERRQ(ierr); 241 242 /* MatPartitioningApply call MatConvert, which is collective */ 243 #if defined PETSC_USE_LOG 244 ierr = PetscLogEventBegin(gamg_setup_events[SET12],0,0,0,0);CHKERRQ(ierr); 245 #endif 246 if( a_cbs == 1) { 247 ierr = MatConvert( Cmat, MATMPIADJ, MAT_INITIAL_MATRIX, &adj ); CHKERRQ(ierr); 248 } 249 else{ 250 /* make a scalar matrix to partition */ 251 Mat tMat; 252 PetscInt ncols; 253 const PetscScalar *vals; 254 const PetscInt *idx; 255 MatInfo info; 256 257 ierr = MatGetInfo(Cmat,MAT_LOCAL,&info); CHKERRQ(ierr); 258 ncols = (PetscInt)info.nz_used/((ncrs0+1)*a_cbs*a_cbs)+1; 259 260 ierr = MatCreateMPIAIJ( wcomm, ncrs0, ncrs0, 261 PETSC_DETERMINE, PETSC_DETERMINE, 262 2*ncols, PETSC_NULL, ncols, PETSC_NULL, 263 &tMat ); 264 CHKERRQ(ierr); 265 266 for ( ii = Istart0; ii < Iend0; ii++ ) { 267 PetscInt dest_row = ii/a_cbs; 268 ierr = MatGetRow(Cmat,ii,&ncols,&idx,&vals); CHKERRQ(ierr); 269 for( jj = 0 ; jj < ncols ; jj++ ){ 270 PetscInt dest_col = idx[jj]/a_cbs; 271 PetscScalar v = 1.0; 272 ierr = MatSetValues(tMat,1,&dest_row,1,&dest_col,&v,ADD_VALUES); CHKERRQ(ierr); 273 } 274 ierr = MatRestoreRow(Cmat,ii,&ncols,&idx,&vals); CHKERRQ(ierr); 275 } 276 ierr = MatAssemblyBegin(tMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 277 ierr = MatAssemblyEnd(tMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 278 279 static int llev = 0; 280 if( llev++ == -1 ) { 281 PetscViewer viewer; char fname[32]; 282 sprintf(fname,"part_mat_%d.mat",llev); 283 PetscViewerBinaryOpen(wcomm,fname,FILE_MODE_WRITE,&viewer); 284 ierr = MatView( tMat, viewer ); CHKERRQ(ierr); 285 ierr = PetscViewerDestroy( &viewer ); 286 } 287 288 ierr = MatConvert( tMat, MATMPIADJ, MAT_INITIAL_MATRIX, &adj ); CHKERRQ(ierr); 289 290 ierr = MatDestroy( &tMat ); CHKERRQ(ierr); 291 } 292 293 if( ncrs0 != 0 ){ 294 const PetscInt *is_idx; 295 MatPartitioning mpart; 296 /* hack to fix global data that pmetis.c uses in 'adj' */ 297 for( nactive = jj = 0 ; jj < npe ; jj++ ) { 298 if( counts[jj] != 0 ) { 299 adj->rmap->range[nactive++] = adj->rmap->range[jj]; 300 } 301 } 302 adj->rmap->range[nactive] = adj->rmap->range[npe]; 303 304 ierr = MatPartitioningCreate( new_comm, &mpart ); CHKERRQ(ierr); 305 ierr = MatPartitioningSetAdjacency( mpart, adj ); CHKERRQ(ierr); 306 ierr = MatPartitioningSetFromOptions( mpart ); CHKERRQ(ierr); 307 ierr = MatPartitioningSetNParts( mpart, new_npe );CHKERRQ(ierr); 308 ierr = MatPartitioningApply( mpart, &isnewproc ); CHKERRQ(ierr); 309 ierr = MatPartitioningDestroy( &mpart ); CHKERRQ(ierr); 310 311 /* collect IS info */ 312 ierr = ISGetLocalSize( isnewproc, &is_sz ); CHKERRQ(ierr); 313 ierr = PetscMalloc( a_cbs*is_sz*sizeof(PetscInt), &isnewproc_idx ); CHKERRQ(ierr); 314 ierr = ISGetIndices( isnewproc, &is_idx ); CHKERRQ(ierr); 315 /* spread partitioning across machine - best way ??? */ 316 NN = npe/new_npe; 317 for( kk = jj = 0 ; kk < is_sz ; kk++ ){ 318 for( ii = 0 ; ii < a_cbs ; ii++, jj++ ) { 319 isnewproc_idx[jj] = is_idx[kk] * NN; /* distribution */ 320 } 321 } 322 ierr = ISRestoreIndices( isnewproc, &is_idx ); CHKERRQ(ierr); 323 ierr = ISDestroy( &isnewproc ); CHKERRQ(ierr); 324 ierr = PetscCommDestroy( &new_comm ); CHKERRQ(ierr); 325 326 is_sz *= a_cbs; 327 } 328 else{ 329 isnewproc_idx = 0; 330 is_sz = 0; 331 } 332 ierr = MatDestroy( &adj ); CHKERRQ(ierr); 333 ierr = ISCreateGeneral( wcomm, is_sz, isnewproc_idx, PETSC_COPY_VALUES, &isnewproc ); 334 if( isnewproc_idx != 0 ) { 335 ierr = PetscFree( isnewproc_idx ); CHKERRQ(ierr); 336 } 337 338 /* 339 Create an index set from the isnewproc index set to indicate the mapping TO 340 */ 341 ierr = ISPartitioningToNumbering( isnewproc, &isnum ); CHKERRQ(ierr); 342 /* 343 Determine how many elements are assigned to each processor 344 */ 345 inpe = npe; 346 ierr = ISPartitioningCount( isnewproc, inpe, counts ); CHKERRQ(ierr); 347 ierr = ISDestroy( &isnewproc ); CHKERRQ(ierr); 348 ncrs_new = counts[mype]/a_cbs; 349 strideNew = ncrs_new*a_ndata_rows; 350 #if defined PETSC_USE_LOG 351 ierr = PetscLogEventEnd(gamg_setup_events[SET12],0,0,0,0); CHKERRQ(ierr); 352 #endif 353 /* Create a vector to contain the newly ordered element information */ 354 ierr = VecCreate( wcomm, &dest_crd ); 355 ierr = VecSetSizes( dest_crd, data_sz*ncrs_new, PETSC_DECIDE ); CHKERRQ(ierr); 356 ierr = VecSetFromOptions( dest_crd ); CHKERRQ(ierr); /*funny vector-get global options?*/ 357 /* 358 There are 'a_ndata_rows*a_ndata_cols' data items per node, (one can think of the vectors of having 359 a block size of ...). Note, ISs are expanded into equation space by 'a_cbs'. 360 */ 361 ierr = PetscMalloc( (ncrs0*data_sz)*sizeof(PetscInt), &tidx ); CHKERRQ(ierr); 362 ierr = ISGetIndices( isnum, &idx ); CHKERRQ(ierr); 363 for(ii=0,jj=0; ii<ncrs0 ; ii++) { 364 PetscInt id = idx[ii*a_cbs]/a_cbs; /* get node back */ 365 for( kk=0; kk<data_sz ; kk++, jj++) tidx[jj] = id*data_sz + kk; 366 } 367 ierr = ISRestoreIndices( isnum, &idx ); CHKERRQ(ierr); 368 ierr = ISCreateGeneral( wcomm, data_sz*ncrs0, tidx, PETSC_COPY_VALUES, &isscat ); 369 CHKERRQ(ierr); 370 ierr = PetscFree( tidx ); CHKERRQ(ierr); 371 /* 372 Create a vector to contain the original vertex information for each element 373 */ 374 ierr = VecCreateSeq( PETSC_COMM_SELF, data_sz*ncrs0, &src_crd ); CHKERRQ(ierr); 375 for( jj=0; jj<a_ndata_cols ; jj++ ) { 376 for( ii=0 ; ii<ncrs0 ; ii++) { 377 for( kk=0; kk<a_ndata_rows ; kk++ ) { 378 PetscInt ix = ii*a_ndata_rows + kk + jj*stride0, jx = ii*data_sz + kk*a_ndata_cols + jj; 379 ierr = VecSetValues( src_crd, 1, &jx, &data[ix], INSERT_VALUES ); CHKERRQ(ierr); 380 } 381 } 382 } 383 ierr = VecAssemblyBegin(src_crd); CHKERRQ(ierr); 384 ierr = VecAssemblyEnd(src_crd); CHKERRQ(ierr); 385 /* 386 Scatter the element vertex information (still in the original vertex ordering) 387 to the correct processor 388 */ 389 ierr = VecScatterCreate( src_crd, PETSC_NULL, dest_crd, isscat, &vecscat); 390 CHKERRQ(ierr); 391 ierr = ISDestroy( &isscat ); CHKERRQ(ierr); 392 ierr = VecScatterBegin(vecscat,src_crd,dest_crd,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 393 ierr = VecScatterEnd(vecscat,src_crd,dest_crd,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 394 ierr = VecScatterDestroy( &vecscat ); CHKERRQ(ierr); 395 ierr = VecDestroy( &src_crd ); CHKERRQ(ierr); 396 /* 397 Put the element vertex data into a new allocation of the gdata->ele 398 */ 399 ierr = PetscFree( *a_coarse_data ); CHKERRQ(ierr); 400 ierr = PetscMalloc( data_sz*ncrs_new*sizeof(PetscReal), a_coarse_data ); CHKERRQ(ierr); 401 402 ierr = VecGetArray( dest_crd, &array ); CHKERRQ(ierr); 403 data = *a_coarse_data; 404 for( jj=0; jj<a_ndata_cols ; jj++ ) { 405 for( ii=0 ; ii<ncrs_new ; ii++) { 406 for( kk=0; kk<a_ndata_rows ; kk++ ) { 407 PetscInt ix = ii*a_ndata_rows + kk + jj*strideNew, jx = ii*data_sz + kk*a_ndata_cols + jj; 408 data[ix] = PetscRealPart(array[jx]); 409 array[jx] = 1.e300; 410 } 411 } 412 } 413 ierr = VecRestoreArray( dest_crd, &array ); CHKERRQ(ierr); 414 ierr = VecDestroy( &dest_crd ); CHKERRQ(ierr); 415 416 /* 417 Invert for MatGetSubMatrix 418 */ 419 ierr = ISInvertPermutation( isnum, ncrs_new*a_cbs, &new_indices ); CHKERRQ(ierr); 420 ierr = ISSort( new_indices ); CHKERRQ(ierr); /* is this needed? */ 421 ierr = ISDestroy( &isnum ); CHKERRQ(ierr); 422 /* A_crs output */ 423 ierr = MatGetSubMatrix( Cmat, new_indices, new_indices, MAT_INITIAL_MATRIX, a_Amat_crs ); 424 CHKERRQ(ierr); 425 426 ierr = MatDestroy( &Cmat ); CHKERRQ(ierr); 427 Cmat = *a_Amat_crs; /* output */ 428 ierr = MatSetBlockSize( Cmat, a_cbs ); CHKERRQ(ierr); 429 430 /* prolongator */ 431 ierr = MatGetOwnershipRange( Pold, &Istart, &Iend ); CHKERRQ(ierr); 432 { 433 IS findices; 434 ierr = ISCreateStride(wcomm,Iend-Istart,Istart,1,&findices); CHKERRQ(ierr); 435 ierr = MatGetSubMatrix( Pold, findices, new_indices, MAT_INITIAL_MATRIX, &Pnew ); 436 CHKERRQ(ierr); 437 ierr = ISDestroy( &findices ); CHKERRQ(ierr); 438 } 439 ierr = MatDestroy( a_P_inout ); CHKERRQ(ierr); 440 *a_P_inout = Pnew; /* output */ 441 442 ierr = ISDestroy( &new_indices ); CHKERRQ(ierr); 443 ierr = PetscFree( counts ); CHKERRQ(ierr); 444 ierr = PetscFree( ranks ); CHKERRQ(ierr); 445 } 446 447 PetscFunctionReturn(0); 448 } 449 450 /* -------------------------------------------------------------------------- */ 451 /* 452 PCSetUp_GAMG - Prepares for the use of the GAMG preconditioner 453 by setting data structures and options. 454 455 Input Parameter: 456 . pc - the preconditioner context 457 458 Application Interface Routine: PCSetUp() 459 460 Notes: 461 The interface routine PCSetUp() is not usually called directly by 462 the user, but instead is called by PCApply() if necessary. 463 */ 464 #undef __FUNCT__ 465 #define __FUNCT__ "PCSetUp_GAMG" 466 PetscErrorCode PCSetUp_GAMG( PC a_pc ) 467 { 468 PetscErrorCode ierr; 469 PC_MG *mg = (PC_MG*)a_pc->data; 470 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 471 Mat Amat = a_pc->mat, Pmat = a_pc->pmat; 472 PetscInt fine_level, level, level1, M, N, bs, nloc, lidx, Istart, Iend; 473 MPI_Comm wcomm = ((PetscObject)a_pc)->comm; 474 PetscMPIInt mype,npe,nactivepe; 475 PetscBool isOK; 476 Mat Aarr[GAMG_MAXLEVELS], Parr[GAMG_MAXLEVELS]; 477 PetscReal *coarse_data = 0, *data, emaxs[GAMG_MAXLEVELS]; 478 MatInfo info; 479 480 PetscFunctionBegin; 481 if( a_pc->setupcalled ) { 482 /* no state data in GAMG to destroy */ 483 ierr = PCReset_MG( a_pc ); CHKERRQ(ierr); 484 } 485 ierr = MPI_Comm_rank(wcomm,&mype);CHKERRQ(ierr); 486 ierr = MPI_Comm_size(wcomm,&npe);CHKERRQ(ierr); 487 /* GAMG requires input of fine-grid matrix. It determines nlevels. */ 488 ierr = MatGetBlockSize( Amat, &bs ); CHKERRQ(ierr); 489 ierr = MatGetSize( Amat, &M, &N );CHKERRQ(ierr); 490 ierr = MatGetOwnershipRange( Amat, &Istart, &Iend ); CHKERRQ(ierr); 491 nloc = (Iend-Istart)/bs; assert((Iend-Istart)%bs == 0); 492 493 /* get data of not around */ 494 if( pc_gamg->m_data == 0 && nloc > 0 ) { 495 ierr = PCSetCoordinates_GAMG( a_pc, -1, 0 ); CHKERRQ( ierr ); 496 } 497 data = pc_gamg->m_data; 498 499 /* Get A_i and R_i */ 500 ierr = MatGetInfo(Amat,MAT_GLOBAL_SUM,&info); CHKERRQ(ierr); 501 PetscPrintf(PETSC_COMM_WORLD,"\t[%d]%s level %d N=%ld, n data rows=%d, n data cols=%d, nnz/row (ave)=%d, np=%d\n", 502 mype,__FUNCT__,0,(long long int)N,(int)pc_gamg->m_data_rows,(int)pc_gamg->m_data_cols, 503 (int)(info.nz_used/(PetscReal)N),(int)npe); 504 for ( level=0, Aarr[0] = Pmat, nactivepe = npe; /* hard wired stopping logic */ 505 level < GAMG_MAXLEVELS-1 && (level==0 || M>TOP_GRID_LIM) && (npe==1 || nactivepe>1); 506 level++ ){ 507 level1 = level + 1; 508 #if (defined PETSC_USE_LOG && defined GAMG_STAGES) 509 ierr = PetscLogStagePush(gamg_stages[level]); CHKERRQ( ierr ); 510 #endif 511 #if defined PETSC_USE_LOG 512 ierr = PetscLogEventBegin(gamg_setup_events[SET1],0,0,0,0);CHKERRQ(ierr); 513 #endif 514 ierr = createProlongation(Aarr[level], data, pc_gamg->m_dim, pc_gamg->m_data_cols, pc_gamg->m_useSA, 515 level, &bs, &Parr[level1], &coarse_data, &isOK, &emaxs[level] ); 516 CHKERRQ(ierr); 517 ierr = PetscFree( data ); CHKERRQ( ierr ); 518 #if defined PETSC_USE_LOG 519 ierr = PetscLogEventEnd(gamg_setup_events[SET1],0,0,0,0);CHKERRQ(ierr); 520 #endif 521 if(level==0) Aarr[0] = Amat; /* use Pmat for finest level setup, but use mat for solver */ 522 523 if( isOK ) { 524 #if defined PETSC_USE_LOG 525 ierr = PetscLogEventBegin(gamg_setup_events[SET2],0,0,0,0);CHKERRQ(ierr); 526 #endif 527 ierr = partitionLevel( Aarr[level], pc_gamg->m_useSA ? bs : 1, pc_gamg->m_data_cols, bs, 528 &Parr[level1], &coarse_data, &nactivepe, &Aarr[level1] ); 529 CHKERRQ(ierr); 530 #if defined PETSC_USE_LOG 531 ierr = PetscLogEventEnd(gamg_setup_events[SET2],0,0,0,0);CHKERRQ(ierr); 532 #endif 533 ierr = MatGetSize( Aarr[level1], &M, &N );CHKERRQ(ierr); 534 ierr = MatGetInfo(Aarr[level1],MAT_GLOBAL_SUM,&info); CHKERRQ(ierr); 535 PetscPrintf(PETSC_COMM_WORLD,"\t\t[%d]%s %d) N=%ld, bs=%d, n data cols=%d, nnz/row (ave)=%d, %d active pes\n", 536 mype,__FUNCT__,(int)level1,(long long int)N,(int)bs,(int)pc_gamg->m_data_cols, 537 (int)(info.nz_used/(PetscReal)N),(int)nactivepe); 538 /* coarse grids with SA can have zero row/cols from singleton aggregates */ 539 /* aggregation method can probably gaurrentee this does not happen! - be safe for now */ 540 541 if( PETSC_TRUE ){ 542 Vec diag; PetscScalar *data_arr,v; PetscInt Istart,Iend,kk,nloceq,id; 543 v = 1.e-10; /* LU factor has hard wired numbers for small diags so this needs to match (yuk) */ 544 ierr = MatGetOwnershipRange(Aarr[level1], &Istart, &Iend); CHKERRQ(ierr); 545 nloceq = Iend-Istart; 546 ierr = MatGetVecs( Aarr[level1], &diag, 0 ); CHKERRQ(ierr); 547 ierr = MatGetDiagonal( Aarr[level1], diag ); CHKERRQ(ierr); 548 ierr = VecGetArray( diag, &data_arr ); CHKERRQ(ierr); 549 for(kk=0;kk<nloceq;kk++){ 550 if(data_arr[kk]==0.0) { 551 id = kk + Istart; 552 ierr = MatSetValues(Aarr[level1],1,&id,1,&id,&v,INSERT_VALUES); 553 CHKERRQ(ierr); 554 PetscPrintf(PETSC_COMM_SELF,"\t[%d]%s warning: added diag to zero (%d) on level %d \n",mype,__FUNCT__,id,level); 555 } 556 } 557 ierr = VecRestoreArray( diag, &data_arr ); CHKERRQ(ierr); 558 ierr = VecDestroy( &diag ); CHKERRQ(ierr); 559 ierr = MatAssemblyBegin(Aarr[level1],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 560 ierr = MatAssemblyEnd(Aarr[level1],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 561 } 562 } 563 else{ 564 coarse_data = 0; 565 break; 566 } 567 data = coarse_data; 568 569 #if (defined PETSC_USE_LOG && defined GAMG_STAGES) 570 ierr = PetscLogStagePop(); CHKERRQ( ierr ); 571 #endif 572 } 573 if( coarse_data ) { 574 ierr = PetscFree( coarse_data ); CHKERRQ( ierr ); 575 } 576 PetscPrintf(PETSC_COMM_WORLD,"\t[%d]%s %d levels\n",0,__FUNCT__,level + 1); 577 pc_gamg->m_data = 0; /* destroyed coordinate data */ 578 pc_gamg->m_Nlevels = level + 1; 579 fine_level = level; 580 ierr = PCMGSetLevels(a_pc,pc_gamg->m_Nlevels,PETSC_NULL);CHKERRQ(ierr); 581 582 /* set default smoothers */ 583 for ( lidx=1, level = pc_gamg->m_Nlevels-2; 584 lidx <= fine_level; 585 lidx++, level--) { 586 PetscReal emax, emin; 587 KSP smoother; PC subpc; 588 ierr = PCMGGetSmoother( a_pc, lidx, &smoother ); CHKERRQ(ierr); 589 ierr = KSPSetType( smoother, KSPCHEBYCHEV );CHKERRQ(ierr); 590 if( emaxs[level] > 0.0 ) emax=emaxs[level]; 591 else{ /* eigen estimate 'emax' */ 592 KSP eksp; Mat Lmat = Aarr[level]; 593 Vec bb, xx; PC pc; 594 595 ierr = MatGetVecs( Lmat, &bb, 0 ); CHKERRQ(ierr); 596 ierr = MatGetVecs( Lmat, &xx, 0 ); CHKERRQ(ierr); 597 { 598 PetscRandom rctx; 599 ierr = PetscRandomCreate(wcomm,&rctx);CHKERRQ(ierr); 600 ierr = PetscRandomSetFromOptions(rctx);CHKERRQ(ierr); 601 ierr = VecSetRandom(bb,rctx);CHKERRQ(ierr); 602 ierr = PetscRandomDestroy( &rctx ); CHKERRQ(ierr); 603 } 604 ierr = KSPCreate(wcomm,&eksp);CHKERRQ(ierr); 605 ierr = KSPSetType( eksp, KSPCG ); CHKERRQ(ierr); 606 ierr = KSPSetInitialGuessNonzero( eksp, PETSC_FALSE ); CHKERRQ(ierr); 607 ierr = KSPSetOperators( eksp, Lmat, Lmat, DIFFERENT_NONZERO_PATTERN ); CHKERRQ( ierr ); 608 ierr = KSPGetPC( eksp, &pc );CHKERRQ( ierr ); 609 ierr = PCSetType( pc, PCPBJACOBI ); CHKERRQ(ierr); /* should be same as above */ 610 ierr = KSPSetTolerances( eksp, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT, 10 ); 611 CHKERRQ(ierr); 612 //ierr = KSPSetConvergenceTest( eksp, KSPSkipConverged, 0, 0 ); CHKERRQ(ierr); 613 ierr = KSPSetNormType( eksp, KSP_NORM_NONE ); CHKERRQ(ierr); 614 615 ierr = KSPSetComputeSingularValues( eksp,PETSC_TRUE ); CHKERRQ(ierr); 616 ierr = KSPSolve( eksp, bb, xx ); CHKERRQ(ierr); 617 ierr = KSPComputeExtremeSingularValues( eksp, &emax, &emin ); CHKERRQ(ierr); 618 ierr = VecDestroy( &xx ); CHKERRQ(ierr); 619 ierr = VecDestroy( &bb ); CHKERRQ(ierr); 620 ierr = KSPDestroy( &eksp ); CHKERRQ(ierr); 621 PetscPrintf(PETSC_COMM_WORLD,"\t\t\t%s max eigen=%e min=%e PC=%s\n",__FUNCT__,emax,emin,PETSC_GAMG_SMOOTHER); 622 } 623 { 624 PetscInt N1, N0, tt; 625 ierr = MatGetSize( Aarr[level], &N1, &tt ); CHKERRQ(ierr); 626 ierr = MatGetSize( Aarr[level+1], &N0, &tt ); CHKERRQ(ierr); 627 emin = 1.*emax/((PetscReal)N1/(PetscReal)N0); /* this should be about the coarsening rate */ 628 emax *= 1.05; 629 630 } 631 632 ierr = KSPSetOperators( smoother, Aarr[level], Aarr[level], DIFFERENT_NONZERO_PATTERN ); 633 ierr = KSPChebychevSetEigenvalues( smoother, emax, emin );CHKERRQ(ierr); 634 /* ierr = KSPSetTolerances(smoother,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,2); CHKERRQ(ierr); */ 635 ierr = KSPGetPC( smoother, &subpc ); CHKERRQ(ierr); 636 ierr = PCSetType( subpc, PETSC_GAMG_SMOOTHER ); CHKERRQ(ierr); 637 ierr = KSPSetNormType( smoother, KSP_NORM_NONE ); CHKERRQ(ierr); 638 } 639 { 640 KSP smoother; /* coarse grid */ 641 Mat Lmat = Aarr[pc_gamg->m_Nlevels-1]; 642 ierr = PCMGGetSmoother( a_pc, 0, &smoother ); CHKERRQ(ierr); 643 ierr = KSPSetOperators( smoother, Lmat, Lmat, DIFFERENT_NONZERO_PATTERN ); 644 CHKERRQ(ierr); 645 ierr = KSPSetNormType( smoother, KSP_NORM_NONE ); CHKERRQ(ierr); 646 } 647 648 /* should be called in PCSetFromOptions_GAMG(), but cannot be called prior to PCMGSetLevels() */ 649 ierr = PCSetFromOptions_MG(a_pc); CHKERRQ(ierr); 650 { 651 PetscBool galerkin; 652 ierr = PCMGGetGalerkin( a_pc, &galerkin); CHKERRQ(ierr); 653 if(galerkin){ 654 SETERRQ(wcomm,PETSC_ERR_ARG_WRONG, "GAMG does galerkin manually so it must not be used in PC_MG."); 655 } 656 } 657 658 /* set interpolation between the levels, clean up */ 659 for (lidx=0,level=pc_gamg->m_Nlevels-1; 660 lidx<fine_level; 661 lidx++, level--){ 662 ierr = PCMGSetInterpolation( a_pc, lidx+1, Parr[level] );CHKERRQ(ierr); 663 if( !PETSC_TRUE ) { 664 PetscViewer viewer; char fname[32]; 665 sprintf(fname,"Pmat_%d.m",(int)level); 666 ierr = PetscViewerASCIIOpen( wcomm, fname, &viewer ); CHKERRQ(ierr); 667 ierr = PetscViewerSetFormat( viewer, PETSC_VIEWER_ASCII_MATLAB); CHKERRQ(ierr); 668 ierr = MatView( Parr[level], viewer ); CHKERRQ(ierr); 669 ierr = PetscViewerDestroy( &viewer ); 670 sprintf(fname,"Amat_%d.m",(int)level); 671 ierr = PetscViewerASCIIOpen( wcomm, fname, &viewer ); CHKERRQ(ierr); 672 ierr = PetscViewerSetFormat( viewer, PETSC_VIEWER_ASCII_MATLAB); CHKERRQ(ierr); 673 ierr = MatView( Aarr[level], viewer ); CHKERRQ(ierr); 674 ierr = PetscViewerDestroy( &viewer ); 675 } 676 ierr = MatDestroy( &Parr[level] ); CHKERRQ(ierr); 677 ierr = MatDestroy( &Aarr[level] ); CHKERRQ(ierr); 678 } 679 680 /* setupcalled is set to 0 so that MG is setup from scratch */ 681 a_pc->setupcalled = 0; 682 ierr = PCSetUp_MG(a_pc);CHKERRQ(ierr); 683 684 PetscFunctionReturn(0); 685 } 686 687 /* ------------------------------------------------------------------------- */ 688 /* 689 PCDestroy_GAMG - Destroys the private context for the GAMG preconditioner 690 that was created with PCCreate_GAMG(). 691 692 Input Parameter: 693 . pc - the preconditioner context 694 695 Application Interface Routine: PCDestroy() 696 */ 697 #undef __FUNCT__ 698 #define __FUNCT__ "PCDestroy_GAMG" 699 PetscErrorCode PCDestroy_GAMG(PC pc) 700 { 701 PetscErrorCode ierr; 702 PC_MG *mg = (PC_MG*)pc->data; 703 PC_GAMG *pc_gamg= (PC_GAMG*)mg->innerctx; 704 705 PetscFunctionBegin; 706 ierr = PCReset_GAMG(pc);CHKERRQ(ierr); 707 ierr = PetscFree(pc_gamg);CHKERRQ(ierr); 708 ierr = PCDestroy_MG(pc);CHKERRQ(ierr); 709 PetscFunctionReturn(0); 710 } 711 712 #undef __FUNCT__ 713 #define __FUNCT__ "PCSetFromOptions_GAMG" 714 PetscErrorCode PCSetFromOptions_GAMG(PC pc) 715 { 716 /* PetscErrorCode ierr; */ 717 /* PC_MG *mg = (PC_MG*)pc->data; */ 718 /* PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; */ 719 /* MPI_Comm comm = ((PetscObject)pc)->comm; */ 720 721 PetscFunctionBegin; 722 PetscFunctionReturn(0); 723 } 724 725 /* -------------------------------------------------------------------------- */ 726 /* 727 PCCreate_GAMG - Creates a GAMG preconditioner context, PC_GAMG 728 729 Input Parameter: 730 . pc - the preconditioner context 731 732 Application Interface Routine: PCCreate() 733 734 */ 735 /* MC 736 PCGAMG - Use algebraic multigrid preconditioning. This preconditioner requires you provide 737 fine grid discretization matrix and coordinates on the fine grid. 738 739 Options Database Key: 740 Multigrid options(inherited) 741 + -pc_mg_cycles <1>: 1 for V cycle, 2 for W-cycle (MGSetCycles) 742 . -pc_mg_smoothup <1>: Number of post-smoothing steps (MGSetNumberSmoothUp) 743 . -pc_mg_smoothdown <1>: Number of pre-smoothing steps (MGSetNumberSmoothDown) 744 -pc_mg_type <multiplicative>: (one of) additive multiplicative full cascade kascade 745 GAMG options: 746 747 Level: intermediate 748 Concepts: multigrid 749 750 .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType, 751 PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), MPSetCycles(), PCMGSetNumberSmoothDown(), 752 PCMGSetNumberSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(), 753 PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(), 754 PCMGSetCyclesOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR() 755 M */ 756 757 EXTERN_C_BEGIN 758 #undef __FUNCT__ 759 #define __FUNCT__ "PCCreate_GAMG" 760 PetscErrorCode PCCreate_GAMG(PC pc) 761 { 762 PetscErrorCode ierr; 763 PC_GAMG *pc_gamg; 764 PC_MG *mg; 765 PetscClassId cookie; 766 767 PetscFunctionBegin; 768 /* PCGAMG is an inherited class of PCMG. Initialize pc as PCMG */ 769 ierr = PCSetType(pc,PCMG);CHKERRQ(ierr); /* calls PCCreate_MG() and MGCreate_Private() */ 770 ierr = PetscObjectChangeTypeName((PetscObject)pc,PCGAMG);CHKERRQ(ierr); 771 772 /* create a supporting struct and attach it to pc */ 773 ierr = PetscNewLog(pc,PC_GAMG,&pc_gamg);CHKERRQ(ierr); 774 mg = (PC_MG*)pc->data; 775 mg->innerctx = pc_gamg; 776 777 pc_gamg->m_Nlevels = -1; 778 779 /* overwrite the pointers of PCMG by the functions of PCGAMG */ 780 pc->ops->setfromoptions = PCSetFromOptions_GAMG; 781 pc->ops->setup = PCSetUp_GAMG; 782 pc->ops->reset = PCReset_GAMG; 783 pc->ops->destroy = PCDestroy_GAMG; 784 785 ierr = PetscObjectComposeFunctionDynamic( (PetscObject)pc, 786 "PCSetCoordinates_C", 787 "PCSetCoordinates_GAMG", 788 PCSetCoordinates_GAMG);CHKERRQ(ierr); 789 #if defined PETSC_USE_LOG 790 static int count = 0; 791 if( count++ == 0 ) { 792 PetscClassIdRegister("GAMG Setup",&cookie); 793 PetscLogEventRegister("GAMG: createProl", cookie, &gamg_setup_events[SET1]); 794 PetscLogEventRegister(" make graph", cookie, &gamg_setup_events[SET3]); 795 PetscLogEventRegister(" MIS/Agg", cookie, &gamg_setup_events[SET4]); 796 PetscLogEventRegister(" geo: growSupp", cookie, &gamg_setup_events[SET5]); 797 PetscLogEventRegister(" geo: triangle", cookie, &gamg_setup_events[SET6]); 798 PetscLogEventRegister(" search & set", cookie, &gamg_setup_events[FIND_V]); 799 PetscLogEventRegister(" SA: init", cookie, &gamg_setup_events[SET7]); 800 /* PetscLogEventRegister(" SA: frmProl0", cookie, &gamg_setup_events[SET8]); */ 801 PetscLogEventRegister(" SA: smooth", cookie, &gamg_setup_events[SET9]); 802 PetscLogEventRegister("GAMG: partLevel", cookie, &gamg_setup_events[SET2]); 803 PetscLogEventRegister(" PL repartition", cookie, &gamg_setup_events[SET12]); 804 /* PetscLogEventRegister(" PL move data", cookie, &gamg_setup_events[SET13]); */ 805 /* PetscLogEventRegister("GAMG: fix", cookie, &gamg_setup_events[SET10]); */ 806 /* PetscLogEventRegister("GAMG: set levels", cookie, &gamg_setup_events[SET11]); */ 807 808 /* create timer stages */ 809 #if defined GAMG_STAGES 810 { 811 char str[32]; 812 sprintf(str,"MG Level %d (finest)",0); 813 PetscLogStageRegister(str, &gamg_stages[0]); 814 PetscInt lidx; 815 for (lidx=1;lidx<9;lidx++){ 816 sprintf(str,"MG Level %d",lidx); 817 PetscLogStageRegister(str, &gamg_stages[lidx]); 818 } 819 } 820 #endif 821 } 822 #endif 823 PetscFunctionReturn(0); 824 } 825 EXTERN_C_END 826