1 /* 2 GAMG geometric-algebric multiogrid PC - Mark Adams 2011 3 */ 4 5 #include <../src/ksp/pc/impls/gamg/gamg.h> /*I "petscpc.h" I*/ 6 #include <private/kspimpl.h> 7 8 #include <assert.h> 9 #include <petscblaslapack.h> 10 11 typedef struct { 12 PetscInt nsmooths; 13 Mat aux_mat; 14 PetscBool sym_graph; 15 }PC_GAMG_AGG; 16 17 #undef __FUNCT__ 18 #define __FUNCT__ "PCGAMGSetNSmooths" 19 /*@ 20 PCGAMGSetNSmooths - Set number of smoothing steps (1 is typical) 21 22 Not Collective on PC 23 24 Input Parameters: 25 . pc - the preconditioner context 26 27 Options Database Key: 28 . -pc_gamg_agg_nsmooths 29 30 Level: intermediate 31 32 Concepts: Aggregation AMG preconditioner 33 34 .seealso: () 35 @*/ 36 PetscErrorCode PCGAMGSetNSmooths(PC pc, PetscInt n) 37 { 38 PetscErrorCode ierr; 39 40 PetscFunctionBegin; 41 PetscValidHeaderSpecific(pc,PC_CLASSID,1); 42 ierr = PetscTryMethod(pc,"PCGAMGSetNSmooths_C",(PC,PetscInt),(pc,n));CHKERRQ(ierr); 43 PetscFunctionReturn(0); 44 } 45 46 EXTERN_C_BEGIN 47 #undef __FUNCT__ 48 #define __FUNCT__ "PCGAMGSetNSmooths_GAMG" 49 PetscErrorCode PCGAMGSetNSmooths_GAMG(PC pc, PetscInt n) 50 { 51 PC_MG *mg = (PC_MG*)pc->data; 52 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 53 PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG*)pc_gamg->subctx; 54 55 PetscFunctionBegin; 56 pc_gamg_agg->nsmooths = n; 57 PetscFunctionReturn(0); 58 } 59 EXTERN_C_END 60 61 #undef __FUNCT__ 62 #define __FUNCT__ "PCGAMGSetSymGraph" 63 /*@ 64 PCGAMGSetSymGraph - 65 66 Not Collective on PC 67 68 Input Parameters: 69 . pc - the preconditioner context 70 71 Options Database Key: 72 . -pc_gamg_sym_graph 73 74 Level: intermediate 75 76 Concepts: Aggregation AMG preconditioner 77 78 .seealso: () 79 @*/ 80 PetscErrorCode PCGAMGSetSymGraph(PC pc, PetscBool n) 81 { 82 PetscErrorCode ierr; 83 84 PetscFunctionBegin; 85 PetscValidHeaderSpecific(pc,PC_CLASSID,1); 86 ierr = PetscTryMethod(pc,"PCGAMGSetSymGraph_C",(PC,PetscBool),(pc,n));CHKERRQ(ierr); 87 PetscFunctionReturn(0); 88 } 89 90 EXTERN_C_BEGIN 91 #undef __FUNCT__ 92 #define __FUNCT__ "PCGAMGSetSymGraph_GAMG" 93 PetscErrorCode PCGAMGSetSymGraph_GAMG(PC pc, PetscBool n) 94 { 95 PC_MG *mg = (PC_MG*)pc->data; 96 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 97 PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG*)pc_gamg->subctx; 98 99 PetscFunctionBegin; 100 pc_gamg_agg->sym_graph = n; 101 PetscFunctionReturn(0); 102 } 103 EXTERN_C_END 104 105 /* -------------------------------------------------------------------------- */ 106 /* 107 PCSetFromOptions_GAMG_AGG 108 109 Input Parameter: 110 . pc - 111 */ 112 #undef __FUNCT__ 113 #define __FUNCT__ "PCSetFromOptions_GAMG_AGG" 114 PetscErrorCode PCSetFromOptions_GAMG_AGG( PC pc ) 115 { 116 PetscErrorCode ierr; 117 PC_MG *mg = (PC_MG*)pc->data; 118 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 119 PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG*)pc_gamg->subctx; 120 PetscBool flag; 121 122 PetscFunctionBegin; 123 /* call base class */ 124 ierr = PCSetFromOptions_GAMG( pc ); CHKERRQ(ierr); 125 126 ierr = PetscOptionsHead("GAMG-AGG options"); CHKERRQ(ierr); 127 { 128 /* -pc_gamg_agg_nsmooths */ 129 pc_gamg_agg->nsmooths = 0; 130 ierr = PetscOptionsInt("-pc_gamg_agg_nsmooths", 131 "smoothing steps for smoothed aggregation, usually 1 (0)", 132 "PCGAMGSetNSmooths", 133 pc_gamg_agg->nsmooths, 134 &pc_gamg_agg->nsmooths, 135 &flag); 136 CHKERRQ(ierr); 137 138 /* -pc_gamg_sym_graph */ 139 pc_gamg_agg->sym_graph = PETSC_FALSE; 140 ierr = PetscOptionsBool("-pc_gamg_sym_graph", 141 "Set for asymetric matrices", 142 "PCGAMGSetSymGraph", 143 pc_gamg_agg->sym_graph, 144 &pc_gamg_agg->sym_graph, 145 &flag); 146 CHKERRQ(ierr); 147 } 148 ierr = PetscOptionsTail();CHKERRQ(ierr); 149 150 if( pc_gamg->verbose > 1 ) { 151 PetscPrintf(PETSC_COMM_WORLD,"[%d]%s done\n",0,__FUNCT__); 152 } 153 154 PetscFunctionReturn(0); 155 } 156 157 /* -------------------------------------------------------------------------- */ 158 /* 159 PCDestroy_AGG 160 161 Input Parameter: 162 . pc - 163 */ 164 #undef __FUNCT__ 165 #define __FUNCT__ "PCDestroy_AGG" 166 PetscErrorCode PCDestroy_AGG( PC pc ) 167 { 168 PetscErrorCode ierr; 169 PC_MG *mg = (PC_MG*)pc->data; 170 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 171 PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG*)pc_gamg->subctx; 172 173 PetscFunctionBegin; 174 if( pc_gamg_agg ) { 175 ierr = PetscFree(pc_gamg_agg);CHKERRQ(ierr); 176 pc_gamg_agg = 0; 177 } 178 179 /* call base class */ 180 ierr = PCDestroy_GAMG( pc );CHKERRQ(ierr); 181 182 PetscFunctionReturn(0); 183 } 184 185 /* -------------------------------------------------------------------------- */ 186 /* 187 PCSetCoordinates_AGG 188 189 Input Parameter: 190 . pc - the preconditioner context 191 */ 192 EXTERN_C_BEGIN 193 #undef __FUNCT__ 194 #define __FUNCT__ "PCSetCoordinates_AGG" 195 PetscErrorCode PCSetCoordinates_AGG( PC pc, PetscInt ndm, PetscReal *coords ) 196 { 197 PC_MG *mg = (PC_MG*)pc->data; 198 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 199 PetscErrorCode ierr; 200 PetscInt arrsz,bs,my0,kk,ii,jj,nloc,Iend; 201 Mat Amat = pc->pmat; 202 203 PetscFunctionBegin; 204 PetscValidHeaderSpecific( Amat, MAT_CLASSID, 1 ); 205 ierr = MatGetBlockSize( Amat, &bs ); CHKERRQ( ierr ); 206 ierr = MatGetOwnershipRange( Amat, &my0, &Iend ); CHKERRQ(ierr); 207 nloc = (Iend-my0)/bs; 208 if((Iend-my0)%bs!=0) SETERRQ1(((PetscObject)Amat)->comm,PETSC_ERR_ARG_WRONG, "Bad local size %d.",nloc); 209 210 /* SA: null space vectors */ 211 if( coords && bs==1 ) pc_gamg->data_cell_cols = 1; /* scalar w/ coords and SA (not needed) */ 212 else if( coords ) pc_gamg->data_cell_cols = (ndm==2 ? 3 : 6); /* elasticity */ 213 else pc_gamg->data_cell_cols = bs; /* no data, force SA with constant null space vectors */ 214 pc_gamg->data_cell_rows = bs; 215 216 arrsz = nloc*pc_gamg->data_cell_rows*pc_gamg->data_cell_cols; 217 218 /* create data - syntactic sugar that should be refactored at some point */ 219 if (pc_gamg->data==0 || (pc_gamg->data_sz != arrsz)) { 220 ierr = PetscFree( pc_gamg->data ); CHKERRQ(ierr); 221 ierr = PetscMalloc(arrsz*sizeof(PetscReal), &pc_gamg->data ); CHKERRQ(ierr); 222 } 223 /* copy data in - column oriented */ 224 for(kk=0;kk<nloc;kk++){ 225 const PetscInt M = Iend - my0; 226 PetscReal *data = &pc_gamg->data[kk*bs]; 227 if( pc_gamg->data_cell_cols==1 ) *data = 1.0; 228 else { 229 for(ii=0;ii<bs;ii++) 230 for(jj=0;jj<bs;jj++) 231 if(ii==jj)data[ii*M + jj] = 1.0; /* translational modes */ 232 else data[ii*M + jj] = 0.0; 233 if( coords ) { 234 if( ndm == 2 ){ /* rotational modes */ 235 data += 2*M; 236 data[0] = -coords[2*kk+1]; 237 data[1] = coords[2*kk]; 238 } 239 else { 240 data += 3*M; 241 data[0] = 0.0; data[M+0] = coords[3*kk+2]; data[2*M+0] = -coords[3*kk+1]; 242 data[1] = -coords[3*kk+2]; data[M+1] = 0.0; data[2*M+1] = coords[3*kk]; 243 data[2] = coords[3*kk+1]; data[M+2] = -coords[3*kk]; data[2*M+2] = 0.0; 244 } 245 } 246 } 247 } 248 249 pc_gamg->data_sz = arrsz; 250 251 PetscFunctionReturn(0); 252 } 253 EXTERN_C_END 254 255 typedef PetscInt NState; 256 static const NState NOT_DONE=-2; 257 static const NState DELETED=-1; 258 static const NState REMOVED=-3; 259 #define IS_SELECTED(s) (s!=DELETED && s!=NOT_DONE && s!=REMOVED) 260 261 /* -------------------------------------------------------------------------- */ 262 /* 263 smoothAggs - greedy grab of with G1 (unsquared graph) -- AIJ specific 264 - AGG-MG specific: clears singletons out of 'selected_2' 265 266 Input Parameter: 267 . Gmat_2 - glabal matrix of graph (data not defined) 268 . Gmat_1 - base graph to grab with 269 . selected_2 - 270 Input/Output Parameter: 271 . llist_aggs_2 - linked list of aggs, ghost lids are based on Gmat_2 (squared graph) 272 */ 273 #undef __FUNCT__ 274 #define __FUNCT__ "smoothAggs" 275 PetscErrorCode smoothAggs( const Mat Gmat_2, /* base (squared) graph */ 276 const Mat Gmat_1, /* base graph, could be unsymmetic */ 277 const IS selected_2, /* [nselected total] selected vertices */ 278 IS llist_aggs_2 /* [nloc_nghost] global ID of aggregate */ 279 ) 280 { 281 PetscErrorCode ierr; 282 PetscBool isMPI; 283 Mat_SeqAIJ *matA_1, *matB_1=0, *matA_2, *matB_2=0; 284 MPI_Comm wcomm = ((PetscObject)Gmat_2)->comm; 285 PetscMPIInt mype; 286 PetscInt lid,*ii,*idx,ix,Iend,my0,nnodes_2,kk,n,j; 287 Mat_MPIAIJ *mpimat_2 = 0, *mpimat_1=0; 288 const PetscInt nloc = Gmat_2->rmap->n; 289 PetscScalar *cpcol_1_state,*cpcol_2_state,*deleted_parent_gid; 290 PetscInt *lid_cprowID_1,*id_llist_2,*lid_cprowID_2; 291 NState *lid_state; 292 293 PetscFunctionBegin; 294 ierr = MPI_Comm_rank( wcomm, &mype ); CHKERRQ(ierr); 295 ierr = MatGetOwnershipRange(Gmat_1,&my0,&Iend); CHKERRQ(ierr); 296 297 if( !PETSC_TRUE ) { 298 PetscViewer viewer; char fname[32]; static int llev=0; 299 sprintf(fname,"Gmat2_%d.m",llev++); 300 PetscViewerASCIIOpen(wcomm,fname,&viewer); 301 ierr = PetscViewerSetFormat( viewer, PETSC_VIEWER_ASCII_MATLAB); CHKERRQ(ierr); 302 ierr = MatView(Gmat_2, viewer ); CHKERRQ(ierr); 303 ierr = PetscViewerDestroy( &viewer ); 304 } 305 306 { /* copy linked list into temp buffer - should not work directly on pointer */ 307 const PetscInt *llist_idx; 308 ierr = ISGetSize( llist_aggs_2, &nnodes_2 ); CHKERRQ(ierr); 309 ierr = PetscMalloc( nnodes_2*sizeof(PetscInt), &id_llist_2 ); CHKERRQ(ierr); 310 ierr = ISGetIndices( llist_aggs_2, &llist_idx ); CHKERRQ(ierr); 311 for(lid=0;lid<nnodes_2;lid++) id_llist_2[lid] = llist_idx[lid]; 312 ierr = ISRestoreIndices( llist_aggs_2, &llist_idx ); CHKERRQ(ierr); 313 } 314 315 /* get submatrices */ 316 ierr = PetscTypeCompare( (PetscObject)Gmat_1, MATMPIAIJ, &isMPI ); CHKERRQ(ierr); 317 if(isMPI) { 318 /* grab matrix objects */ 319 mpimat_2 = (Mat_MPIAIJ*)Gmat_2->data; 320 mpimat_1 = (Mat_MPIAIJ*)Gmat_1->data; 321 matA_1 = (Mat_SeqAIJ*)mpimat_1->A->data; 322 matB_1 = (Mat_SeqAIJ*)mpimat_1->B->data; 323 matA_2 = (Mat_SeqAIJ*)mpimat_2->A->data; 324 matB_2 = (Mat_SeqAIJ*)mpimat_2->B->data; 325 326 /* force compressed row storage for B matrix in AuxMat */ 327 matB_1->compressedrow.check = PETSC_TRUE; 328 ierr = MatCheckCompressedRow(mpimat_1->B,&matB_1->compressedrow,matB_1->i,Gmat_1->rmap->n,-1.0); 329 CHKERRQ(ierr); 330 331 ierr = PetscMalloc( nloc*sizeof(PetscInt), &lid_cprowID_2 ); CHKERRQ(ierr); 332 ierr = PetscMalloc( nloc*sizeof(PetscInt), &lid_cprowID_1 ); CHKERRQ(ierr); 333 for(lid=0;lid<nloc;lid++) lid_cprowID_1[lid] = lid_cprowID_2[lid] = -1; 334 for (ix=0; ix<matB_1->compressedrow.nrows; ix++) { 335 PetscInt lid = matB_1->compressedrow.rindex[ix]; 336 lid_cprowID_1[lid] = ix; 337 } 338 for (ix=0; ix<matB_2->compressedrow.nrows; ix++) { 339 PetscInt lid = matB_2->compressedrow.rindex[ix]; 340 lid_cprowID_2[lid] = ix; 341 } 342 } 343 else { 344 matA_1 = (Mat_SeqAIJ*)Gmat_1->data; 345 matA_2 = (Mat_SeqAIJ*)Gmat_2->data; 346 lid_cprowID_2 = lid_cprowID_1 = 0; 347 } 348 assert( matA_1 && !matA_1->compressedrow.use ); 349 assert( matB_1==0 || matB_1->compressedrow.use ); 350 assert( matA_2 && !matA_2->compressedrow.use ); 351 assert( matB_2==0 || matB_2->compressedrow.use ); 352 353 /* get state of locals and selected gid for deleted */ 354 ierr = PetscMalloc( nloc*sizeof(NState), &lid_state ); CHKERRQ(ierr); 355 ierr = PetscMalloc( nloc*sizeof(PetscScalar), &deleted_parent_gid ); CHKERRQ(ierr); 356 for( lid = 0 ; lid < nloc ; lid++ ) { 357 deleted_parent_gid[lid] = -1.0; 358 lid_state[lid] = DELETED; 359 } 360 /* set index into compressed row 'lid_cprowID', not -1 means its a boundary node */ 361 { 362 PetscInt nSelected; 363 const PetscInt *selected_idx; 364 /* set local selected */ 365 ierr = ISGetSize( selected_2, &nSelected ); CHKERRQ(ierr); 366 ierr = ISGetIndices( selected_2, &selected_idx ); CHKERRQ(ierr); 367 for(kk=0;kk<nSelected;kk++){ 368 PetscInt lid = selected_idx[kk]; 369 if(lid<nloc) lid_state[lid] = (NState)(lid+my0); /* selected flag */ 370 else break; 371 /* remove singletons */ 372 ii = matA_2->i; n = ii[lid+1] - ii[lid]; 373 if( n < 2 ) { 374 if(!lid_cprowID_2 || (ix=lid_cprowID_2[lid])==-1 || (matB_2->compressedrow.i[ix+1]-matB_2->compressedrow.i[ix])==0){ 375 lid_state[lid] = REMOVED; 376 } 377 } 378 } 379 ierr = ISRestoreIndices( selected_2, &selected_idx ); CHKERRQ(ierr); 380 } 381 /* map local to selected local, -1 means a ghost owns it */ 382 for(lid=kk=0;lid<nloc;lid++){ 383 NState state = lid_state[lid]; 384 if( IS_SELECTED(state) ){ 385 PetscInt flid = lid; 386 do{ 387 if(flid<nloc){ 388 deleted_parent_gid[flid] = (PetscScalar)(lid + my0); 389 } 390 kk++; 391 } while( (flid=id_llist_2[flid]) != -1 ); 392 } 393 } 394 /* get 'cpcol_1_state', 'cpcol_2_state' - uses mpimat_1->lvec & mpimat_2->lvec for temp space */ 395 if (isMPI) { 396 Vec tempVec; 397 398 /* get 'cpcol_1_state' */ 399 ierr = MatGetVecs( Gmat_1, &tempVec, 0 ); CHKERRQ(ierr); 400 for(kk=0,j=my0;kk<nloc;kk++,j++){ 401 PetscScalar v = (PetscScalar)lid_state[kk]; 402 ierr = VecSetValues( tempVec, 1, &j, &v, INSERT_VALUES ); CHKERRQ(ierr); 403 } 404 ierr = VecAssemblyBegin( tempVec ); CHKERRQ(ierr); 405 ierr = VecAssemblyEnd( tempVec ); CHKERRQ(ierr); 406 ierr = VecScatterBegin(mpimat_1->Mvctx,tempVec, mpimat_1->lvec,INSERT_VALUES,SCATTER_FORWARD); 407 CHKERRQ(ierr); 408 ierr = VecScatterEnd(mpimat_1->Mvctx,tempVec, mpimat_1->lvec,INSERT_VALUES,SCATTER_FORWARD); 409 CHKERRQ(ierr); 410 ierr = VecGetArray( mpimat_1->lvec, &cpcol_1_state ); CHKERRQ(ierr); 411 ierr = VecDestroy( &tempVec ); CHKERRQ(ierr); 412 413 /* get 'cpcol_2_state' */ 414 ierr = MatGetVecs( Gmat_2, &tempVec, 0 ); CHKERRQ(ierr); 415 for(kk=0,j=my0;kk<nloc;kk++,j++){ 416 PetscScalar v = (PetscScalar)lid_state[kk]; 417 ierr = VecSetValues( tempVec, 1, &j, &v, INSERT_VALUES ); CHKERRQ(ierr); 418 } 419 ierr = VecAssemblyBegin( tempVec ); CHKERRQ(ierr); 420 ierr = VecAssemblyEnd( tempVec ); CHKERRQ(ierr); 421 ierr = VecScatterBegin(mpimat_2->Mvctx,tempVec, mpimat_2->lvec,INSERT_VALUES,SCATTER_FORWARD); 422 CHKERRQ(ierr); 423 ierr = VecScatterEnd(mpimat_2->Mvctx,tempVec, mpimat_2->lvec,INSERT_VALUES,SCATTER_FORWARD); 424 CHKERRQ(ierr); 425 ierr = VecGetArray( mpimat_2->lvec, &cpcol_2_state ); CHKERRQ(ierr); 426 ierr = VecDestroy( &tempVec ); CHKERRQ(ierr); 427 } /* ismpi */ 428 429 /* doit */ 430 for(lid=0;lid<nloc;lid++){ 431 NState state = lid_state[lid]; 432 if( IS_SELECTED(state) ) { /* steal locals */ 433 ii = matA_1->i; n = ii[lid+1] - ii[lid]; 434 idx = matA_1->j + ii[lid]; 435 for (j=0; j<n; j++) { 436 PetscInt flid, lidj = idx[j], sgid; 437 NState statej = lid_state[lidj]; 438 if (statej==DELETED && (sgid=(PetscInt)PetscRealPart(deleted_parent_gid[lidj])) != lid+my0) { /* steal local */ 439 deleted_parent_gid[lidj] = (PetscScalar)(lid+my0); /* send this with _2 */ 440 if( sgid >= my0 && sgid < my0+nloc ){ /* I'm stealing this local from a local */ 441 PetscInt hav=0, flid2=sgid-my0, lastid; 442 /* looking for local from local so id_llist_2 works */ 443 for( lastid=flid2, flid=id_llist_2[flid2] ; flid!=-1 ; flid=id_llist_2[flid] ) { 444 if( flid == lidj ) { 445 id_llist_2[lastid] = id_llist_2[flid]; /* remove lidj from list */ 446 id_llist_2[flid] = id_llist_2[lid]; id_llist_2[lid] = flid; /* insert 'lidj' into head of llist */ 447 hav++; 448 break; 449 } 450 lastid = flid; 451 } 452 if(hav!=1){ 453 if(hav==0)SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"failed to find adj in 'selected' lists - structurally unsymmetric matrix"); 454 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"found node %d times???",hav); 455 } 456 } 457 else{ /* I'm stealing this local, owned by a ghost - ok to use _2, local */ 458 assert(sgid==-1); 459 id_llist_2[lidj] = id_llist_2[lid]; id_llist_2[lid] = lidj; /* insert 'lidj' into head of llist */ 460 /* local remove at end, off add/rm at end */ 461 } 462 } 463 } 464 } 465 else if( state == DELETED && lid_cprowID_1 ) { 466 PetscInt sgidold = (PetscInt)PetscRealPart(deleted_parent_gid[lid]); 467 /* see if I have a selected ghost neighbor that will steal me */ 468 if( (ix=lid_cprowID_1[lid]) != -1 ){ 469 ii = matB_1->compressedrow.i; n = ii[ix+1] - ii[ix]; 470 idx = matB_1->j + ii[ix]; 471 for( j=0 ; j<n ; j++ ) { 472 PetscInt cpid = idx[j]; 473 NState statej = (NState)PetscRealPart(cpcol_1_state[cpid]); 474 if( IS_SELECTED(statej) && sgidold != (PetscInt)statej ) { /* ghost will steal this, remove from my list */ 475 deleted_parent_gid[lid] = (PetscScalar)statej; /* send who selected with _2 */ 476 if( sgidold>=my0 && sgidold<(my0+nloc) ) { /* this was mine */ 477 PetscInt lastid,hav=0,flid,oldslidj=sgidold-my0; 478 /* remove from 'oldslidj' list, local so _2 is OK */ 479 for( lastid=oldslidj, flid=id_llist_2[oldslidj] ; flid != -1 ; flid=id_llist_2[flid] ) { 480 if( flid == lid ) { 481 id_llist_2[lastid] = id_llist_2[flid]; /* remove lid from oldslidj list */ 482 hav++; 483 break; 484 } 485 lastid = flid; 486 } 487 if(hav!=1){ 488 if(hav==0)SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"failed to find adj in 'selected' lists - structurally unsymmetric matrix"); 489 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"found node %d times???",hav); 490 } 491 id_llist_2[lid] = -1; /* terminate linked list - needed? */ 492 } 493 else assert(id_llist_2[lid] == -1); 494 } 495 } 496 } 497 } /* selected/deleted */ 498 else assert(state == REMOVED || !lid_cprowID_1); 499 } /* node loop */ 500 501 if( isMPI ) { 502 PetscScalar *cpcol_2_sel_gid; 503 Vec tempVec; 504 PetscInt cpid; 505 506 ierr = VecRestoreArray( mpimat_1->lvec, &cpcol_1_state ); CHKERRQ(ierr); 507 ierr = VecRestoreArray( mpimat_2->lvec, &cpcol_2_state ); CHKERRQ(ierr); 508 509 /* get 'cpcol_2_sel_gid' */ 510 ierr = MatGetVecs( Gmat_2, &tempVec, 0 ); CHKERRQ(ierr); 511 for(kk=0,j=my0;kk<nloc;kk++,j++){ 512 ierr = VecSetValues( tempVec, 1, &j, &deleted_parent_gid[kk], INSERT_VALUES ); CHKERRQ(ierr); 513 } 514 ierr = VecAssemblyBegin( tempVec ); CHKERRQ(ierr); 515 ierr = VecAssemblyEnd( tempVec ); CHKERRQ(ierr); 516 ierr = VecScatterBegin(mpimat_2->Mvctx,tempVec, mpimat_2->lvec,INSERT_VALUES,SCATTER_FORWARD); 517 CHKERRQ(ierr); 518 ierr = VecScatterEnd(mpimat_2->Mvctx,tempVec, mpimat_2->lvec,INSERT_VALUES,SCATTER_FORWARD); 519 CHKERRQ(ierr); 520 ierr = VecDestroy( &tempVec ); CHKERRQ(ierr); 521 522 ierr = VecGetArray( mpimat_2->lvec, &cpcol_2_sel_gid ); CHKERRQ(ierr); 523 524 /* look for deleted ghosts and see if they moved */ 525 for(lid=0;lid<nloc;lid++){ 526 NState state = lid_state[lid]; 527 if( IS_SELECTED(state) ){ 528 PetscInt flid,lastid,old_sgid=lid+my0; 529 /* look for deleted ghosts and see if they moved */ 530 for( lastid=lid, flid=id_llist_2[lid] ; flid!=-1 ; flid=id_llist_2[flid] ) { 531 if( flid>=nloc ) { 532 PetscInt cpid = flid-nloc, sgid_new = (PetscInt)PetscRealPart(cpcol_2_sel_gid[cpid]); 533 if( sgid_new != old_sgid && sgid_new != -1 ) { 534 id_llist_2[lastid] = id_llist_2[flid]; /* remove 'flid' from list */ 535 id_llist_2[flid] = -1; 536 flid = lastid; 537 } /* if it changed parents */ 538 else lastid = flid; 539 } /* for ghost nodes */ 540 else lastid = flid; 541 } /* loop over list of deleted */ 542 } /* selected */ 543 } 544 545 /* look at ghosts, see if they changed, and moved here */ 546 for(cpid=0;cpid<nnodes_2-nloc;cpid++){ 547 PetscInt sgid_new = (PetscInt)PetscRealPart(cpcol_2_sel_gid[cpid]); 548 if( sgid_new>=my0 && sgid_new<(my0+nloc) ) { /* this is mine */ 549 PetscInt lastid,flid,slid_new=sgid_new-my0,flidj=nloc+cpid,hav=0; 550 for( lastid=slid_new, flid=id_llist_2[slid_new] ; flid != -1 ; flid=id_llist_2[flid] ) { 551 if( flid == flidj ) { 552 hav++; 553 break; 554 } 555 lastid = flid; 556 } 557 if( hav != 1 ){ 558 assert(id_llist_2[flidj] == -1); 559 id_llist_2[flidj] = id_llist_2[slid_new]; id_llist_2[slid_new] = flidj; /* insert 'flidj' into head of llist */ 560 } 561 } 562 } 563 564 ierr = VecRestoreArray( mpimat_2->lvec, &cpcol_2_sel_gid ); CHKERRQ(ierr); 565 ierr = PetscFree( lid_cprowID_1 ); CHKERRQ(ierr); 566 ierr = PetscFree( lid_cprowID_2 ); CHKERRQ(ierr); 567 } 568 569 /* copy out new aggs */ 570 ierr = ISGeneralSetIndices(llist_aggs_2, nnodes_2, id_llist_2, PETSC_COPY_VALUES ); CHKERRQ(ierr); 571 572 ierr = PetscFree( id_llist_2 ); CHKERRQ(ierr); 573 ierr = PetscFree( deleted_parent_gid ); CHKERRQ(ierr); 574 ierr = PetscFree( lid_state ); CHKERRQ(ierr); 575 576 PetscFunctionReturn(0); 577 } 578 579 /* -------------------------------------------------------------------------- */ 580 /* 581 PCSetData_AGG 582 583 Input Parameter: 584 . pc - 585 */ 586 #undef __FUNCT__ 587 #define __FUNCT__ "PCSetData_AGG" 588 PetscErrorCode PCSetData_AGG( PC pc ) 589 { 590 PetscErrorCode ierr; 591 PetscFunctionBegin; 592 ierr = PCSetCoordinates_AGG( pc, -1, PETSC_NULL ); CHKERRQ(ierr); 593 PetscFunctionReturn(0); 594 } 595 596 /* -------------------------------------------------------------------------- */ 597 /* 598 formProl0 599 600 Input Parameter: 601 . selected - list of selected local ID, includes selected ghosts 602 . locals_llist - linked list with aggregates 603 . bs - block size 604 . nSAvec - num columns of new P 605 . my0crs - global index of locals 606 . data_stride - bs*(nloc nodes + ghost nodes) 607 . data_in[data_stride*nSAvec] - local data on fine grid 608 . flid_fgid[data_stride/bs] - make local to global IDs, includes ghosts in 'locals_llist' 609 Output Parameter: 610 . a_data_out - in with fine grid data (w/ghosts), out with coarse grid data 611 . a_Prol - prolongation operator 612 */ 613 #undef __FUNCT__ 614 #define __FUNCT__ "formProl0" 615 PetscErrorCode formProl0( IS selected, /* list of selected local ID, includes selected ghosts */ 616 IS locals_llist, /* linked list from selected vertices of aggregate unselected vertices */ 617 const PetscInt bs, 618 const PetscInt nSAvec, 619 const PetscInt my0crs, 620 const PetscInt data_stride, 621 PetscReal data_in[], 622 const PetscInt flid_fgid[], 623 PetscReal **a_data_out, 624 Mat a_Prol /* prolongation operator (output)*/ 625 ) 626 { 627 PetscErrorCode ierr; 628 PetscInt Istart,Iend,nFineLoc,clid,flid,aggID,kk,jj,ii,mm,nLocalSelected,ndone,nSelected,minsz; 629 MPI_Comm wcomm = ((PetscObject)a_Prol)->comm; 630 PetscMPIInt mype, npe; 631 const PetscInt *selected_idx,*llist_idx; 632 PetscReal *out_data; 633 #define OUT_AGGS 634 #ifdef OUT_AGGS 635 static PetscInt llev = 0; char fname[32]; FILE *file; 636 sprintf(fname,"aggs_%d.m",llev++); 637 file = fopen(fname,"w"); 638 fprintf(file,"figure,\n"); 639 #endif 640 641 PetscFunctionBegin; 642 ierr = MPI_Comm_rank(wcomm,&mype);CHKERRQ(ierr); 643 ierr = MPI_Comm_size(wcomm,&npe);CHKERRQ(ierr); 644 ierr = MatGetOwnershipRange( a_Prol, &Istart, &Iend ); CHKERRQ(ierr); 645 nFineLoc = (Iend-Istart)/bs; assert((Iend-Istart)%bs==0); 646 647 ierr = ISGetSize( selected, &nSelected ); CHKERRQ(ierr); 648 ierr = ISGetIndices( selected, &selected_idx ); CHKERRQ(ierr); 649 ierr = ISGetIndices( locals_llist, &llist_idx ); CHKERRQ(ierr); 650 for(kk=0,nLocalSelected=0;kk<nSelected;kk++){ 651 PetscInt lid = selected_idx[kk]; 652 if( lid<nFineLoc && llist_idx[lid] != -1 ) nLocalSelected++; 653 } 654 655 /* aloc space for coarse point data (output) */ 656 #define DATA_OUT_STRIDE (nLocalSelected*nSAvec) 657 ierr = PetscMalloc( DATA_OUT_STRIDE*nSAvec*sizeof(PetscReal), &out_data ); CHKERRQ(ierr); 658 for(ii=0;ii<DATA_OUT_STRIDE*nSAvec;ii++) out_data[ii]=1.e300; 659 *a_data_out = out_data; /* output - stride nLocalSelected*nSAvec */ 660 661 /* find points and set prolongation */ 662 minsz = 100; 663 ndone = 0; 664 for( mm = clid = 0 ; mm < nSelected ; mm++ ){ 665 PetscInt lid = selected_idx[mm]; 666 PetscInt cgid = my0crs + clid, cids[100]; 667 668 if( lid>=nFineLoc || llist_idx[lid]==-1 ) continue; /* skip ghost or singleton */ 669 670 /* count agg */ 671 aggID = 0; 672 flid = selected_idx[mm]; assert(flid != -1); 673 do{ 674 aggID++; 675 } while( (flid=llist_idx[flid]) != -1 ); 676 if( aggID<minsz ) minsz = aggID; 677 678 /* get block */ 679 { 680 PetscBLASInt asz=aggID,M=asz*bs,N=nSAvec,INFO; 681 PetscBLASInt Mdata=M+((N-M>0)?N-M:0),LDA=Mdata,LWORK=N*bs; 682 PetscScalar *qqc,*qqr,*TAU,*WORK; 683 PetscInt *fids; 684 685 ierr = PetscMalloc( (Mdata*N)*sizeof(PetscScalar), &qqc ); CHKERRQ(ierr); 686 ierr = PetscMalloc( (M*N)*sizeof(PetscScalar), &qqr ); CHKERRQ(ierr); 687 ierr = PetscMalloc( N*sizeof(PetscScalar), &TAU ); CHKERRQ(ierr); 688 ierr = PetscMalloc( LWORK*sizeof(PetscScalar), &WORK ); CHKERRQ(ierr); 689 ierr = PetscMalloc( M*sizeof(PetscInt), &fids ); CHKERRQ(ierr); 690 691 flid = selected_idx[mm]; 692 aggID = 0; 693 do{ 694 /* copy in B_i matrix - column oriented */ 695 PetscReal *data = &data_in[flid*bs]; 696 for( kk = ii = 0; ii < bs ; ii++ ) { 697 for( jj = 0; jj < N ; jj++ ) { 698 qqc[jj*Mdata + aggID*bs + ii] = data[jj*data_stride + ii]; 699 } 700 } 701 #ifdef OUT_AGGS 702 { 703 char str[] = "plot(%e,%e,'r*'), hold on,\n", col[] = "rgbkmc", sim[] = "*os+h>d<vx^"; 704 str[12] = col[clid%6]; str[13] = sim[(clid/6)%11]; 705 fprintf(file,str,data[2*data_stride+1],-data[2*data_stride]); 706 } 707 #endif 708 /* set fine IDs */ 709 for(kk=0;kk<bs;kk++) fids[aggID*bs + kk] = flid_fgid[flid]*bs + kk; 710 711 aggID++; 712 }while( (flid=llist_idx[flid]) != -1 ); 713 714 /* pad with zeros */ 715 for( ii = asz*bs; ii < Mdata ; ii++ ) { 716 for( jj = 0; jj < N ; jj++, kk++ ) { 717 qqc[jj*Mdata + ii] = .0; 718 } 719 } 720 721 ndone += aggID; 722 /* QR */ 723 LAPACKgeqrf_( &Mdata, &N, qqc, &LDA, TAU, WORK, &LWORK, &INFO ); 724 if( INFO != 0 ) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"xGEQRS error"); 725 /* get R - column oriented - output B_{i+1} */ 726 { 727 PetscReal *data = &out_data[clid*nSAvec]; 728 for( jj = 0; jj < nSAvec ; jj++ ) { 729 for( ii = 0; ii < nSAvec ; ii++ ) { 730 assert(data[jj*DATA_OUT_STRIDE + ii] == 1.e300); 731 if( ii <= jj ) data[jj*DATA_OUT_STRIDE + ii] = PetscRealPart(qqc[jj*Mdata + ii]); 732 else data[jj*DATA_OUT_STRIDE + ii] = 0.; 733 } 734 } 735 } 736 737 /* get Q - row oriented */ 738 LAPACKungqr_( &Mdata, &N, &N, qqc, &LDA, TAU, WORK, &LWORK, &INFO ); 739 if( INFO != 0 ) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"xORGQR error arg %d",-INFO); 740 741 for( ii = 0 ; ii < M ; ii++ ){ 742 for( jj = 0 ; jj < N ; jj++ ) { 743 qqr[N*ii + jj] = qqc[jj*Mdata + ii]; 744 } 745 } 746 747 /* add diagonal block of P0 */ 748 for(kk=0;kk<N;kk++) { 749 cids[kk] = N*cgid + kk; /* global col IDs in P0 */ 750 } 751 ierr = MatSetValues(a_Prol,M,fids,N,cids,qqr,INSERT_VALUES); CHKERRQ(ierr); 752 753 ierr = PetscFree( qqc ); CHKERRQ(ierr); 754 ierr = PetscFree( qqr ); CHKERRQ(ierr); 755 ierr = PetscFree( TAU ); CHKERRQ(ierr); 756 ierr = PetscFree( WORK ); CHKERRQ(ierr); 757 ierr = PetscFree( fids ); CHKERRQ(ierr); 758 } /* scoping */ 759 clid++; 760 } /* for all coarse nodes */ 761 762 /* ierr = MPI_Allreduce( &ndone, &ii, 1, MPIU_INT, MPIU_SUM, wcomm ); */ 763 /* MatGetSize( a_Prol, &kk, &jj ); */ 764 /* ierr = MPI_Allreduce( &minsz, &jj, 1, MPIU_INT, MPIU_MIN, wcomm ); */ 765 /* PetscPrintf(PETSC_COMM_WORLD," **** [%d]%s %d total done, N=%d (%d local done), min agg. size = %d\n",mype,__FUNCT__,ii,kk/bs,ndone,jj); */ 766 767 #ifdef OUT_AGGS 768 fclose(file); 769 #endif 770 ierr = ISRestoreIndices( selected, &selected_idx ); CHKERRQ(ierr); 771 ierr = ISRestoreIndices( locals_llist, &llist_idx ); CHKERRQ(ierr); 772 ierr = MatAssemblyBegin(a_Prol,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 773 ierr = MatAssemblyEnd(a_Prol,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 774 775 PetscFunctionReturn(0); 776 } 777 778 /* -------------------------------------------------------------------------- */ 779 /* 780 PCGAMGgraph_AGG 781 782 Input Parameter: 783 . pc - this 784 . Amat - matrix on this fine level 785 Output Parameter: 786 . a_Gmat - 787 */ 788 #undef __FUNCT__ 789 #define __FUNCT__ "PCGAMGgraph_AGG" 790 PetscErrorCode PCGAMGgraph_AGG( PC pc, 791 const Mat Amat, 792 Mat *a_Gmat 793 ) 794 { 795 PetscErrorCode ierr; 796 PC_MG *mg = (PC_MG*)pc->data; 797 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 798 const PetscInt verbose = pc_gamg->verbose; 799 const PetscReal vfilter = pc_gamg->threshold; 800 PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG*)pc_gamg->subctx; 801 PetscMPIInt mype,npe; 802 Mat Gmat, Gmat2; 803 MPI_Comm wcomm = ((PetscObject)Amat)->comm; 804 805 PetscFunctionBegin; 806 ierr = MPI_Comm_rank( wcomm, &mype); CHKERRQ(ierr); 807 ierr = MPI_Comm_size( wcomm, &npe); CHKERRQ(ierr); 808 809 ierr = createSimpleGraph( Amat, &Gmat ); CHKERRQ( ierr ); 810 811 ierr = scaleFilterGraph( &Gmat, vfilter, pc_gamg_agg->sym_graph, verbose ); CHKERRQ( ierr ); 812 813 ierr = MatTransposeMatMult( Gmat, Gmat, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &Gmat2 ); 814 CHKERRQ(ierr); 815 816 /* attach auxilary matrix */ 817 pc_gamg_agg->aux_mat = Gmat; 818 819 *a_Gmat = Gmat2; 820 821 PetscFunctionReturn(0); 822 } 823 824 /* -------------------------------------------------------------------------- */ 825 /* 826 PCGAMGCoarsen_AGG 827 828 Input Parameter: 829 . pc - this 830 . Gmat2 - matrix on this fine level 831 Output Parameter: 832 . a_selected - prolongation operator to the next level 833 . a_llist_parent - data of coarse grid points (num local columns in 'a_P_out') 834 */ 835 #undef __FUNCT__ 836 #define __FUNCT__ "PCGAMGCoarsen_AGG" 837 PetscErrorCode PCGAMGCoarsen_AGG( PC pc, 838 const Mat Gmat2, 839 IS *a_selected, 840 IS *a_llist_parent 841 ) 842 { 843 PetscErrorCode ierr; 844 PC_MG *mg = (PC_MG*)pc->data; 845 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 846 PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG*)pc_gamg->subctx; 847 Mat Gmat1; /* unsquared graph (not symetrized!) */ 848 IS perm, selected, llist_parent; 849 PetscInt Ii,nloc,bs,n,m; 850 PetscInt *permute; 851 PetscBool *bIndexSet; 852 MatCoarsen crs; 853 MPI_Comm wcomm = ((PetscObject)Gmat2)->comm; 854 /* PetscMPIInt mype,npe; */ 855 856 PetscFunctionBegin; 857 /* ierr = MPI_Comm_rank( wcomm, &mype); CHKERRQ(ierr); */ 858 /* ierr = MPI_Comm_size( wcomm, &npe); CHKERRQ(ierr); */ 859 ierr = MatGetLocalSize( Gmat2, &n, &m ); CHKERRQ(ierr); 860 ierr = MatGetBlockSize( Gmat2, &bs ); CHKERRQ(ierr); assert(bs==1); 861 nloc = n/bs; 862 863 /* get unsquared graph */ 864 Gmat1 = pc_gamg_agg->aux_mat; pc_gamg_agg->aux_mat = 0; 865 866 /* get MIS aggs */ 867 /* randomize */ 868 ierr = PetscMalloc( nloc*sizeof(PetscInt), &permute ); CHKERRQ(ierr); 869 ierr = PetscMalloc( nloc*sizeof(PetscBool), &bIndexSet ); CHKERRQ(ierr); 870 for ( Ii = 0; Ii < nloc ; Ii++ ){ 871 bIndexSet[Ii] = PETSC_FALSE; 872 permute[Ii] = Ii; 873 } 874 srand(1); /* make deterministic */ 875 for ( Ii = 0; Ii < nloc ; Ii++ ) { 876 PetscInt iSwapIndex = rand()%nloc; 877 if (!bIndexSet[iSwapIndex] && iSwapIndex != Ii) { 878 PetscInt iTemp = permute[iSwapIndex]; 879 permute[iSwapIndex] = permute[Ii]; 880 permute[Ii] = iTemp; 881 bIndexSet[iSwapIndex] = PETSC_TRUE; 882 } 883 } 884 ierr = PetscFree( bIndexSet ); CHKERRQ(ierr); 885 886 ierr = ISCreateGeneral(PETSC_COMM_SELF, nloc, permute, PETSC_USE_POINTER, &perm); 887 CHKERRQ(ierr); 888 #if defined PETSC_USE_LOG 889 ierr = PetscLogEventBegin(gamg_setup_events[SET4],0,0,0,0);CHKERRQ(ierr); 890 #endif 891 ierr = MatCoarsenCreate( wcomm, &crs ); CHKERRQ(ierr); 892 /* ierr = MatCoarsenSetType( crs, MATCOARSENMIS ); CHKERRQ(ierr); */ 893 ierr = MatCoarsenSetFromOptions( crs ); CHKERRQ(ierr); 894 ierr = MatCoarsenSetGreedyOrdering( crs, perm ); CHKERRQ(ierr); 895 ierr = MatCoarsenSetAdjacency( crs, Gmat2 ); CHKERRQ(ierr); 896 ierr = MatCoarsenSetVerbose( crs, pc_gamg->verbose ); CHKERRQ(ierr); 897 ierr = MatCoarsenSetStrictAggs( crs, PETSC_TRUE ); CHKERRQ(ierr); 898 ierr = MatCoarsenApply( crs ); CHKERRQ(ierr); 899 ierr = MatCoarsenGetMISAggLists( crs, &selected, &llist_parent ); CHKERRQ(ierr); 900 ierr = MatCoarsenDestroy( &crs ); CHKERRQ(ierr); 901 902 ierr = ISDestroy( &perm ); CHKERRQ(ierr); 903 ierr = PetscFree( permute ); CHKERRQ(ierr); 904 #if defined PETSC_USE_LOG 905 ierr = PetscLogEventEnd(gamg_setup_events[SET4],0,0,0,0);CHKERRQ(ierr); 906 #endif 907 /* smooth aggs */ 908 ierr = smoothAggs( Gmat2, Gmat1, selected, llist_parent ); CHKERRQ(ierr); 909 910 ierr = MatDestroy( &Gmat1 ); CHKERRQ(ierr); 911 912 *a_selected = selected; 913 *a_llist_parent = llist_parent; 914 915 PetscFunctionReturn(0); 916 } 917 918 /* -------------------------------------------------------------------------- */ 919 /* 920 PCGAMGprolongator_AGG 921 922 Input Parameter: 923 . pc - this 924 . Amat - matrix on this fine level 925 . Graph - used to get ghost data for nodes in 926 . selected - [nselected inc. chosts] 927 . llist_parent - [nloc + Gmat.nghost] linked list 928 Output Parameter: 929 . a_P_out - prolongation operator to the next level 930 */ 931 #undef __FUNCT__ 932 #define __FUNCT__ "PCGAMGprolongator_AGG" 933 PetscErrorCode PCGAMGprolongator_AGG( PC pc, 934 const Mat Amat, 935 const Mat Gmat, 936 IS selected, 937 IS llist_parent, 938 Mat *a_P_out 939 ) 940 { 941 PC_MG *mg = (PC_MG*)pc->data; 942 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 943 const PetscInt verbose = pc_gamg->verbose; 944 const PetscInt data_cols = pc_gamg->data_cell_cols; 945 PetscErrorCode ierr; 946 PetscInt Istart,Iend,nloc,ii,jj,kk,my0,nLocalSelected,bs; 947 Mat Prol; 948 PetscMPIInt mype, npe; 949 MPI_Comm wcomm = ((PetscObject)Amat)->comm; 950 const PetscInt *selected_idx,*llist_idx,col_bs=data_cols; 951 PetscReal *data_w_ghost; 952 PetscInt myCrs0, nbnodes=0, *flid_fgid; 953 954 PetscFunctionBegin; 955 ierr = MPI_Comm_rank( wcomm, &mype); CHKERRQ(ierr); 956 ierr = MPI_Comm_size( wcomm, &npe); CHKERRQ(ierr); 957 ierr = MatGetOwnershipRange( Amat, &Istart, &Iend ); CHKERRQ(ierr); 958 ierr = MatGetBlockSize( Amat, &bs ); CHKERRQ( ierr ); 959 nloc = (Iend-Istart)/bs; my0 = Istart/bs; assert((Iend-Istart)%bs==0); 960 961 /* get 'nLocalSelected' */ 962 ierr = ISGetSize( selected, &kk ); CHKERRQ(ierr); 963 ierr = ISGetIndices( selected, &selected_idx ); CHKERRQ(ierr); 964 ierr = ISGetIndices( llist_parent, &llist_idx ); CHKERRQ(ierr); 965 for(ii=0,nLocalSelected=0;ii<kk;ii++){ 966 PetscInt lid = selected_idx[ii]; 967 /* filter out singletons */ 968 if( lid<nloc && llist_idx[lid] != -1) nLocalSelected++; 969 } 970 ierr = ISRestoreIndices( selected, &selected_idx ); CHKERRQ(ierr); 971 ierr = ISRestoreIndices( llist_parent, &llist_idx ); CHKERRQ(ierr); 972 973 /* create prolongator, create P matrix */ 974 ierr = MatCreateMPIAIJ( wcomm, 975 nloc*bs, nLocalSelected*col_bs, 976 PETSC_DETERMINE, PETSC_DETERMINE, 977 data_cols, PETSC_NULL, data_cols, PETSC_NULL, 978 &Prol ); 979 CHKERRQ(ierr); 980 981 /* can get all points "removed" */ 982 ierr = MatGetSize( Prol, &kk, &ii ); CHKERRQ(ierr); 983 if( ii==0 ) { 984 if( verbose ) { 985 PetscPrintf(wcomm,"[%d]%s no selected points on coarse grid\n",mype,__FUNCT__); 986 } 987 ierr = MatDestroy( &Prol ); CHKERRQ(ierr); 988 *a_P_out = PETSC_NULL; /* out */ 989 PetscFunctionReturn(0); 990 } 991 if( verbose ) { 992 PetscPrintf(PETSC_COMM_WORLD,"\t\t[%d]%s New grid %d nodes\n",mype,__FUNCT__,ii/bs); 993 } 994 ierr = MatGetOwnershipRangeColumn( Prol, &myCrs0, &kk ); CHKERRQ(ierr); 995 myCrs0 = myCrs0/col_bs; 996 997 /* create global vector of data in 'data_w_ghost' */ 998 #if defined PETSC_USE_LOG 999 ierr = PetscLogEventBegin(gamg_setup_events[SET7],0,0,0,0);CHKERRQ(ierr); 1000 #endif 1001 if (npe > 1) { /* */ 1002 PetscReal *tmp_gdata,*tmp_ldata,*tp2; 1003 ierr = PetscMalloc( nloc*sizeof(PetscReal), &tmp_ldata ); CHKERRQ(ierr); 1004 for( jj = 0 ; jj < data_cols ; jj++ ){ 1005 for( kk = 0 ; kk < bs ; kk++) { 1006 PetscInt ii,nnodes; 1007 const PetscReal *tp = pc_gamg->data + jj*bs*nloc + kk; 1008 for( ii = 0 ; ii < nloc ; ii++, tp += bs ){ 1009 tmp_ldata[ii] = *tp; 1010 } 1011 ierr = getDataWithGhosts( Gmat, 1, tmp_ldata, &nnodes, &tmp_gdata ); 1012 CHKERRQ(ierr); 1013 if(jj==0 && kk==0) { /* now I know how many todal nodes - allocate */ 1014 ierr = PetscMalloc( nnodes*bs*data_cols*sizeof(PetscReal), &data_w_ghost ); CHKERRQ(ierr); 1015 nbnodes = bs*nnodes; 1016 } 1017 tp2 = data_w_ghost + jj*bs*nnodes + kk; 1018 for( ii = 0 ; ii < nnodes ; ii++, tp2 += bs ){ 1019 *tp2 = tmp_gdata[ii]; 1020 } 1021 ierr = PetscFree( tmp_gdata ); CHKERRQ(ierr); 1022 } 1023 } 1024 ierr = PetscFree( tmp_ldata ); CHKERRQ(ierr); 1025 } 1026 else { 1027 nbnodes = bs*nloc; 1028 data_w_ghost = (PetscReal*)pc_gamg->data; 1029 } 1030 1031 /* get P0 */ 1032 if( npe > 1 ){ 1033 PetscReal *fid_glid_loc,*fiddata; 1034 PetscInt nnodes; 1035 1036 ierr = PetscMalloc( nloc*sizeof(PetscReal), &fid_glid_loc ); CHKERRQ(ierr); 1037 for(kk=0;kk<nloc;kk++) fid_glid_loc[kk] = (PetscReal)(my0+kk); 1038 ierr = getDataWithGhosts( Gmat, 1, fid_glid_loc, &nnodes, &fiddata ); 1039 CHKERRQ(ierr); 1040 ierr = PetscMalloc( nnodes*sizeof(PetscInt), &flid_fgid ); CHKERRQ(ierr); 1041 for(kk=0;kk<nnodes;kk++) flid_fgid[kk] = (PetscInt)fiddata[kk]; 1042 ierr = PetscFree( fiddata ); CHKERRQ(ierr); 1043 assert(nnodes==nbnodes/bs); 1044 ierr = PetscFree( fid_glid_loc ); CHKERRQ(ierr); 1045 } 1046 else { 1047 ierr = PetscMalloc( nloc*sizeof(PetscInt), &flid_fgid ); CHKERRQ(ierr); 1048 for(kk=0;kk<nloc;kk++) flid_fgid[kk] = my0 + kk; 1049 } 1050 #if defined PETSC_USE_LOG 1051 ierr = PetscLogEventEnd(gamg_setup_events[SET7],0,0,0,0);CHKERRQ(ierr); 1052 ierr = PetscLogEventBegin(gamg_setup_events[SET8],0,0,0,0);CHKERRQ(ierr); 1053 #endif 1054 { 1055 PetscReal *data_out; 1056 ierr = formProl0( selected, llist_parent, bs, data_cols, myCrs0, nbnodes, 1057 data_w_ghost, flid_fgid, &data_out, Prol ); 1058 CHKERRQ(ierr); 1059 ierr = PetscFree( pc_gamg->data ); CHKERRQ( ierr ); 1060 pc_gamg->data = data_out; 1061 pc_gamg->data_cell_rows = data_cols; 1062 pc_gamg->data_sz = data_cols*data_cols*nLocalSelected; 1063 } 1064 #if defined PETSC_USE_LOG 1065 ierr = PetscLogEventEnd(gamg_setup_events[SET8],0,0,0,0);CHKERRQ(ierr); 1066 #endif 1067 if (npe > 1) ierr = PetscFree( data_w_ghost ); CHKERRQ(ierr); 1068 ierr = PetscFree( flid_fgid ); CHKERRQ(ierr); 1069 1070 /* attach block size of columns */ 1071 if( pc_gamg->col_bs_id == -1 ) { 1072 ierr = PetscObjectComposedDataRegister( &pc_gamg->col_bs_id ); assert(pc_gamg->col_bs_id != -1 ); 1073 } 1074 ierr = PetscObjectComposedDataSetInt( (PetscObject)Prol, pc_gamg->col_bs_id, data_cols ); CHKERRQ(ierr); 1075 1076 *a_P_out = Prol; /* out */ 1077 1078 PetscFunctionReturn(0); 1079 } 1080 1081 /* -------------------------------------------------------------------------- */ 1082 /* 1083 PCGAMGoptprol_AGG 1084 1085 Input Parameter: 1086 . pc - this 1087 . Amat - matrix on this fine level 1088 In/Output Parameter: 1089 . a_P_out - prolongation operator to the next level 1090 */ 1091 #undef __FUNCT__ 1092 #define __FUNCT__ "PCGAMGoptprol_AGG" 1093 PetscErrorCode PCGAMGoptprol_AGG( PC pc, 1094 const Mat Amat, 1095 Mat *a_P 1096 ) 1097 { 1098 PetscErrorCode ierr; 1099 PC_MG *mg = (PC_MG*)pc->data; 1100 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 1101 const PetscInt verbose = pc_gamg->verbose; 1102 PC_GAMG_AGG *pc_gamg_agg = (PC_GAMG_AGG*)pc_gamg->subctx; 1103 PetscInt jj; 1104 PetscMPIInt mype,npe; 1105 Mat Prol = *a_P; 1106 MPI_Comm wcomm = ((PetscObject)Amat)->comm; 1107 1108 PetscFunctionBegin; 1109 ierr = MPI_Comm_rank( wcomm, &mype); CHKERRQ(ierr); 1110 ierr = MPI_Comm_size( wcomm, &npe); CHKERRQ(ierr); 1111 1112 /* smooth P0 */ 1113 for( jj = 0 ; jj < pc_gamg_agg->nsmooths ; jj++ ){ 1114 Mat tMat; 1115 Vec diag; 1116 PetscReal alpha, emax, emin; 1117 #if defined PETSC_USE_LOG 1118 ierr = PetscLogEventBegin(gamg_setup_events[SET9],0,0,0,0);CHKERRQ(ierr); 1119 #endif 1120 if( jj == 0 ) { 1121 KSP eksp; 1122 Vec bb, xx; 1123 PC pc; 1124 ierr = MatGetVecs( Amat, &bb, 0 ); CHKERRQ(ierr); 1125 ierr = MatGetVecs( Amat, &xx, 0 ); CHKERRQ(ierr); 1126 { 1127 PetscRandom rctx; 1128 ierr = PetscRandomCreate(wcomm,&rctx);CHKERRQ(ierr); 1129 ierr = PetscRandomSetFromOptions(rctx);CHKERRQ(ierr); 1130 ierr = VecSetRandom(bb,rctx);CHKERRQ(ierr); 1131 ierr = PetscRandomDestroy( &rctx ); CHKERRQ(ierr); 1132 } 1133 ierr = KSPCreate(wcomm,&eksp); CHKERRQ(ierr); 1134 ierr = KSPAppendOptionsPrefix( eksp, "est_"); CHKERRQ(ierr); 1135 ierr = KSPSetFromOptions( eksp ); CHKERRQ(ierr); 1136 ierr = KSPSetInitialGuessNonzero( eksp, PETSC_FALSE ); CHKERRQ(ierr); 1137 ierr = KSPSetOperators( eksp, Amat, Amat, SAME_NONZERO_PATTERN ); 1138 CHKERRQ( ierr ); 1139 ierr = KSPGetPC( eksp, &pc ); CHKERRQ( ierr ); 1140 ierr = PCSetType( pc, PCJACOBI ); CHKERRQ(ierr); /* smoother */ 1141 ierr = KSPSetTolerances(eksp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,10); 1142 CHKERRQ(ierr); 1143 ierr = KSPSetNormType( eksp, KSP_NORM_NONE ); CHKERRQ(ierr); 1144 ierr = KSPSetComputeSingularValues( eksp,PETSC_TRUE ); CHKERRQ(ierr); 1145 1146 /* solve - keep stuff out of logging */ 1147 ierr = PetscLogEventDeactivate(KSP_Solve);CHKERRQ(ierr); 1148 ierr = PetscLogEventDeactivate(PC_Apply);CHKERRQ(ierr); 1149 ierr = KSPSolve( eksp, bb, xx ); CHKERRQ(ierr); 1150 ierr = PetscLogEventActivate(KSP_Solve);CHKERRQ(ierr); 1151 ierr = PetscLogEventActivate(PC_Apply);CHKERRQ(ierr); 1152 1153 ierr = KSPComputeExtremeSingularValues( eksp, &emax, &emin ); CHKERRQ(ierr); 1154 if( verbose ) { 1155 PetscPrintf(wcomm,"\t\t\t%s smooth P0: max eigen=%e min=%e PC=%s\n", 1156 __FUNCT__,emax,emin,PCJACOBI); 1157 } 1158 ierr = VecDestroy( &xx ); CHKERRQ(ierr); 1159 ierr = VecDestroy( &bb ); CHKERRQ(ierr); 1160 ierr = KSPDestroy( &eksp ); CHKERRQ(ierr); 1161 1162 if( pc_gamg->emax_id == -1 ) { 1163 ierr = PetscObjectComposedDataRegister( &pc_gamg->emax_id ); 1164 assert(pc_gamg->emax_id != -1 ); 1165 } 1166 ierr = PetscObjectComposedDataSetScalar( (PetscObject)Amat, pc_gamg->emax_id, emax ); CHKERRQ(ierr); 1167 } 1168 1169 /* smooth P1 := (I - omega/lam D^{-1}A)P0 */ 1170 ierr = MatMatMult( Amat, Prol, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &tMat ); CHKERRQ(ierr); 1171 ierr = MatGetVecs( Amat, &diag, 0 ); CHKERRQ(ierr); 1172 ierr = MatGetDiagonal( Amat, diag ); CHKERRQ(ierr); /* effectively PCJACOBI */ 1173 ierr = VecReciprocal( diag ); CHKERRQ(ierr); 1174 ierr = MatDiagonalScale( tMat, diag, 0 ); CHKERRQ(ierr); 1175 ierr = VecDestroy( &diag ); CHKERRQ(ierr); 1176 alpha = -1.5/emax; 1177 ierr = MatAYPX( tMat, alpha, Prol, SUBSET_NONZERO_PATTERN ); CHKERRQ(ierr); 1178 ierr = MatDestroy( &Prol ); CHKERRQ(ierr); 1179 Prol = tMat; 1180 #if defined PETSC_USE_LOG 1181 ierr = PetscLogEventEnd(gamg_setup_events[SET9],0,0,0,0);CHKERRQ(ierr); 1182 #endif 1183 } 1184 1185 *a_P = Prol; 1186 1187 PetscFunctionReturn(0); 1188 } 1189 1190 /* -------------------------------------------------------------------------- */ 1191 /* 1192 PCCreateGAMG_AGG 1193 1194 Input Parameter: 1195 . pc - 1196 */ 1197 #undef __FUNCT__ 1198 #define __FUNCT__ "PCCreateGAMG_AGG" 1199 PetscErrorCode PCCreateGAMG_AGG( PC pc ) 1200 { 1201 PetscErrorCode ierr; 1202 PC_MG *mg = (PC_MG*)pc->data; 1203 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 1204 PC_GAMG_AGG *pc_gamg_agg; 1205 1206 PetscFunctionBegin; 1207 /* create sub context for SA */ 1208 ierr = PetscNewLog( pc, PC_GAMG_AGG, &pc_gamg_agg ); CHKERRQ(ierr); 1209 assert(!pc_gamg->subctx); 1210 pc_gamg->subctx = pc_gamg_agg; 1211 1212 pc->ops->setfromoptions = PCSetFromOptions_GAMG_AGG; 1213 pc->ops->destroy = PCDestroy_AGG; 1214 /* reset does not do anything; setup not virtual */ 1215 1216 /* set internal function pointers */ 1217 pc_gamg->graph = PCGAMGgraph_AGG; 1218 pc_gamg->coarsen = PCGAMGCoarsen_AGG; 1219 pc_gamg->prolongator = PCGAMGprolongator_AGG; 1220 pc_gamg->optprol = PCGAMGoptprol_AGG; 1221 1222 pc_gamg->createdefaultdata = PCSetData_AGG; 1223 1224 ierr = PetscObjectComposeFunctionDynamic( (PetscObject)pc, 1225 "PCSetCoordinates_C", 1226 "PCSetCoordinates_AGG", 1227 PCSetCoordinates_AGG); 1228 PetscFunctionReturn(0); 1229 } 1230