Actual source code: ex15.c
1: static char help[] = "Example of using graph partitioning to partition a graph\n\n";
3: /*T
4: Concepts: Mat^mat partitioning
5: Concepts: Mat^image segmentation
6: Processors: n
7: T*/
9: #include <petscmat.h>
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;
21: PetscInitialize(&argc, &args, (char*) 0, help);
22: comm = PETSC_COMM_WORLD;
23: PetscOptionsGetInt(NULL,NULL, "-N", &N, NULL);
24: MPI_Comm_rank(comm,&rank);
25: MatCreate(comm, &A);
26: MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, N, N);
27: MatSetFromOptions(A);
28: MatSeqAIJSetPreallocation(A, 3, NULL);
29: MatMPIAIJSetPreallocation(A, 3, NULL, 2, NULL);
30: PetscOptionsGetBool(NULL,NULL,"-test_vertex_weights",&set_vweights,NULL);
31: PetscOptionsGetBool(NULL,NULL,"-test_use_edge_weights",&use_edge_weights,NULL);
32: /* Create a linear mesh */
33: MatGetOwnershipRange(A, &start, &end);
34: if (set_vweights) {
35: PetscMalloc1(end-start,&vweights);
36: for (r = start; r < end; ++r)
37: vweights[r-start] = rank+1;
38: }
39: for (r = start; r < end; ++r) {
40: if (r == 0) {
41: PetscInt cols[2];
42: PetscScalar vals[2];
44: cols[0] = r; cols[1] = r+1;
45: vals[0] = 1.0; vals[1] = use_edge_weights? 2.0: 1.0;
47: MatSetValues(A, 1, &r, 2, cols, vals, INSERT_VALUES);
48: } else if (r == N-1) {
49: PetscInt cols[2];
50: PetscScalar vals[2];
52: cols[0] = r-1; cols[1] = r;
53: vals[0] = use_edge_weights? 3.0:1.0; vals[1] = 1.0;
55: MatSetValues(A, 1, &r, 2, cols, vals, INSERT_VALUES);
56: } else {
57: PetscInt cols[3];
58: PetscScalar vals[3];
60: cols[0] = r-1; cols[1] = r; cols[2] = r+1;
61: /* ADJ matrix needs to be symmetric */
62: vals[0] = use_edge_weights? (cols[0]==0? 2.0:5.0):1.0;
63: vals[1] = 1.0;
64: vals[2] = use_edge_weights? (cols[2]==N-1? 3.0:5.0):1.0;
66: MatSetValues(A, 1, &r, 3, cols, vals, INSERT_VALUES);
67: }
68: }
69: MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
70: MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
72: MatPartitioningCreate(comm, &part);
73: MatPartitioningSetAdjacency(part, A);
74: if (set_vweights) {
75: MatPartitioningSetVertexWeights(part,vweights);
76: }
77: if (use_edge_weights) {
78: MatPartitioningSetUseEdgeWeights(part,use_edge_weights);
80: MatPartitioningGetUseEdgeWeights(part,&use_edge_weights);
82: }
83: MatPartitioningSetFromOptions(part);
84: MatPartitioningApply(part, &is);
85: ISView(is, PETSC_VIEWER_STDOUT_WORLD);
86: ISDestroy(&is);
87: MatPartitioningDestroy(&part);
89: MatDestroy(&A);
90: PetscFinalize();
91: return 0;
92: }
94: /*TEST
96: test:
97: nsize: 3
98: requires: parmetis
99: args: -mat_partitioning_type parmetis
101: test:
102: suffix: 2
103: nsize: 3
104: requires: ptscotch
105: args: -mat_partitioning_type ptscotch
107: test:
108: suffix: 3
109: nsize: 4
110: requires: party
111: args: -mat_partitioning_type party
113: test:
114: suffix: 4
115: nsize: 3
116: requires: chaco
117: args: -mat_partitioning_type chaco
119: test:
120: suffix: 5
121: nsize: 3
122: requires: parmetis
123: args: -mat_partitioning_type hierarch -mat_partitioning_hierarchical_nfineparts 3 -mat_partitioning_nparts 10 -N 100
125: test:
126: suffix: 6
127: nsize: 3
128: requires: parmetis
129: 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: test:
132: suffix: 7
133: nsize: 2
134: requires: parmetis
135: 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: test:
138: suffix: 8
139: nsize: 2
140: requires: parmetis
141: args: -mat_partitioning_type parmetis -mat_partitioning_nparts 3 -test_use_edge_weights 1
143: test:
144: suffix: 9
145: nsize: 2
146: requires: ptscotch
147: args: -mat_partitioning_type ptscotch -mat_partitioning_nparts 3 -test_use_edge_weights 1 -mat_partitioning_ptscotch_proc_weight 0
149: TEST*/