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 21 PetscCall(PetscInitialize(&argc, &args, (char*) 0, help)); 22 comm = PETSC_COMM_WORLD; 23 PetscCall(PetscOptionsGetInt(NULL,NULL, "-N", &N, NULL)); 24 PetscCallMPI(MPI_Comm_rank(comm,&rank)); 25 PetscCall(MatCreate(comm, &A)); 26 PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, N, N)); 27 PetscCall(MatSetFromOptions(A)); 28 PetscCall(MatSeqAIJSetPreallocation(A, 3, NULL)); 29 PetscCall(MatMPIAIJSetPreallocation(A, 3, NULL, 2, NULL)); 30 PetscCall(PetscOptionsGetBool(NULL,NULL,"-test_vertex_weights",&set_vweights,NULL)); 31 PetscCall(PetscOptionsGetBool(NULL,NULL,"-test_use_edge_weights",&use_edge_weights,NULL)); 32 /* Create a linear mesh */ 33 PetscCall(MatGetOwnershipRange(A, &start, &end)); 34 if (set_vweights) { 35 PetscCall(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]; 43 44 cols[0] = r; cols[1] = r+1; 45 vals[0] = 1.0; vals[1] = use_edge_weights? 2.0: 1.0; 46 47 PetscCall(MatSetValues(A, 1, &r, 2, cols, vals, INSERT_VALUES)); 48 } else if (r == N-1) { 49 PetscInt cols[2]; 50 PetscScalar vals[2]; 51 52 cols[0] = r-1; cols[1] = r; 53 vals[0] = use_edge_weights? 3.0:1.0; vals[1] = 1.0; 54 55 PetscCall(MatSetValues(A, 1, &r, 2, cols, vals, INSERT_VALUES)); 56 } else { 57 PetscInt cols[3]; 58 PetscScalar vals[3]; 59 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; 65 66 PetscCall(MatSetValues(A, 1, &r, 3, cols, vals, INSERT_VALUES)); 67 } 68 } 69 PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 70 PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 71 72 PetscCall(MatPartitioningCreate(comm, &part)); 73 PetscCall(MatPartitioningSetAdjacency(part, A)); 74 if (set_vweights) { 75 PetscCall(MatPartitioningSetVertexWeights(part,vweights)); 76 } 77 if (use_edge_weights) { 78 PetscCall(MatPartitioningSetUseEdgeWeights(part,use_edge_weights)); 79 80 PetscCall(MatPartitioningGetUseEdgeWeights(part,&use_edge_weights)); 81 PetscCheck(use_edge_weights,comm,PETSC_ERR_ARG_INCOMP, "use_edge_weights flag does not setup correctly "); 82 } 83 PetscCall(MatPartitioningSetFromOptions(part)); 84 PetscCall(MatPartitioningApply(part, &is)); 85 PetscCall(ISView(is, PETSC_VIEWER_STDOUT_WORLD)); 86 PetscCall(ISDestroy(&is)); 87 PetscCall(MatPartitioningDestroy(&part)); 88 89 PetscCall(MatDestroy(&A)); 90 PetscCall(PetscFinalize()); 91 return 0; 92 } 93 94 /*TEST 95 96 test: 97 nsize: 3 98 requires: parmetis 99 args: -mat_partitioning_type parmetis 100 101 test: 102 suffix: 2 103 nsize: 3 104 requires: ptscotch 105 args: -mat_partitioning_type ptscotch 106 107 test: 108 suffix: 3 109 nsize: 4 110 requires: party 111 args: -mat_partitioning_type party 112 113 test: 114 suffix: 4 115 nsize: 3 116 requires: chaco 117 args: -mat_partitioning_type chaco 118 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 124 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 130 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 136 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 142 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 148 149 TEST*/ 150