Actual source code: ex4.c
2: static char help[] = "Creates a matrix, inserts some values, and tests MatCreateSubMatrices() and MatZeroEntries().\n\n";
4: #include <petscmat.h>
6: int main(int argc, char **argv)
7: {
8: Mat mat, submat, submat1, *submatrices;
9: PetscInt m = 10, n = 10, i = 4, tmp, rstart, rend;
10: IS irow, icol;
11: PetscScalar value = 1.0;
12: PetscViewer sviewer;
13: PetscBool allA = PETSC_FALSE;
16: PetscInitialize(&argc, &argv, (char *)0, help);
17: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_COMMON);
18: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_ASCII_COMMON);
20: MatCreate(PETSC_COMM_WORLD, &mat);
21: MatSetSizes(mat, PETSC_DECIDE, PETSC_DECIDE, m, n);
22: MatSetFromOptions(mat);
23: MatSetUp(mat);
24: MatGetOwnershipRange(mat, &rstart, &rend);
25: for (i = rstart; i < rend; i++) {
26: value = (PetscReal)i + 1;
27: tmp = i % 5;
28: MatSetValues(mat, 1, &tmp, 1, &i, &value, INSERT_VALUES);
29: }
30: MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);
31: MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);
32: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "Original matrix\n");
33: MatView(mat, PETSC_VIEWER_STDOUT_WORLD);
35: /* Test MatCreateSubMatrix_XXX_All(), i.e., submatrix = A */
36: PetscOptionsGetBool(NULL, NULL, "-test_all", &allA, NULL);
37: if (allA) {
38: ISCreateStride(PETSC_COMM_SELF, m, 0, 1, &irow);
39: ISCreateStride(PETSC_COMM_SELF, n, 0, 1, &icol);
40: MatCreateSubMatrices(mat, 1, &irow, &icol, MAT_INITIAL_MATRIX, &submatrices);
41: MatCreateSubMatrices(mat, 1, &irow, &icol, MAT_REUSE_MATRIX, &submatrices);
42: submat = *submatrices;
44: /* sviewer will cause the submatrices (one per processor) to be printed in the correct order */
45: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "\nSubmatrices with all\n");
46: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "--------------------\n");
47: PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sviewer);
48: MatView(submat, sviewer);
49: PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sviewer);
50: PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
52: ISDestroy(&irow);
53: ISDestroy(&icol);
55: /* test getting a reference on a submat */
56: PetscObjectReference((PetscObject)submat);
57: MatDestroySubMatrices(1, &submatrices);
58: MatDestroy(&submat);
59: }
61: /* Form submatrix with rows 2-4 and columns 4-8 */
62: ISCreateStride(PETSC_COMM_SELF, 3, 2, 1, &irow);
63: ISCreateStride(PETSC_COMM_SELF, 5, 4, 1, &icol);
64: MatCreateSubMatrices(mat, 1, &irow, &icol, MAT_INITIAL_MATRIX, &submatrices);
65: submat = *submatrices;
67: /* Test reuse submatrices */
68: MatCreateSubMatrices(mat, 1, &irow, &icol, MAT_REUSE_MATRIX, &submatrices);
70: /* sviewer will cause the submatrices (one per processor) to be printed in the correct order */
71: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "\nSubmatrices\n");
72: PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sviewer);
73: MatView(submat, sviewer);
74: PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sviewer);
75: PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
76: PetscObjectReference((PetscObject)submat);
77: MatDestroySubMatrices(1, &submatrices);
78: MatDestroy(&submat);
80: /* Form submatrix with rows 2-4 and all columns */
81: ISDestroy(&icol);
82: ISCreateStride(PETSC_COMM_SELF, 10, 0, 1, &icol);
83: MatCreateSubMatrices(mat, 1, &irow, &icol, MAT_INITIAL_MATRIX, &submatrices);
84: MatCreateSubMatrices(mat, 1, &irow, &icol, MAT_REUSE_MATRIX, &submatrices);
85: submat = *submatrices;
87: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "\nSubmatrices with allcolumns\n");
88: PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sviewer);
89: MatView(submat, sviewer);
90: PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sviewer);
91: PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
93: /* Test MatDuplicate */
94: MatDuplicate(submat, MAT_COPY_VALUES, &submat1);
95: MatDestroy(&submat1);
97: /* Zero the original matrix */
98: PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "Original zeroed matrix\n");
99: MatZeroEntries(mat);
100: MatView(mat, PETSC_VIEWER_STDOUT_WORLD);
102: ISDestroy(&irow);
103: ISDestroy(&icol);
104: PetscObjectReference((PetscObject)submat);
105: MatDestroySubMatrices(1, &submatrices);
106: MatDestroy(&submat);
107: MatDestroy(&mat);
108: PetscFinalize();
109: return 0;
110: }
112: /*TEST
114: test:
115: args: -mat_type aij
117: test:
118: suffix: 2
119: args: -mat_type dense
121: test:
122: suffix: 3
123: nsize: 3
124: args: -mat_type aij
126: test:
127: suffix: 4
128: nsize: 3
129: args: -mat_type dense
131: test:
132: suffix: 5
133: nsize: 3
134: args: -mat_type aij -test_all
136: TEST*/