Actual source code: ex59.c
petsc-3.7.3 2016-08-01
2: static char help[] = "Tests MatGetSubmatrix() in parallel.";
4: #include <petscmat.h>
8: int main(int argc,char **args)
9: {
10: Mat C,A;
11: PetscInt i,j,m = 3,n = 2,rstart,rend;
12: PetscMPIInt size,rank;
14: PetscScalar v;
15: IS isrow,iscol;
17: PetscInitialize(&argc,&args,(char*)0,help);
18: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
19: MPI_Comm_size(PETSC_COMM_WORLD,&size);
20: n = 2*size;
22: MatCreate(PETSC_COMM_WORLD,&C);
23: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
24: MatSetFromOptions(C);
25: MatSetUp(C);
27: /*
28: This is JUST to generate a nice test matrix, all processors fill up
29: the entire matrix. This is not something one would ever do in practice.
30: */
31: MatGetOwnershipRange(C,&rstart,&rend);
32: for (i=rstart; i<rend; i++) {
33: for (j=0; j<m*n; j++) {
34: v = i + j + 1;
35: MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES);
36: }
37: }
39: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
40: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
41: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
42: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
44: /*
45: Generate a new matrix consisting of every second row and column of
46: the original matrix
47: */
48: MatGetOwnershipRange(C,&rstart,&rend);
49: /* Create parallel IS with the rows we want on THIS processor */
50: ISCreateStride(PETSC_COMM_WORLD,(rend-rstart)/2,rstart,2,&isrow);
51: /* Create parallel IS with the rows we want on THIS processor (same as rows for now) */
52: ISCreateStride(PETSC_COMM_WORLD,(rend-rstart)/2,rstart,2,&iscol);
54: MatGetSubMatrix(C,isrow,iscol,MAT_INITIAL_MATRIX,&A);
55: MatGetSubMatrix(C,isrow,iscol,MAT_REUSE_MATRIX,&A);
56: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
58: ISDestroy(&isrow);
59: ISDestroy(&iscol);
60: MatDestroy(&A);
61: MatDestroy(&C);
62: PetscFinalize();
63: return 0;
64: }