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