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 300 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; 170 PetscInt neq,NN,Istart,Iend,Istart0,Iend0,ncrs_new; 171 PetscMPIInt new_npe,nactive,ncrs0; 172 PetscBool flag = PETSC_FALSE; 173 174 PetscFunctionBegin; 175 ierr = MPI_Comm_rank( wcomm, &mype ); CHKERRQ(ierr); 176 ierr = MPI_Comm_size( wcomm, &npe ); CHKERRQ(ierr); 177 /* RAP */ 178 ierr = MatPtAP( a_Amat_fine, Pold, MAT_INITIAL_MATRIX, 2.0, &Cmat ); CHKERRQ(ierr); 179 180 ierr = MatSetBlockSize( Cmat, a_cbs ); CHKERRQ(ierr); 181 ierr = MatGetOwnershipRange( Cmat, &Istart0, &Iend0 ); CHKERRQ(ierr); 182 ncrs0 = (Iend0-Istart0)/a_cbs; assert((Iend0-Istart0)%a_cbs == 0); 183 184 ierr = PetscOptionsHasName(PETSC_NULL,"-pc_gamg_avoid_repartitioning",&flag); 185 CHKERRQ( ierr ); 186 if( flag ) { 187 *a_Amat_crs = Cmat; /* output */ 188 } 189 else { 190 /* Repartition Cmat_{k} and move colums of P^{k}_{k-1} and coordinates accordingly */ 191 Mat adj; 192 const PetscInt *idx,data_sz=a_ndata_rows*a_ndata_cols; 193 const PetscInt stride0=ncrs0*a_ndata_rows; 194 PetscInt is_sz,*isnewproc_idx,ii,jj,kk,strideNew,*tidx; 195 /* create sub communicator */ 196 MPI_Comm cm,new_comm; 197 MPI_Group wg, g2; 198 PetscMPIInt *ranks,*counts; 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(PetscMPIInt), &counts ); CHKERRQ(ierr); 214 215 ierr = MPI_Allgather( &ncrs0, 1, MPI_INT, counts, 1, MPI_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 ierr = ISPartitioningCount( isnewproc, npe, counts ); CHKERRQ(ierr); 346 ierr = ISDestroy( &isnewproc ); CHKERRQ(ierr); 347 ncrs_new = counts[mype]/a_cbs; 348 strideNew = ncrs_new*a_ndata_rows; 349 #if defined PETSC_USE_LOG 350 ierr = PetscLogEventEnd(gamg_setup_events[SET12],0,0,0,0); CHKERRQ(ierr); 351 #endif 352 /* Create a vector to contain the newly ordered element information */ 353 ierr = VecCreate( wcomm, &dest_crd ); 354 ierr = VecSetSizes( dest_crd, data_sz*ncrs_new, PETSC_DECIDE ); CHKERRQ(ierr); 355 ierr = VecSetFromOptions( dest_crd ); CHKERRQ(ierr); /*funny vector-get global options?*/ 356 /* 357 There are 'a_ndata_rows*a_ndata_cols' data items per node, (one can think of the vectors of having 358 a block size of ...). Note, ISs are expanded into equation space by 'a_cbs'. 359 */ 360 ierr = PetscMalloc( (ncrs0*data_sz)*sizeof(PetscInt), &tidx ); CHKERRQ(ierr); 361 ierr = ISGetIndices( isnum, &idx ); CHKERRQ(ierr); 362 for(ii=0,jj=0; ii<ncrs0 ; ii++) { 363 PetscInt id = idx[ii*a_cbs]/a_cbs; /* get node back */ 364 for( kk=0; kk<data_sz ; kk++, jj++) tidx[jj] = id*data_sz + kk; 365 } 366 ierr = ISRestoreIndices( isnum, &idx ); CHKERRQ(ierr); 367 ierr = ISCreateGeneral( wcomm, data_sz*ncrs0, tidx, PETSC_COPY_VALUES, &isscat ); 368 CHKERRQ(ierr); 369 ierr = PetscFree( tidx ); CHKERRQ(ierr); 370 /* 371 Create a vector to contain the original vertex information for each element 372 */ 373 ierr = VecCreateSeq( PETSC_COMM_SELF, data_sz*ncrs0, &src_crd ); CHKERRQ(ierr); 374 for( jj=0; jj<a_ndata_cols ; jj++ ) { 375 for( ii=0 ; ii<ncrs0 ; ii++) { 376 for( kk=0; kk<a_ndata_rows ; kk++ ) { 377 PetscInt ix = ii*a_ndata_rows + kk + jj*stride0, jx = ii*data_sz + kk*a_ndata_cols + jj; 378 ierr = VecSetValues( src_crd, 1, &jx, &data[ix], INSERT_VALUES ); CHKERRQ(ierr); 379 } 380 } 381 } 382 ierr = VecAssemblyBegin(src_crd); CHKERRQ(ierr); 383 ierr = VecAssemblyEnd(src_crd); CHKERRQ(ierr); 384 /* 385 Scatter the element vertex information (still in the original vertex ordering) 386 to the correct processor 387 */ 388 ierr = VecScatterCreate( src_crd, PETSC_NULL, dest_crd, isscat, &vecscat); 389 CHKERRQ(ierr); 390 ierr = ISDestroy( &isscat ); CHKERRQ(ierr); 391 ierr = VecScatterBegin(vecscat,src_crd,dest_crd,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 392 ierr = VecScatterEnd(vecscat,src_crd,dest_crd,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 393 ierr = VecScatterDestroy( &vecscat ); CHKERRQ(ierr); 394 ierr = VecDestroy( &src_crd ); CHKERRQ(ierr); 395 /* 396 Put the element vertex data into a new allocation of the gdata->ele 397 */ 398 ierr = PetscFree( *a_coarse_data ); CHKERRQ(ierr); 399 ierr = PetscMalloc( data_sz*ncrs_new*sizeof(PetscReal), a_coarse_data ); CHKERRQ(ierr); 400 401 ierr = VecGetArray( dest_crd, &array ); CHKERRQ(ierr); 402 data = *a_coarse_data; 403 for( jj=0; jj<a_ndata_cols ; jj++ ) { 404 for( ii=0 ; ii<ncrs_new ; ii++) { 405 for( kk=0; kk<a_ndata_rows ; kk++ ) { 406 PetscInt ix = ii*a_ndata_rows + kk + jj*strideNew, jx = ii*data_sz + kk*a_ndata_cols + jj; 407 data[ix] = PetscRealPart(array[jx]); 408 array[jx] = 1.e300; 409 } 410 } 411 } 412 ierr = VecRestoreArray( dest_crd, &array ); CHKERRQ(ierr); 413 ierr = VecDestroy( &dest_crd ); CHKERRQ(ierr); 414 415 /* 416 Invert for MatGetSubMatrix 417 */ 418 ierr = ISInvertPermutation( isnum, ncrs_new*a_cbs, &new_indices ); CHKERRQ(ierr); 419 ierr = ISSort( new_indices ); CHKERRQ(ierr); /* is this needed? */ 420 ierr = ISDestroy( &isnum ); CHKERRQ(ierr); 421 /* A_crs output */ 422 ierr = MatGetSubMatrix( Cmat, new_indices, new_indices, MAT_INITIAL_MATRIX, a_Amat_crs ); 423 CHKERRQ(ierr); 424 425 ierr = MatDestroy( &Cmat ); CHKERRQ(ierr); 426 Cmat = *a_Amat_crs; /* output */ 427 ierr = MatSetBlockSize( Cmat, a_cbs ); CHKERRQ(ierr); 428 429 /* prolongator */ 430 ierr = MatGetOwnershipRange( Pold, &Istart, &Iend ); CHKERRQ(ierr); 431 { 432 IS findices; 433 ierr = ISCreateStride(wcomm,Iend-Istart,Istart,1,&findices); CHKERRQ(ierr); 434 ierr = MatGetSubMatrix( Pold, findices, new_indices, MAT_INITIAL_MATRIX, &Pnew ); 435 CHKERRQ(ierr); 436 ierr = ISDestroy( &findices ); CHKERRQ(ierr); 437 } 438 ierr = MatDestroy( a_P_inout ); CHKERRQ(ierr); 439 *a_P_inout = Pnew; /* output */ 440 441 ierr = ISDestroy( &new_indices ); CHKERRQ(ierr); 442 ierr = PetscFree( counts ); CHKERRQ(ierr); 443 ierr = PetscFree( ranks ); CHKERRQ(ierr); 444 } 445 446 PetscFunctionReturn(0); 447 } 448 449 /* -------------------------------------------------------------------------- */ 450 /* 451 PCSetUp_GAMG - Prepares for the use of the GAMG preconditioner 452 by setting data structures and options. 453 454 Input Parameter: 455 . pc - the preconditioner context 456 457 Application Interface Routine: PCSetUp() 458 459 Notes: 460 The interface routine PCSetUp() is not usually called directly by 461 the user, but instead is called by PCApply() if necessary. 462 */ 463 #undef __FUNCT__ 464 #define __FUNCT__ "PCSetUp_GAMG" 465 PetscErrorCode PCSetUp_GAMG( PC a_pc ) 466 { 467 PetscErrorCode ierr; 468 PC_MG *mg = (PC_MG*)a_pc->data; 469 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 470 Mat Amat = a_pc->mat, Pmat = a_pc->pmat; 471 PetscInt fine_level, level, level1, M, N, bs, nloc, lidx, Istart, Iend; 472 MPI_Comm wcomm = ((PetscObject)a_pc)->comm; 473 PetscMPIInt mype,npe,nactivepe; 474 PetscBool isOK; 475 Mat Aarr[GAMG_MAXLEVELS], Parr[GAMG_MAXLEVELS]; 476 PetscReal *coarse_data = 0, *data, emaxs[GAMG_MAXLEVELS]; 477 MatInfo info; 478 479 PetscFunctionBegin; 480 if( a_pc->setupcalled ) { 481 /* no state data in GAMG to destroy */ 482 ierr = PCReset_MG( a_pc ); CHKERRQ(ierr); 483 } 484 ierr = MPI_Comm_rank(wcomm,&mype);CHKERRQ(ierr); 485 ierr = MPI_Comm_size(wcomm,&npe);CHKERRQ(ierr); 486 /* GAMG requires input of fine-grid matrix. It determines nlevels. */ 487 ierr = MatGetBlockSize( Amat, &bs ); CHKERRQ(ierr); 488 ierr = MatGetSize( Amat, &M, &N );CHKERRQ(ierr); 489 ierr = MatGetOwnershipRange( Amat, &Istart, &Iend ); CHKERRQ(ierr); 490 nloc = (Iend-Istart)/bs; assert((Iend-Istart)%bs == 0); 491 492 /* get data of not around */ 493 if( pc_gamg->m_data == 0 && nloc > 0 ) { 494 ierr = PCSetCoordinates_GAMG( a_pc, -1, 0 ); CHKERRQ( ierr ); 495 } 496 data = pc_gamg->m_data; 497 498 /* Get A_i and R_i */ 499 ierr = MatGetInfo(Amat,MAT_GLOBAL_SUM,&info); CHKERRQ(ierr); 500 PetscPrintf(PETSC_COMM_WORLD,"\t[%d]%s level %d N=%d, n data rows=%d, n data cols=%d, nnz/row (ave)=%d, np=%d\n", 501 mype,__FUNCT__,0,N,pc_gamg->m_data_rows,pc_gamg->m_data_cols,(PetscInt)(info.nz_used/(PetscReal)N),npe); 502 for ( level=0, Aarr[0] = Pmat, nactivepe = npe; /* hard wired stopping logic */ 503 level < GAMG_MAXLEVELS-1 && (level==0 || M>TOP_GRID_LIM) && (npe==1 || nactivepe>1); 504 level++ ){ 505 level1 = level + 1; 506 #if (defined PETSC_USE_LOG && defined GAMG_STAGES) 507 ierr = PetscLogStagePush(gamg_stages[level]); CHKERRQ( ierr ); 508 #endif 509 #if defined PETSC_USE_LOG 510 ierr = PetscLogEventBegin(gamg_setup_events[SET1],0,0,0,0);CHKERRQ(ierr); 511 #endif 512 ierr = createProlongation(Aarr[level], data, pc_gamg->m_dim, pc_gamg->m_data_cols, pc_gamg->m_useSA, 513 level, &bs, &Parr[level1], &coarse_data, &isOK, &emaxs[level] ); 514 CHKERRQ(ierr); 515 ierr = PetscFree( data ); CHKERRQ( ierr ); 516 #if defined PETSC_USE_LOG 517 ierr = PetscLogEventEnd(gamg_setup_events[SET1],0,0,0,0);CHKERRQ(ierr); 518 #endif 519 if(level==0) Aarr[0] = Amat; /* use Pmat for finest level setup, but use mat for solver */ 520 521 if( isOK ) { 522 #if defined PETSC_USE_LOG 523 ierr = PetscLogEventBegin(gamg_setup_events[SET2],0,0,0,0);CHKERRQ(ierr); 524 #endif 525 ierr = partitionLevel( Aarr[level], pc_gamg->m_useSA ? bs : 1, pc_gamg->m_data_cols, bs, 526 &Parr[level1], &coarse_data, &nactivepe, &Aarr[level1] ); 527 CHKERRQ(ierr); 528 #if defined PETSC_USE_LOG 529 ierr = PetscLogEventEnd(gamg_setup_events[SET2],0,0,0,0);CHKERRQ(ierr); 530 #endif 531 ierr = MatGetSize( Aarr[level1], &M, &N );CHKERRQ(ierr); 532 ierr = MatGetInfo(Aarr[level1],MAT_GLOBAL_SUM,&info); CHKERRQ(ierr); 533 PetscPrintf(PETSC_COMM_WORLD,"\t\t[%d]%s %d) N=%d, bs=%d, n data cols=%d, nnz/row (ave)=%d, %d active pes\n", 534 mype,__FUNCT__,level1,N,bs,pc_gamg->m_data_cols,(PetscInt)(info.nz_used/(PetscReal)N),nactivepe); 535 /* coarse grids with SA can have zero row/cols from singleton aggregates */ 536 /* aggregation method can probably gaurrentee this does not happen! - be safe for now */ 537 538 if( PETSC_TRUE ){ 539 Vec diag; PetscScalar *data_arr,v; PetscInt Istart,Iend,kk,nloceq,id; 540 v = 1.e-10; /* LU factor has hard wired numbers for small diags so this needs to match (yuk) */ 541 ierr = MatGetOwnershipRange(Aarr[level1], &Istart, &Iend); CHKERRQ(ierr); 542 nloceq = Iend-Istart; 543 ierr = MatGetVecs( Aarr[level1], &diag, 0 ); CHKERRQ(ierr); 544 ierr = MatGetDiagonal( Aarr[level1], diag ); CHKERRQ(ierr); 545 ierr = VecGetArray( diag, &data_arr ); CHKERRQ(ierr); 546 for(kk=0;kk<nloceq;kk++){ 547 if(data_arr[kk]==0.0) { 548 id = kk + Istart; 549 ierr = MatSetValues(Aarr[level1],1,&id,1,&id,&v,INSERT_VALUES); 550 CHKERRQ(ierr); 551 PetscPrintf(PETSC_COMM_SELF,"\t[%d]%s warning: added diag to zero (%d) on level %d \n",mype,__FUNCT__,id,level); 552 } 553 } 554 ierr = VecRestoreArray( diag, &data_arr ); CHKERRQ(ierr); 555 ierr = VecDestroy( &diag ); CHKERRQ(ierr); 556 ierr = MatAssemblyBegin(Aarr[level1],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 557 ierr = MatAssemblyEnd(Aarr[level1],MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 558 } 559 } 560 else{ 561 coarse_data = 0; 562 break; 563 } 564 data = coarse_data; 565 566 #if (defined PETSC_USE_LOG && defined GAMG_STAGES) 567 ierr = PetscLogStagePop(); CHKERRQ( ierr ); 568 #endif 569 } 570 if( coarse_data ) { 571 ierr = PetscFree( coarse_data ); CHKERRQ( ierr ); 572 } 573 PetscPrintf(PETSC_COMM_WORLD,"\t[%d]%s %d levels\n",0,__FUNCT__,level + 1); 574 pc_gamg->m_data = 0; /* destroyed coordinate data */ 575 pc_gamg->m_Nlevels = level + 1; 576 fine_level = level; 577 ierr = PCMGSetLevels(a_pc,pc_gamg->m_Nlevels,PETSC_NULL);CHKERRQ(ierr); 578 579 /* set default smoothers */ 580 for ( lidx=1, level = pc_gamg->m_Nlevels-2; 581 lidx <= fine_level; 582 lidx++, level--) { 583 PetscReal emax, emin; 584 KSP smoother; PC subpc; 585 ierr = PCMGGetSmoother( a_pc, lidx, &smoother ); CHKERRQ(ierr); 586 ierr = KSPSetType( smoother, KSPCHEBYCHEV );CHKERRQ(ierr); 587 if( emaxs[level] > 0.0 ) emax=emaxs[level]; 588 else{ /* eigen estimate 'emax' */ 589 KSP eksp; Mat Lmat = Aarr[level]; 590 Vec bb, xx; PC pc; 591 592 ierr = MatGetVecs( Lmat, &bb, 0 ); CHKERRQ(ierr); 593 ierr = MatGetVecs( Lmat, &xx, 0 ); CHKERRQ(ierr); 594 { 595 PetscRandom rctx; 596 ierr = PetscRandomCreate(wcomm,&rctx);CHKERRQ(ierr); 597 ierr = PetscRandomSetFromOptions(rctx);CHKERRQ(ierr); 598 ierr = VecSetRandom(bb,rctx);CHKERRQ(ierr); 599 ierr = PetscRandomDestroy( &rctx ); CHKERRQ(ierr); 600 } 601 ierr = KSPCreate(wcomm,&eksp);CHKERRQ(ierr); 602 ierr = KSPSetType( eksp, KSPCG ); CHKERRQ(ierr); 603 ierr = KSPSetInitialGuessNonzero( eksp, PETSC_FALSE ); CHKERRQ(ierr); 604 ierr = KSPSetOperators( eksp, Lmat, Lmat, DIFFERENT_NONZERO_PATTERN ); CHKERRQ( ierr ); 605 ierr = KSPGetPC( eksp, &pc );CHKERRQ( ierr ); 606 ierr = PCSetType( pc, PCPBJACOBI ); CHKERRQ(ierr); /* should be same as above */ 607 ierr = KSPSetTolerances( eksp, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT, 10 ); 608 CHKERRQ(ierr); 609 //ierr = KSPSetConvergenceTest( eksp, KSPSkipConverged, 0, 0 ); CHKERRQ(ierr); 610 ierr = KSPSetNormType( eksp, KSP_NORM_NONE ); CHKERRQ(ierr); 611 612 ierr = KSPSetComputeSingularValues( eksp,PETSC_TRUE ); CHKERRQ(ierr); 613 ierr = KSPSolve( eksp, bb, xx ); CHKERRQ(ierr); 614 ierr = KSPComputeExtremeSingularValues( eksp, &emax, &emin ); CHKERRQ(ierr); 615 ierr = VecDestroy( &xx ); CHKERRQ(ierr); 616 ierr = VecDestroy( &bb ); CHKERRQ(ierr); 617 ierr = KSPDestroy( &eksp ); CHKERRQ(ierr); 618 PetscPrintf(PETSC_COMM_WORLD,"\t\t\t%s max eigen=%e min=%e PC=%s\n",__FUNCT__,emax,emin,PETSC_GAMG_SMOOTHER); 619 } 620 { 621 PetscInt N1, N0, tt; 622 ierr = MatGetSize( Aarr[level], &N1, &tt ); CHKERRQ(ierr); 623 ierr = MatGetSize( Aarr[level+1], &N0, &tt ); CHKERRQ(ierr); 624 emin = 1.*emax/((PetscReal)N1/(PetscReal)N0); /* this should be about the coarsening rate */ 625 emax *= 1.05; 626 627 } 628 629 ierr = KSPSetOperators( smoother, Aarr[level], Aarr[level], DIFFERENT_NONZERO_PATTERN ); 630 ierr = KSPChebychevSetEigenvalues( smoother, emax, emin );CHKERRQ(ierr); 631 /* ierr = KSPSetTolerances(smoother,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,2); CHKERRQ(ierr); */ 632 ierr = KSPGetPC( smoother, &subpc ); CHKERRQ(ierr); 633 ierr = PCSetType( subpc, PETSC_GAMG_SMOOTHER ); CHKERRQ(ierr); 634 ierr = KSPSetNormType( smoother, KSP_NORM_NONE ); CHKERRQ(ierr); 635 } 636 { 637 KSP smoother; /* coarse grid */ 638 Mat Lmat = Aarr[pc_gamg->m_Nlevels-1]; 639 ierr = PCMGGetSmoother( a_pc, 0, &smoother ); CHKERRQ(ierr); 640 ierr = KSPSetOperators( smoother, Lmat, Lmat, DIFFERENT_NONZERO_PATTERN ); 641 CHKERRQ(ierr); 642 ierr = KSPSetNormType( smoother, KSP_NORM_NONE ); CHKERRQ(ierr); 643 } 644 645 /* should be called in PCSetFromOptions_GAMG(), but cannot be called prior to PCMGSetLevels() */ 646 ierr = PCSetFromOptions_MG(a_pc); CHKERRQ(ierr); 647 { 648 PetscBool galerkin; 649 ierr = PCMGGetGalerkin( a_pc, &galerkin); CHKERRQ(ierr); 650 if(galerkin){ 651 SETERRQ(wcomm,PETSC_ERR_ARG_WRONG, "GAMG does galerkin manually so it must not be used in PC_MG."); 652 } 653 } 654 655 /* set interpolation between the levels, clean up */ 656 for (lidx=0,level=pc_gamg->m_Nlevels-1; 657 lidx<fine_level; 658 lidx++, level--){ 659 ierr = PCMGSetInterpolation( a_pc, lidx+1, Parr[level] );CHKERRQ(ierr); 660 if( !PETSC_TRUE ) { 661 PetscViewer viewer; char fname[32]; 662 sprintf(fname,"Pmat_%d.m",level); 663 ierr = PetscViewerASCIIOpen( wcomm, fname, &viewer ); CHKERRQ(ierr); 664 ierr = PetscViewerSetFormat( viewer, PETSC_VIEWER_ASCII_MATLAB); CHKERRQ(ierr); 665 ierr = MatView( Parr[level], viewer ); CHKERRQ(ierr); 666 ierr = PetscViewerDestroy( &viewer ); 667 sprintf(fname,"Amat_%d.m",level); 668 ierr = PetscViewerASCIIOpen( wcomm, fname, &viewer ); CHKERRQ(ierr); 669 ierr = PetscViewerSetFormat( viewer, PETSC_VIEWER_ASCII_MATLAB); CHKERRQ(ierr); 670 ierr = MatView( Aarr[level], viewer ); CHKERRQ(ierr); 671 ierr = PetscViewerDestroy( &viewer ); 672 } 673 ierr = MatDestroy( &Parr[level] ); CHKERRQ(ierr); 674 ierr = MatDestroy( &Aarr[level] ); CHKERRQ(ierr); 675 } 676 677 /* setupcalled is set to 0 so that MG is setup from scratch */ 678 a_pc->setupcalled = 0; 679 ierr = PCSetUp_MG(a_pc);CHKERRQ(ierr); 680 681 PetscFunctionReturn(0); 682 } 683 684 /* ------------------------------------------------------------------------- */ 685 /* 686 PCDestroy_GAMG - Destroys the private context for the GAMG preconditioner 687 that was created with PCCreate_GAMG(). 688 689 Input Parameter: 690 . pc - the preconditioner context 691 692 Application Interface Routine: PCDestroy() 693 */ 694 #undef __FUNCT__ 695 #define __FUNCT__ "PCDestroy_GAMG" 696 PetscErrorCode PCDestroy_GAMG(PC pc) 697 { 698 PetscErrorCode ierr; 699 PC_MG *mg = (PC_MG*)pc->data; 700 PC_GAMG *pc_gamg= (PC_GAMG*)mg->innerctx; 701 702 PetscFunctionBegin; 703 ierr = PCReset_GAMG(pc);CHKERRQ(ierr); 704 ierr = PetscFree(pc_gamg);CHKERRQ(ierr); 705 ierr = PCDestroy_MG(pc);CHKERRQ(ierr); 706 PetscFunctionReturn(0); 707 } 708 709 #undef __FUNCT__ 710 #define __FUNCT__ "PCSetFromOptions_GAMG" 711 PetscErrorCode PCSetFromOptions_GAMG(PC pc) 712 { 713 /* PetscErrorCode ierr; */ 714 /* PC_MG *mg = (PC_MG*)pc->data; */ 715 /* PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; */ 716 /* MPI_Comm comm = ((PetscObject)pc)->comm; */ 717 718 PetscFunctionBegin; 719 PetscFunctionReturn(0); 720 } 721 722 /* -------------------------------------------------------------------------- */ 723 /* 724 PCCreate_GAMG - Creates a GAMG preconditioner context, PC_GAMG 725 726 Input Parameter: 727 . pc - the preconditioner context 728 729 Application Interface Routine: PCCreate() 730 731 */ 732 /* MC 733 PCGAMG - Use algebraic multigrid preconditioning. This preconditioner requires you provide 734 fine grid discretization matrix and coordinates on the fine grid. 735 736 Options Database Key: 737 Multigrid options(inherited) 738 + -pc_mg_cycles <1>: 1 for V cycle, 2 for W-cycle (MGSetCycles) 739 . -pc_mg_smoothup <1>: Number of post-smoothing steps (MGSetNumberSmoothUp) 740 . -pc_mg_smoothdown <1>: Number of pre-smoothing steps (MGSetNumberSmoothDown) 741 -pc_mg_type <multiplicative>: (one of) additive multiplicative full cascade kascade 742 GAMG options: 743 744 Level: intermediate 745 Concepts: multigrid 746 747 .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType, 748 PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), MPSetCycles(), PCMGSetNumberSmoothDown(), 749 PCMGSetNumberSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(), 750 PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(), 751 PCMGSetCyclesOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR() 752 M */ 753 754 EXTERN_C_BEGIN 755 #undef __FUNCT__ 756 #define __FUNCT__ "PCCreate_GAMG" 757 PetscErrorCode PCCreate_GAMG(PC pc) 758 { 759 PetscErrorCode ierr; 760 PC_GAMG *pc_gamg; 761 PC_MG *mg; 762 PetscClassId cookie; 763 764 PetscFunctionBegin; 765 /* PCGAMG is an inherited class of PCMG. Initialize pc as PCMG */ 766 ierr = PCSetType(pc,PCMG);CHKERRQ(ierr); /* calls PCCreate_MG() and MGCreate_Private() */ 767 ierr = PetscObjectChangeTypeName((PetscObject)pc,PCGAMG);CHKERRQ(ierr); 768 769 /* create a supporting struct and attach it to pc */ 770 ierr = PetscNewLog(pc,PC_GAMG,&pc_gamg);CHKERRQ(ierr); 771 mg = (PC_MG*)pc->data; 772 mg->innerctx = pc_gamg; 773 774 pc_gamg->m_Nlevels = -1; 775 776 /* overwrite the pointers of PCMG by the functions of PCGAMG */ 777 pc->ops->setfromoptions = PCSetFromOptions_GAMG; 778 pc->ops->setup = PCSetUp_GAMG; 779 pc->ops->reset = PCReset_GAMG; 780 pc->ops->destroy = PCDestroy_GAMG; 781 782 ierr = PetscObjectComposeFunctionDynamic( (PetscObject)pc, 783 "PCSetCoordinates_C", 784 "PCSetCoordinates_GAMG", 785 PCSetCoordinates_GAMG);CHKERRQ(ierr); 786 #if defined PETSC_USE_LOG 787 static int count = 0; 788 if( count++ == 0 ) { 789 PetscClassIdRegister("GAMG Setup",&cookie); 790 PetscLogEventRegister("GAMG: createProl", cookie, &gamg_setup_events[SET1]); 791 PetscLogEventRegister(" make graph", cookie, &gamg_setup_events[SET3]); 792 PetscLogEventRegister(" MIS/Agg", cookie, &gamg_setup_events[SET4]); 793 PetscLogEventRegister(" geo: growSupp", cookie, &gamg_setup_events[SET5]); 794 PetscLogEventRegister(" geo: triangle", cookie, &gamg_setup_events[SET6]); 795 PetscLogEventRegister(" search & set", cookie, &gamg_setup_events[FIND_V]); 796 PetscLogEventRegister(" SA: init", cookie, &gamg_setup_events[SET7]); 797 /* PetscLogEventRegister(" SA: frmProl0", cookie, &gamg_setup_events[SET8]); */ 798 PetscLogEventRegister(" SA: smooth", cookie, &gamg_setup_events[SET9]); 799 PetscLogEventRegister("GAMG: partLevel", cookie, &gamg_setup_events[SET2]); 800 PetscLogEventRegister(" PL repartition", cookie, &gamg_setup_events[SET12]); 801 /* PetscLogEventRegister(" PL move data", cookie, &gamg_setup_events[SET13]); */ 802 /* PetscLogEventRegister("GAMG: fix", cookie, &gamg_setup_events[SET10]); */ 803 /* PetscLogEventRegister("GAMG: set levels", cookie, &gamg_setup_events[SET11]); */ 804 805 /* create timer stages */ 806 #if defined GAMG_STAGES 807 { 808 char str[32]; 809 sprintf(str,"MG Level %d (finest)",0); 810 PetscLogStageRegister(str, &gamg_stages[0]); 811 PetscInt lidx; 812 for (lidx=1;lidx<9;lidx++){ 813 sprintf(str,"MG Level %d",lidx); 814 PetscLogStageRegister(str, &gamg_stages[lidx]); 815 } 816 } 817 #endif 818 } 819 #endif 820 PetscFunctionReturn(0); 821 } 822 EXTERN_C_END 823