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 PetscScalar v; 12 IS isrow,iscol; 13 14 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 15 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 16 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 17 n = 2*size; 18 19 PetscCall(MatCreate(PETSC_COMM_WORLD,&C)); 20 PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n)); 21 PetscCall(MatSetFromOptions(C)); 22 PetscCall(MatSetUp(C)); 23 24 /* 25 This is JUST to generate a nice test matrix, all processors fill up 26 the entire matrix. This is not something one would ever do in practice. 27 */ 28 PetscCall(MatGetOwnershipRange(C,&rstart,&rend)); 29 for (i=rstart; i<rend; i++) { 30 for (j=0; j<m*n; j++) { 31 v = i + j + 1; 32 PetscCall(MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES)); 33 } 34 } 35 36 PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY)); 37 PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY)); 38 PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON)); 39 PetscCall(MatView(C,PETSC_VIEWER_STDOUT_WORLD)); 40 41 /* 42 Generate a new matrix consisting of every second row and column of 43 the original matrix 44 */ 45 PetscCall(MatGetOwnershipRange(C,&rstart,&rend)); 46 /* Create parallel IS with the rows we want on THIS processor */ 47 PetscCall(ISCreateStride(PETSC_COMM_WORLD,(rend-rstart)/2,rstart,2,&isrow)); 48 /* Create parallel IS with the rows we want on THIS processor (same as rows for now) */ 49 PetscCall(ISCreateStride(PETSC_COMM_WORLD,(rend-rstart)/2,rstart,2,&iscol)); 50 51 PetscCall(MatCreateSubMatrix(C,isrow,iscol,MAT_INITIAL_MATRIX,&A)); 52 PetscCall(MatCreateSubMatrix(C,isrow,iscol,MAT_REUSE_MATRIX,&A)); 53 PetscCall(MatView(A,PETSC_VIEWER_STDOUT_WORLD)); 54 55 PetscCall(ISDestroy(&isrow)); 56 PetscCall(ISDestroy(&iscol)); 57 PetscCall(MatDestroy(&A)); 58 PetscCall(MatDestroy(&C)); 59 PetscCall(PetscFinalize()); 60 return 0; 61 } 62 63 /*TEST 64 65 test: 66 67 test: 68 suffix: 2 69 nsize: 3 70 71 test: 72 suffix: 2_baij 73 nsize: 3 74 args: -mat_type baij 75 76 test: 77 suffix: 2_sbaij 78 nsize: 3 79 args: -mat_type sbaij 80 81 test: 82 suffix: baij 83 args: -mat_type baij 84 output_file: output/ex59_1_baij.out 85 86 test: 87 suffix: sbaij 88 args: -mat_type sbaij 89 output_file: output/ex59_1_sbaij.out 90 91 TEST*/ 92