Actual source code: ex15.c

petsc-master 2019-05-18
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;
 18:   PetscMPIInt     rank;
 19:   PetscErrorCode  ierr;

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

 42:       cols[0] = r;   cols[1] = r+1;
 43:       vals[0] = 1.0; vals[1] = 1.0;

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

 50:       cols[0] = r-1; cols[1] = r;
 51:       vals[0] = 1.0; vals[1] = 1.0;

 53:       MatSetValues(A, 1, &r, 2, cols, vals, INSERT_VALUES);
 54:     } else {
 55:       PetscInt    cols[3];
 56:       PetscScalar vals[3];

 58:       cols[0] = r-1; cols[1] = r;   cols[2] = r+1;
 59:       vals[0] = 1.0; vals[1] = 1.0; vals[2] = 1.0;

 61:       MatSetValues(A, 1, &r, 3, cols, vals, INSERT_VALUES);
 62:     }
 63:   }
 64:   MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
 65:   MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);

 67:   MatPartitioningCreate(PETSC_COMM_WORLD, &part);
 68:   MatPartitioningSetAdjacency(part, A);
 69:   if (set_vweights) {
 70:     MatPartitioningSetVertexWeights(part,vweights);
 71:   }
 72:   MatPartitioningSetFromOptions(part);
 73:   MatPartitioningApply(part, &is);
 74:   ISView(is, PETSC_VIEWER_STDOUT_WORLD);
 75:   ISDestroy(&is);
 76:   MatPartitioningDestroy(&part);

 78:   MatDestroy(&A);
 79:   PetscFinalize();
 80:   return ierr;
 81: }


 84: /*TEST

 86:    test:
 87:       nsize: 3
 88:       requires: parmetis
 89:       args: -mat_partitioning_type parmetis

 91:    test:
 92:       suffix: 2
 93:       nsize: 3
 94:       requires: ptscotch
 95:       args: -mat_partitioning_type ptscotch

 97:    test:
 98:       suffix: 3
 99:       nsize: 4
100:       requires: party
101:       args: -mat_partitioning_type party

103:    test:
104:       suffix: 4
105:       nsize: 3
106:       requires: chaco
107:       args: -mat_partitioning_type chaco

109:    test:
110:       suffix: 5
111:       nsize: 3
112:       requires: parmetis
113:       args: -mat_partitioning_type hierarch -mat_partitioning_hierarchical_nfineparts 3 -mat_partitioning_nparts 10 -N 100

115:    test:
116:       suffix: 6
117:       nsize: 3
118:       requires: parmetis
119:       args: -mat_partitioning_type hierarch -mat_partitioning_hierarchical_nfineparts 3 -mat_partitioning_nparts 10 -N 100 -test_vertex_weights 1

121:    test:
122:       suffix: 7
123:       nsize: 2
124:       requires: parmetis
125:       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
126: TEST*/