Actual source code: ex8.c
1: static char help[] = "Tests automatic allocation of matrix storage space.\n\n";
3: #include <petscmat.h>
5: int main(int argc, char **args)
6: {
7: Mat C;
8: PetscInt i, j, m = 3, n = 3, Ii, J;
9: PetscScalar v;
10: MatInfo info;
12: PetscFunctionBeginUser;
13: PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
14: PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
15: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
17: /* create the matrix for the five point stencil, YET AGAIN */
18: PetscCall(MatCreate(PETSC_COMM_SELF, &C));
19: PetscCall(MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, m * n, m * n));
20: PetscCall(MatSetFromOptions(C));
21: PetscCall(MatSetUp(C));
22: for (i = 0; i < m; i++) {
23: for (j = 0; j < n; j++) {
24: v = -1.0;
25: Ii = j + n * i;
26: if (i > 0) {
27: J = Ii - n;
28: PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
29: }
30: if (i < m - 1) {
31: J = Ii + n;
32: PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
33: }
34: if (j > 0) {
35: J = Ii - 1;
36: PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
37: }
38: if (j < n - 1) {
39: J = Ii + 1;
40: PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
41: }
42: v = 4.0;
43: PetscCall(MatSetValues(C, 1, &Ii, 1, &Ii, &v, INSERT_VALUES));
44: }
45: }
46: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
47: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
48: PetscCall(MatView(C, PETSC_VIEWER_STDOUT_SELF));
50: PetscCall(MatGetInfo(C, MAT_LOCAL, &info));
51: PetscCall(PetscPrintf(PETSC_COMM_SELF, "matrix nonzeros = %" PetscInt_FMT ", allocated nonzeros = %" PetscInt_FMT "\n", (PetscInt)info.nz_used, (PetscInt)info.nz_allocated));
53: PetscCall(MatDestroy(&C));
54: PetscCall(PetscFinalize());
55: return 0;
56: }
58: /*TEST
60: test:
62: TEST*/