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