Actual source code: ex59.c
2: static char help[] = "Tests MatCreateSubmatrix() in parallel.";
4: #include <petscmat.h>
6: int main(int argc, char **args)
7: {
8: Mat C, A;
9: PetscInt i, j, m = 3, n = 2, rstart, rend;
10: PetscMPIInt size, rank;
11: PetscScalar v;
12: IS isrow, iscol;
15: PetscInitialize(&argc, &args, (char *)0, help);
16: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
17: MPI_Comm_size(PETSC_COMM_WORLD, &size);
18: n = 2 * size;
20: MatCreate(PETSC_COMM_WORLD, &C);
21: MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, m * n, m * n);
22: MatSetFromOptions(C);
23: MatSetUp(C);
25: /*
26: This is JUST to generate a nice test matrix, all processors fill up
27: the entire matrix. This is not something one would ever do in practice.
28: */
29: MatGetOwnershipRange(C, &rstart, &rend);
30: for (i = rstart; i < rend; i++) {
31: for (j = 0; j < m * n; j++) {
32: v = i + j + 1;
33: MatSetValues(C, 1, &i, 1, &j, &v, INSERT_VALUES);
34: }
35: }
37: MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY);
38: MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY);
39: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_COMMON);
40: MatView(C, PETSC_VIEWER_STDOUT_WORLD);
42: /*
43: Generate a new matrix consisting of every second row and column of
44: the original matrix
45: */
46: MatGetOwnershipRange(C, &rstart, &rend);
47: /* Create parallel IS with the rows we want on THIS processor */
48: ISCreateStride(PETSC_COMM_WORLD, (rend - rstart) / 2, rstart, 2, &isrow);
49: /* Create parallel IS with the rows we want on THIS processor (same as rows for now) */
50: ISCreateStride(PETSC_COMM_WORLD, (rend - rstart) / 2, rstart, 2, &iscol);
52: MatCreateSubMatrix(C, isrow, iscol, MAT_INITIAL_MATRIX, &A);
53: MatCreateSubMatrix(C, isrow, iscol, MAT_REUSE_MATRIX, &A);
54: MatView(A, PETSC_VIEWER_STDOUT_WORLD);
56: ISDestroy(&isrow);
57: ISDestroy(&iscol);
58: MatDestroy(&A);
59: MatDestroy(&C);
60: PetscFinalize();
61: return 0;
62: }
64: /*TEST
66: test:
68: test:
69: suffix: 2
70: nsize: 3
72: test:
73: suffix: 2_baij
74: nsize: 3
75: args: -mat_type baij
77: test:
78: suffix: 2_sbaij
79: nsize: 3
80: args: -mat_type sbaij
82: test:
83: suffix: baij
84: args: -mat_type baij
85: output_file: output/ex59_1_baij.out
87: test:
88: suffix: sbaij
89: args: -mat_type sbaij
90: output_file: output/ex59_1_sbaij.out
92: TEST*/