Actual source code: ex9.c
petsc-3.4.2 2013-07-02

      2: static char help[] = "Tests DMCreateColoring() in 3d.\n\n";

      4: #include <petscmat.h>
      5: #include <petscdmda.h>

      9: int main(int argc,char **argv)
     10: {
     11:   PetscInt        i,M = 3,N = 5,P=3,s=1,w=2,m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE;
     12:   PetscErrorCode  ierr;
     13:   PetscInt        *lx = NULL,*ly = NULL,*lz = NULL;
     14:   DM              da;
     15:   PetscBool       flg = PETSC_FALSE,test_order = PETSC_FALSE;
     16:   ISColoring      coloring;
     17:   Mat             mat;
     18:   DMDAStencilType stencil_type = DMDA_STENCIL_BOX;
     19:   Vec             lvec,dvec;
     20:   MatFDColoring   fdcoloring;

     22:   PetscInitialize(&argc,&argv,(char*)0,help);

     24:   /* Read options */
     25:   PetscOptionsGetInt(NULL,"-M",&M,NULL);
     26:   PetscOptionsGetInt(NULL,"-N",&N,NULL);
     27:   PetscOptionsGetInt(NULL,"-P",&P,NULL);
     28:   PetscOptionsGetInt(NULL,"-m",&m,NULL);
     29:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
     30:   PetscOptionsGetInt(NULL,"-p",&p,NULL);
     31:   PetscOptionsGetInt(NULL,"-s",&s,NULL);
     32:   PetscOptionsGetInt(NULL,"-w",&w,NULL);
     33:   PetscOptionsGetBool(NULL,"-star",&flg,NULL);
     34:   if (flg) stencil_type =  DMDA_STENCIL_STAR;
     35:   PetscOptionsGetBool(NULL,"-test_order",&test_order,NULL);
     36:   flg  = PETSC_FALSE;
     37:   PetscOptionsGetBool(NULL,"-distribute",&flg,NULL);
     38:   if (flg) {
     39:     if (m == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -m option with -distribute option");
     40:     PetscMalloc(m*sizeof(PetscInt),&lx);
     41:     for (i=0; i<m-1; i++) lx[i] = 4;
     42:     lx[m-1] = M - 4*(m-1);

     44:     if (n == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -n option with -distribute option");
     45:     PetscMalloc(n*sizeof(PetscInt),&ly);
     46:     for (i=0; i<n-1; i++) ly[i] = 2;
     47:     ly[n-1] = N - 2*(n-1);

     49:     if (p == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -p option with -distribute option");
     50:     PetscMalloc(p*sizeof(PetscInt),&lz);
     51:     for (i=0; i<p-1; i++) lz[i] = 2;
     52:     lz[p-1] = P - 2*(p-1);
     53:   }

     55:   /* Create distributed array and get vectors */
     56:   DMDACreate3d(PETSC_COMM_WORLD,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,stencil_type,M,N,P,m,n,p,w,s,lx,ly,lz,&da);
     57:   PetscFree(lx);
     58:   PetscFree(ly);
     59:   PetscFree(lz);

     61:   DMCreateColoring(da,IS_COLORING_GLOBAL,MATMPIAIJ,&coloring);
     62:   DMCreateMatrix(da,MATMPIAIJ,&mat);
     63:   MatFDColoringCreate(mat,coloring,&fdcoloring);

     65:   DMCreateGlobalVector(da,&dvec);
     66:   DMCreateLocalVector(da,&lvec);

     68:   /* Free memory */
     69:   MatFDColoringDestroy(&fdcoloring);
     70:   VecDestroy(&dvec);
     71:   VecDestroy(&lvec);
     72:   MatDestroy(&mat);
     73:   ISColoringDestroy(&coloring);
     74:   DMDestroy(&da);
     75:   PetscFinalize();
     76:   return 0;
     77: }