Actual source code: ex131.c
petsc-3.14.6 2021-03-30
2: static char help[] = "Tests MatMult() on MatLoad() matrix \n\n";
4: #include <petscmat.h>
6: int main(int argc,char **args)
7: {
8: Mat A;
9: Vec x,b;
11: PetscViewer fd; /* viewer */
12: char file[PETSC_MAX_PATH_LEN]; /* input file name */
13: PetscBool flg;
15: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
16: /* Determine file from which we read the matrix A */
17: PetscOptionsGetString(NULL,NULL,"-f",file,sizeof(file),&flg);
18: if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -f option");
20: /* Load matrix A */
21: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
22: MatCreate(PETSC_COMM_WORLD,&A);
23: MatLoad(A,fd);
24: flg = PETSC_FALSE;
25: VecCreate(PETSC_COMM_WORLD,&x);
26: 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: PetscInfo(0,"Using vector of ones for RHS\n");
32: MatGetLocalSize(A,&m,NULL);
33: VecSetSizes(x,m,PETSC_DECIDE);
34: VecSetFromOptions(x);
35: VecSet(x,one);
36: }
37: } else {
38: VecLoad(x,fd);
39: PetscViewerDestroy(&fd);
40: }
41: VecDuplicate(x,&b);
42: MatMult(A,x,b);
44: /* Print (for testing only) */
45: MatView(A,0);
46: VecView(b,0);
47: /* Free data structures */
48: MatDestroy(&A);
49: VecDestroy(&x);
50: VecDestroy(&b);
51: PetscFinalize();
52: return ierr;
53: }