Actual source code: ex9.c
petsc-3.5.4 2015-05-23
2: static char help[] = "Tests DMCreateColoring() in 3d.\n\n";
4: #include <petscmat.h>
5: #include <petscdm.h>
6: #include <petscdmda.h>
10: int main(int argc,char **argv)
11: {
12: PetscInt i,M = 3,N = 5,P=3,s=1,w=2,m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE;
13: PetscErrorCode ierr;
14: PetscInt *lx = NULL,*ly = NULL,*lz = NULL;
15: DM da;
16: PetscBool flg = PETSC_FALSE,test_order = PETSC_FALSE;
17: ISColoring coloring;
18: Mat mat;
19: DMDAStencilType stencil_type = DMDA_STENCIL_BOX;
20: Vec lvec,dvec;
21: MatFDColoring fdcoloring;
23: PetscInitialize(&argc,&argv,(char*)0,help);
25: /* Read options */
26: PetscOptionsGetInt(NULL,"-M",&M,NULL);
27: PetscOptionsGetInt(NULL,"-N",&N,NULL);
28: PetscOptionsGetInt(NULL,"-P",&P,NULL);
29: PetscOptionsGetInt(NULL,"-m",&m,NULL);
30: PetscOptionsGetInt(NULL,"-n",&n,NULL);
31: PetscOptionsGetInt(NULL,"-p",&p,NULL);
32: PetscOptionsGetInt(NULL,"-s",&s,NULL);
33: PetscOptionsGetInt(NULL,"-w",&w,NULL);
34: PetscOptionsGetBool(NULL,"-star",&flg,NULL);
35: if (flg) stencil_type = DMDA_STENCIL_STAR;
36: PetscOptionsGetBool(NULL,"-test_order",&test_order,NULL);
37: flg = PETSC_FALSE;
38: PetscOptionsGetBool(NULL,"-distribute",&flg,NULL);
39: if (flg) {
40: if (m == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -m option with -distribute option");
41: PetscMalloc1(m,&lx);
42: for (i=0; i<m-1; i++) lx[i] = 4;
43: lx[m-1] = M - 4*(m-1);
45: if (n == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -n option with -distribute option");
46: PetscMalloc1(n,&ly);
47: for (i=0; i<n-1; i++) ly[i] = 2;
48: ly[n-1] = N - 2*(n-1);
50: if (p == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -p option with -distribute option");
51: PetscMalloc1(p,&lz);
52: for (i=0; i<p-1; i++) lz[i] = 2;
53: lz[p-1] = P - 2*(p-1);
54: }
56: /* Create distributed array and get vectors */
57: DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,stencil_type,M,N,P,m,n,p,w,s,lx,ly,lz,&da);
58: PetscFree(lx);
59: PetscFree(ly);
60: PetscFree(lz);
62: DMSetMatType(da,MATMPIAIJ);
63: DMCreateColoring(da,IS_COLORING_GLOBAL,&coloring);
64: DMSetMatType(da,MATMPIAIJ);
65: DMCreateMatrix(da,&mat);
66: MatFDColoringCreate(mat,coloring,&fdcoloring);
67: MatFDColoringSetUp(mat,coloring,fdcoloring);
69: DMCreateGlobalVector(da,&dvec);
70: DMCreateLocalVector(da,&lvec);
72: /* Free memory */
73: MatFDColoringDestroy(&fdcoloring);
74: VecDestroy(&dvec);
75: VecDestroy(&lvec);
76: MatDestroy(&mat);
77: ISColoringDestroy(&coloring);
78: DMDestroy(&da);
79: PetscFinalize();
80: return 0;
81: }