1 2 static char help[] = "Tests MatCreateSubmatrix() with entire matrix, modified from ex59.c."; 3 4 #include <petscmat.h> 5 6 int main(int argc,char **args) 7 { 8 Mat C,A,Adup; 9 PetscInt i,j,m = 3,n = 2,rstart,rend; 10 PetscMPIInt size,rank; 11 PetscScalar v; 12 IS isrow; 13 PetscBool detect_bug = PETSC_FALSE; 14 15 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 16 PetscCall(PetscOptionsHasName(NULL,NULL,"-detect_bug",&detect_bug)); 17 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 18 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 19 n = 2*size; 20 21 PetscCall(MatCreate(PETSC_COMM_WORLD,&C)); 22 PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n)); 23 PetscCall(MatSetFromOptions(C)); 24 PetscCall(MatSetUp(C)); 25 26 /* 27 This is JUST to generate a nice test matrix, all processors fill up 28 the entire matrix. This is not something one would ever do in practice. 29 */ 30 PetscCall(MatGetOwnershipRange(C,&rstart,&rend)); 31 for (i=rstart; i<rend; i++) { 32 for (j=0; j<m*n; j++) { 33 v = i + j + 1; 34 PetscCall(MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES)); 35 } 36 } 37 PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY)); 38 PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY)); 39 PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON)); 40 PetscCall(MatView(C,PETSC_VIEWER_STDOUT_WORLD)); 41 PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD)); 42 43 /* 44 Generate a new matrix consisting every row and column of the original matrix 45 */ 46 PetscCall(MatGetOwnershipRange(C,&rstart,&rend)); 47 48 /* Create parallel IS with the rows we want on THIS processor */ 49 if (detect_bug && rank == 0) { 50 PetscCall(ISCreateStride(PETSC_COMM_WORLD,1,rstart,1,&isrow)); 51 } else { 52 PetscCall(ISCreateStride(PETSC_COMM_WORLD,rend-rstart,rstart,1,&isrow)); 53 } 54 PetscCall(MatCreateSubMatrix(C,isrow,NULL,MAT_INITIAL_MATRIX,&A)); 55 56 /* Change C to test the case MAT_REUSE_MATRIX */ 57 if (rank == 0) { 58 i = 0; j = 0; v = 100; 59 PetscCall(MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES)); 60 } 61 PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY)); 62 PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY)); 63 64 PetscCall(MatCreateSubMatrix(C,isrow,NULL,MAT_REUSE_MATRIX,&A)); 65 PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON)); 66 PetscCall(MatView(A,PETSC_VIEWER_STDOUT_WORLD)); 67 PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD)); 68 69 /* Test MatDuplicate */ 70 PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&Adup)); 71 PetscCall(MatDestroy(&Adup)); 72 73 PetscCall(ISDestroy(&isrow)); 74 PetscCall(MatDestroy(&A)); 75 PetscCall(MatDestroy(&C)); 76 PetscCall(PetscFinalize()); 77 return 0; 78 } 79 80 /*TEST 81 82 test: 83 nsize: 2 84 filter: grep -v "Mat Object" 85 requires: !complex 86 87 test: 88 suffix: 2 89 nsize: 3 90 args: -detect_bug 91 filter: grep -v "Mat Object" 92 requires: !complex 93 94 TEST*/ 95