xref: /petsc/src/mat/tutorials/ex15.c (revision 28b400f66ebc7ae0049166a2294dfcd3df27e64b)
1 static char help[] = "Example of using graph partitioning to partition a graph\n\n";
2 
3 /*T
4    Concepts: Mat^mat partitioning
5    Concepts: Mat^image segmentation
6    Processors: n
7 T*/
8 
9 #include <petscmat.h>
10 
11 int main(int argc, char **args)
12 {
13   Mat             A;
14   MatPartitioning part;
15   IS              is;
16   PetscInt        r,N = 10, start, end, *vweights;
17   PetscBool       set_vweights=PETSC_FALSE,use_edge_weights=PETSC_FALSE;
18   PetscMPIInt     rank;
19   MPI_Comm        comm;
20   PetscErrorCode  ierr;
21 
22   ierr = PetscInitialize(&argc, &args, (char*) 0, help);if (ierr) return ierr;
23   comm = PETSC_COMM_WORLD;
24   CHKERRQ(PetscOptionsGetInt(NULL,NULL, "-N", &N, NULL));
25   CHKERRMPI(MPI_Comm_rank(comm,&rank));
26   CHKERRQ(MatCreate(comm, &A));
27   CHKERRQ(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, N, N));
28   CHKERRQ(MatSetFromOptions(A));
29   CHKERRQ(MatSeqAIJSetPreallocation(A, 3, NULL));
30   CHKERRQ(MatMPIAIJSetPreallocation(A, 3, NULL, 2, NULL));
31   CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-test_vertex_weights",&set_vweights,NULL));
32   CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-test_use_edge_weights",&use_edge_weights,NULL));
33   /* Create a linear mesh */
34   CHKERRQ(MatGetOwnershipRange(A, &start, &end));
35   if (set_vweights) {
36     CHKERRQ(PetscMalloc1(end-start,&vweights));
37     for (r = start; r < end; ++r)
38       vweights[r-start] = rank+1;
39   }
40   for (r = start; r < end; ++r) {
41     if (r == 0) {
42       PetscInt    cols[2];
43       PetscScalar vals[2];
44 
45       cols[0] = r;   cols[1] = r+1;
46       vals[0] = 1.0; vals[1] = use_edge_weights? 2.0: 1.0;
47 
48       CHKERRQ(MatSetValues(A, 1, &r, 2, cols, vals, INSERT_VALUES));
49     } else if (r == N-1) {
50       PetscInt    cols[2];
51       PetscScalar vals[2];
52 
53       cols[0] = r-1; cols[1] = r;
54       vals[0] = use_edge_weights? 3.0:1.0; vals[1] = 1.0;
55 
56       CHKERRQ(MatSetValues(A, 1, &r, 2, cols, vals, INSERT_VALUES));
57     } else {
58       PetscInt    cols[3];
59       PetscScalar vals[3];
60 
61       cols[0] = r-1; cols[1] = r;   cols[2] = r+1;
62       /* ADJ matrix needs to be symmetric */
63       vals[0] = use_edge_weights? (cols[0]==0? 2.0:5.0):1.0;
64       vals[1] = 1.0;
65       vals[2] = use_edge_weights? (cols[2]==N-1? 3.0:5.0):1.0;
66 
67       CHKERRQ(MatSetValues(A, 1, &r, 3, cols, vals, INSERT_VALUES));
68     }
69   }
70   CHKERRQ(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
71   CHKERRQ(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
72 
73   CHKERRQ(MatPartitioningCreate(comm, &part));
74   CHKERRQ(MatPartitioningSetAdjacency(part, A));
75   if (set_vweights) {
76     CHKERRQ(MatPartitioningSetVertexWeights(part,vweights));
77   }
78   if (use_edge_weights) {
79     CHKERRQ(MatPartitioningSetUseEdgeWeights(part,use_edge_weights));
80 
81     CHKERRQ(MatPartitioningGetUseEdgeWeights(part,&use_edge_weights));
82     PetscCheck(use_edge_weights,comm,PETSC_ERR_ARG_INCOMP, "use_edge_weights flag does not setup correctly ");
83   }
84   CHKERRQ(MatPartitioningSetFromOptions(part));
85   CHKERRQ(MatPartitioningApply(part, &is));
86   CHKERRQ(ISView(is, PETSC_VIEWER_STDOUT_WORLD));
87   CHKERRQ(ISDestroy(&is));
88   CHKERRQ(MatPartitioningDestroy(&part));
89 
90   CHKERRQ(MatDestroy(&A));
91   ierr = PetscFinalize();
92   return ierr;
93 }
94 
95 /*TEST
96 
97    test:
98       nsize: 3
99       requires: parmetis
100       args: -mat_partitioning_type parmetis
101 
102    test:
103       suffix: 2
104       nsize: 3
105       requires: ptscotch
106       args: -mat_partitioning_type ptscotch
107 
108    test:
109       suffix: 3
110       nsize: 4
111       requires: party
112       args: -mat_partitioning_type party
113 
114    test:
115       suffix: 4
116       nsize: 3
117       requires: chaco
118       args: -mat_partitioning_type chaco
119 
120    test:
121       suffix: 5
122       nsize: 3
123       requires: parmetis
124       args: -mat_partitioning_type hierarch -mat_partitioning_hierarchical_nfineparts 3 -mat_partitioning_nparts 10 -N 100
125 
126    test:
127       suffix: 6
128       nsize: 3
129       requires: parmetis
130       args: -mat_partitioning_type hierarch -mat_partitioning_hierarchical_nfineparts 3 -mat_partitioning_nparts 10 -N 100 -test_vertex_weights 1 -mat_partitioning_use_edge_weights 1
131 
132    test:
133       suffix: 7
134       nsize: 2
135       requires: parmetis
136       args: -mat_partitioning_type hierarch -mat_partitioning_hierarchical_nfineparts 2 -mat_partitioning_nparts 10  -mat_partitioning_hierarchical_fineparttype hierarch -malloc_dump -N 100 -mat_partitioning_improve 1
137 
138    test:
139       suffix: 8
140       nsize: 2
141       requires: parmetis
142       args: -mat_partitioning_type parmetis -mat_partitioning_nparts 3 -test_use_edge_weights 1
143 
144    test:
145       suffix: 9
146       nsize: 2
147       requires: ptscotch
148       args: -mat_partitioning_type ptscotch -mat_partitioning_nparts 3 -test_use_edge_weights 1 -mat_partitioning_ptscotch_proc_weight 0
149 
150 TEST*/
151