Actual source code: ex136.c
petsc-3.6.1 2015-08-06
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 writing to the file
37: */
38: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"fileoutput",FILE_MODE_WRITE,&fd);
39: PetscViewerBinarySetFlowControl(fd,3);
40: /*
41: Save the matrix and vector; then destroy the viewer.
42: */
43: MatView(A,fd);
44: PetscViewerDestroy(&fd);
46: /* load the new matrix */
47: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"fileoutput",FILE_MODE_READ,&fd);
48: MatCreate(PETSC_COMM_WORLD,&B);
49: MatSetFromOptions(B);
50: MatLoad(B,fd);
51: PetscViewerDestroy(&fd);
53: MatEqual(A,B,&flg);
54: if (flg) {
55: PetscPrintf(PETSC_COMM_WORLD,"Matrices are equal\n");
56: } else {
57: PetscPrintf(PETSC_COMM_WORLD,"Matrices are not equal\n");
58: }
60: MatDestroy(&A);
61: MatDestroy(&B);
62: PetscFinalize();
63: return 0;
64: }