Actual source code: ex1.c

petsc-3.3-p7 2013-05-11
  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:   const MatOrderingType rtype = MATORDERINGRCM;
 33:   PetscBool             flg,PetscPreLoad = PETSC_FALSE;

 35:   PetscInitialize(&argc,&args,(char *)0,help);


 38:   /* 
 39:      Determine files from which we read the two linear systems
 40:      (matrix and right-hand-side vector).
 41:   */
 42:   PetscOptionsGetString(PETSC_NULL,"-f0",file[0],PETSC_MAX_PATH_LEN,&flg);
 43:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f0 option");
 44:   PetscOptionsGetString(PETSC_NULL,"-f1",file[1],PETSC_MAX_PATH_LEN,&flg);
 45:   if (flg) PetscPreLoad = PETSC_TRUE;

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

 61:     /* - - - - - - - - - - - New Stage - - - - - - - - - - - - -
 62:                            Load system i
 63:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

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


 80:     /* - - - - - - - - - - - New Stage - - - - - - - - - - - - -
 81:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 83:     PetscPreLoadStage("Reordering");
 84:     MatGetOrdering(A,rtype,&isrow,&iscol);

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

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