xref: /petsc/src/mat/tests/ex131.c (revision ffa8c5705e8ab2cf85ee1d14dbe507a6e2eb5283)
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   PetscCall(PetscInitialize(&argc,&args,(char*)0,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");
18 
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));
42 
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 }
53