Actual source code: ex1.c

petsc-master 2016-07-24
Report Typos and Errors
  2: static char help[] = "Reads a PETSc matrix and vector from a file and reorders it.\n\
  3:   -f0 <input_file> : first file to load (small system)\n\
  4:   -f1 <input_file> : second file to load (larger system)\n\n";

  6: /*T
  7:    Concepts: Mat^ordering a matrix - loading a binary matrix and vector;
  8:    Concepts: Mat^loading a binary matrix and vector;
  9:    Concepts: Vectors^loading a binary vector;
 10:    Concepts: PetscLog^preloading executable
 11:    Processors: 1
 12: T*/

 14: /*
 15:   Include "petscmat.h" so that we can use matrices.
 16:   automatically includes:
 17:      petscsys.h       - base PETSc routines   petscvec.h    - vectors
 18:      petscmat.h    - matrices
 19:      petscis.h     - index sets            petscviewer.h - viewers
 20: */
 21: #include <petscmat.h>

 25: int main(int argc,char **args)
 26: {
 27:   Mat             A;                      /* matrix */
 28:   PetscViewer     fd;                     /* viewer */
 29:   char            file[2][PETSC_MAX_PATH_LEN];           /* input file name */
 30:   IS              isrow,iscol;            /* row and column permutations */
 31:   PetscErrorCode  ierr;
 32:   MatOrderingType rtype = MATORDERINGRCM;
 33:   PetscBool       flg,PetscPreLoad = PETSC_FALSE;

 35:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 36:   /*
 37:      Determine files from which we read the two linear systems
 38:      (matrix and right-hand-side vector).
 39:   */
 40:   PetscOptionsGetString(NULL,NULL,"-f0",file[0],PETSC_MAX_PATH_LEN,&flg);
 41:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f0 option");
 42:   PetscOptionsGetString(NULL,NULL,"-f1",file[1],PETSC_MAX_PATH_LEN,&flg);
 43:   if (flg) PetscPreLoad = PETSC_TRUE;

 45:   /* -----------------------------------------------------------
 46:                   Beginning of loop
 47:      ----------------------------------------------------------- */
 48:   /*
 49:      Loop through the reordering 2 times.
 50:       - The intention here is to preload and solve a small system;
 51:         then load another (larger) system and solve it as well.
 52:         This process preloads the instructions with the smaller
 53:         system so that more accurate performance monitoring (via
 54:         -log_summary) can be done with the larger one (that actually
 55:         is the system of interest).
 56:   */
 57:   PetscPreLoadBegin(PetscPreLoad,"Load");

 59:   /* - - - - - - - - - - - New Stage - - - - - - - - - - - - -
 60:                          Load system i
 61:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 63:   /*
 64:      Open binary file.  Note that we use FILE_MODE_READ to indicate
 65:      reading from this file.
 66:   */
 67:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[PetscPreLoadIt],FILE_MODE_READ,&fd);

 69:   /*
 70:      Load the matrix; then destroy the viewer.
 71:   */
 72:   MatCreate(PETSC_COMM_WORLD,&A);
 73:   MatSetType(A,MATSEQAIJ);
 74:   MatLoad(A,fd);
 75:   PetscViewerDestroy(&fd);


 78:   /* - - - - - - - - - - - New Stage - - - - - - - - - - - - -
 79:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 81:   PetscPreLoadStage("Reordering");
 82:   MatGetOrdering(A,rtype,&isrow,&iscol);

 84:   /*
 85:      Free work space.  All PETSc objects should be destroyed when they
 86:      are no longer needed.
 87:   */
 88:   MatDestroy(&A);
 89:   ISDestroy(&isrow);
 90:   ISDestroy(&iscol);
 91:   PetscPreLoadEnd();

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