xref: /petsc/src/mat/tests/ex14.c (revision 732aec7a18f2199fb53bb9a2f3aef439a834ce31)
1 static char help[] = "Tests sequential and parallel MatGetRow() and MatRestoreRow().\n";
2 
3 #include <petscmat.h>
4 
main(int argc,char ** args)5 int main(int argc, char **args)
6 {
7   Mat                C;
8   PetscInt           i, j, m = 5, n = 5, Ii, J, nz, rstart, rend;
9   PetscMPIInt        rank;
10   const PetscInt    *idx;
11   PetscScalar        v;
12   const PetscScalar *values;
13 
14   PetscFunctionBeginUser;
15   PetscCall(PetscInitialize(&argc, &args, NULL, help));
16   /* Create the matrix for the five point stencil, YET AGAIN */
17   PetscCall(MatCreate(PETSC_COMM_WORLD, &C));
18   PetscCall(MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, m * n, m * n));
19   PetscCall(MatSetFromOptions(C));
20   PetscCall(MatSetUp(C));
21   for (i = 0; i < m; i++) {
22     for (j = 0; j < n; j++) {
23       v  = -1.0;
24       Ii = j + n * i;
25       if (i > 0) {
26         J = Ii - n;
27         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
28       }
29       if (i < m - 1) {
30         J = Ii + n;
31         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
32       }
33       if (j > 0) {
34         J = Ii - 1;
35         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
36       }
37       if (j < n - 1) {
38         J = Ii + 1;
39         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
40       }
41       v = 4.0;
42       PetscCall(MatSetValues(C, 1, &Ii, 1, &Ii, &v, INSERT_VALUES));
43     }
44   }
45   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
46   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
47   PetscCall(MatView(C, PETSC_VIEWER_STDOUT_WORLD));
48 
49   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
50   PetscCall(MatGetOwnershipRange(C, &rstart, &rend));
51   for (i = rstart; i < rend; i++) {
52     PetscCall(MatGetRow(C, i, &nz, &idx, &values));
53     if (rank == 0) {
54       for (j = 0; j < nz; j++) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT " %g ", idx[j], (double)PetscRealPart(values[j])));
55       PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
56     }
57     PetscCall(MatRestoreRow(C, i, &nz, &idx, &values));
58   }
59 
60   PetscCall(MatDestroy(&C));
61   PetscCall(PetscFinalize());
62   return 0;
63 }
64 
65 /*TEST
66 
67    test:
68 
69 TEST*/
70