Actual source code: ex131.c

petsc-3.6.4 2016-04-12
Report Typos and Errors
  2: static char help[] = "Tests MatMult() on MatLoad() matrix \n\n";

  4: #include <petscmat.h>

  8: int main(int argc,char **args)
  9: {
 10:   Mat            A;
 11:   Vec            x,b;
 13:   PetscViewer    fd;              /* viewer */
 14:   char           file[PETSC_MAX_PATH_LEN]; /* input file name */
 15:   PetscBool      flg;

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

 19:   /* Determine file from which we read the matrix A */
 20:   PetscOptionsGetString(NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);
 21:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option");

 23:   /* Load matrix A */
 24:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
 25:   MatCreate(PETSC_COMM_WORLD,&A);
 26:   MatLoad(A,fd);
 27:   flg  = PETSC_FALSE;
 28:   VecCreate(PETSC_COMM_WORLD,&x);
 29:   PetscOptionsGetString(NULL,"-vec",file,PETSC_MAX_PATH_LEN,&flg);
 30:   if (flg) {
 31:     if (file[0] == '0') {
 32:       PetscInt    m;
 33:       PetscScalar one = 1.0;
 34:       PetscInfo(0,"Using vector of ones for RHS\n");
 35:       MatGetLocalSize(A,&m,NULL);
 36:       VecSetSizes(x,m,PETSC_DECIDE);
 37:       VecSetFromOptions(x);
 38:       VecSet(x,one);
 39:     }
 40:   } else {
 41:     VecLoad(x,fd);
 42:     PetscViewerDestroy(&fd);
 43:   }
 44:   VecDuplicate(x,&b);
 45:   MatMult(A,x,b);

 47:   /* Print (for testing only) */
 48:   MatView(A,0);
 49:   VecView(b,0);
 50:   /* Free data structures */
 51:   MatDestroy(&A);
 52:   VecDestroy(&x);
 53:   VecDestroy(&b);
 54:   PetscFinalize();
 55:   return 0;
 56: }