Actual source code: ex136.c
petsc-dev 2014-02-02
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;
15: MatType type = MATMPIBAIJ;
17: PetscInitialize(&argc,&args,(char*)0,help);
18: PetscOptionsHasName(NULL,"-aij",&flg);
19: if (flg) type = MATMPIAIJ;
21: PetscOptionsGetString(NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);
22: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option");
24: /*
25: Open binary file. Note that we use FILE_MODE_READ to indicate
26: reading from this file.
27: */
28: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
30: /*
31: Load the matrix; then destroy the viewer.
32: */
33: MatCreate(PETSC_COMM_WORLD,&A);
34: MatSetType(A,type);
35: MatSetFromOptions(A);
36: MatLoad(A,fd);
37: PetscViewerDestroy(&fd);
39: /*
40: Open another binary file. Note that we use FILE_MODE_WRITE to indicate
41: reading from this file.
42: */
43: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"fileoutput",FILE_MODE_WRITE,&fd);
44: PetscViewerBinarySetFlowControl(fd,3);
45: /*
46: Save the matrix and vector; then destroy the viewer.
47: */
48: MatView(A,fd);
49: PetscViewerDestroy(&fd);
51: /* load the new matrix */
52: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"fileoutput",FILE_MODE_READ,&fd);
53: MatCreate(PETSC_COMM_WORLD,&B);
54: MatSetType(B,type);
55: MatSetFromOptions(B);
56: MatLoad(B,fd);
57: PetscViewerDestroy(&fd);
59: MatEqual(A,B,&flg);
60: if (flg) {
61: PetscPrintf(PETSC_COMM_WORLD,"Matrices are equal\n");
62: } else {
63: PetscPrintf(PETSC_COMM_WORLD,"Matrices are not equal\n");
64: }
66: MatDestroy(&A);
67: MatDestroy(&B);
68: PetscFinalize();
69: return 0;
70: }