Actual source code: ex15.c

petsc-3.14.6 2021-03-30
Report Typos and Errors
  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;
 20:   PetscErrorCode  ierr;

 22:   PetscInitialize(&argc, &args, (char*) 0, help);if (ierr) return ierr;
 23:   comm = PETSC_COMM_WORLD;
 24:   PetscOptionsGetInt(NULL,NULL, "-N", &N, NULL);
 25:   MPI_Comm_rank(comm,&rank);
 26:   MatCreate(comm, &A);
 27:   MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, N, N);
 28:   MatSetFromOptions(A);
 29:   MatSeqAIJSetPreallocation(A, 3, NULL);
 30:   MatMPIAIJSetPreallocation(A, 3, NULL, 2, NULL);
 31:   PetscOptionsGetBool(NULL,NULL,"-test_vertex_weights",&set_vweights,NULL);
 32:   PetscOptionsGetBool(NULL,NULL,"-test_use_edge_weights",&use_edge_weights,NULL);
 33:   /* Create a linear mesh */
 34:   MatGetOwnershipRange(A, &start, &end);
 35:   if (set_vweights) {
 36:     PetscMalloc1(end-start,&vweights);
 37:     for (r = start; r < end; ++r)
 38:       vweights[r-start] = rank+1;
 39:   }
 40:   for (r = start; r < end; ++r) {
 41:     if (r == 0) {
 42:       PetscInt    cols[2];
 43:       PetscScalar vals[2];

 45:       cols[0] = r;   cols[1] = r+1;
 46:       vals[0] = 1.0; vals[1] = use_edge_weights? 2.0: 1.0;

 48:       MatSetValues(A, 1, &r, 2, cols, vals, INSERT_VALUES);
 49:     } else if (r == N-1) {
 50:       PetscInt    cols[2];
 51:       PetscScalar vals[2];

 53:       cols[0] = r-1; cols[1] = r;
 54:       vals[0] = use_edge_weights? 3.0:1.0; vals[1] = 1.0;

 56:       MatSetValues(A, 1, &r, 2, cols, vals, INSERT_VALUES);
 57:     } else {
 58:       PetscInt    cols[3];
 59:       PetscScalar vals[3];

 61:       cols[0] = r-1; cols[1] = r;   cols[2] = r+1;
 62:       /* ADJ matrix needs to be symmetric */
 63:       vals[0] = use_edge_weights? (cols[0]==0? 2.0:5.0):1.0;
 64:       vals[1] = 1.0;
 65:       vals[2] = use_edge_weights? (cols[2]==N-1? 3.0:5.0):1.0;

 67:       MatSetValues(A, 1, &r, 3, cols, vals, INSERT_VALUES);
 68:     }
 69:   }
 70:   MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
 71:   MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);

 73:   MatPartitioningCreate(comm, &part);
 74:   MatPartitioningSetAdjacency(part, A);
 75:   if (set_vweights) {
 76:     MatPartitioningSetVertexWeights(part,vweights);
 77:   }
 78:   if (use_edge_weights) {
 79:     MatPartitioningSetUseEdgeWeights(part,use_edge_weights);

 81:     MatPartitioningGetUseEdgeWeights(part,&use_edge_weights);
 82:     if (!use_edge_weights) SETERRQ(comm,PETSC_ERR_ARG_INCOMP, "use_edge_weights flag does not setup correctly \n");
 83:   }
 84:   MatPartitioningSetFromOptions(part);
 85:   MatPartitioningApply(part, &is);
 86:   ISView(is, PETSC_VIEWER_STDOUT_WORLD);
 87:   ISDestroy(&is);
 88:   MatPartitioningDestroy(&part);

 90:   MatDestroy(&A);
 91:   PetscFinalize();
 92:   return ierr;
 93: }


 96: /*TEST

 98:    test:
 99:       nsize: 3
100:       requires: parmetis
101:       args: -mat_partitioning_type parmetis

103:    test:
104:       suffix: 2
105:       nsize: 3
106:       requires: ptscotch
107:       args: -mat_partitioning_type ptscotch

109:    test:
110:       suffix: 3
111:       nsize: 4
112:       requires: party
113:       args: -mat_partitioning_type party

115:    test:
116:       suffix: 4
117:       nsize: 3
118:       requires: chaco
119:       args: -mat_partitioning_type chaco

121:    test:
122:       suffix: 5
123:       nsize: 3
124:       requires: parmetis
125:       args: -mat_partitioning_type hierarch -mat_partitioning_hierarchical_nfineparts 3 -mat_partitioning_nparts 10 -N 100

127:    test:
128:       suffix: 6
129:       nsize: 3
130:       requires: parmetis
131:       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

133:    test:
134:       suffix: 7
135:       nsize: 2
136:       requires: parmetis
137:       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

139:    test:
140:       suffix: 8
141:       nsize: 2
142:       requires: parmetis
143:       args: -mat_partitioning_type parmetis -mat_partitioning_nparts 3 -test_use_edge_weights 1

145:    test:
146:       suffix: 9
147:       nsize: 2
148:       requires: ptscotch
149:       args: -mat_partitioning_type ptscotch -mat_partitioning_nparts 3 -test_use_edge_weights 1 -mat_partitioning_ptscotch_proc_weight 0

151: TEST*/