Actual source code: ex15.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  1: static char help[] = "Example of using graph partitioning to segment an image\n\n";

  3: /*T
  4:    Concepts: Mat^mat partitioning
  5:    Concepts: Mat^image segmentation
  6:    Processors: n
  7: T*/

  9: #include <petscmat.h>

 13: int main(int argc, char **args)
 14: {
 15:   Mat             A;
 16:   MatPartitioning part;
 17:   IS              is;
 18:   PetscInt        r,N = 10, start, end;
 19:   PetscErrorCode  ierr;

 21:   PetscInitialize(&argc, &args, (char*) 0, help);
 22:   PetscOptionsGetInt(NULL, "-N", &N, NULL);
 23:   MatCreate(PETSC_COMM_WORLD, &A);
 24:   MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, N, N);
 25:   MatSetFromOptions(A);
 26:   MatSeqAIJSetPreallocation(A, 3, NULL);
 27:   MatMPIAIJSetPreallocation(A, 3, NULL, 2, NULL);

 29:   /* Create a linear mesh */
 30:   MatGetOwnershipRange(A, &start, &end);
 31:   for (r = start; r < end; ++r) {
 32:     if (r == 0) {
 33:       PetscInt    cols[2];
 34:       PetscScalar vals[2];

 36:       cols[0] = r;   cols[1] = r+1;
 37:       vals[0] = 1.0; vals[1] = 1.0;

 39:       MatSetValues(A, 1, &r, 2, cols, vals, INSERT_VALUES);
 40:     } else if (r == N-1) {
 41:       PetscInt    cols[2];
 42:       PetscScalar vals[2];

 44:       cols[0] = r-1; cols[1] = r;
 45:       vals[0] = 1.0; vals[1] = 1.0;

 47:       MatSetValues(A, 1, &r, 2, cols, vals, INSERT_VALUES);
 48:     } else {
 49:       PetscInt    cols[3];
 50:       PetscScalar vals[3];

 52:       cols[0] = r-1; cols[1] = r;   cols[2] = r+1;
 53:       vals[0] = 1.0; vals[1] = 1.0; vals[2] = 1.0;

 55:       MatSetValues(A, 1, &r, 3, cols, vals, INSERT_VALUES);
 56:     }
 57:   }
 58:   MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
 59:   MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);

 61:   MatPartitioningCreate(PETSC_COMM_WORLD, &part);
 62:   MatPartitioningSetAdjacency(part, A);
 63:   MatPartitioningSetFromOptions(part);
 64:   /*MatPartitioningSetVertexWeights(part, const PetscInt weights[]);*/
 65:   /*MatPartitioningSetPartitionWeights(part,const PetscReal weights[]);*/
 66:   MatPartitioningApply(part, &is);
 67:   ISView(is, PETSC_VIEWER_STDOUT_WORLD);
 68:   ISDestroy(&is);
 69:   MatPartitioningDestroy(&part);

 71:   MatDestroy(&A);
 72:   PetscFinalize();
 73:   return 0;
 74: }