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