Actual source code: ex136.c
1: static char help[] = "Tests MatLoad() MatView() for MPIBAIJ.\n\n";
3: #include <petscmat.h>
5: int main(int argc, char **args)
6: {
7: Mat A, B;
8: char file[PETSC_MAX_PATH_LEN];
9: PetscBool flg;
10: PetscViewer fd;
12: PetscFunctionBeginUser;
13: PetscCall(PetscInitialize(&argc, &args, NULL, help));
14: PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
15: PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -f option");
17: /*
18: Open binary file. Note that we use FILE_MODE_READ to indicate
19: reading from this file.
20: */
21: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));
23: /*
24: Load the matrix; then destroy the viewer.
25: */
26: PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
27: PetscCall(MatSetFromOptions(A));
28: PetscCall(MatLoad(A, fd));
29: PetscCall(PetscViewerDestroy(&fd));
31: /*
32: Open another binary file. Note that we use FILE_MODE_WRITE to indicate writing to the file
33: */
34: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "fileoutput", FILE_MODE_WRITE, &fd));
35: PetscCall(PetscViewerBinarySetFlowControl(fd, 3));
36: /*
37: Save the matrix and vector; then destroy the viewer.
38: */
39: PetscCall(MatView(A, fd));
40: PetscCall(PetscViewerDestroy(&fd));
42: /* load the new matrix */
43: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "fileoutput", FILE_MODE_READ, &fd));
44: PetscCall(MatCreate(PETSC_COMM_WORLD, &B));
45: PetscCall(MatSetFromOptions(B));
46: PetscCall(MatLoad(B, fd));
47: PetscCall(PetscViewerDestroy(&fd));
49: PetscCall(MatEqual(A, B, &flg));
50: if (flg) {
51: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Matrices are equal\n"));
52: } else {
53: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Matrices are not equal\n"));
54: }
56: PetscCall(MatDestroy(&A));
57: PetscCall(MatDestroy(&B));
58: PetscCall(PetscFinalize());
59: return 0;
60: }
62: /*TEST
64: test:
65: nsize: 3
66: requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
67: args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info
69: test:
70: suffix: 2
71: nsize: 5
72: requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
73: args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info
75: test:
76: suffix: 3
77: nsize: 7
78: requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
79: args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info
81: test:
82: suffix: 4
83: nsize: 3
84: requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
85: args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info -mat_type baij
87: test:
88: suffix: 5
89: nsize: 5
90: requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
91: args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info -mat_type baij
93: test:
94: suffix: 6
95: nsize: 7
96: requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
97: args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info -mat_type baij
99: TEST*/