Actual source code: ex73.c

  1: /*$Id: ex73.c,v 1.11 2001/04/10 19:35:44 bsmith Exp $*/

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

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

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

 22: int main(int argc,char **args)
 23: {
 24:   MatType         mtype = MATMPIAIJ; /* matrix format */
 25:   Mat             A,B;               /* matrix */
 26:   PetscViewer     fd;                /* viewer */
 27:   char            file[128];         /* input file name */
 28:   PetscTruth      flg;
 29:   int             ierr,*nlocal,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,127,&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(int),&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: }