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