xref: /petsc/src/mat/tutorials/ex11.c (revision b122ec5aa1bd4469eb4e0673542fb7de3f411254)
1 
2 static char help[] = "Tests MatMeshToDual()\n\n";
3 
4 /*T
5    Concepts: Mat^mesh partitioning
6    Processors: n
7 T*/
8 
9 /*
10   Include "petscmat.h" so that we can use matrices.
11   automatically includes:
12      petscsys.h       - base PETSc routines   petscvec.h    - vectors
13      petscmat.h    - matrices
14      petscis.h     - index sets            petscviewer.h - viewers
15 */
16 #include <petscmat.h>
17 
18 int main(int argc,char **args)
19 {
20   Mat             mesh,dual;
21   PetscInt        Nvertices = 6;       /* total number of vertices */
22   PetscInt        ncells    = 2;       /* number cells on this process */
23   PetscInt        *ii,*jj;
24   PetscMPIInt     size,rank;
25   MatPartitioning part;
26   IS              is;
27 
28   CHKERRQ(PetscInitialize(&argc,&args,(char*)0,help));
29   CHKERRMPI(MPI_Comm_size(MPI_COMM_WORLD,&size));
30   PetscCheckFalse(size != 2,PETSC_COMM_WORLD,PETSC_ERR_SUP,"This example is for exactly two processes");
31   CHKERRMPI(MPI_Comm_rank(MPI_COMM_WORLD,&rank));
32 
33   CHKERRQ(PetscMalloc1(3,&ii));
34   CHKERRQ(PetscMalloc1(6,&jj));
35   ii[0] = 0; ii[1] = 3; ii[2] = 6;
36   if (rank == 0) {
37     jj[0] = 0; jj[1] = 1; jj[2] = 2; jj[3] = 1; jj[4] = 2; jj[5] = 3;
38   } else {
39     jj[0] = 1; jj[1] = 4; jj[2] = 5; jj[3] = 1; jj[4] = 3; jj[5] = 5;
40   }
41   CHKERRQ(MatCreateMPIAdj(MPI_COMM_WORLD,ncells,Nvertices,ii,jj,NULL,&mesh));
42   CHKERRQ(MatMeshToCellGraph(mesh,2,&dual));
43   CHKERRQ(MatView(dual,PETSC_VIEWER_STDOUT_WORLD));
44 
45   CHKERRQ(MatPartitioningCreate(MPI_COMM_WORLD,&part));
46   CHKERRQ(MatPartitioningSetAdjacency(part,dual));
47   CHKERRQ(MatPartitioningSetFromOptions(part));
48   CHKERRQ(MatPartitioningApply(part,&is));
49   CHKERRQ(ISView(is,PETSC_VIEWER_STDOUT_WORLD));
50   CHKERRQ(ISDestroy(&is));
51   CHKERRQ(MatPartitioningDestroy(&part));
52 
53   CHKERRQ(MatDestroy(&mesh));
54   CHKERRQ(MatDestroy(&dual));
55   CHKERRQ(PetscFinalize());
56   return 0;
57 }
58 
59 /*TEST
60 
61    build:
62      requires: parmetis
63 
64    test:
65       nsize: 2
66       requires: parmetis
67 
68 TEST*/
69