Actual source code: ex1.c

petsc-3.10.5 2019-03-28
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>

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

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

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

 57:   /* - - - - - - - - - - - New Stage - - - - - - - - - - - - -
 58:                          Load system i
 59:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

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


 76:   /* - - - - - - - - - - - New Stage - - - - - - - - - - - - -
 77:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 79:   PetscPreLoadStage("Reordering");
 80:   MatGetOrdering(A,rtype,&isrow,&iscol);

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

 91:   PetscFinalize();
 92:   return ierr;
 93: }



 97: /*TEST

 99:    test:
100:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
101:       args: -f0 ${DATAFILESPATH}/matrices/medium -viewer_binary_skip_info
102:       TODO: Need to develop comparison test

104: TEST*/