Actual source code: ex181.c
petsc-3.6.1 2015-08-06
2: static char help[] = "Tests MatGetSubmatrix() with entire matrix, modified from ex59.c.";
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;
16: PetscBool detect_bug = PETSC_FALSE;
18: PetscInitialize(&argc,&args,(char*)0,help);
19: PetscOptionsHasName(NULL,"-detect_bug",&detect_bug);
20: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
21: MPI_Comm_size(PETSC_COMM_WORLD,&size);
22: n = 2*size;
24: MatCreate(PETSC_COMM_WORLD,&C);
25: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
26: MatSetFromOptions(C);
27: MatSetUp(C);
29: /*
30: This is JUST to generate a nice test matrix, all processors fill up
31: the entire matrix. This is not something one would ever do in practice.
32: */
33: MatGetOwnershipRange(C,&rstart,&rend);
34: for (i=rstart; i<rend; i++) {
35: for (j=0; j<m*n; j++) {
36: v = i + j + 1;
37: MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES);
38: }
39: }
40: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
41: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
42: PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
43: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
45: /*
46: Generate a new matrix consisting every row and column of the original matrix
47: */
48: MatGetOwnershipRange(C,&rstart,&rend);
49:
50: /* Create parallel IS with the rows we want on THIS processor */
51: if (detect_bug && !rank) {
52: ISCreateStride(PETSC_COMM_WORLD,1,rstart,1,&isrow);
53: } else {
54: ISCreateStride(PETSC_COMM_WORLD,rend-rstart,rstart,1,&isrow);
55: }
56: MatGetSubMatrix(C,isrow,NULL,MAT_INITIAL_MATRIX,&A);
58: /* Change C to test the case MAT_REUSE_MATRIX */
59: if (!rank ) {
60: i = 0; j = 0; v = 100;
61: MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES);
62: }
63: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
64: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
65:
66: MatGetSubMatrix(C,isrow,NULL,MAT_REUSE_MATRIX,&A);
67: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
69: ISDestroy(&isrow);
70: MatDestroy(&A);
71: MatDestroy(&C);
72: PetscFinalize();
73: return 0;
74: }