Actual source code: ex59.c
petsc-3.8.4 2018-03-24
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: }