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