Actual source code: valid.c

petsc-dev 2014-02-02
Report Typos and Errors
  1: #include <petsc-private/matimpl.h>      /*I "petscmat.h"  I*/
  2: #include <petscsf.h>

  4: PETSC_EXTERN PetscErrorCode MatColoringCreateBipartiteGraph(MatColoring,PetscSF *,PetscSF *);

  8: PETSC_EXTERN PetscErrorCode MatColoringTestValid(MatColoring mc,ISColoring coloring)
  9: {
 11:   Mat            m=mc->mat;
 12:   PetscSF        etor,etoc;
 13:   PetscInt       s,e;
 14:   PetscInt       ncolors,nrows,ncols;
 15:   IS             *colors;
 16:   PetscInt       i,j,k,l;
 17:   PetscInt       *staterow,*statecol,*statespread;
 18:   PetscInt       nindices;
 19:   const PetscInt *indices;
 20:   PetscInt       dist=mc->dist;
 21:   const PetscInt *degrees;
 22:   PetscInt       *stateleafrow,*stateleafcol,nleafrows,nleafcols,idx,nentries;

 25:   /* get the communication structures and the colors */
 26:   MatColoringCreateBipartiteGraph(mc,&etoc,&etor);
 27:   ISColoringGetIS(coloring,&ncolors,&colors);
 28:   PetscSFGetGraph(etor,&nrows,&nleafrows,NULL,NULL);
 29:   PetscSFGetGraph(etoc,&ncols,&nleafcols,NULL,NULL);
 30:   MatGetOwnershipRangeColumn(m,&s,&e);
 31:   PetscMalloc(sizeof(PetscInt)*ncols,&statecol);
 32:   PetscMalloc(sizeof(PetscInt)*nrows,&staterow);
 33:   PetscMalloc(sizeof(PetscInt)*nleafcols,&stateleafcol);
 34:   PetscMalloc(sizeof(PetscInt)*nleafrows,&stateleafrow);

 36:   for (l=0;l<ncolors;l++) {
 37:     for (k=0;k<ncols;k++) {
 38:       statecol[k] = -1;
 39:     }
 40:     ISGetLocalSize(colors[l],&nindices);
 41:     ISGetIndices(colors[l],&indices);
 42:     for (k=0;k<nindices;k++) {
 43:       statecol[indices[k]-s] = indices[k];
 44:     }
 45:     ISRestoreIndices(colors[l],&indices);
 46:     statespread = statecol;
 47:     for (k=0;k<dist;k++) {
 48:       if (k%2 == 1) {
 49:         PetscSFComputeDegreeBegin(etor,&degrees);
 50:         PetscSFComputeDegreeEnd(etor,&degrees);
 51:         nentries=0;
 52:         for(i=0;i<nrows;i++) {
 53:           nentries += degrees[i];
 54:         }
 55:         idx=0;
 56:         for(i=0;i<nrows;i++) {
 57:           for (j=0;j<degrees[i];j++) {
 58:             stateleafrow[idx] = staterow[i];
 59:             idx++;
 60:           }
 61:           statecol[i]=0.;
 62:         }
 63:         if (idx != nentries) SETERRQ2(PetscObjectComm((PetscObject)mc),PETSC_ERR_NOT_CONVERGED,"Bad number of entries %d vs %d",idx,nentries);
 64:         PetscLogEventBegin(Mat_Coloring_Comm,mc,0,0,0);
 65:         PetscSFReduceBegin(etoc,MPIU_INT,stateleafrow,statecol,MPI_MAX);
 66:         PetscSFReduceEnd(etoc,MPIU_INT,stateleafrow,statecol,MPI_MAX);
 67:         PetscLogEventEnd(Mat_Coloring_Comm,mc,0,0,0);
 68:         statespread = statecol;
 69:       } else {
 70:         PetscSFComputeDegreeBegin(etoc,&degrees);
 71:         PetscSFComputeDegreeEnd(etoc,&degrees);
 72:         nentries=0;
 73:         for(i=0;i<ncols;i++) {
 74:           nentries += degrees[i];
 75:         }
 76:         idx=0;
 77:         for(i=0;i<ncols;i++) {
 78:           for (j=0;j<degrees[i];j++) {
 79:             stateleafcol[idx] = statecol[i];
 80:             idx++;
 81:           }
 82:           staterow[i]=0.;
 83:         }
 84:         if (idx != nentries) SETERRQ2(PetscObjectComm((PetscObject)mc),PETSC_ERR_NOT_CONVERGED,"Bad number of entries %d vs %d",idx,nentries);
 85:         PetscLogEventBegin(Mat_Coloring_Comm,mc,0,0,0);
 86:         PetscSFReduceBegin(etor,MPIU_INT,stateleafcol,staterow,MPI_MAX);
 87:         PetscSFReduceEnd(etor,MPIU_INT,stateleafcol,staterow,MPI_MAX);
 88:         PetscLogEventEnd(Mat_Coloring_Comm,mc,0,0,0);
 89:         statespread = staterow;
 90:       }
 91:     }
 92:     for (k=0;k<nindices;k++) {
 93:       if (statespread[indices[k]-s] != indices[k]) {
 94:         PetscPrintf(PetscObjectComm((PetscObject)mc),"%d of color %d conflicts with %d\n",indices[k],l,statespread[indices[k]-s]);
 95:       }
 96:     }
 97:     ISRestoreIndices(colors[l],&indices);
 98:   }
 99:   PetscFree(statecol);
100:   PetscFree(staterow);
101:   PetscFree(stateleafcol);
102:   PetscFree(stateleafrow);
103:   PetscSFDestroy(&etor);
104:   PetscSFDestroy(&etoc);
105:   return(0);
106: }