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 PetscFunctionBeginUser; 15 PetscCall(PetscInitialize(&argc, &args, (char *)0, help)); 16 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 17 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); 18 n = 2 * size; 19 20 PetscCall(MatCreate(PETSC_COMM_WORLD, &C)); 21 PetscCall(MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, m * n, m * n)); 22 PetscCall(MatSetFromOptions(C)); 23 PetscCall(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 PetscCall(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 PetscCall(MatSetValues(C, 1, &i, 1, &j, &v, INSERT_VALUES)); 34 } 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 42 /* 43 Generate a new matrix consisting of every second row and column of 44 the original matrix 45 */ 46 PetscCall(MatGetOwnershipRange(C, &rstart, &rend)); 47 /* Create parallel IS with the rows we want on THIS processor */ 48 PetscCall(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 PetscCall(ISCreateStride(PETSC_COMM_WORLD, (rend - rstart) / 2, rstart, 2, &iscol)); 51 52 PetscCall(MatCreateSubMatrix(C, isrow, iscol, MAT_INITIAL_MATRIX, &A)); 53 PetscCall(MatCreateSubMatrix(C, isrow, iscol, MAT_REUSE_MATRIX, &A)); 54 PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD)); 55 56 PetscCall(ISDestroy(&isrow)); 57 PetscCall(ISDestroy(&iscol)); 58 PetscCall(MatDestroy(&A)); 59 PetscCall(MatDestroy(&C)); 60 PetscCall(PetscFinalize()); 61 return 0; 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