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) PetscCall(MatPartitioningSetVertexWeights(part,vweights)); 69 if (use_edge_weights) { 70 PetscCall(MatPartitioningSetUseEdgeWeights(part,use_edge_weights)); 71 72 PetscCall(MatPartitioningGetUseEdgeWeights(part,&use_edge_weights)); 73 PetscCheck(use_edge_weights,comm,PETSC_ERR_ARG_INCOMP, "use_edge_weights flag does not setup correctly "); 74 } 75 PetscCall(MatPartitioningSetFromOptions(part)); 76 PetscCall(MatPartitioningApply(part, &is)); 77 PetscCall(ISView(is, PETSC_VIEWER_STDOUT_WORLD)); 78 PetscCall(ISDestroy(&is)); 79 PetscCall(MatPartitioningDestroy(&part)); 80 81 PetscCall(MatDestroy(&A)); 82 PetscCall(PetscFinalize()); 83 return 0; 84 } 85 86 /*TEST 87 88 test: 89 nsize: 3 90 requires: parmetis 91 args: -mat_partitioning_type parmetis 92 93 test: 94 suffix: 2 95 nsize: 3 96 requires: ptscotch 97 args: -mat_partitioning_type ptscotch 98 99 test: 100 suffix: 3 101 nsize: 4 102 requires: party 103 args: -mat_partitioning_type party 104 105 test: 106 suffix: 4 107 nsize: 3 108 requires: chaco 109 args: -mat_partitioning_type chaco 110 111 test: 112 suffix: 5 113 nsize: 3 114 requires: parmetis 115 args: -mat_partitioning_type hierarch -mat_partitioning_hierarchical_nfineparts 3 -mat_partitioning_nparts 10 -N 100 116 117 test: 118 suffix: 6 119 nsize: 3 120 requires: parmetis 121 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 122 123 test: 124 suffix: 7 125 nsize: 2 126 requires: parmetis 127 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 128 129 test: 130 suffix: 8 131 nsize: 2 132 requires: parmetis 133 args: -mat_partitioning_type parmetis -mat_partitioning_nparts 3 -test_use_edge_weights 1 134 135 test: 136 suffix: 9 137 nsize: 2 138 requires: ptscotch 139 args: -mat_partitioning_type ptscotch -mat_partitioning_nparts 3 -test_use_edge_weights 1 -mat_partitioning_ptscotch_proc_weight 0 140 141 TEST*/ 142