Actual source code: ex80.c


  2: static char help[] = "Partition tiny grid.\n\n";

  4: /*
  5:   Include "petscmat.h" so that we can use matrices.  Note that this file
  6:   automatically includes:
  7:      petscsys.h       - base PETSc routines   petscvec.h - vectors
  8:      petscmat.h - matrices
  9:      petscis.h     - index sets
 10:      petscviewer.h - viewers
 11: */
 12: #include <petscmat.h>

 14: int main(int argc,char **args)
 15: {
 16:   Mat             A;
 17:   PetscMPIInt     rank,size;
 18:   PetscInt        *ia,*ja;
 19:   MatPartitioning part;
 20:   IS              is,isn;

 22:   PetscInitialize(&argc,&args,(char*)0,help);
 23:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 25:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 27:   PetscMalloc1(5,&ia);
 28:   PetscMalloc1(16,&ja);
 29:   if (rank == 0) {
 30:     ja[0] = 1; ja[1] = 4; ja[2] = 0; ja[3] = 2; ja[4] = 5; ja[5] = 1; ja[6] = 3; ja[7] = 6;
 31:     ja[8] = 2; ja[9] = 7;
 32:     ia[0] = 0; ia[1] = 2; ia[2] = 5; ia[3] = 8; ia[4] = 10;
 33:   } else if (rank == 1) {
 34:     ja[0] = 0; ja[1] = 5; ja[2] = 8; ja[3] = 1; ja[4] = 4; ja[5] = 6; ja[6] = 9; ja[7] = 2;
 35:     ja[8] = 5; ja[9] = 7; ja[10] = 10; ja[11] = 3; ja[12] = 6; ja[13] = 11;
 36:     ia[0] = 0; ia[1] = 3; ia[2] = 7; ia[3] = 11; ia[4] = 14;
 37:   } else if (rank == 2) {
 38:     ja[0] = 4; ja[1] = 9; ja[2] = 12; ja[3] = 5; ja[4] = 8; ja[5] = 10; ja[6] = 13; ja[7] = 6;
 39:     ja[8] = 9; ja[9] = 11; ja[10] = 14; ja[11] = 7; ja[12] = 10; ja[13] = 15;
 40:     ia[0] = 0; ia[1] = 3; ia[2] = 7; ia[3] = 11; ia[4] = 14;
 41:   } else {
 42:     ja[0] = 8; ja[1] = 13; ja[2] = 9; ja[3] = 12; ja[4] = 14; ja[5] = 10; ja[6] = 13; ja[7] = 15;
 43:     ja[8] = 11; ja[9] = 14;
 44:     ia[0] = 0; ia[1] = 2; ia[2] = 5; ia[3] = 8; ia[4] = 10;
 45:   }

 47:   MatCreateMPIAdj(PETSC_COMM_WORLD,4,16,ia,ja,NULL,&A);
 48:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);

 50:   /*
 51:        Partition the graph of the matrix
 52:   */
 53:   MatPartitioningCreate(PETSC_COMM_WORLD,&part);
 54:   MatPartitioningSetAdjacency(part,A);
 55:   MatPartitioningSetFromOptions(part);
 56:   /* get new processor owner number of each vertex */
 57:   MatPartitioningApply(part,&is);
 58:   /* get new global number of each old global number */
 59:   ISPartitioningToNumbering(is,&isn);
 60:   ISView(isn,PETSC_VIEWER_STDOUT_WORLD);
 61:   ISDestroy(&is);

 63:   ISDestroy(&isn);
 64:   MatPartitioningDestroy(&part);

 66:   /*
 67:        Free work space.  All PETSc objects should be destroyed when they
 68:        are no longer needed.
 69:   */
 70:   MatDestroy(&A);

 72:   PetscFinalize();
 73:   return 0;
 74: }