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