1 2 static char help[] = "Tests the use of MatZeroRows() for parallel matrices.\n\ 3 This example also tests the use of MatDuplicate() for both MPIAIJ and MPIBAIJ matrices"; 4 5 #include <petscmat.h> 6 7 extern PetscErrorCode TestMatZeroRows_Basic(Mat,IS,PetscScalar); 8 extern PetscErrorCode TestMatZeroRows_with_no_allocation(Mat,IS,PetscScalar); 9 10 int main(int argc,char **args) 11 { 12 Mat A; 13 PetscInt i,j,m = 3,n,Ii,J,Imax; 14 PetscMPIInt rank,size; 15 PetscScalar v,diag=-4.0; 16 IS is; 17 18 PetscFunctionBeginUser; 19 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 20 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 21 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); 22 n = 2*size; 23 24 /* create A Square matrix for the five point stencil,YET AGAIN*/ 25 PetscCall(MatCreate(PETSC_COMM_WORLD,&A)); 26 PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n)); 27 PetscCall(MatSetFromOptions(A)); 28 PetscCall(MatSetUp(A)); 29 for (i=0; i<m; i++) { 30 for (j=2*rank; j<2*rank+2; j++) { 31 v = -1.0; Ii = j + n*i; 32 if (i>0) {J = Ii - n; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));} 33 if (i<m-1) {J = Ii + n; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));} 34 if (j>0) {J = Ii - 1; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));} 35 if (j<n-1) {J = Ii + 1; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));} 36 v = 4.0; PetscCall(MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES)); 37 } 38 } 39 PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 40 PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 41 42 /* Create AN IS required by MatZeroRows() */ 43 Imax = n*rank; if (Imax>= n*m -m - 1) Imax = m*n - m - 1; 44 PetscCall(ISCreateStride(PETSC_COMM_SELF,m,Imax,1,&is)); 45 46 PetscCall(TestMatZeroRows_Basic(A,is,0.0)); 47 PetscCall(TestMatZeroRows_Basic(A,is,diag)); 48 49 PetscCall(TestMatZeroRows_with_no_allocation(A,is,0.0)); 50 PetscCall(TestMatZeroRows_with_no_allocation(A,is,diag)); 51 52 PetscCall(MatDestroy(&A)); 53 54 /* Now Create a rectangular matrix with five point stencil (app) 55 n+size is used so that this dimension is always divisible by size. 56 This way, we can always use bs = size for any number of procs */ 57 PetscCall(MatCreate(PETSC_COMM_WORLD,&A)); 58 PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*(n+size))); 59 PetscCall(MatSetFromOptions(A)); 60 PetscCall(MatSetUp(A)); 61 for (i=0; i<m; i++) { 62 for (j=2*rank; j<2*rank+2; j++) { 63 v = -1.0; Ii = j + n*i; 64 if (i>0) {J = Ii - n; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));} 65 if (i<m-1) {J = Ii + n; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));} 66 if (j>0) {J = Ii - 1; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));} 67 if (j<n+size-1) {J = Ii + 1; PetscCall(MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES));} 68 v = 4.0; PetscCall(MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES)); 69 } 70 } 71 PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 72 PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 73 74 PetscCall(TestMatZeroRows_Basic(A,is,0.0)); 75 PetscCall(TestMatZeroRows_Basic(A,is,diag)); 76 77 PetscCall(MatDestroy(&A)); 78 PetscCall(ISDestroy(&is)); 79 PetscCall(PetscFinalize()); 80 return 0; 81 } 82 83 PetscErrorCode TestMatZeroRows_Basic(Mat A,IS is,PetscScalar diag) 84 { 85 Mat B; 86 PetscBool keepnonzeropattern; 87 88 /* Now copy A into B, and test it with MatZeroRows() */ 89 PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&B)); 90 91 PetscCall(PetscOptionsHasName(NULL,NULL,"-keep_nonzero_pattern",&keepnonzeropattern)); 92 if (keepnonzeropattern) PetscCall(MatSetOption(B,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE)); 93 94 PetscCall(MatZeroRowsIS(B,is,diag,0,0)); 95 PetscCall(MatView(B,PETSC_VIEWER_STDOUT_WORLD)); 96 PetscCall(MatDestroy(&B)); 97 return 0; 98 } 99 100 PetscErrorCode TestMatZeroRows_with_no_allocation(Mat A,IS is,PetscScalar diag) 101 { 102 Mat B; 103 104 /* Now copy A into B, and test it with MatZeroRows() */ 105 PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&B)); 106 /* Set this flag after assembly. This way, it affects only MatZeroRows() */ 107 PetscCall(MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE)); 108 109 PetscCall(MatZeroRowsIS(B,is,diag,0,0)); 110 PetscCall(MatView(B,PETSC_VIEWER_STDOUT_WORLD)); 111 PetscCall(MatDestroy(&B)); 112 return 0; 113 } 114 115 /*TEST 116 117 test: 118 nsize: 2 119 filter: grep -v " MPI process" 120 121 test: 122 suffix: 2 123 nsize: 3 124 args: -mat_type mpibaij -mat_block_size 3 125 filter: grep -v " MPI process" 126 127 test: 128 suffix: 3 129 nsize: 3 130 args: -mat_type mpiaij -keep_nonzero_pattern 131 filter: grep -v " MPI process" 132 133 test: 134 suffix: 4 135 nsize: 3 136 args: -keep_nonzero_pattern -mat_type mpibaij -mat_block_size 3 137 filter: grep -v " MPI process" 138 139 TEST*/ 140