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 itnn";

  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 petscsles.h

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

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

 35:   /* 
 36:      Determine file from which we read the matrix
 37:   */
 38:   PetscOptionsGetString(PETSC_NULL,"-f",file,127,&flg);

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

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

 52:   MatView(A,PETSC_VIEWER_DRAW_WORLD);

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

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

 75:   ISSort(is);
 76:   ISAllGather(is,&isn);


 79:   MatGetSubMatrix(A,is,isn,PETSC_DECIDE,MAT_INITIAL_MATRIX,&B);
 80:   ISDestroy(is);
 81:   ISDestroy(isn);

 83:   MatView(B,PETSC_VIEWER_DRAW_WORLD);

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


 93:   PetscFinalize();
 94:   return 0;
 95: }