Actual source code: hem.c
petsc-3.7.1 2016-05-15
Report Typos and Errors

      2: #include <petsc/private/matimpl.h>    /*I "petscmat.h" I*/
      3: #include <../src/mat/impls/aij/seq/aij.h>
      4: #include <../src/mat/impls/aij/mpi/mpiaij.h>

      6: /* linked list methods
      7:  *
      8:  *  PetscCDCreate
      9:  */
     12: PetscErrorCode PetscCDCreate(PetscInt a_size, PetscCoarsenData **a_out)
     13: {
     14:   PetscErrorCode   ierr;
     15:   PetscCoarsenData *ail;
     16:   PetscInt         ii;

     19:   /* alocate pool, partially */
     20:   PetscNew(&ail);
     21:   *a_out               = ail;
     22:   ail->pool_list.next  = NULL;
     23:   ail->pool_list.array = NULL;
     24:   ail->chk_sz          = 0;
     25:   /* allocate array */
     26:   ail->size = a_size;
     27:   PetscMalloc1(a_size, &ail->array);
     28:   for (ii=0; ii<a_size; ii++) ail->array[ii] = NULL;
     29:   ail->extra_nodes = NULL;
     30:   ail->mat         = NULL;
     31:   /* ail->removedIS = NULL; */
     32:   return(0);
     33: }

     35: /* NPDestroy
     36:  */
     39: PetscErrorCode PetscCDDestroy(PetscCoarsenData *ail)
     40: {
     42:   PetscCDArrNd   *n = &ail->pool_list;

     45:   n = n->next;
     46:   while (n) {
     47:     PetscCDArrNd *lstn = n;
     48:     n    = n->next;
     49:     PetscFree(lstn);
     50:   }
     51:   if (ail->pool_list.array) {
     52:     PetscFree(ail->pool_list.array);
     53:   }
     54:   /* if (ail->removedIS) { */
     55:   /*   ISDestroy(&ail->removedIS); */
     56:   /* } */
     57:   /* delete this (+array) */
     58:   PetscFree(ail->array);
     59:   /* delete this (+agg+pool array) */
     60:   PetscFree(ail);
     61:   return(0);
     62: }
     63: /* PetscCDSetChuckSize
     64:  */
     67: PetscErrorCode PetscCDSetChuckSize(PetscCoarsenData *ail, PetscInt a_sz)
     68: {
     70:   ail->chk_sz = a_sz;
     71:   return(0);
     72: }
     73: /*  PetscCDGetNewNode
     74:  */
     77: PetscErrorCode PetscCDGetNewNode(PetscCoarsenData *ail, PetscCDIntNd **a_out, PetscInt a_id)
     78: {

     82:   if (ail->extra_nodes) {
     83:     PetscCDIntNd *node = ail->extra_nodes;
     84:     ail->extra_nodes = node->next;
     85:     node->gid        = a_id;
     86:     node->next       = NULL;
     87:     *a_out           = node;
     88:   } else {
     89:     if (!ail->pool_list.array) {
     90:       if (!ail->chk_sz) ail->chk_sz = 10; /* use a chuck size of ail->size? */
     91:       PetscMalloc1(ail->chk_sz, &ail->pool_list.array);
     92:       ail->new_node       = ail->pool_list.array;
     93:       ail->new_left       = ail->chk_sz;
     94:       ail->new_node->next = NULL;
     95:     } else if (!ail->new_left) {
     96:       PetscCDArrNd *node;
     97:       PetscMalloc(ail->chk_sz*sizeof(PetscCDIntNd) + sizeof(PetscCDArrNd), &node);
     98:       node->array         = (PetscCDIntNd*)(node + 1);
     99:       node->next          = ail->pool_list.next;
    100:       ail->pool_list.next = node;
    101:       ail->new_left       = ail->chk_sz;
    102:       ail->new_node       = node->array;
    103:     }
    104:     ail->new_node->gid  = a_id;
    105:     ail->new_node->next = NULL;
    106:     *a_out              = ail->new_node++; ail->new_left--;
    107:   }
    108:   return(0);
    109: }

    111: /* PetscLLNSetID
    112:  */
    115: PetscErrorCode PetscLLNSetID(PetscCDIntNd *a_this, PetscInt a_id)
    116: {
    118:   a_this->gid = a_id;
    119:   return(0);
    120: }
    121: /* PetscLLNGetID
    122:  */
    125: PetscErrorCode PetscLLNGetID(const PetscCDIntNd *a_this, PetscInt *a_gid)
    126: {
    128:   *a_gid = a_this->gid;
    129:   return(0);
    130: }
    131: /* PetscCDGetHeadPos
    132:  */
    135: PetscErrorCode PetscCDGetHeadPos(const PetscCoarsenData *ail, PetscInt a_idx, PetscCDPos *pos)
    136: {
    138:   if (a_idx>=ail->size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"a_idx >= ail->size: a_idx=%d.",a_idx);
    139:   *pos = ail->array[a_idx];
    140:   return(0);
    141: }
    142: /* PetscCDGetNextPos
    143:  */
    146: PetscErrorCode PetscCDGetNextPos(const PetscCoarsenData *ail, PetscInt l_idx, PetscCDPos *pos)
    147: {
    149:   if (!(*pos)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"NULL input position.");
    150:   *pos = (*pos)->next;
    151:   return(0);
    152: }

    154: /* PetscCDAppendID
    155:  */
    158: PetscErrorCode PetscCDAppendID(PetscCoarsenData *ail, PetscInt a_idx, PetscInt a_id)
    159: {
    161:   PetscCDIntNd   *n,*n2;

    164:   PetscCDGetNewNode(ail, &n, a_id);
    165:   if (a_idx>=ail->size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Index %d out of range.",a_idx);
    166:   if (!(n2=ail->array[a_idx])) ail->array[a_idx] = n;
    167:   else {
    168:     do {
    169:       if (!n2->next) {
    170:         n2->next = n;
    171:         if (n->next) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"n should not have a next");
    172:         break;
    173:       }
    174:       n2 = n2->next;
    175:     } while (n2);
    176:     if (!n2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"n2 should be non-null");
    177:   }
    178:   return(0);
    179: }
    180: /* PetscCDAppendNode
    181:  */
    184: PetscErrorCode PetscCDAppendNode(PetscCoarsenData *ail, PetscInt a_idx,  PetscCDIntNd *a_n)
    185: {
    186:   PetscCDIntNd *n2;

    189:   if (a_idx>=ail->size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Index %d out of range.",a_idx);
    190:   if (!(n2=ail->array[a_idx])) ail->array[a_idx] = a_n;
    191:   else {
    192:     do {
    193:       if (!n2->next) {
    194:         n2->next  = a_n;
    195:         a_n->next = NULL;
    196:         break;
    197:       }
    198:       n2 = n2->next;
    199:     } while (n2);
    200:     if (!n2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"n2 should be non-null");
    201:   }
    202:   return(0);
    203: }

    205: /* PetscCDRemoveNextNode: a_last->next, this exposes single linked list structure to API
    206:  */
    209: PetscErrorCode PetscCDRemoveNextNode(PetscCoarsenData *ail, PetscInt a_idx,  PetscCDIntNd *a_last)
    210: {
    211:   PetscCDIntNd *del;

    214:   if (a_idx>=ail->size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Index %d out of range.",a_idx);
    215:   if (!a_last->next) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"a_last should have a next");
    216:   del          = a_last->next;
    217:   a_last->next = del->next;
    218:   /* del->next = NULL; -- this still used in a iterator so keep it intact -- need to fix this with a double linked list */
    219:   /* could reuse n2 but PetscCDAppendNode sometimes uses it */
    220:   return(0);
    221: }

    223: /* PetscCDPrint
    224:  */
    227: PetscErrorCode PetscCDPrint(const PetscCoarsenData *ail, MPI_Comm comm)
    228: {
    230:   PetscCDIntNd   *n;
    231:   PetscInt       ii,kk;
    232:   PetscMPIInt    rank;

    235:   MPI_Comm_rank(comm, &rank);
    236:   for (ii=0; ii<ail->size; ii++) {
    237:     kk = 0;
    238:     n  = ail->array[ii];
    239:     if (n) PetscPrintf(comm,"[%d]%s list %d:\n",rank,__FUNCT__,ii);
    240:     while (n) {
    241:       PetscPrintf(comm,"\t[%d] %d) id %d\n",rank,++kk,n->gid);
    242:       n = n->next;
    243:     }
    244:   }
    245:   return(0);
    246: }
    247: /* PetscCDAppendRemove
    248:  */
    251: PetscErrorCode PetscCDAppendRemove(PetscCoarsenData *ail, PetscInt a_destidx, PetscInt a_srcidx)
    252: {
    253:   PetscCDIntNd *n;

    256:   if (a_srcidx>=ail->size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Index %d out of range.",a_srcidx);
    257:   if (a_destidx>=ail->size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Index %d out of range.",a_destidx);
    258:   if (a_destidx==a_srcidx) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"a_destidx==a_srcidx %d.",a_destidx);
    259:   n = ail->array[a_destidx];
    260:   if (!n) ail->array[a_destidx] = ail->array[a_srcidx];
    261:   else {
    262:     do {
    263:       if (!n->next) {
    264:         n->next = ail->array[a_srcidx];
    265:         break;
    266:       }
    267:       n = n->next;
    268:     } while (1);
    269:   }
    270:   ail->array[a_srcidx] = NULL;
    271:   return(0);
    272: }

    274: /* PetscCDRemoveAll
    275:  */
    278: PetscErrorCode PetscCDRemoveAll(PetscCoarsenData *ail, PetscInt a_idx)
    279: {
    280:   PetscCDIntNd *rem,*n1;

    283:   if (a_idx>=ail->size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Index %d out of range.",a_idx);
    284:   rem               = ail->array[a_idx];
    285:   ail->array[a_idx] = NULL;
    286:   if (!(n1=ail->extra_nodes)) ail->extra_nodes = rem;
    287:   else {
    288:     while (n1->next) n1 = n1->next;
    289:     n1->next = rem;
    290:   }
    291:   return(0);
    292: }

    294: /* PetscCDSizeAt
    295:  */
    298: PetscErrorCode PetscCDSizeAt(const PetscCoarsenData *ail, PetscInt a_idx, PetscInt *a_sz)
    299: {
    300:   PetscCDIntNd *n1;
    301:   PetscInt     sz = 0;

    304:   if (a_idx>=ail->size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Index %d out of range.",a_idx);
    305:   n1 = ail->array[a_idx];
    306:   while (n1) {
    307:     n1 = n1->next;
    308:     sz++;
    309:   }
    310:   *a_sz = sz;
    311:   return(0);
    312: }

    314: /* PetscCDEmptyAt
    315:  */
    318: PetscErrorCode PetscCDEmptyAt(const PetscCoarsenData *ail, PetscInt a_idx, PetscBool *a_e)
    319: {
    321:   if (a_idx>=ail->size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Index %d out of range.",a_idx);
    322:   *a_e = (PetscBool)(ail->array[a_idx]==NULL);
    323:   return(0);
    324: }

    326: /* PetscCDGetMIS
    327:  */
    330: PetscErrorCode PetscCDGetMIS(PetscCoarsenData *ail, IS *a_mis)
    331: {
    333:   PetscCDIntNd   *n;
    334:   PetscInt       ii,kk;
    335:   PetscInt       *permute;

    338:   for (ii=kk=0; ii<ail->size; ii++) {
    339:     n = ail->array[ii];
    340:     if (n) kk++;
    341:   }
    342:   PetscMalloc1(kk, &permute);
    343:   for (ii=kk=0; ii<ail->size; ii++) {
    344:     n = ail->array[ii];
    345:     if (n) permute[kk++] = ii;
    346:   }
    347:   ISCreateGeneral(PETSC_COMM_SELF, kk, permute, PETSC_OWN_POINTER, a_mis);
    348:   return(0);
    349: }
    350: /* PetscCDGetMat
    351:  */
    354: PetscErrorCode PetscCDGetMat(const PetscCoarsenData *ail, Mat *a_mat)
    355: {
    357:   *a_mat = ail->mat;
    358:   return(0);
    359: }

    361: /* PetscCDSetMat
    362:  */
    365: PetscErrorCode PetscCDSetMat(PetscCoarsenData *ail, Mat a_mat)
    366: {
    368:   ail->mat = a_mat;
    369:   return(0);
    370: }

    373: /* PetscCDGetASMBlocks
    374:  */
    377: PetscErrorCode PetscCDGetASMBlocks(const PetscCoarsenData *ail, const PetscInt a_bs, PetscInt *a_sz, IS **a_local_is)
    378: {
    380:   PetscCDIntNd   *n;
    381:   PetscInt       lsz,ii,kk,*idxs,jj;
    382:   IS             *is_loc;

    385:   for (ii=kk=0; ii<ail->size; ii++) {
    386:     if (ail->array[ii]) kk++;
    387:   }
    388:   *a_sz = kk; /* out */

    390:   PetscMalloc1(kk, &is_loc);
    391:   *a_local_is = is_loc; /* out */

    393:   for (ii=kk=0; ii<ail->size; ii++) {
    394:     for (lsz=0, n=ail->array[ii]; n; lsz++, n=n->next) /* void */;
    395:     if (lsz) {
    396:       PetscMalloc1(a_bs*lsz, &idxs);
    397:       for (lsz = 0, n=ail->array[ii]; n; n = n->next) {
    398:         PetscInt gid;
    399:         PetscLLNGetID(n, &gid);
    400:         for (jj=0; jj<a_bs; lsz++,jj++) idxs[lsz] = a_bs*gid + jj;
    401:       }
    402:       ISCreateGeneral(PETSC_COMM_SELF, lsz, idxs, PETSC_OWN_POINTER, &is_loc[kk++]);
    403:     }
    404:   }
    405:   if (*a_sz != kk) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"*a_sz %D != kk %D",*a_sz,kk);
    406:   return(0);
    407: }

    410: /* PetscCDSetRemovedIS
    411:  */
    412: /* PetscErrorCode PetscCDSetRemovedIS(PetscCoarsenData *ail, MPI_Comm comm, const PetscInt a_sz, PetscInt a_ids[]) */
    413: /* { */

    416: /*   if (ail->removedIS) { */
    417: /*     ISDestroy(&ail->removedIS); */
    418: /*   } */
    419: /*   ISCreateGeneral(comm, a_sz, a_ids, PETSC_COPY_VALUES, &ail->removedIS); */
    420: /*   return(0); */
    421: /* } */

    423: /* PetscCDGetRemovedIS
    424:  */
    425: /* PetscErrorCode PetscCDGetRemovedIS(PetscCoarsenData *ail, IS *a_is) */
    426: /* { */
    427: /*   *a_is = ail->removedIS; */
    428: /*   ail->removedIS = NULL; /\* hack to relinquish ownership *\/ */
    429: /*   return(0); */
    430: /* } */

    432: /* ********************************************************************** */
    433: /* edge for priority queue */
    434: typedef struct edge_tag {
    435:   PetscReal weight;
    436:   PetscInt  lid0,gid1,cpid1;
    437: } Edge;

    439: static int gamg_hem_compare(const void *a, const void *b)
    440: {
    441:   PetscReal va = ((Edge*)a)->weight, vb = ((Edge*)b)->weight;
    442:   return (va < vb) ? 1 : (va == vb) ? 0 : -1; /* 0 for equal */
    443: }

    445: /* -------------------------------------------------------------------------- */
    446: /*
    447:    heavyEdgeMatchAgg - parallel heavy edge matching (HEM). MatAIJ specific!!!

    449:    Input Parameter:
    450:    . perm - permutation
    451:    . a_Gmat - glabal matrix of graph (data not defined)

    453:    Output Parameter:
    454:    . a_locals_llist - array of list of local nodes rooted at local node
    455: */
    458: static PetscErrorCode heavyEdgeMatchAgg(IS perm,Mat a_Gmat,PetscCoarsenData **a_locals_llist)
    459: {
    460:   PetscErrorCode   ierr;
    461:   PetscBool        isMPI;
    462:   MPI_Comm         comm;
    463:   PetscInt         sub_it,kk,n,ix,*idx,*ii,iter,Iend,my0;
    464:   PetscMPIInt      rank,size;
    465:   const PetscInt   nloc = a_Gmat->rmap->n,n_iter=6; /* need to figure out how to stop this */
    466:   PetscInt         *lid_cprowID,*lid_gid;
    467:   PetscBool        *lid_matched;
    468:   Mat_SeqAIJ       *matA, *matB=0;
    469:   Mat_MPIAIJ       *mpimat     =0;
    470:   PetscScalar      one         =1.;
    471:   PetscCoarsenData *agg_llists = NULL,*deleted_list = NULL;
    472:   Mat              cMat,tMat,P;
    473:   MatScalar        *ap;
    474:   PetscMPIInt      tag1,tag2;

    477:   PetscObjectGetComm((PetscObject)a_Gmat,&comm);
    478:   MPI_Comm_rank(comm, &rank);
    479:   MPI_Comm_size(comm, &size);
    480:   MatGetOwnershipRange(a_Gmat, &my0, &Iend);
    481:   PetscCommGetNewTag(comm, &tag1);
    482:   PetscCommGetNewTag(comm, &tag2);

    484:   PetscMalloc1(nloc, &lid_gid); /* explicit array needed */
    485:   PetscMalloc1(nloc, &lid_cprowID);
    486:   PetscMalloc1(nloc, &lid_matched);

    488:   PetscCDCreate(nloc, &agg_llists);
    489:   /* PetscCDSetChuckSize(agg_llists, nloc+1); */
    490:   *a_locals_llist = agg_llists;
    491:   PetscCDCreate(size, &deleted_list);
    492:   PetscCDSetChuckSize(deleted_list, 100);
    493:   /* setup 'lid_gid' for scatters and add self to all lists */
    494:   for (kk=0; kk<nloc; kk++) {
    495:     lid_gid[kk] = kk + my0;
    496:     PetscCDAppendID(agg_llists, kk, my0+kk);
    497:   }

    499:   /* make a copy of the graph, this gets destroyed in iterates */
    500:   MatDuplicate(a_Gmat,MAT_COPY_VALUES,&cMat);
    501:   PetscObjectTypeCompare((PetscObject)a_Gmat, MATMPIAIJ, &isMPI);
    502:   iter = 0;
    503:   while (iter++ < n_iter) {
    504:     PetscScalar    *cpcol_gid,*cpcol_max_ew,*cpcol_max_pe,*lid_max_ew;
    505:     PetscBool      *cpcol_matched;
    506:     PetscMPIInt    *cpcol_pe,proc;
    507:     Vec            locMaxEdge,locMaxPE,ghostMaxEdge,ghostMaxPE;
    508:     PetscInt       nEdges,n_nz_row,jj;
    509:     Edge           *Edges;
    510:     PetscInt       gid;
    511:     const PetscInt *perm_ix, n_sub_its = 120;

    513:     /* get submatrices of cMat */
    514:     if (isMPI) {
    515:       mpimat = (Mat_MPIAIJ*)cMat->data;
    516:       matA   = (Mat_SeqAIJ*)mpimat->A->data;
    517:       matB   = (Mat_SeqAIJ*)mpimat->B->data;
    518:       if (!matB->compressedrow.use) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"matB must have compressed row usage");
    519:     } else {
    520:       matA = (Mat_SeqAIJ*)cMat->data;
    521:     }

    523:     /* set max edge on nodes */
    524:     MatCreateVecs(cMat, &locMaxEdge, 0);
    525:     MatCreateVecs(cMat, &locMaxPE, 0);

    527:     /* get 'cpcol_pe' & 'cpcol_gid' & init. 'cpcol_matched' using 'mpimat->lvec' */
    528:     if (mpimat) {
    529:       Vec         vec;
    530:       PetscScalar vval;

    532:       MatCreateVecs(cMat, &vec, 0);
    533:       /* cpcol_pe */
    534:       vval = (PetscScalar)(rank);
    535:       for (kk=0,gid=my0; kk<nloc; kk++,gid++) {
    536:         VecSetValues(vec, 1, &gid, &vval, INSERT_VALUES); /* set with GID */
    537:       }
    538:       VecAssemblyBegin(vec);
    539:       VecAssemblyEnd(vec);
    540:       VecScatterBegin(mpimat->Mvctx,vec,mpimat->lvec,INSERT_VALUES,SCATTER_FORWARD);
    541:       VecScatterEnd(mpimat->Mvctx,vec,mpimat->lvec,INSERT_VALUES,SCATTER_FORWARD);
    542:       VecGetArray(mpimat->lvec, &cpcol_gid); /* get proc ID in 'cpcol_gid' */
    543:       VecGetLocalSize(mpimat->lvec, &n);
    544:       PetscMalloc1(n, &cpcol_pe);
    545:       for (kk=0; kk<n; kk++) cpcol_pe[kk] = (PetscMPIInt)PetscRealPart(cpcol_gid[kk]);
    546:       VecRestoreArray(mpimat->lvec, &cpcol_gid);

    548:       /* cpcol_gid */
    549:       for (kk=0,gid=my0; kk<nloc; kk++,gid++) {
    550:         vval = (PetscScalar)(gid);
    551:         VecSetValues(vec, 1, &gid, &vval, INSERT_VALUES); /* set with GID */
    552:       }
    553:       VecAssemblyBegin(vec);
    554:       VecAssemblyEnd(vec);
    555:       VecScatterBegin(mpimat->Mvctx,vec,mpimat->lvec,INSERT_VALUES,SCATTER_FORWARD);
    556:       VecScatterEnd(mpimat->Mvctx,vec,mpimat->lvec,INSERT_VALUES,SCATTER_FORWARD);
    557:       VecDestroy(&vec);
    558:       VecGetArray(mpimat->lvec, &cpcol_gid); /* get proc ID in 'cpcol_gid' */

    560:       /* cpcol_matched */
    561:       VecGetLocalSize(mpimat->lvec, &n);
    562:       PetscMalloc1(n, &cpcol_matched);
    563:       for (kk=0; kk<n; kk++) cpcol_matched[kk] = PETSC_FALSE;
    564:     }

    566:     /* need an inverse map - locals */
    567:     for (kk=0; kk<nloc; kk++) lid_cprowID[kk] = -1;
    568:     /* set index into compressed row 'lid_cprowID' */
    569:     if (matB) {
    570:       for (ix=0; ix<matB->compressedrow.nrows; ix++) {
    571:         lid_cprowID[matB->compressedrow.rindex[ix]] = ix;
    572:       }
    573:     }

    575:     /* get removed IS, use '' */
    576:     /* if (iter==1) { */
    577:     /*   PetscInt *lid_rem,idx; */
    578:     /*   PetscMalloc1(nloc, &lid_rem); */
    579:     /*   for (kk=idx=0;kk<nloc;kk++) { */
    580:     /*     PetscInt nn,lid=kk; */
    581:     /*     ii = matA->i; nn = ii[lid+1] - ii[lid]; */
    582:     /*     if ((ix=lid_cprowID[lid]) != -1) { /\* if I have any ghost neighbors *\/ */
    583:     /*       ii = matB->compressedrow.i; */
    584:     /*       nn += ii[ix+1] - ii[ix]; */
    585:     /*     } */
    586:     /*     if (nn < 2) { */
    587:     /*       lid_rem[idx++] = kk + my0; */
    588:     /*     } */
    589:     /*   } */
    590:     /*   PetscCDSetRemovedIS(agg_llists, comm, idx, lid_rem); */
    591:     /*   PetscFree(lid_rem); */
    592:     /* } */

    594:     /* compute 'locMaxEdge' & 'locMaxPE', and create list of edges, count edges' */
    595:     for (nEdges=0,kk=0,gid=my0; kk<nloc; kk++,gid++) {
    596:       PetscReal   max_e = 0., tt;
    597:       PetscScalar vval;
    598:       PetscInt    lid   = kk;
    599:       PetscMPIInt max_pe=rank,pe;
    600:       ii = matA->i; n = ii[lid+1] - ii[lid]; idx = matA->j + ii[lid];
    601:       ap = matA->a + ii[lid];
    602:       for (jj=0; jj<n; jj++) {
    603:         PetscInt lidj = idx[jj];
    604:         if (lidj != lid && PetscRealPart(ap[jj]) > max_e) max_e = PetscRealPart(ap[jj]);
    605:         if (lidj > lid) nEdges++;
    606:       }
    607:       if ((ix=lid_cprowID[lid]) != -1) { /* if I have any ghost neighbors */
    608:         ii  = matB->compressedrow.i; n = ii[ix+1] - ii[ix];
    609:         ap  = matB->a + ii[ix];
    610:         idx = matB->j + ii[ix];
    611:         for (jj=0; jj<n; jj++) {
    612:           if ((tt=PetscRealPart(ap[jj])) > max_e) max_e = tt;
    613:           nEdges++;
    614:           if ((pe=cpcol_pe[idx[jj]]) > max_pe) max_pe = pe;
    615:         }
    616:       }
    617:       vval = max_e;
    618:       VecSetValues(locMaxEdge, 1, &gid, &vval, INSERT_VALUES);

    620:       vval = (PetscScalar)max_pe;
    621:       VecSetValues(locMaxPE, 1, &gid, &vval, INSERT_VALUES);
    622:     }
    623:     VecAssemblyBegin(locMaxEdge);
    624:     VecAssemblyEnd(locMaxEdge);
    625:     VecAssemblyBegin(locMaxPE);
    626:     VecAssemblyEnd(locMaxPE);

    628:     /* get 'cpcol_max_ew' & 'cpcol_max_pe' */
    629:     if (mpimat) {
    630:       VecDuplicate(mpimat->lvec, &ghostMaxEdge);
    631:       VecScatterBegin(mpimat->Mvctx,locMaxEdge,ghostMaxEdge,INSERT_VALUES,SCATTER_FORWARD);
    632:       VecScatterEnd(mpimat->Mvctx,locMaxEdge,ghostMaxEdge,INSERT_VALUES,SCATTER_FORWARD);
    633:       VecGetArray(ghostMaxEdge, &cpcol_max_ew);

    635:       VecDuplicate(mpimat->lvec, &ghostMaxPE);
    636:       VecScatterBegin(mpimat->Mvctx,locMaxPE,ghostMaxPE,INSERT_VALUES,SCATTER_FORWARD);
    637:         VecScatterEnd(mpimat->Mvctx,locMaxPE,ghostMaxPE,INSERT_VALUES,SCATTER_FORWARD);
    638:       VecGetArray(ghostMaxPE, &cpcol_max_pe);
    639:     }

    641:     /* setup sorted list of edges */
    642:     PetscMalloc1(nEdges, &Edges);
    643:     ISGetIndices(perm, &perm_ix);
    644:     for (nEdges=n_nz_row=kk=0; kk<nloc; kk++) {
    645:       PetscInt nn, lid = perm_ix[kk];
    646:       ii = matA->i; nn = n = ii[lid+1] - ii[lid]; idx = matA->j + ii[lid];
    647:       ap = matA->a + ii[lid];
    648:       for (jj=0; jj<n; jj++) {
    649:         PetscInt lidj = idx[jj];
    650:         if (lidj > lid) {
    651:           Edges[nEdges].lid0   = lid;
    652:           Edges[nEdges].gid1   = lidj + my0;
    653:           Edges[nEdges].cpid1  = -1;
    654:           Edges[nEdges].weight = PetscRealPart(ap[jj]);
    655:           nEdges++;
    656:         }
    657:       }
    658:       if ((ix=lid_cprowID[lid]) != -1) { /* if I have any ghost neighbors */
    659:         ii  = matB->compressedrow.i; n = ii[ix+1] - ii[ix];
    660:         ap  = matB->a + ii[ix];
    661:         idx = matB->j + ii[ix];
    662:         nn += n;
    663:         for (jj=0; jj<n; jj++) {
    664:           Edges[nEdges].lid0   = lid;
    665:           Edges[nEdges].gid1   = (PetscInt)PetscRealPart(cpcol_gid[idx[jj]]);
    666:           Edges[nEdges].cpid1  = idx[jj];
    667:           Edges[nEdges].weight = PetscRealPart(ap[jj]);
    668:           nEdges++;
    669:         }
    670:       }
    671:       if (nn > 1) n_nz_row++;
    672:       else if (iter == 1) {
    673:         /* should select this because it is technically in the MIS but lets not */
    674:         PetscCDRemoveAll(agg_llists, lid);
    675:       }
    676:     }
    677:     ISRestoreIndices(perm,&perm_ix);

    679:     qsort(Edges, nEdges, sizeof(Edge), gamg_hem_compare);

    681:     /* projection matrix */
    682:     MatCreateAIJ(comm, nloc, nloc, PETSC_DETERMINE, PETSC_DETERMINE, 1, 0, 1, 0, &P);

    684:     /* clear matched flags */
    685:     for (kk=0; kk<nloc; kk++) lid_matched[kk] = PETSC_FALSE;
    686:     /* process - communicate - process */
    687:     for (sub_it=0; sub_it<n_sub_its; sub_it++) {
    688:       PetscInt nactive_edges;

    690:       VecGetArray(locMaxEdge, &lid_max_ew);
    691:       for (kk=nactive_edges=0; kk<nEdges; kk++) {
    692:         /* HEM */
    693:         const Edge     *e   = &Edges[kk];
    694:         const PetscInt lid0 =e->lid0,gid1=e->gid1,cpid1=e->cpid1,gid0=lid0+my0,lid1=gid1-my0;
    695:         PetscBool      isOK = PETSC_TRUE;

    697:         /* skip if either (local) vertex is done already */
    698:         if (lid_matched[lid0] || (gid1>=my0 && gid1<Iend && lid_matched[gid1-my0])) continue;

    700:         /* skip if ghost vertex is done */
    701:         if (cpid1 != -1 && cpcol_matched[cpid1]) continue;

    703:         nactive_edges++;
    704:         /* skip if I have a bigger edge someplace (lid_max_ew gets updated) */
    705:         if (PetscRealPart(lid_max_ew[lid0]) > e->weight + 1.e-12) continue;

    707:         if (cpid1 == -1) {
    708:           if (PetscRealPart(lid_max_ew[lid1]) > e->weight + 1.e-12) continue;
    709:         } else {
    710:           /* see if edge might get matched on other proc */
    711:           PetscReal g_max_e = PetscRealPart(cpcol_max_ew[cpid1]);
    712:           if (g_max_e > e->weight + 1.e-12) continue;
    713:           else if (e->weight > g_max_e - 1.e-12 && (PetscMPIInt)PetscRealPart(cpcol_max_pe[cpid1]) > rank) {
    714:             /* check for max_e == to this edge and larger processor that will deal with this */
    715:             continue;
    716:           }
    717:         }

    719:         /* check ghost for v0 */
    720:         if (isOK) {
    721:           PetscReal max_e,ew;
    722:           if ((ix=lid_cprowID[lid0]) != -1) { /* if I have any ghost neighbors */
    723:             ii  = matB->compressedrow.i; n = ii[ix+1] - ii[ix];
    724:             ap  = matB->a + ii[ix];
    725:             idx = matB->j + ii[ix];
    726:             for (jj=0; jj<n && isOK; jj++) {
    727:               PetscInt lidj = idx[jj];
    728:               if (cpcol_matched[lidj]) continue;
    729:               ew = PetscRealPart(ap[jj]); max_e = PetscRealPart(cpcol_max_ew[lidj]);
    730:               /* check for max_e == to this edge and larger processor that will deal with this */
    731:               if (ew > max_e - 1.e-12 && ew > PetscRealPart(lid_max_ew[lid0]) - 1.e-12 && (PetscMPIInt)PetscRealPart(cpcol_max_pe[lidj]) > rank) {
    732:                 isOK = PETSC_FALSE;
    733:               }
    734:             }
    735:           }

    737:           /* for v1 */
    738:           if (cpid1 == -1 && isOK) {
    739:             if ((ix=lid_cprowID[lid1]) != -1) { /* if I have any ghost neighbors */
    740:               ii  = matB->compressedrow.i; n = ii[ix+1] - ii[ix];
    741:               ap  = matB->a + ii[ix];
    742:               idx = matB->j + ii[ix];
    743:               for (jj=0; jj<n && isOK; jj++) {
    744:                 PetscInt lidj = idx[jj];
    745:                 if (cpcol_matched[lidj]) continue;
    746:                 ew = PetscRealPart(ap[jj]); max_e = PetscRealPart(cpcol_max_ew[lidj]);
    747:                 /* check for max_e == to this edge and larger processor that will deal with this */
    748:                 if (ew > max_e - 1.e-12 && ew > PetscRealPart(lid_max_ew[lid1]) - 1.e-12 && (PetscMPIInt)PetscRealPart(cpcol_max_pe[lidj]) > rank) {
    749:                   isOK = PETSC_FALSE;
    750:                 }
    751:               }
    752:             }
    753:           }
    754:         }

    756:         /* do it */
    757:         if (isOK) {
    758:           if (cpid1 == -1) {
    759:             lid_matched[lid1] = PETSC_TRUE;  /* keep track of what we've done this round */
    760:             PetscCDAppendRemove(agg_llists, lid0, lid1);
    761:           } else if (sub_it != n_sub_its-1) {
    762:             /* add gid1 to list of ghost deleted by me -- I need their children */
    763:             proc = cpcol_pe[cpid1];

    765:             cpcol_matched[cpid1] = PETSC_TRUE; /* messing with VecGetArray array -- needed??? */

    767:             PetscCDAppendID(deleted_list, proc, cpid1); /* cache to send messages */
    768:             PetscCDAppendID(deleted_list, proc, lid0);
    769:           } else continue;

    771:           lid_matched[lid0] = PETSC_TRUE; /* keep track of what we've done this round */
    772:           /* set projection */
    773:           MatSetValues(P,1,&gid0,1,&gid0,&one,INSERT_VALUES);
    774:           MatSetValues(P,1,&gid1,1,&gid0,&one,INSERT_VALUES);
    775:         } /* matched */
    776:       } /* edge loop */

    778:       /* deal with deleted ghost on first pass */
    779:       if (size>1 && sub_it != n_sub_its-1) {
    780:         PetscCDPos pos;  PetscBool ise = PETSC_FALSE;
    781:         PetscInt   nSend1, **sbuffs1,nSend2;
    782: #define REQ_BF_SIZE 100
    783:         MPI_Request *sreqs2[REQ_BF_SIZE],*rreqs2[REQ_BF_SIZE];
    784:         MPI_Status status;

    786:         /* send request */
    787:         for (proc=0,nSend1=0; proc<size; proc++) {
    788:           PetscCDEmptyAt(deleted_list,proc,&ise);
    789:           if (!ise) nSend1++;
    790:         }
    791:         PetscMalloc1(nSend1, &sbuffs1);
    792:         /* PetscMalloc4(nSend1, sbuffs1, nSend1, rbuffs1, nSend1, sreqs1, nSend1, rreqs1); */
    793:         /* PetscFree4(sbuffs1,rbuffs1,sreqs1,rreqs1); */
    794:         for (proc=0,nSend1=0; proc<size; proc++) {
    795:           /* count ghosts */
    796:           PetscCDSizeAt(deleted_list,proc,&n);
    797:           if (n>0) {
    798: #define CHUNCK_SIZE 100
    799:             PetscInt    *sbuff,*pt;
    800:             MPI_Request *request;
    801:             n   /= 2;
    802:             PetscMalloc1(2 + 2*n + n*CHUNCK_SIZE + 2, &sbuff);
    803:             /* PetscMalloc4(2+2*n,sbuffs1[nSend1],n*CHUNCK_SIZE,rbuffs1[nSend1],1,rreqs2[nSend1],1,sreqs2[nSend1]); */
    804:             /* save requests */
    805:             sbuffs1[nSend1] = sbuff;
    806:             request         = (MPI_Request*)sbuff;
    807:             sbuff           = pt = (PetscInt*)(request+1);
    808:             *pt++           = n; *pt++ = rank;

    810:             PetscCDGetHeadPos(deleted_list,proc,&pos);
    811:             while (pos) {
    812:               PetscInt lid0, cpid, gid;
    813:               PetscLLNGetID(pos, &cpid);
    814:               gid   = (PetscInt)PetscRealPart(cpcol_gid[cpid]);
    815:               PetscCDGetNextPos(deleted_list,proc,&pos);
    816:               PetscLLNGetID(pos, &lid0);
    817:               PetscCDGetNextPos(deleted_list,proc,&pos);
    818:               *pt++ = gid; *pt++ = lid0;
    819:             }
    820:             /* send request tag1 [n, proc, n*[gid1,lid0] ] */
    821:             MPI_Isend(sbuff, 2*n+2, MPIU_INT, proc, tag1, comm, request);
    822:             /* post recieve */
    823:             request        = (MPI_Request*)pt;
    824:             rreqs2[nSend1] = request; /* cache recv request */
    825:             pt             = (PetscInt*)(request+1);
    826:             MPI_Irecv(pt, n*CHUNCK_SIZE, MPIU_INT, proc, tag2, comm, request);
    827:             /* clear list */
    828:             PetscCDRemoveAll(deleted_list, proc);
    829:             nSend1++;
    830:           }
    831:         }
    832:         /* recieve requests, send response, clear lists */
    833:         kk     = nactive_edges;
    834:         MPIU_Allreduce(&kk,&nactive_edges,1,MPIU_INT,MPI_SUM,comm); /* not correct syncronization and global */
    835:         nSend2 = 0;
    836:         while (1) {
    837: #define BF_SZ 10000
    838:           PetscMPIInt flag,count;
    839:           PetscInt rbuff[BF_SZ],*pt,*pt2,*pt3,count2,*sbuff,count3;
    840:           MPI_Request *request;
    841:           MPI_Iprobe(MPI_ANY_SOURCE, tag1, comm, &flag, &status);
    842:           if (!flag) break;
    843:           MPI_Get_count(&status, MPIU_INT, &count);
    844:           if (count > BF_SZ) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"buffer too small for recieve: %d",count);
    845:           proc = status.MPI_SOURCE;
    846:           /* recieve request tag1 [n, proc, n*[gid1,lid0] ] */
    847:           MPI_Recv(rbuff, count, MPIU_INT, proc, tag1, comm, &status);
    848:           /* count sends */
    849:           pt = rbuff; count3 = count2 = 0;
    850:           n  = *pt++; kk = *pt++;
    851:           while (n--) {
    852:             PetscInt gid1=*pt++, lid1=gid1-my0; kk=*pt++;
    853:             if (lid_matched[lid1]) {
    854:               PetscPrintf(PETSC_COMM_SELF,"\t *** [%d]%s %d) ERROR recieved deleted gid %d, deleted by (lid) %d from proc %d\n",rank,__FUNCT__,sub_it,gid1,kk);
    855:               PetscSleep(1);
    856:             }
    857:             lid_matched[lid1] = PETSC_TRUE; /* keep track of what we've done this round */
    858:             PetscCDSizeAt(agg_llists, lid1, &kk);
    859:             count2           += kk + 2;
    860:             count3++; /* number of verts requested (n) */
    861:           }
    862:           if (count2 > count3*CHUNCK_SIZE) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Irecv will be too small: %d",count2);
    863:           /* send tag2 *[lid0, n, n*[gid] ] */
    864:           PetscMalloc(count2*sizeof(PetscInt) + sizeof(MPI_Request), &sbuff);
    865:           request          = (MPI_Request*)sbuff;
    866:           sreqs2[nSend2++] = request; /* cache request */
    867:           if (nSend2==REQ_BF_SIZE) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"buffer too small for requests: %d",nSend2);
    868:           pt2 = sbuff = (PetscInt*)(request+1);
    869:           pt  = rbuff;
    870:           n   = *pt++; kk = *pt++;
    871:           while (n--) {
    872:             /* read [n, proc, n*[gid1,lid0] */
    873:             PetscInt gid1=*pt++, lid1=gid1-my0, lid0=*pt++;
    874:             /* write [lid0, n, n*[gid] ] */
    875:             *pt2++ = lid0;
    876:             pt3    = pt2++; /* save pointer for later */
    877:             /* for (pos=PetscCDGetHeadPos(agg_llists,lid1) ; pos ; pos=PetscCDGetNextPos(agg_llists,lid1,pos)) { */
    878:             PetscCDGetHeadPos(agg_llists,lid1,&pos);
    879:             while (pos) {
    880:               PetscInt gid;
    881:               PetscLLNGetID(pos, &gid);
    882:               PetscCDGetNextPos(agg_llists,lid1,&pos);
    883:               *pt2++ = gid;
    884:             }
    885:             *pt3 = (pt2-pt3)-1;
    886:             /* clear list */
    887:             PetscCDRemoveAll(agg_llists, lid1);
    888:           }
    889:           /* send requested data tag2 *[lid0, n, n*[gid1] ] */
    890:           MPI_Isend(sbuff, count2, MPIU_INT, proc, tag2, comm, request);
    891:         }

    893:         /* recieve tag2 *[lid0, n, n*[gid] ] */
    894:         for (kk=0; kk<nSend1; kk++) {
    895:           PetscMPIInt count;
    896:           MPI_Request *request;
    897:           PetscInt    *pt, *pt2;
    898:           request = rreqs2[kk]; /* no need to free -- buffer is in 'sbuffs1' */
    899:           MPI_Wait(request, &status);
    900:           MPI_Get_count(&status, MPIU_INT, &count);
    901:           pt      = pt2 = (PetscInt*)(request+1);
    902:           while (pt-pt2 < count) {
    903:             PetscInt lid0 = *pt++, n = *pt++;
    904:             while (n--) {
    905:               PetscInt gid1 = *pt++;
    906:               PetscCDAppendID(agg_llists, lid0, gid1);
    907:             }
    908:           }
    909:         }

    911:         /* wait for tag1 isends */
    912:         while (nSend1--) {
    913:           MPI_Request *request;
    914:           request = (MPI_Request*)sbuffs1[nSend1];
    915:           MPI_Wait(request, &status);
    916:           PetscFree(request);
    917:         }
    918:         PetscFree(sbuffs1);

    920:         /* wait for tag2 isends */
    921:         while (nSend2--) {
    922:           MPI_Request *request = sreqs2[nSend2];
    923:           MPI_Wait(request, &status);
    924:           PetscFree(request);
    925:         }

    927:         VecRestoreArray(ghostMaxEdge, &cpcol_max_ew);
    928:         VecRestoreArray(ghostMaxPE, &cpcol_max_pe);

    930:         /* get 'cpcol_matched' - use locMaxPE, ghostMaxEdge, cpcol_max_ew */
    931:         for (kk=0,gid=my0; kk<nloc; kk++,gid++) {
    932:           PetscScalar vval = lid_matched[kk] ? 1.0 : 0.0;
    933:           VecSetValues(locMaxPE, 1, &gid, &vval, INSERT_VALUES); /* set with GID */
    934:         }
    935:         VecAssemblyBegin(locMaxPE);
    936:         VecAssemblyEnd(locMaxPE);
    937:         VecScatterBegin(mpimat->Mvctx,locMaxPE,ghostMaxEdge,INSERT_VALUES,SCATTER_FORWARD);
    938:           VecScatterEnd(mpimat->Mvctx,locMaxPE,ghostMaxEdge,INSERT_VALUES,SCATTER_FORWARD);
    939:         VecGetArray(ghostMaxEdge, &cpcol_max_ew);
    940:         VecGetLocalSize(mpimat->lvec, &n);
    941:         for (kk=0; kk<n; kk++) {
    942:           cpcol_matched[kk] = (PetscBool)(PetscRealPart(cpcol_max_ew[kk]) != 0.0);
    943:         }

    945:         VecRestoreArray(ghostMaxEdge, &cpcol_max_ew);
    946:       } /* size > 1 */

    948:       /* compute 'locMaxEdge' */
    949:       VecRestoreArray(locMaxEdge, &lid_max_ew);
    950:       for (kk=0,gid=my0; kk<nloc; kk++,gid++) {
    951:         PetscReal max_e = 0.,tt;
    952:         PetscScalar vval;
    953:         PetscInt lid = kk;
    954:         if (lid_matched[lid]) vval = 0.;
    955:         else {
    956:           ii = matA->i; n = ii[lid+1] - ii[lid]; idx = matA->j + ii[lid];
    957:           ap = matA->a + ii[lid];
    958:           for (jj=0; jj<n; jj++) {
    959:             PetscInt lidj = idx[jj];
    960:             if (lid_matched[lidj]) continue; /* this is new - can change local max */
    961:             if (lidj != lid && PetscRealPart(ap[jj]) > max_e) max_e = PetscRealPart(ap[jj]);
    962:           }
    963:           if (lid_cprowID && (ix=lid_cprowID[lid]) != -1) { /* if I have any ghost neighbors */
    964:             ii  = matB->compressedrow.i; n = ii[ix+1] - ii[ix];
    965:             ap  = matB->a + ii[ix];
    966:             idx = matB->j + ii[ix];
    967:             for (jj=0; jj<n; jj++) {
    968:               PetscInt lidj = idx[jj];
    969:               if (cpcol_matched[lidj]) continue;
    970:               if ((tt=PetscRealPart(ap[jj])) > max_e) max_e = tt;
    971:             }
    972:           }
    973:         }
    974:         vval = (PetscScalar)max_e;
    975:         VecSetValues(locMaxEdge, 1, &gid, &vval, INSERT_VALUES); /* set with GID */
    976:       }
    977:       VecAssemblyBegin(locMaxEdge);
    978:       VecAssemblyEnd(locMaxEdge);

    980:       if (size>1 && sub_it != n_sub_its-1) {
    981:         /* compute 'cpcol_max_ew' */
    982:         VecScatterBegin(mpimat->Mvctx,locMaxEdge,ghostMaxEdge,INSERT_VALUES,SCATTER_FORWARD);
    983:           VecScatterEnd(mpimat->Mvctx,locMaxEdge,ghostMaxEdge,INSERT_VALUES,SCATTER_FORWARD);
    984:         VecGetArray(ghostMaxEdge, &cpcol_max_ew);
    985:         VecGetArray(locMaxEdge, &lid_max_ew);

    987:         /* compute 'cpcol_max_pe' */
    988:         for (kk=0,gid=my0; kk<nloc; kk++,gid++) {
    989:           PetscInt lid = kk;
    990:           PetscReal ew,v1_max_e,v0_max_e=PetscRealPart(lid_max_ew[lid]);
    991:           PetscScalar vval;
    992:           PetscMPIInt max_pe=rank,pe;
    993:           if (lid_matched[lid]) vval = (PetscScalar)rank;
    994:           else if ((ix=lid_cprowID[lid]) != -1) { /* if I have any ghost neighbors */
    995:             ii  = matB->compressedrow.i; n = ii[ix+1] - ii[ix];
    996:             ap  = matB->a + ii[ix];
    997:             idx = matB->j + ii[ix];
    998:             for (jj=0; jj<n; jj++) {
    999:               PetscInt lidj = idx[jj];
    1000:               if (cpcol_matched[lidj]) continue;
    1001:               ew = PetscRealPart(ap[jj]); v1_max_e = PetscRealPart(cpcol_max_ew[lidj]);
    1002:               /* get max pe that has a max_e == to this edge w */
    1003:               if ((pe=cpcol_pe[idx[jj]]) > max_pe && ew > v1_max_e - 1.e-12 && ew > v0_max_e - 1.e-12) max_pe = pe;
    1004:             }
    1005:             vval = (PetscScalar)max_pe;
    1006:           }
    1007:           VecSetValues(locMaxPE, 1, &gid, &vval, INSERT_VALUES);
    1008:         }
    1009:         VecAssemblyBegin(locMaxPE);
    1010:         VecAssemblyEnd(locMaxPE);

    1012:         VecScatterBegin(mpimat->Mvctx,locMaxPE,ghostMaxPE,INSERT_VALUES,SCATTER_FORWARD);
    1013:           VecScatterEnd(mpimat->Mvctx,locMaxPE,ghostMaxPE,INSERT_VALUES,SCATTER_FORWARD);
    1014:         VecGetArray(ghostMaxPE, &cpcol_max_pe);
    1015:         VecRestoreArray(locMaxEdge, &lid_max_ew);
    1016:       } /* deal with deleted ghost */
    1017:       PetscInfo3(a_Gmat,"\t %D.%D: %D active edges.\n",iter,sub_it,nactive_edges);
    1018:       if (!nactive_edges) break;
    1019:     } /* sub_it loop */

    1021:     /* clean up iteration */
    1022:     PetscFree(Edges);
    1023:     if (mpimat) {
    1024:       VecRestoreArray(ghostMaxEdge, &cpcol_max_ew);
    1025:       VecDestroy(&ghostMaxEdge);
    1026:       VecRestoreArray(ghostMaxPE, &cpcol_max_pe);
    1027:       VecDestroy(&ghostMaxPE);
    1028:       PetscFree(cpcol_pe);
    1029:       PetscFree(cpcol_matched);
    1030:     }

    1032:     VecDestroy(&locMaxEdge);
    1033:     VecDestroy(&locMaxPE);

    1035:     if (mpimat) {
    1036:       VecRestoreArray(mpimat->lvec, &cpcol_gid);
    1037:     }

    1039:     /* create next G if needed */
    1040:     if (iter == n_iter) { /* hard wired test - need to look at full surrounded nodes or something */
    1041:       MatDestroy(&P);
    1042:       MatDestroy(&cMat);
    1043:       break;
    1044:     } else {
    1045:       Vec diag;
    1046:       /* add identity for unmatched vertices so they stay alive */
    1047:       for (kk=0,gid=my0; kk<nloc; kk++,gid++) {
    1048:         if (!lid_matched[kk]) {
    1049:           gid  = kk+my0;
    1050:           MatGetRow(cMat,gid,&n,0,0);
    1051:           if (n>1) {
    1052:             MatSetValues(P,1,&gid,1,&gid,&one,INSERT_VALUES);
    1053:           }
    1054:           MatRestoreRow(cMat,gid,&n,0,0);
    1055:         }
    1056:       }
    1057:       MatAssemblyBegin(P,MAT_FINAL_ASSEMBLY);
    1058:       MatAssemblyEnd(P,MAT_FINAL_ASSEMBLY);

    1060:       /* project to make new graph with colapsed edges */
    1061:       MatPtAP(cMat,P,MAT_INITIAL_MATRIX,1.0,&tMat);
    1062:       MatDestroy(&P);
    1063:       MatDestroy(&cMat);
    1064:       cMat = tMat;
    1065:       MatCreateVecs(cMat, &diag, 0);
    1066:       MatGetDiagonal(cMat, diag); /* effectively PCJACOBI */
    1067:       VecReciprocal(diag);
    1068:       VecSqrtAbs(diag);
    1069:       MatDiagonalScale(cMat, diag, diag);
    1070:       VecDestroy(&diag);
    1071:     }
    1072:   } /* coarsen iterator */

    1074:   /* make fake matrix */
    1075:   if (size>1) {
    1076:     Mat mat;
    1077:     PetscCDPos pos;
    1078:     PetscInt gid, NN, MM, jj = 0, mxsz = 0;

    1080:     for (kk=0; kk<nloc; kk++) {
    1081:       PetscCDSizeAt(agg_llists, kk, &jj);
    1082:       if (jj > mxsz) mxsz = jj;
    1083:     }
    1084:     MatGetSize(a_Gmat, &MM, &NN);
    1085:     if (mxsz > MM-nloc) mxsz = MM-nloc;

    1087:     MatCreateAIJ(comm, nloc, nloc,PETSC_DETERMINE, PETSC_DETERMINE,0, 0, mxsz, 0, &mat);

    1089:     /* */
    1090:     for (kk=0,gid=my0; kk<nloc; kk++,gid++) {
    1091:       /* for (pos=PetscCDGetHeadPos(agg_llists,kk) ; pos ; pos=PetscCDGetNextPos(agg_llists,kk,pos)) { */
    1092:       PetscCDGetHeadPos(agg_llists,kk,&pos);
    1093:       while (pos) {
    1094:         PetscInt gid1;
    1095:         PetscLLNGetID(pos, &gid1);
    1096:         PetscCDGetNextPos(agg_llists,kk,&pos);

    1098:         if (gid1 < my0 || gid1 >= my0+nloc) {
    1099:           MatSetValues(mat,1,&gid,1,&gid1,&one,ADD_VALUES);
    1100:         }
    1101:       }
    1102:     }
    1103:     MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
    1104:     MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);

    1106:     PetscCDSetMat(agg_llists, mat);
    1107:   }

    1109:   PetscFree(lid_cprowID);
    1110:   PetscFree(lid_gid);
    1111:   PetscFree(lid_matched);
    1112:   PetscCDDestroy(deleted_list);
    1113:   return(0);
    1114: }

    1116: typedef struct {
    1117:   int dummy;
    1118: } MatCoarsen_HEM;
    1119: /*
    1120:    HEM coarsen, simple greedy.
    1121: */
    1124: static PetscErrorCode MatCoarsenApply_HEM(MatCoarsen coarse)
    1125: {
    1126:   /* MatCoarsen_HEM *HEM = (MatCoarsen_HEM*)coarse->subctx; */
    1128:   Mat            mat = coarse->graph;

    1132:   if (!coarse->perm) {
    1133:     IS       perm;
    1134:     PetscInt n,m;
    1135:
    1136:     MatGetLocalSize(mat, &m, &n);
    1137:     ISCreateStride(PetscObjectComm((PetscObject)mat), m, 0, 1, &perm);
    1138:     heavyEdgeMatchAgg(perm, mat, &coarse->agg_lists);
    1139:     ISDestroy(&perm);
    1140:   } else {
    1141:     heavyEdgeMatchAgg(coarse->perm, mat, &coarse->agg_lists);
    1142:   }
    1143:   return(0);
    1144: }

    1148: static PetscErrorCode MatCoarsenView_HEM(MatCoarsen coarse,PetscViewer viewer)
    1149: {
    1150:   /* MatCoarsen_HEM *HEM = (MatCoarsen_HEM*)coarse->subctx; */
    1152:   PetscMPIInt    rank;
    1153:   PetscBool      iascii;

    1157:   MPI_Comm_rank(PetscObjectComm((PetscObject)coarse),&rank);
    1158:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    1159:   if (iascii) {
    1160:     PetscViewerASCIIPushSynchronized(viewer);
    1161:     PetscViewerASCIISynchronizedPrintf(viewer,"  [%d] HEM aggregator\n",rank);
    1162:     PetscViewerFlush(viewer);
    1163:     PetscViewerASCIIPopSynchronized(viewer);
    1164:   }
    1165:   return(0);
    1166: }

    1170: static PetscErrorCode MatCoarsenDestroy_HEM(MatCoarsen coarse)
    1171: {
    1172:   MatCoarsen_HEM *HEM = (MatCoarsen_HEM*)coarse->subctx;

    1177:   PetscFree(HEM);
    1178:   return(0);
    1179: }

    1181: /*MC
    1182:    MATCOARSENHEM - A coarsener that uses HEM a simple greedy coarsener

    1184:    Level: beginner

    1186: .keywords: Coarsen, create, context

    1188: .seealso: MatCoarsenSetType(), MatCoarsenType, MatCoarsenCreate()

    1190: M*/

    1194: PETSC_EXTERN PetscErrorCode MatCoarsenCreate_HEM(MatCoarsen coarse)
    1195: {
    1197:   MatCoarsen_HEM *HEM;

    1200:   PetscNewLog(coarse,&HEM);
    1201:   coarse->subctx       = (void*)HEM;
    1202:   coarse->ops->apply   = MatCoarsenApply_HEM;
    1203:   coarse->ops->view    = MatCoarsenView_HEM;
    1204:   coarse->ops->destroy = MatCoarsenDestroy_HEM;
    1205:   /* coarse->ops->setfromoptions = MatCoarsenSetFromOptions_HEM; */
    1206:   return(0);
    1207: }