Actual source code: ex73.c

  2: static char help[] = "Reads a PETSc matrix from a file partitions it\n\n";

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

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

 21: int main(int argc,char **args)
 22: {
 23:   const MatType   mtype = MATMPIAIJ; /* matrix format */
 24:   Mat             A,B;               /* matrix */
 25:   PetscViewer     fd;                /* viewer */
 26:   char            file[PETSC_MAX_PATH_LEN];         /* input file name */
 27:   PetscTruth      flg;
 28:   PetscInt        ierr,*nlocal;
 29:   PetscMPIInt     rank,size;
 30:   MatPartitioning part;
 31:   IS              is,isn;

 33:   PetscInitialize(&argc,&args,(char *)0,help);
 34:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 35:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 37:   /* 
 38:      Determine file from which we read the matrix
 39:   */
 40:   PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN-1,&flg);

 42:   /* 
 43:        Open binary file.  Note that we use PETSC_FILE_RDONLY to indicate
 44:        reading from this file.
 45:   */
 46:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,PETSC_FILE_RDONLY,&fd);

 48:   /*
 49:       Load the matrix and vector; then destroy the viewer.
 50:   */
 51:   MatLoad(fd,mtype,&A);
 52:   PetscViewerDestroy(fd);

 54:   MatView(A,PETSC_VIEWER_DRAW_WORLD);

 56:   /*
 57:        Partition the graph of the matrix 
 58:   */
 59:   MatPartitioningCreate(PETSC_COMM_WORLD,&part);
 60:   MatPartitioningSetAdjacency(part,A);
 61:   MatPartitioningSetFromOptions(part);
 62:   /* get new processor owner number of each vertex */
 63:   MatPartitioningApply(part,&is);
 64:   /* get new global number of each old global number */
 65:   ISPartitioningToNumbering(is,&isn);
 66:   PetscMalloc(size*sizeof(PetscInt),&nlocal);
 67:   /* get number of new vertices for each processor */
 68:   ISPartitioningCount(is,nlocal);
 69:   ISDestroy(is);

 71:   /* get old global number of each new global number */
 72:   ISInvertPermutation(isn,nlocal[rank],&is);
 73:   PetscFree(nlocal);
 74:   ISDestroy(isn);
 75:   MatPartitioningDestroy(part);

 77:   ISSort(is);
 78:   ISAllGather(is,&isn);


 81:   MatGetSubMatrix(A,is,isn,PETSC_DECIDE,MAT_INITIAL_MATRIX,&B);
 82:   ISDestroy(is);
 83:   ISDestroy(isn);

 85:   MatView(B,PETSC_VIEWER_DRAW_WORLD);

 87:   /*
 88:        Free work space.  All PETSc objects should be destroyed when they
 89:        are no longer needed.
 90:   */
 91:   MatDestroy(A);
 92:   MatDestroy(B);


 95:   PetscFinalize();
 96:   return 0;
 97: }