Actual source code: natural.c
petsc-3.10.5 2019-03-28
Report Typos and Errors

      1:  #include <petsc/private/matimpl.h>
      2:  #include <petsc/private/isimpl.h>

      4: static PetscErrorCode MatColoringApply_Natural(MatColoring mc,ISColoring *iscoloring)
      5: {
      6:   PetscErrorCode  ierr;
      7:   PetscInt        start,end,i,bs = 1,n;
      8:   ISColoringValue *colors;
      9:   MPI_Comm        comm;
     10:   PetscBool       flg1,flg2;
     11:   Mat             mat     = mc->mat;
     12:   Mat             mat_seq = mc->mat;
     13:   PetscMPIInt     size;
     14:   ISColoring      iscoloring_seq;
     15:   ISColoringValue *colors_loc;
     16:   PetscInt        rstart,rend,N_loc,nc;

     19:   /* this is ugly way to get blocksize but cannot call MatGetBlockSize() because AIJ can have bs > 1 */
     20:   PetscObjectTypeCompare((PetscObject)mat,MATSEQBAIJ,&flg1);
     21:   PetscObjectTypeCompare((PetscObject)mat,MATMPIBAIJ,&flg2);
     22:   if (flg1 || flg2) {
     23:     MatGetBlockSize(mat,&bs);
     24:   }

     26:   PetscObjectGetComm((PetscObject)mat,&comm);
     27:   MPI_Comm_size(comm,&size);
     28:   if (size > 1) {
     29:     /* create a sequential iscoloring on all processors */
     30:     MatGetSeqNonzeroStructure(mat,&mat_seq);
     31:   }

     33:   MatGetSize(mat_seq,&n,NULL);
     34:   MatGetOwnershipRange(mat_seq,&start,&end);
     35:   n    = n/bs;
     36:   if (n > IS_COLORING_MAX-1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Maximum color size exceeded");

     38:   start = start/bs;
     39:   end   = end/bs;
     40:   PetscMalloc1(end-start+1,&colors);
     41:   for (i=start; i<end; i++) {
     42:     colors[i-start] = (ISColoringValue)i;
     43:   }
     44:   ISColoringCreate(comm,n,end-start,colors,PETSC_OWN_POINTER,iscoloring);

     46:   if (size > 1) {
     47:     MatDestroySeqNonzeroStructure(&mat_seq);

     49:     /* convert iscoloring_seq to a parallel iscoloring */
     50:     iscoloring_seq = *iscoloring;
     51:     rstart         = mat->rmap->rstart/bs;
     52:     rend           = mat->rmap->rend/bs;
     53:     N_loc          = rend - rstart; /* number of local nodes */

     55:     /* get local colors for each local node */
     56:     PetscMalloc1(N_loc+1,&colors_loc);
     57:     for (i=rstart; i<rend; i++) {
     58:       colors_loc[i-rstart] = iscoloring_seq->colors[i];
     59:     }
     60:     /* create a parallel iscoloring */
     61:     nc   = iscoloring_seq->n;
     62:     ISColoringCreate(comm,nc,N_loc,colors_loc,PETSC_OWN_POINTER,iscoloring);
     63:     ISColoringDestroy(&iscoloring_seq);
     64:   }
     65:   return(0);
     66: }

     68: PETSC_EXTERN PetscErrorCode MatColoringCreate_Natural(MatColoring mc)
     69: {
     71:     mc->data                = NULL;
     72:     mc->ops->apply          = MatColoringApply_Natural;
     73:     mc->ops->view           = NULL;
     74:     mc->ops->destroy        = NULL;
     75:     mc->ops->setfromoptions = NULL;
     76:     return(0);
     77: }