Actual source code: ex136.c
petsc-3.8.4 2018-03-24
2: static char help[] = "Tests MatLoad() MatView() for MPIBAIJ.\n\n";
4: #include <petscmat.h>
6: int main(int argc,char **args)
7: {
8: Mat A,B;
10: char file[PETSC_MAX_PATH_LEN];
11: PetscBool flg;
12: PetscViewer fd;
14: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
15: PetscOptionsGetString(NULL,NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);
16: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option");
18: /*
19: Open binary file. Note that we use FILE_MODE_READ to indicate
20: reading from this file.
21: */
22: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
24: /*
25: Load the matrix; then destroy the viewer.
26: */
27: MatCreate(PETSC_COMM_WORLD,&A);
28: MatSetFromOptions(A);
29: MatLoad(A,fd);
30: PetscViewerDestroy(&fd);
32: /*
33: Open another binary file. Note that we use FILE_MODE_WRITE to indicate writing to the file
34: */
35: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"fileoutput",FILE_MODE_WRITE,&fd);
36: PetscViewerBinarySetFlowControl(fd,3);
37: /*
38: Save the matrix and vector; then destroy the viewer.
39: */
40: MatView(A,fd);
41: PetscViewerDestroy(&fd);
43: /* load the new matrix */
44: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"fileoutput",FILE_MODE_READ,&fd);
45: MatCreate(PETSC_COMM_WORLD,&B);
46: MatSetFromOptions(B);
47: MatLoad(B,fd);
48: PetscViewerDestroy(&fd);
50: MatEqual(A,B,&flg);
51: if (flg) {
52: PetscPrintf(PETSC_COMM_WORLD,"Matrices are equal\n");
53: } else {
54: PetscPrintf(PETSC_COMM_WORLD,"Matrices are not equal\n");
55: }
57: MatDestroy(&A);
58: MatDestroy(&B);
59: PetscFinalize();
60: return ierr;
61: }