Actual source code: ex131.c

  1: static char help[] = "Tests MatMult() on MatLoad() matrix \n\n";

  3: #include <petscmat.h>

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

 13:   PetscFunctionBeginUser;
 14:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 15:   /* Determine file from which we read the matrix A */
 16:   PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
 17:   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -f option");

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

 43:   /* Print (for testing only) */
 44:   PetscCall(MatView(A, 0));
 45:   PetscCall(VecView(b, 0));
 46:   /* Free data structures */
 47:   PetscCall(MatDestroy(&A));
 48:   PetscCall(VecDestroy(&x));
 49:   PetscCall(VecDestroy(&b));
 50:   PetscCall(PetscFinalize());
 51:   return 0;
 52: }