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 <petsc-private/kspimpl.h> 7 8 #if defined(PETSC_HAVE_TRIANGLE) 9 #define REAL PetscReal 10 #include <triangle.h> 11 #endif 12 13 #include <petscblaslapack.h> 14 15 /* Private context for the GAMG preconditioner */ 16 typedef struct { 17 PetscInt lid; /* local vertex index */ 18 PetscInt degree; /* vertex degree */ 19 } GAMGNode; 20 21 int petsc_geo_mg_compare(const void *a, const void *b) 22 { 23 return (((GAMGNode*)a)->degree - ((GAMGNode*)b)->degree); 24 } 25 26 /* -------------------------------------------------------------------------- */ 27 /* 28 PCSetCoordinates_GEO 29 30 Input Parameter: 31 . pc - the preconditioner context 32 */ 33 EXTERN_C_BEGIN 34 #undef __FUNCT__ 35 #define __FUNCT__ "PCSetCoordinates_GEO" 36 PetscErrorCode PCSetCoordinates_GEO(PC pc, PetscInt ndm, PetscInt a_nloc, PetscReal *coords) 37 { 38 PC_MG *mg = (PC_MG*)pc->data; 39 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 40 PetscErrorCode ierr; 41 PetscInt arrsz,bs,my0,kk,ii,nloc,Iend; 42 Mat Amat = pc->pmat; 43 44 PetscFunctionBegin; 45 PetscValidHeaderSpecific(Amat, MAT_CLASSID, 1); 46 ierr = MatGetBlockSize(Amat, &bs);CHKERRQ(ierr); 47 48 ierr = MatGetOwnershipRange(Amat, &my0, &Iend);CHKERRQ(ierr); 49 nloc = (Iend-my0)/bs; 50 51 if (nloc!=a_nloc) SETERRQ2(PetscObjectComm((PetscObject)Amat),PETSC_ERR_ARG_WRONG, "Stokes not supported nloc = %d %d.",a_nloc,nloc); 52 if ((Iend-my0)%bs!=0) SETERRQ1(PetscObjectComm((PetscObject)Amat),PETSC_ERR_ARG_WRONG, "Bad local size %d.",nloc); 53 54 pc_gamg->data_cell_rows = 1; 55 if (coords==0 && nloc > 0) SETERRQ(PetscObjectComm((PetscObject)Amat),PETSC_ERR_ARG_WRONG, "Need coordinates for pc_gamg_type 'geo'."); 56 pc_gamg->data_cell_cols = ndm; /* coordinates */ 57 58 arrsz = nloc*pc_gamg->data_cell_rows*pc_gamg->data_cell_cols; 59 60 /* create data - syntactic sugar that should be refactored at some point */ 61 if (pc_gamg->data==0 || (pc_gamg->data_sz != arrsz)) { 62 ierr = PetscFree(pc_gamg->data);CHKERRQ(ierr); 63 ierr = PetscMalloc((arrsz+1)*sizeof(PetscReal), &pc_gamg->data);CHKERRQ(ierr); 64 } 65 for (kk=0; kk<arrsz; kk++) pc_gamg->data[kk] = -999.; 66 pc_gamg->data[arrsz] = -99.; 67 /* copy data in - column oriented */ 68 for (kk = 0; kk < nloc; kk++) { 69 for (ii = 0; ii < ndm; ii++) { 70 pc_gamg->data[ii*nloc + kk] = coords[kk*ndm + ii]; 71 } 72 } 73 if (pc_gamg->data[arrsz] != -99.) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"pc_gamg->data[arrsz %D] %g != -99.",arrsz,pc_gamg->data[arrsz]); 74 pc_gamg->data_sz = arrsz; 75 PetscFunctionReturn(0); 76 } 77 EXTERN_C_END 78 79 /* -------------------------------------------------------------------------- */ 80 /* 81 PCSetData_GEO 82 83 Input Parameter: 84 . pc - 85 */ 86 #undef __FUNCT__ 87 #define __FUNCT__ "PCSetData_GEO" 88 PetscErrorCode PCSetData_GEO(PC pc, Mat m) 89 { 90 PetscFunctionBegin; 91 SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"GEO MG needs coordinates"); 92 } 93 94 /* -------------------------------------------------------------------------- */ 95 /* 96 PCSetFromOptions_GEO 97 98 Input Parameter: 99 . pc - 100 */ 101 #undef __FUNCT__ 102 #define __FUNCT__ "PCSetFromOptions_GEO" 103 PetscErrorCode PCSetFromOptions_GEO(PC pc) 104 { 105 PetscErrorCode ierr; 106 PC_MG *mg = (PC_MG*)pc->data; 107 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 108 109 PetscFunctionBegin; 110 ierr = PetscOptionsHead("GAMG-GEO options");CHKERRQ(ierr); 111 { 112 /* -pc_gamg_sa_nsmooths */ 113 /* pc_gamg_sa->smooths = 0; */ 114 /* ierr = PetscOptionsInt("-pc_gamg_agg_nsmooths", */ 115 /* "smoothing steps for smoothed aggregation, usually 1 (0)", */ 116 /* "PCGAMGSetNSmooths_AGG", */ 117 /* pc_gamg_sa->smooths, */ 118 /* &pc_gamg_sa->smooths, */ 119 /* &flag); */ 120 /* CHKERRQ(ierr); */ 121 } 122 ierr = PetscOptionsTail();CHKERRQ(ierr); 123 124 /* call base class */ 125 ierr = PCSetFromOptions_GAMG(pc);CHKERRQ(ierr); 126 127 if (pc_gamg->verbose) { 128 PetscPrintf(PetscObjectComm((PetscObject)pc),"[%d]%s done\n",0,__FUNCT__); 129 } 130 PetscFunctionReturn(0); 131 } 132 133 /* -------------------------------------------------------------------------- */ 134 /* 135 triangulateAndFormProl 136 137 Input Parameter: 138 . selected_2 - list of selected local ID, includes selected ghosts 139 . data_stride - 140 . coords[2*data_stride] - column vector of local coordinates w/ ghosts 141 . nselected_1 - selected IDs that go with base (1) graph 142 . clid_lid_1[nselected_1] - lids of selected (c) nodes ??????????? 143 . agg_lists_1 - list of aggregates 144 . crsGID[selected.size()] - global index for prolongation operator 145 . bs - block size 146 Output Parameter: 147 . a_Prol - prolongation operator 148 . a_worst_best - measure of worst missed fine vertex, 0 is no misses 149 */ 150 #undef __FUNCT__ 151 #define __FUNCT__ "triangulateAndFormProl" 152 static PetscErrorCode triangulateAndFormProl(IS selected_2, /* list of selected local ID, includes selected ghosts */ 153 const PetscInt data_stride, 154 const PetscReal coords[], /* column vector of local coordinates w/ ghosts */ 155 const PetscInt nselected_1, /* list of selected local ID, includes selected ghosts */ 156 const PetscInt clid_lid_1[], 157 const PetscCoarsenData *agg_lists_1, /* selected_1 vertices of aggregate unselected vertices */ 158 const PetscInt crsGID[], 159 const PetscInt bs, 160 Mat a_Prol, /* prolongation operator (output) */ 161 PetscReal *a_worst_best) /* measure of worst missed fine vertex, 0 is no misses */ 162 { 163 #if defined(PETSC_HAVE_TRIANGLE) 164 PetscErrorCode ierr; 165 PetscInt jj,tid,tt,idx,nselected_2; 166 struct triangulateio in,mid; 167 const PetscInt *selected_idx_2; 168 PetscMPIInt rank,size; 169 PetscInt Istart,Iend,nFineLoc,myFine0; 170 int kk,nPlotPts,sid; 171 MPI_Comm comm; 172 PetscReal tm; 173 174 PetscFunctionBegin; 175 ierr = PetscObjectGetComm((PetscObject)a_Prol,&comm);CHKERRQ(ierr); 176 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 177 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 178 ierr = ISGetSize(selected_2, &nselected_2);CHKERRQ(ierr); 179 if (nselected_2 == 1 || nselected_2 == 2) { /* 0 happens on idle processors */ 180 *a_worst_best = 100.0; /* this will cause a stop, but not globalized (should not happen) */ 181 } else *a_worst_best = 0.0; 182 ierr = MPI_Allreduce(a_worst_best, &tm, 1, MPIU_REAL, MPIU_MAX, comm);CHKERRQ(ierr); 183 if (tm > 0.0) { 184 *a_worst_best = 100.0; 185 PetscFunctionReturn(0); 186 } 187 ierr = MatGetOwnershipRange(a_Prol, &Istart, &Iend);CHKERRQ(ierr); 188 nFineLoc = (Iend-Istart)/bs; myFine0 = Istart/bs; 189 nPlotPts = nFineLoc; /* locals */ 190 /* traingle */ 191 /* Define input points - in*/ 192 in.numberofpoints = nselected_2; 193 in.numberofpointattributes = 0; 194 /* get nselected points */ 195 ierr = PetscMalloc(2*(nselected_2)*sizeof(REAL), &in.pointlist);CHKERRQ(ierr); 196 ierr = ISGetIndices(selected_2, &selected_idx_2);CHKERRQ(ierr); 197 198 for (kk=0,sid=0; kk<nselected_2; kk++,sid += 2) { 199 PetscInt lid = selected_idx_2[kk]; 200 in.pointlist[sid] = coords[lid]; 201 in.pointlist[sid+1] = coords[data_stride + lid]; 202 if (lid>=nFineLoc) nPlotPts++; 203 } 204 if (sid != 2*nselected_2) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"sid %D != 2*nselected_2 %D",sid,nselected_2); 205 206 in.numberofsegments = 0; 207 in.numberofedges = 0; 208 in.numberofholes = 0; 209 in.numberofregions = 0; 210 in.trianglelist = 0; 211 in.segmentmarkerlist = 0; 212 in.pointattributelist = 0; 213 in.pointmarkerlist = 0; 214 in.triangleattributelist = 0; 215 in.trianglearealist = 0; 216 in.segmentlist = 0; 217 in.holelist = 0; 218 in.regionlist = 0; 219 in.edgelist = 0; 220 in.edgemarkerlist = 0; 221 in.normlist = 0; 222 223 /* triangulate */ 224 mid.pointlist = 0; /* Not needed if -N switch used. */ 225 /* Not needed if -N switch used or number of point attributes is zero: */ 226 mid.pointattributelist = 0; 227 mid.pointmarkerlist = 0; /* Not needed if -N or -B switch used. */ 228 mid.trianglelist = 0; /* Not needed if -E switch used. */ 229 /* Not needed if -E switch used or number of triangle attributes is zero: */ 230 mid.triangleattributelist = 0; 231 mid.neighborlist = 0; /* Needed only if -n switch used. */ 232 /* Needed only if segments are output (-p or -c) and -P not used: */ 233 mid.segmentlist = 0; 234 /* Needed only if segments are output (-p or -c) and -P and -B not used: */ 235 mid.segmentmarkerlist = 0; 236 mid.edgelist = 0; /* Needed only if -e switch used. */ 237 mid.edgemarkerlist = 0; /* Needed if -e used and -B not used. */ 238 mid.numberoftriangles = 0; 239 240 /* Triangulate the points. Switches are chosen to read and write a */ 241 /* PSLG (p), preserve the convex hull (c), number everything from */ 242 /* zero (z), assign a regional attribute to each element (A), and */ 243 /* produce an edge list (e), a Voronoi diagram (v), and a triangle */ 244 /* neighbor list (n). */ 245 if (nselected_2 != 0) { /* inactive processor */ 246 char args[] = "npczQ"; /* c is needed ? */ 247 triangulate(args, &in, &mid, (struct triangulateio*) NULL); 248 /* output .poly files for 'showme' */ 249 if (!PETSC_TRUE) { 250 static int level = 1; 251 FILE *file; char fname[32]; 252 253 sprintf(fname,"C%d_%d.poly",level,rank); file = fopen(fname, "w"); 254 /*First line: <# of vertices> <dimension (must be 2)> <# of attributes> <# of boundary markers (0 or 1)>*/ 255 fprintf(file, "%d %d %d %d\n",in.numberofpoints,2,0,0); 256 /*Following lines: <vertex #> <x> <y> */ 257 for (kk=0,sid=0; kk<in.numberofpoints; kk++,sid += 2) { 258 fprintf(file, "%d %e %e\n",kk,in.pointlist[sid],in.pointlist[sid+1]); 259 } 260 /*One line: <# of segments> <# of boundary markers (0 or 1)> */ 261 fprintf(file, "%d %d\n",0,0); 262 /*Following lines: <segment #> <endpoint> <endpoint> [boundary marker] */ 263 /* One line: <# of holes> */ 264 fprintf(file, "%d\n",0); 265 /* Following lines: <hole #> <x> <y> */ 266 /* Optional line: <# of regional attributes and/or area constraints> */ 267 /* Optional following lines: <region #> <x> <y> <attribute> <maximum area> */ 268 fclose(file); 269 270 /* elems */ 271 sprintf(fname,"C%d_%d.ele",level,rank); file = fopen(fname, "w"); 272 /* First line: <# of triangles> <nodes per triangle> <# of attributes> */ 273 fprintf(file, "%d %d %d\n",mid.numberoftriangles,3,0); 274 /* Remaining lines: <triangle #> <node> <node> <node> ... [attributes] */ 275 for (kk=0,sid=0; kk<mid.numberoftriangles; kk++,sid += 3) { 276 fprintf(file, "%d %d %d %d\n",kk,mid.trianglelist[sid],mid.trianglelist[sid+1],mid.trianglelist[sid+2]); 277 } 278 fclose(file); 279 280 sprintf(fname,"C%d_%d.node",level,rank); file = fopen(fname, "w"); 281 /* First line: <# of vertices> <dimension (must be 2)> <# of attributes> <# of boundary markers (0 or 1)> */ 282 /* fprintf(file, "%d %d %d %d\n",in.numberofpoints,2,0,0); */ 283 fprintf(file, "%d %d %d %d\n",nPlotPts,2,0,0); 284 /*Following lines: <vertex #> <x> <y> */ 285 for (kk=0,sid=0; kk<in.numberofpoints; kk++,sid+=2) { 286 fprintf(file, "%d %e %e\n",kk,in.pointlist[sid],in.pointlist[sid+1]); 287 } 288 289 sid /= 2; 290 for (jj=0; jj<nFineLoc; jj++) { 291 PetscBool sel = PETSC_TRUE; 292 for (kk=0; kk<nselected_2 && sel; kk++) { 293 PetscInt lid = selected_idx_2[kk]; 294 if (lid == jj) sel = PETSC_FALSE; 295 } 296 if (sel) fprintf(file, "%d %e %e\n",sid++,coords[jj],coords[data_stride + jj]); 297 } 298 fclose(file); 299 if (sid != nPlotPts) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"sid %D != nPlotPts %D",sid,nPlotPts); 300 level++; 301 } 302 } 303 #if defined PETSC_GAMG_USE_LOG 304 ierr = PetscLogEventBegin(petsc_gamg_setup_events[FIND_V],0,0,0,0);CHKERRQ(ierr); 305 #endif 306 { /* form P - setup some maps */ 307 PetscInt clid,mm,*nTri,*node_tri; 308 309 ierr = PetscMalloc(nselected_2*sizeof(PetscInt), &node_tri);CHKERRQ(ierr); 310 ierr = PetscMalloc(nselected_2*sizeof(PetscInt), &nTri);CHKERRQ(ierr); 311 312 /* need list of triangles on node */ 313 for (kk=0; kk<nselected_2; kk++) nTri[kk] = 0; 314 for (tid=0,kk=0; tid<mid.numberoftriangles; tid++) { 315 for (jj=0; jj<3; jj++) { 316 PetscInt cid = mid.trianglelist[kk++]; 317 if (nTri[cid] == 0) node_tri[cid] = tid; 318 nTri[cid]++; 319 } 320 } 321 #define EPS 1.e-12 322 /* find points and set prolongation */ 323 for (mm = clid = 0; mm < nFineLoc; mm++) { 324 PetscBool ise; 325 ierr = PetscCDEmptyAt(agg_lists_1,mm,&ise);CHKERRQ(ierr); 326 if (!ise) { 327 const PetscInt lid = mm; 328 /* for (clid_iterator=0;clid_iterator<nselected_1;clid_iterator++) { */ 329 PetscScalar AA[3][3]; 330 PetscBLASInt N=3,NRHS=1,LDA=3,IPIV[3],LDB=3,INFO; 331 PetscCDPos pos; 332 ierr = PetscCDGetHeadPos(agg_lists_1,lid,&pos);CHKERRQ(ierr); 333 while (pos) { 334 PetscInt flid; 335 ierr = PetscLLNGetID(pos, &flid);CHKERRQ(ierr); 336 ierr = PetscCDGetNextPos(agg_lists_1,lid,&pos);CHKERRQ(ierr); 337 338 if (flid < nFineLoc) { /* could be a ghost */ 339 PetscInt bestTID = -1; PetscReal best_alpha = 1.e10; 340 const PetscInt fgid = flid + myFine0; 341 /* compute shape function for gid */ 342 const PetscReal fcoord[3] = {coords[flid],coords[data_stride+flid],1.0}; 343 PetscBool haveit =PETSC_FALSE; PetscScalar alpha[3]; PetscInt clids[3]; 344 345 /* look for it */ 346 for (tid = node_tri[clid], jj=0; 347 jj < 5 && !haveit && tid != -1; 348 jj++) { 349 for (tt=0; tt<3; tt++) { 350 PetscInt cid2 = mid.trianglelist[3*tid + tt]; 351 PetscInt lid2 = selected_idx_2[cid2]; 352 AA[tt][0] = coords[lid2]; AA[tt][1] = coords[data_stride + lid2]; AA[tt][2] = 1.0; 353 clids[tt] = cid2; /* store for interp */ 354 } 355 356 for (tt=0; tt<3; tt++) alpha[tt] = (PetscScalar)fcoord[tt]; 357 358 /* SUBROUTINE DGESV(N, NRHS, A, LDA, IPIV, B, LDB, INFO) */ 359 PetscStackCall("LAPACKgesv",LAPACKgesv_(&N, &NRHS, (PetscScalar*)AA, &LDA, IPIV, alpha, &LDB, &INFO)); 360 { 361 PetscBool have=PETSC_TRUE; PetscReal lowest=1.e10; 362 for (tt = 0, idx = 0; tt < 3; tt++) { 363 if (PetscRealPart(alpha[tt]) > (1.0+EPS) || PetscRealPart(alpha[tt]) < -EPS) have = PETSC_FALSE; 364 if (PetscRealPart(alpha[tt]) < lowest) { 365 lowest = PetscRealPart(alpha[tt]); 366 idx = tt; 367 } 368 } 369 haveit = have; 370 } 371 tid = mid.neighborlist[3*tid + idx]; 372 } 373 374 if (!haveit) { 375 /* brute force */ 376 for (tid=0; tid<mid.numberoftriangles && !haveit; tid++) { 377 for (tt=0; tt<3; tt++) { 378 PetscInt cid2 = mid.trianglelist[3*tid + tt]; 379 PetscInt lid2 = selected_idx_2[cid2]; 380 AA[tt][0] = coords[lid2]; AA[tt][1] = coords[data_stride + lid2]; AA[tt][2] = 1.0; 381 clids[tt] = cid2; /* store for interp */ 382 } 383 for (tt=0; tt<3; tt++) alpha[tt] = fcoord[tt]; 384 /* SUBROUTINE DGESV(N, NRHS, A, LDA, IPIV, B, LDB, INFO) */ 385 PetscStackCall("LAPACKgesv",LAPACKgesv_(&N, &NRHS, (PetscScalar*)AA, &LDA, IPIV, alpha, &LDB, &INFO)); 386 { 387 PetscBool have=PETSC_TRUE; PetscReal worst=0.0, v; 388 for (tt=0; tt<3 && have; tt++) { 389 if (PetscRealPart(alpha[tt]) > 1.0+EPS || PetscRealPart(alpha[tt]) < -EPS) have=PETSC_FALSE; 390 if ((v=PetscAbs(PetscRealPart(alpha[tt])-0.5)) > worst) worst = v; 391 } 392 if (worst < best_alpha) { 393 best_alpha = worst; bestTID = tid; 394 } 395 haveit = have; 396 } 397 } 398 } 399 if (!haveit) { 400 if (best_alpha > *a_worst_best) *a_worst_best = best_alpha; 401 /* use best one */ 402 for (tt=0; tt<3; tt++) { 403 PetscInt cid2 = mid.trianglelist[3*bestTID + tt]; 404 PetscInt lid2 = selected_idx_2[cid2]; 405 AA[tt][0] = coords[lid2]; AA[tt][1] = coords[data_stride + lid2]; AA[tt][2] = 1.0; 406 clids[tt] = cid2; /* store for interp */ 407 } 408 for (tt=0; tt<3; tt++) alpha[tt] = fcoord[tt]; 409 /* SUBROUTINE DGESV(N, NRHS, A, LDA, IPIV, B, LDB, INFO) */ 410 PetscStackCall("LAPACKgesv",LAPACKgesv_(&N, &NRHS, (PetscScalar*)AA, &LDA, IPIV, alpha, &LDB, &INFO)); 411 } 412 413 /* put in row of P */ 414 for (idx=0; idx<3; idx++) { 415 PetscScalar shp = alpha[idx]; 416 if (PetscAbs(PetscRealPart(shp)) > 1.e-6) { 417 PetscInt cgid = crsGID[clids[idx]]; 418 PetscInt jj = cgid*bs, ii = fgid*bs; /* need to gloalize */ 419 for (tt=0; tt < bs; tt++, ii++, jj++) { 420 ierr = MatSetValues(a_Prol,1,&ii,1,&jj,&shp,INSERT_VALUES);CHKERRQ(ierr); 421 } 422 } 423 } 424 } 425 } /* aggregates iterations */ 426 clid++; 427 } /* a coarse agg */ 428 } /* for all fine nodes */ 429 430 ierr = ISRestoreIndices(selected_2, &selected_idx_2);CHKERRQ(ierr); 431 ierr = MatAssemblyBegin(a_Prol,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 432 ierr = MatAssemblyEnd(a_Prol,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 433 434 ierr = PetscFree(node_tri);CHKERRQ(ierr); 435 ierr = PetscFree(nTri);CHKERRQ(ierr); 436 } 437 #if defined PETSC_GAMG_USE_LOG 438 ierr = PetscLogEventEnd(petsc_gamg_setup_events[FIND_V],0,0,0,0);CHKERRQ(ierr); 439 #endif 440 free(mid.trianglelist); 441 free(mid.neighborlist); 442 ierr = PetscFree(in.pointlist);CHKERRQ(ierr); 443 PetscFunctionReturn(0); 444 #else 445 SETERRQ(PetscObjectComm((PetscObject)a_Prol),PETSC_ERR_PLIB,"configure with TRIANGLE to use geometric MG"); 446 #endif 447 } 448 /* -------------------------------------------------------------------------- */ 449 /* 450 getGIDsOnSquareGraph - square graph, get 451 452 Input Parameter: 453 . nselected_1 - selected local indices (includes ghosts in input Gmat1) 454 . clid_lid_1 - [nselected_1] lids of selected nodes 455 . Gmat1 - graph that goes with 'selected_1' 456 Output Parameter: 457 . a_selected_2 - selected local indices (includes ghosts in output a_Gmat_2) 458 . a_Gmat_2 - graph that is squared of 'Gmat_1' 459 . a_crsGID[a_selected_2.size()] - map of global IDs of coarse grid nodes 460 */ 461 #undef __FUNCT__ 462 #define __FUNCT__ "getGIDsOnSquareGraph" 463 static PetscErrorCode getGIDsOnSquareGraph(const PetscInt nselected_1,const PetscInt clid_lid_1[],const Mat Gmat1,IS *a_selected_2,Mat *a_Gmat_2,PetscInt **a_crsGID) 464 { 465 PetscErrorCode ierr; 466 PetscMPIInt rank,size; 467 PetscInt *crsGID, kk,my0,Iend,nloc; 468 MPI_Comm comm; 469 470 PetscFunctionBegin; 471 ierr = PetscObjectGetComm((PetscObject)Gmat1,&comm);CHKERRQ(ierr); 472 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 473 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 474 ierr = MatGetOwnershipRange(Gmat1,&my0,&Iend);CHKERRQ(ierr); /* AIJ */ 475 nloc = Iend - my0; /* this does not change */ 476 477 if (size == 1) { /* not much to do in serial */ 478 ierr = PetscMalloc(nselected_1*sizeof(PetscInt), &crsGID);CHKERRQ(ierr); 479 for (kk=0; kk<nselected_1; kk++) crsGID[kk] = kk; 480 *a_Gmat_2 = 0; 481 ierr = ISCreateGeneral(PETSC_COMM_SELF,nselected_1,clid_lid_1,PETSC_COPY_VALUES,a_selected_2);CHKERRQ(ierr); 482 } else { 483 PetscInt idx,num_fine_ghosts,num_crs_ghost,myCrs0; 484 Mat_MPIAIJ *mpimat2; 485 Mat Gmat2; 486 Vec locState; 487 PetscScalar *cpcol_state; 488 489 /* scan my coarse zero gid, set 'lid_state' with coarse GID */ 490 kk = nselected_1; 491 MPI_Scan(&kk, &myCrs0, 1, MPIU_INT, MPIU_SUM, comm); 492 myCrs0 -= nselected_1; 493 494 if (a_Gmat_2) { /* output */ 495 /* grow graph to get wider set of selected vertices to cover fine grid, invalidates 'llist' */ 496 ierr = MatTransposeMatMult(Gmat1, Gmat1, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &Gmat2);CHKERRQ(ierr); 497 *a_Gmat_2 = Gmat2; /* output */ 498 } else Gmat2 = Gmat1; /* use local to get crsGIDs at least */ 499 /* get coarse grid GIDS for selected (locals and ghosts) */ 500 mpimat2 = (Mat_MPIAIJ*)Gmat2->data; 501 ierr = MatGetVecs(Gmat2, &locState, 0);CHKERRQ(ierr); 502 ierr = VecSet(locState, (PetscScalar)(PetscReal)(-1));CHKERRQ(ierr); /* set with UNKNOWN state */ 503 for (kk=0; kk<nselected_1; kk++) { 504 PetscInt fgid = clid_lid_1[kk] + my0; 505 PetscScalar v = (PetscScalar)(kk+myCrs0); 506 ierr = VecSetValues(locState, 1, &fgid, &v, INSERT_VALUES);CHKERRQ(ierr); /* set with PID */ 507 } 508 ierr = VecAssemblyBegin(locState);CHKERRQ(ierr); 509 ierr = VecAssemblyEnd(locState);CHKERRQ(ierr); 510 ierr = VecScatterBegin(mpimat2->Mvctx,locState,mpimat2->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 511 ierr = VecScatterEnd(mpimat2->Mvctx,locState,mpimat2->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 512 ierr = VecGetLocalSize(mpimat2->lvec, &num_fine_ghosts);CHKERRQ(ierr); 513 ierr = VecGetArray(mpimat2->lvec, &cpcol_state);CHKERRQ(ierr); 514 for (kk=0,num_crs_ghost=0; kk<num_fine_ghosts; kk++) { 515 if ((PetscInt)PetscRealPart(cpcol_state[kk]) != -1) num_crs_ghost++; 516 } 517 ierr = PetscMalloc((nselected_1+num_crs_ghost)*sizeof(PetscInt), &crsGID);CHKERRQ(ierr); /* output */ 518 { 519 PetscInt *selected_set; 520 ierr = PetscMalloc((nselected_1+num_crs_ghost)*sizeof(PetscInt), &selected_set);CHKERRQ(ierr); 521 /* do ghost of 'crsGID' */ 522 for (kk=0,idx=nselected_1; kk<num_fine_ghosts; kk++) { 523 if ((PetscInt)PetscRealPart(cpcol_state[kk]) != -1) { 524 PetscInt cgid = (PetscInt)PetscRealPart(cpcol_state[kk]); 525 selected_set[idx] = nloc + kk; 526 crsGID[idx++] = cgid; 527 } 528 } 529 if (idx != (nselected_1+num_crs_ghost)) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"idx %D != (nselected_1 %D + num_crs_ghost %D)",idx,nselected_1,num_crs_ghost); 530 ierr = VecRestoreArray(mpimat2->lvec, &cpcol_state);CHKERRQ(ierr); 531 /* do locals in 'crsGID' */ 532 ierr = VecGetArray(locState, &cpcol_state);CHKERRQ(ierr); 533 for (kk=0,idx=0; kk<nloc; kk++) { 534 if ((PetscInt)PetscRealPart(cpcol_state[kk]) != -1) { 535 PetscInt cgid = (PetscInt)PetscRealPart(cpcol_state[kk]); 536 selected_set[idx] = kk; 537 crsGID[idx++] = cgid; 538 } 539 } 540 if (idx != nselected_1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"idx %D != nselected_1 %D",idx,nselected_1); 541 ierr = VecRestoreArray(locState, &cpcol_state);CHKERRQ(ierr); 542 543 if (a_selected_2 != 0) { /* output */ 544 ierr = ISCreateGeneral(PETSC_COMM_SELF,(nselected_1+num_crs_ghost),selected_set,PETSC_OWN_POINTER,a_selected_2);CHKERRQ(ierr); 545 } else { 546 ierr = PetscFree(selected_set);CHKERRQ(ierr); 547 } 548 } 549 ierr = VecDestroy(&locState);CHKERRQ(ierr); 550 } 551 *a_crsGID = crsGID; /* output */ 552 PetscFunctionReturn(0); 553 } 554 555 /* -------------------------------------------------------------------------- */ 556 /* 557 PCGAMGgraph_GEO 558 559 Input Parameter: 560 . pc - this 561 . Amat - matrix on this fine level 562 Output Parameter: 563 . a_Gmat 564 */ 565 #undef __FUNCT__ 566 #define __FUNCT__ "PCGAMGgraph_GEO" 567 PetscErrorCode PCGAMGgraph_GEO(PC pc,const Mat Amat,Mat *a_Gmat) 568 { 569 PetscErrorCode ierr; 570 PC_MG *mg = (PC_MG*)pc->data; 571 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 572 const PetscInt verbose = pc_gamg->verbose; 573 const PetscReal vfilter = pc_gamg->threshold; 574 PetscMPIInt rank,size; 575 MPI_Comm comm; 576 Mat Gmat; 577 PetscBool set,flg,symm; 578 579 PetscFunctionBegin; 580 ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 581 #if defined PETSC_USE_LOG 582 ierr = PetscLogEventBegin(PC_GAMGGgraph_GEO,0,0,0,0);CHKERRQ(ierr); 583 #endif 584 ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 585 ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 586 587 ierr = MatIsSymmetricKnown(Amat, &set, &flg);CHKERRQ(ierr); 588 symm = (PetscBool)!(set && flg); 589 590 ierr = PCGAMGCreateGraph(Amat, &Gmat);CHKERRQ(ierr); 591 ierr = PCGAMGFilterGraph(&Gmat, vfilter, symm, verbose);CHKERRQ(ierr); 592 593 *a_Gmat = Gmat; 594 #if defined PETSC_USE_LOG 595 ierr = PetscLogEventEnd(PC_GAMGGgraph_GEO,0,0,0,0);CHKERRQ(ierr); 596 #endif 597 PetscFunctionReturn(0); 598 } 599 600 /* -------------------------------------------------------------------------- */ 601 /* 602 PCGAMGcoarsen_GEO 603 604 Input Parameter: 605 . a_pc - this 606 . a_Gmat - graph 607 Output Parameter: 608 . a_llist_parent - linked list from selected indices for data locality only 609 */ 610 #undef __FUNCT__ 611 #define __FUNCT__ "PCGAMGcoarsen_GEO" 612 PetscErrorCode PCGAMGcoarsen_GEO(PC a_pc,Mat *a_Gmat,PetscCoarsenData **a_llist_parent) 613 { 614 PetscErrorCode ierr; 615 PC_MG *mg = (PC_MG*)a_pc->data; 616 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 617 PetscInt Istart,Iend,nloc,kk,Ii,ncols; 618 PetscMPIInt rank,size; 619 IS perm; 620 GAMGNode *gnodes; 621 PetscInt *permute; 622 Mat Gmat = *a_Gmat; 623 MPI_Comm comm; 624 MatCoarsen crs; 625 626 PetscFunctionBegin; 627 ierr = PetscObjectGetComm((PetscObject)a_pc,&comm);CHKERRQ(ierr); 628 #if defined PETSC_USE_LOG 629 ierr = PetscLogEventBegin(PC_GAMGCoarsen_GEO,0,0,0,0);CHKERRQ(ierr); 630 #endif 631 ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 632 ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 633 ierr = MatGetOwnershipRange(Gmat, &Istart, &Iend);CHKERRQ(ierr); 634 nloc = (Iend-Istart); 635 636 /* create random permutation with sort for geo-mg */ 637 ierr = PetscMalloc(nloc*sizeof(GAMGNode), &gnodes);CHKERRQ(ierr); 638 ierr = PetscMalloc(nloc*sizeof(PetscInt), &permute);CHKERRQ(ierr); 639 640 for (Ii=Istart; Ii<Iend; Ii++) { /* locals only? */ 641 ierr = MatGetRow(Gmat,Ii,&ncols,0,0);CHKERRQ(ierr); 642 { 643 PetscInt lid = Ii - Istart; 644 gnodes[lid].lid = lid; 645 gnodes[lid].degree = ncols; 646 } 647 ierr = MatRestoreRow(Gmat,Ii,&ncols,0,0);CHKERRQ(ierr); 648 } 649 /* randomize */ 650 srand(1); /* make deterministic */ 651 if (PETSC_TRUE) { 652 PetscBool *bIndexSet; 653 ierr = PetscMalloc(nloc*sizeof(PetscBool), &bIndexSet);CHKERRQ(ierr); 654 for (Ii = 0; Ii < nloc; Ii++) bIndexSet[Ii] = PETSC_FALSE; 655 for (Ii = 0; Ii < nloc; Ii++) { 656 PetscInt iSwapIndex = rand()%nloc; 657 if (!bIndexSet[iSwapIndex] && iSwapIndex != Ii) { 658 GAMGNode iTemp = gnodes[iSwapIndex]; 659 gnodes[iSwapIndex] = gnodes[Ii]; 660 gnodes[Ii] = iTemp; 661 bIndexSet[Ii] = PETSC_TRUE; 662 bIndexSet[iSwapIndex] = PETSC_TRUE; 663 } 664 } 665 ierr = PetscFree(bIndexSet);CHKERRQ(ierr); 666 } 667 /* only sort locals */ 668 qsort(gnodes, nloc, sizeof(GAMGNode), petsc_geo_mg_compare); 669 /* create IS of permutation */ 670 for (kk=0; kk<nloc; kk++) permute[kk] = gnodes[kk].lid; /* locals only */ 671 ierr = ISCreateGeneral(PETSC_COMM_SELF, nloc, permute, PETSC_OWN_POINTER, &perm);CHKERRQ(ierr); 672 673 ierr = PetscFree(gnodes);CHKERRQ(ierr); 674 675 /* get MIS aggs */ 676 677 ierr = MatCoarsenCreate(comm, &crs);CHKERRQ(ierr); 678 ierr = MatCoarsenSetType(crs, MATCOARSENMIS);CHKERRQ(ierr); 679 ierr = MatCoarsenSetGreedyOrdering(crs, perm);CHKERRQ(ierr); 680 ierr = MatCoarsenSetAdjacency(crs, Gmat);CHKERRQ(ierr); 681 ierr = MatCoarsenSetVerbose(crs, pc_gamg->verbose);CHKERRQ(ierr); 682 ierr = MatCoarsenSetStrictAggs(crs, PETSC_FALSE);CHKERRQ(ierr); 683 ierr = MatCoarsenApply(crs);CHKERRQ(ierr); 684 ierr = MatCoarsenGetData(crs, a_llist_parent);CHKERRQ(ierr); 685 ierr = MatCoarsenDestroy(&crs);CHKERRQ(ierr); 686 687 ierr = ISDestroy(&perm);CHKERRQ(ierr); 688 #if defined PETSC_USE_LOG 689 ierr = PetscLogEventEnd(PC_GAMGCoarsen_GEO,0,0,0,0);CHKERRQ(ierr); 690 #endif 691 PetscFunctionReturn(0); 692 } 693 694 /* -------------------------------------------------------------------------- */ 695 /* 696 PCGAMGProlongator_GEO 697 698 Input Parameter: 699 . pc - this 700 . Amat - matrix on this fine level 701 . Graph - used to get ghost data for nodes in 702 . selected_1 - [nselected] 703 . agg_lists - [nselected] 704 Output Parameter: 705 . a_P_out - prolongation operator to the next level 706 */ 707 #undef __FUNCT__ 708 #define __FUNCT__ "PCGAMGProlongator_GEO" 709 PetscErrorCode PCGAMGProlongator_GEO(PC pc,const Mat Amat,const Mat Gmat,PetscCoarsenData *agg_lists,Mat *a_P_out) 710 { 711 PC_MG *mg = (PC_MG*)pc->data; 712 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 713 const PetscInt verbose = pc_gamg->verbose; 714 const PetscInt dim = pc_gamg->data_cell_cols, data_cols = pc_gamg->data_cell_cols; 715 PetscErrorCode ierr; 716 PetscInt Istart,Iend,nloc,my0,jj,kk,ncols,nLocalSelected,bs,*clid_flid; 717 Mat Prol; 718 PetscMPIInt rank, size; 719 MPI_Comm comm; 720 IS selected_2,selected_1; 721 const PetscInt *selected_idx; 722 723 PetscFunctionBegin; 724 ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 725 #if defined PETSC_USE_LOG 726 ierr = PetscLogEventBegin(PC_GAMGProlongator_GEO,0,0,0,0);CHKERRQ(ierr); 727 #endif 728 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 729 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 730 ierr = MatGetOwnershipRange(Amat, &Istart, &Iend);CHKERRQ(ierr); 731 ierr = MatGetBlockSize(Amat, &bs);CHKERRQ(ierr); 732 nloc = (Iend-Istart)/bs; my0 = Istart/bs; 733 if ((Iend-Istart) % bs) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"(Iend %D - Istart %D) % bs %D",Iend,Istart,bs); 734 735 /* get 'nLocalSelected' */ 736 ierr = PetscCDGetMIS(agg_lists, &selected_1);CHKERRQ(ierr); 737 ierr = ISGetSize(selected_1, &jj);CHKERRQ(ierr); 738 ierr = PetscMalloc(jj*sizeof(PetscInt), &clid_flid);CHKERRQ(ierr); 739 ierr = ISGetIndices(selected_1, &selected_idx);CHKERRQ(ierr); 740 for (kk=0,nLocalSelected=0; kk<jj; kk++) { 741 PetscInt lid = selected_idx[kk]; 742 if (lid<nloc) { 743 ierr = MatGetRow(Gmat,lid+my0,&ncols,0,0);CHKERRQ(ierr); 744 if (ncols>1) clid_flid[nLocalSelected++] = lid; /* fiter out singletons */ 745 ierr = MatRestoreRow(Gmat,lid+my0,&ncols,0,0);CHKERRQ(ierr); 746 } 747 } 748 ierr = ISRestoreIndices(selected_1, &selected_idx);CHKERRQ(ierr); 749 ierr = ISDestroy(&selected_1);CHKERRQ(ierr); /* this is selected_1 in serial */ 750 751 /* create prolongator, create P matrix */ 752 ierr = MatCreate(comm, &Prol);CHKERRQ(ierr); 753 ierr = MatSetSizes(Prol,nloc*bs,nLocalSelected*bs,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 754 ierr = MatSetBlockSizes(Prol, bs, bs);CHKERRQ(ierr); 755 ierr = MatSetType(Prol, MATAIJ);CHKERRQ(ierr); 756 ierr = MatSeqAIJSetPreallocation(Prol,3*data_cols,NULL);CHKERRQ(ierr); 757 ierr = MatMPIAIJSetPreallocation(Prol,3*data_cols,NULL,3*data_cols,NULL);CHKERRQ(ierr); 758 /* ierr = MatCreateAIJ(comm, */ 759 /* nloc*bs, nLocalSelected*bs, */ 760 /* PETSC_DETERMINE, PETSC_DETERMINE, */ 761 /* 3*data_cols, NULL, */ 762 /* 3*data_cols, NULL, */ 763 /* &Prol); */ 764 /* CHKERRQ(ierr); */ 765 766 /* can get all points "removed" - but not on geomg */ 767 ierr = MatGetSize(Prol, &kk, &jj);CHKERRQ(ierr); 768 if (jj==0) { 769 if (verbose) PetscPrintf(comm,"[%d]%s ERROE: no selected points on coarse grid\n",rank,__FUNCT__); 770 ierr = PetscFree(clid_flid);CHKERRQ(ierr); 771 ierr = MatDestroy(&Prol);CHKERRQ(ierr); 772 *a_P_out = NULL; /* out */ 773 PetscFunctionReturn(0); 774 } 775 776 { 777 PetscReal *coords; 778 PetscInt data_stride; 779 PetscInt *crsGID = NULL; 780 Mat Gmat2; 781 782 if (dim != data_cols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"dim %D != data_cols %D",dim,data_cols); 783 /* grow ghost data for better coarse grid cover of fine grid */ 784 #if defined PETSC_GAMG_USE_LOG 785 ierr = PetscLogEventBegin(petsc_gamg_setup_events[SET5],0,0,0,0);CHKERRQ(ierr); 786 #endif 787 /* messy method, squares graph and gets some data */ 788 ierr = getGIDsOnSquareGraph(nLocalSelected, clid_flid, Gmat, &selected_2, &Gmat2, &crsGID);CHKERRQ(ierr); 789 /* llist is now not valid wrt squared graph, but will work as iterator in 'triangulateAndFormProl' */ 790 #if defined PETSC_GAMG_USE_LOG 791 ierr = PetscLogEventEnd(petsc_gamg_setup_events[SET5],0,0,0,0);CHKERRQ(ierr); 792 #endif 793 /* create global vector of coorindates in 'coords' */ 794 if (size > 1) { 795 ierr = PCGAMGGetDataWithGhosts(Gmat2, dim, pc_gamg->data, &data_stride, &coords);CHKERRQ(ierr); 796 } else { 797 coords = (PetscReal*)pc_gamg->data; 798 data_stride = pc_gamg->data_sz/pc_gamg->data_cell_cols; 799 } 800 ierr = MatDestroy(&Gmat2);CHKERRQ(ierr); 801 802 /* triangulate */ 803 if (dim == 2) { 804 PetscReal metric,tm; 805 #if defined PETSC_GAMG_USE_LOG 806 ierr = PetscLogEventBegin(petsc_gamg_setup_events[SET6],0,0,0,0);CHKERRQ(ierr); 807 #endif 808 ierr = triangulateAndFormProl(selected_2, data_stride, coords,nLocalSelected, clid_flid, agg_lists, crsGID, bs, Prol, &metric);CHKERRQ(ierr); 809 #if defined PETSC_GAMG_USE_LOG 810 ierr = PetscLogEventEnd(petsc_gamg_setup_events[SET6],0,0,0,0);CHKERRQ(ierr); 811 #endif 812 ierr = PetscFree(crsGID);CHKERRQ(ierr); 813 814 /* clean up and create coordinates for coarse grid (output) */ 815 if (size > 1) ierr = PetscFree(coords);CHKERRQ(ierr); 816 817 ierr = MPI_Allreduce(&metric, &tm, 1, MPIU_REAL, MPIU_MAX, comm);CHKERRQ(ierr); 818 if (tm > 1.) { /* needs to be globalized - should not happen */ 819 if (verbose) PetscPrintf(comm,"[%d]%s failed metric for coarse grid %e\n",rank,__FUNCT__,tm); 820 ierr = MatDestroy(&Prol);CHKERRQ(ierr); 821 Prol = NULL; 822 } else if (metric > .0) { 823 if (verbose) PetscPrintf(comm,"[%d]%s worst metric for coarse grid = %e\n",rank,__FUNCT__,metric); 824 } 825 } else SETERRQ(comm,PETSC_ERR_PLIB,"3D not implemented for 'geo' AMG"); 826 { /* create next coords - output */ 827 PetscReal *crs_crds; 828 ierr = PetscMalloc(dim*nLocalSelected*sizeof(PetscReal), &crs_crds);CHKERRQ(ierr); 829 for (kk=0; kk<nLocalSelected; kk++) { /* grab local select nodes to promote - output */ 830 PetscInt lid = clid_flid[kk]; 831 for (jj=0; jj<dim; jj++) crs_crds[jj*nLocalSelected + kk] = pc_gamg->data[jj*nloc + lid]; 832 } 833 834 ierr = PetscFree(pc_gamg->data);CHKERRQ(ierr); 835 pc_gamg->data = crs_crds; /* out */ 836 pc_gamg->data_sz = dim*nLocalSelected; 837 } 838 ierr = ISDestroy(&selected_2);CHKERRQ(ierr); 839 } 840 841 *a_P_out = Prol; /* out */ 842 ierr = PetscFree(clid_flid);CHKERRQ(ierr); 843 #if defined PETSC_USE_LOG 844 ierr = PetscLogEventEnd(PC_GAMGProlongator_GEO,0,0,0,0);CHKERRQ(ierr); 845 #endif 846 PetscFunctionReturn(0); 847 } 848 849 /* -------------------------------------------------------------------------- */ 850 /* 851 PCCreateGAMG_GEO 852 853 Input Parameter: 854 . pc - 855 */ 856 #undef __FUNCT__ 857 #define __FUNCT__ "PCCreateGAMG_GEO" 858 PetscErrorCode PCCreateGAMG_GEO(PC pc) 859 { 860 PetscErrorCode ierr; 861 PC_MG *mg = (PC_MG*)pc->data; 862 PC_GAMG *pc_gamg = (PC_GAMG*)mg->innerctx; 863 864 PetscFunctionBegin; 865 pc->ops->setfromoptions = PCSetFromOptions_GEO; 866 /* pc->ops->destroy = PCDestroy_GEO; */ 867 /* reset does not do anything; setup not virtual */ 868 869 /* set internal function pointers */ 870 pc_gamg->graph = PCGAMGgraph_GEO; 871 pc_gamg->coarsen = PCGAMGcoarsen_GEO; 872 pc_gamg->prolongator = PCGAMGProlongator_GEO; 873 pc_gamg->optprol = 0; 874 pc_gamg->formkktprol = 0; 875 876 pc_gamg->createdefaultdata = PCSetData_GEO; 877 878 ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc, 879 "PCSetCoordinates_C", 880 "PCSetCoordinates_GEO", 881 PCSetCoordinates_GEO);CHKERRQ(ierr); 882 PetscFunctionReturn(0); 883 } 884