Actual source code: ex193.c

  1: /*
  2:  * ex193.c
  3:  *
  4:  *  Created on: Jul 29, 2015
  5:  *      Author: Fande Kong fdkong.jd@gmail.com
  6:  */
  7: /*
  8:  * An example demonstrates how to use hierarchical partitioning approach
  9:  */

 11: #include <petscmat.h>

 13: static char help[] = "Illustrates use of hierarchical partitioning.\n";

 15: int main(int argc, char **args)
 16: {
 17:   Mat             A;    /* matrix */
 18:   PetscInt        m, n; /* mesh dimensions in x- and y- directions */
 19:   PetscInt        i, j, Ii, J, Istart, Iend;
 20:   PetscMPIInt     size;
 21:   PetscScalar     v;
 22:   MatPartitioning part;
 23:   IS              coarseparts, fineparts;
 24:   IS              is, isn, isrows;
 25:   MPI_Comm        comm;

 28:   PetscInitialize(&argc, &args, (char *)0, help);
 29:   comm = PETSC_COMM_WORLD;
 30:   MPI_Comm_size(comm, &size);
 31:   PetscOptionsBegin(comm, NULL, "ex193", "hierarchical partitioning");
 32:   m = 15;
 33:   PetscOptionsInt("-M", "Number of mesh points in the x-direction", "partitioning", m, &m, NULL);
 34:   n = 15;
 35:   PetscOptionsInt("-N", "Number of mesh points in the y-direction", "partitioning", n, &n, NULL);
 36:   PetscOptionsEnd();

 38:   /*
 39:      Assemble the matrix for the five point stencil (finite difference), YET AGAIN
 40:   */
 41:   MatCreate(comm, &A);
 42:   MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, m * n, m * n);
 43:   MatSetFromOptions(A);
 44:   MatSetUp(A);
 45:   MatGetOwnershipRange(A, &Istart, &Iend);
 46:   for (Ii = Istart; Ii < Iend; Ii++) {
 47:     v = -1.0;
 48:     i = Ii / n;
 49:     j = Ii - i * n;
 50:     if (i > 0) {
 51:       J = Ii - n;
 52:       MatSetValues(A, 1, &Ii, 1, &J, &v, INSERT_VALUES);
 53:     }
 54:     if (i < m - 1) {
 55:       J = Ii + n;
 56:       MatSetValues(A, 1, &Ii, 1, &J, &v, INSERT_VALUES);
 57:     }
 58:     if (j > 0) {
 59:       J = Ii - 1;
 60:       MatSetValues(A, 1, &Ii, 1, &J, &v, INSERT_VALUES);
 61:     }
 62:     if (j < n - 1) {
 63:       J = Ii + 1;
 64:       MatSetValues(A, 1, &Ii, 1, &J, &v, INSERT_VALUES);
 65:     }
 66:     v = 4.0;
 67:     MatSetValues(A, 1, &Ii, 1, &Ii, &v, INSERT_VALUES);
 68:   }
 69:   MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
 70:   MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
 71:   MatView(A, PETSC_VIEWER_STDOUT_WORLD);
 72:   /*
 73:    Partition the graph of the matrix
 74:   */
 75:   MatPartitioningCreate(comm, &part);
 76:   MatPartitioningSetAdjacency(part, A);
 77:   MatPartitioningSetType(part, MATPARTITIONINGHIERARCH);
 78:   MatPartitioningHierarchicalSetNcoarseparts(part, 2);
 79:   MatPartitioningHierarchicalSetNfineparts(part, 4);
 80:   MatPartitioningSetFromOptions(part);
 81:   /* get new processor owner number of each vertex */
 82:   MatPartitioningApply(part, &is);
 83:   /* coarse parts */
 84:   MatPartitioningHierarchicalGetCoarseparts(part, &coarseparts);
 85:   ISView(coarseparts, PETSC_VIEWER_STDOUT_WORLD);
 86:   /* fine parts */
 87:   MatPartitioningHierarchicalGetFineparts(part, &fineparts);
 88:   ISView(fineparts, PETSC_VIEWER_STDOUT_WORLD);
 89:   /* partitioning */
 90:   ISView(is, PETSC_VIEWER_STDOUT_WORLD);
 91:   /* get new global number of each old global number */
 92:   ISPartitioningToNumbering(is, &isn);
 93:   ISView(isn, PETSC_VIEWER_STDOUT_WORLD);
 94:   ISBuildTwoSided(is, NULL, &isrows);
 95:   ISView(isrows, PETSC_VIEWER_STDOUT_WORLD);
 96:   ISDestroy(&is);
 97:   ISDestroy(&coarseparts);
 98:   ISDestroy(&fineparts);
 99:   ISDestroy(&isrows);
100:   ISDestroy(&isn);
101:   MatPartitioningDestroy(&part);
102:   MatDestroy(&A);
103:   PetscFinalize();
104:   return 0;
105: }

107: /*TEST

109:    test:
110:       nsize: 4
111:       args: -mat_partitioning_hierarchical_Nfineparts 2
112:       requires: parmetis
113:       TODO: cannot run because parmetis does reproduce across all machines, probably due to nonportable random number generator

115: TEST*/