1 static char help[] = "Tests binary I/O of matrices and illustrates user-defined event logging.\n\n";
2
3 #include <petscmat.h>
4
5 /* Note: Most applications would not read and write the same matrix within
6 the same program. This example is intended only to demonstrate
7 both input and output. */
8
main(int argc,char ** args)9 int main(int argc, char **args)
10 {
11 Mat C;
12 PetscScalar v;
13 PetscInt i, j, Ii, J, Istart, Iend, N, m = 4, n = 4;
14 PetscMPIInt rank, size;
15 PetscViewer viewer;
16 PetscLogEvent MATRIX_GENERATE, MATRIX_READ;
17
18 PetscFunctionBeginUser;
19 PetscCall(PetscInitialize(&argc, &args, NULL, help));
20 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
21 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
22 PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
23 PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
24 N = m * n;
25
26 /* PART 1: Generate matrix, then write it in binary format */
27
28 PetscCall(PetscLogEventRegister("Generate Matrix", 0, &MATRIX_GENERATE));
29 PetscCall(PetscLogEventBegin(MATRIX_GENERATE, 0, 0, 0, 0));
30
31 /* Generate matrix */
32 PetscCall(MatCreate(PETSC_COMM_WORLD, &C));
33 PetscCall(MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, N, N));
34 PetscCall(MatSetFromOptions(C));
35 PetscCall(MatSetUp(C));
36 PetscCall(MatGetOwnershipRange(C, &Istart, &Iend));
37 for (Ii = Istart; Ii < Iend; Ii++) {
38 v = -1.0;
39 i = Ii / n;
40 j = Ii - i * n;
41 if (i > 0) {
42 J = Ii - n;
43 PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES));
44 }
45 if (i < m - 1) {
46 J = Ii + n;
47 PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES));
48 }
49 if (j > 0) {
50 J = Ii - 1;
51 PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES));
52 }
53 if (j < n - 1) {
54 J = Ii + 1;
55 PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES));
56 }
57 v = 4.0;
58 PetscCall(MatSetValues(C, 1, &Ii, 1, &Ii, &v, ADD_VALUES));
59 }
60 PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
61 PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
62 PetscCall(MatView(C, PETSC_VIEWER_STDOUT_WORLD));
63
64 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "writing matrix in binary to matrix.dat ...\n"));
65 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "matrix.dat", FILE_MODE_WRITE, &viewer));
66 PetscCall(MatView(C, viewer));
67 PetscCall(PetscViewerDestroy(&viewer));
68 PetscCall(MatDestroy(&C));
69 PetscCall(PetscLogEventEnd(MATRIX_GENERATE, 0, 0, 0, 0));
70
71 /* PART 2: Read in matrix in binary format */
72
73 /* All processors wait until test matrix has been dumped */
74 PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
75
76 PetscCall(PetscLogEventRegister("Read Matrix", 0, &MATRIX_READ));
77 PetscCall(PetscLogEventBegin(MATRIX_READ, 0, 0, 0, 0));
78 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "reading matrix in binary from matrix.dat ...\n"));
79 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "matrix.dat", FILE_MODE_READ, &viewer));
80 PetscCall(MatCreate(PETSC_COMM_WORLD, &C));
81 PetscCall(MatLoad(C, viewer));
82 PetscCall(PetscViewerDestroy(&viewer));
83 PetscCall(PetscLogEventEnd(MATRIX_READ, 0, 0, 0, 0));
84 PetscCall(MatView(C, PETSC_VIEWER_STDOUT_WORLD));
85
86 /* Free data structures */
87 PetscCall(MatDestroy(&C));
88
89 PetscCall(PetscFinalize());
90 return 0;
91 }
92
93 /*TEST
94
95 test:
96 filter: grep -v " MPI process"
97
98 TEST*/
99