Actual source code: ex15.c

  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: }

 95: /*TEST

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

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

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

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

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

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

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

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

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

150: TEST*/