Actual source code: ex80.c

petsc-3.7.7 2017-09-25
Report Typos and Errors
  2: static char help[] = "Partition tiny grid.\n\n";

  4: /*T
  5:    Concepts: partitioning
  6:    Processors: 4
  7: T*/

  9: /*
 10:   Include "petscmat.h" so that we can use matrices.  Note that this file
 11:   automatically includes:
 12:      petscsys.h       - base PETSc routines   petscvec.h - vectors
 13:      petscmat.h - matrices
 14:      petscis.h     - index sets
 15:      petscviewer.h - viewers
 16: */
 17: #include <petscmat.h>

 21: int main(int argc,char **args)
 22: {
 23:   Mat             A;
 24:   PetscErrorCode  ierr;
 25:   PetscMPIInt     rank,size;
 26:   PetscInt        *ia,*ja;
 27:   MatPartitioning part;
 28:   IS              is,isn;

 30:   PetscInitialize(&argc,&args,(char*)0,help);
 31:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 32:   if (size != 4) SETERRQ(PETSC_COMM_WORLD,1,"Must run with 4 processors");
 33:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 35:   PetscMalloc1(5,&ia);
 36:   PetscMalloc1(16,&ja);
 37:   if (!rank) {
 38:     ja[0] = 1; ja[1] = 4; ja[2] = 0; ja[3] = 2; ja[4] = 5; ja[5] = 1; ja[6] = 3; ja[7] = 6;
 39:     ja[8] = 2; ja[9] = 7;
 40:     ia[0] = 0; ia[1] = 2; ia[2] = 5; ia[3] = 8; ia[4] = 10;
 41:   } else if (rank == 1) {
 42:     ja[0] = 0; ja[1] = 5; ja[2] = 8; ja[3] = 1; ja[4] = 4; ja[5] = 6; ja[6] = 9; ja[7] = 2;
 43:     ja[8] = 5; ja[9] = 7; ja[10] = 10; ja[11] = 3; ja[12] = 6; ja[13] = 11;
 44:     ia[0] = 0; ia[1] = 3; ia[2] = 7; ia[3] = 11; ia[4] = 14;
 45:   } else if (rank == 2) {
 46:     ja[0] = 4; ja[1] = 9; ja[2] = 12; ja[3] = 5; ja[4] = 8; ja[5] = 10; ja[6] = 13; ja[7] = 6;
 47:     ja[8] = 9; ja[9] = 11; ja[10] = 14; ja[11] = 7; ja[12] = 10; ja[13] = 15;
 48:     ia[0] = 0; ia[1] = 3; ia[2] = 7; ia[3] = 11; ia[4] = 14;
 49:   } else {
 50:     ja[0] = 8; ja[1] = 13; ja[2] = 9; ja[3] = 12; ja[4] = 14; ja[5] = 10; ja[6] = 13; ja[7] = 15;
 51:     ja[8] = 11; ja[9] = 14;
 52:     ia[0] = 0; ia[1] = 2; ia[2] = 5; ia[3] = 8; ia[4] = 10;
 53:   }

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

 58:   /*
 59:        Partition the graph of the matrix
 60:   */
 61:   MatPartitioningCreate(PETSC_COMM_WORLD,&part);
 62:   MatPartitioningSetAdjacency(part,A);
 63:   MatPartitioningSetFromOptions(part);
 64:   /* get new processor owner number of each vertex */
 65:   MatPartitioningApply(part,&is);
 66:   /* get new global number of each old global number */
 67:   ISPartitioningToNumbering(is,&isn);
 68:   ISView(isn,PETSC_VIEWER_STDOUT_WORLD);
 69:   ISDestroy(&is);

 71:   ISDestroy(&isn);
 72:   MatPartitioningDestroy(&part);

 74:   /*
 75:        Free work space.  All PETSc objects should be destroyed when they
 76:        are no longer needed.
 77:   */
 78:   MatDestroy(&A);


 81:   PetscFinalize();
 82:   return 0;
 83: }