1 2 static char help[] = "Tests MatCreateSubmatrix() in parallel."; 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; 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 37 CHKERRQ(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY)); 38 CHKERRQ(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY)); 39 CHKERRQ(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON)); 40 CHKERRQ(MatView(C,PETSC_VIEWER_STDOUT_WORLD)); 41 42 /* 43 Generate a new matrix consisting of every second row and column of 44 the original matrix 45 */ 46 CHKERRQ(MatGetOwnershipRange(C,&rstart,&rend)); 47 /* Create parallel IS with the rows we want on THIS processor */ 48 CHKERRQ(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 CHKERRQ(ISCreateStride(PETSC_COMM_WORLD,(rend-rstart)/2,rstart,2,&iscol)); 51 52 CHKERRQ(MatCreateSubMatrix(C,isrow,iscol,MAT_INITIAL_MATRIX,&A)); 53 CHKERRQ(MatCreateSubMatrix(C,isrow,iscol,MAT_REUSE_MATRIX,&A)); 54 CHKERRQ(MatView(A,PETSC_VIEWER_STDOUT_WORLD)); 55 56 CHKERRQ(ISDestroy(&isrow)); 57 CHKERRQ(ISDestroy(&iscol)); 58 CHKERRQ(MatDestroy(&A)); 59 CHKERRQ(MatDestroy(&C)); 60 ierr = PetscFinalize(); 61 return ierr; 62 } 63 64 /*TEST 65 66 test: 67 68 test: 69 suffix: 2 70 nsize: 3 71 72 test: 73 suffix: 2_baij 74 nsize: 3 75 args: -mat_type baij 76 77 test: 78 suffix: 2_sbaij 79 nsize: 3 80 args: -mat_type sbaij 81 82 test: 83 suffix: baij 84 args: -mat_type baij 85 output_file: output/ex59_1_baij.out 86 87 test: 88 suffix: sbaij 89 args: -mat_type sbaij 90 output_file: output/ex59_1_sbaij.out 91 92 TEST*/ 93