Actual source code: ex59.c
petsc-3.13.6 2020-09-29
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;
12: PetscScalar v;
13: IS isrow,iscol;
15: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
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 ierr;
62: }
65: /*TEST
67: test:
69: test:
70: suffix: 2
71: nsize: 3
73: test:
74: suffix: 2_baij
75: nsize: 3
76: args: -mat_type baij
78: test:
79: suffix: 2_sbaij
80: nsize: 3
81: args: -mat_type sbaij
83: test:
84: suffix: baij
85: args: -mat_type baij
86: output_file: output/ex59_1_baij.out
88: test:
89: suffix: sbaij
90: args: -mat_type sbaij
91: output_file: output/ex59_1_sbaij.out
93: TEST*/