1 2 static char help[] = "Tests MatCreateSubmatrix() with certain entire rows of matrix, modified from ex181.c."; 3 4 #include <petscmat.h> 5 6 int main(int argc,char **args) 7 { 8 Mat C,A; 9 PetscInt i,j,m = 3,n = 2,rstart,rend,cstart,cend; 10 PetscMPIInt size,rank; 11 PetscErrorCode ierr; 12 PetscScalar v; 13 IS isrow,iscol; 14 15 ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr; 16 CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 17 CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 18 n = 2*size; 19 20 CHKERRQ(MatCreate(PETSC_COMM_WORLD,&C)); 21 CHKERRQ(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n)); 22 CHKERRQ(MatSetFromOptions(C)); 23 CHKERRQ(MatSetUp(C)); 24 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 CHKERRQ(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 CHKERRQ(MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES)); 34 } 35 } 36 CHKERRQ(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY)); 37 CHKERRQ(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY)); 38 CHKERRQ(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON)); 39 CHKERRQ(MatView(C,PETSC_VIEWER_STDOUT_WORLD)); 40 CHKERRQ(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD)); 41 42 /* 43 Generate a new matrix consisting every column of the original matrix 44 */ 45 CHKERRQ(MatGetOwnershipRange(C,&rstart,&rend)); 46 CHKERRQ(MatGetOwnershipRangeColumn(C,&cstart,&cend)); 47 48 CHKERRQ(ISCreateStride(PETSC_COMM_WORLD,rend-rstart > 0 ? rend-rstart-1 : 0,rstart,1,&isrow)); 49 CHKERRQ(ISCreateStride(PETSC_COMM_WORLD,cend-cstart,cstart,1,&iscol)); 50 CHKERRQ(MatCreateSubMatrix(C,isrow,NULL,MAT_INITIAL_MATRIX,&A)); 51 52 /* Change C to test the case MAT_REUSE_MATRIX */ 53 if (rank == 0) { 54 i = 0; j = 0; v = 100; 55 CHKERRQ(MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES)); 56 } 57 CHKERRQ(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY)); 58 CHKERRQ(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY)); 59 60 CHKERRQ(MatCreateSubMatrix(C,isrow,NULL,MAT_REUSE_MATRIX,&A)); 61 CHKERRQ(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON)); 62 CHKERRQ(MatView(A,PETSC_VIEWER_STDOUT_WORLD)); 63 CHKERRQ(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD)); 64 65 CHKERRQ(ISDestroy(&isrow)); 66 CHKERRQ(ISDestroy(&iscol)); 67 CHKERRQ(MatDestroy(&A)); 68 CHKERRQ(MatDestroy(&C)); 69 ierr = PetscFinalize(); 70 return ierr; 71 } 72 73 /*TEST 74 75 test: 76 nsize: 2 77 78 TEST*/ 79