xref: /petsc/src/mat/tests/ex131.c (revision df4cd43f92eaa320656440c40edb1046daee8f75)
1 
2 static char help[] = "Tests MatMult() on MatLoad() matrix \n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc, char **args)
7 {
8   Mat         A;
9   Vec         x, b;
10   PetscViewer fd;                       /* viewer */
11   char        file[PETSC_MAX_PATH_LEN]; /* input file name */
12   PetscBool   flg;
13 
14   PetscFunctionBeginUser;
15   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
16   /* Determine file from which we read the matrix A */
17   PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
18   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -f option");
19 
20   /* Load matrix A */
21   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));
22   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
23   PetscCall(MatLoad(A, fd));
24   flg = PETSC_FALSE;
25   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
26   PetscCall(PetscOptionsGetString(NULL, NULL, "-vec", file, sizeof(file), &flg));
27   if (flg) {
28     if (file[0] == '0') {
29       PetscInt    m;
30       PetscScalar one = 1.0;
31       PetscCall(PetscInfo(0, "Using vector of ones for RHS\n"));
32       PetscCall(MatGetLocalSize(A, &m, NULL));
33       PetscCall(VecSetSizes(x, m, PETSC_DECIDE));
34       PetscCall(VecSetFromOptions(x));
35       PetscCall(VecSet(x, one));
36     }
37   } else {
38     PetscCall(VecLoad(x, fd));
39     PetscCall(PetscViewerDestroy(&fd));
40   }
41   PetscCall(VecDuplicate(x, &b));
42   PetscCall(MatMult(A, x, b));
43 
44   /* Print (for testing only) */
45   PetscCall(MatView(A, 0));
46   PetscCall(VecView(b, 0));
47   /* Free data structures */
48   PetscCall(MatDestroy(&A));
49   PetscCall(VecDestroy(&x));
50   PetscCall(VecDestroy(&b));
51   PetscCall(PetscFinalize());
52   return 0;
53 }
54