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