1 /* 2 * ex195.c 3 * 4 * Created on: Aug 24, 2015 5 * Author: Fande Kong <fdkong.jd@gmail.com> 6 */ 7 8 static char help[] = " Demonstrate the use of MatConvert_Nest_AIJ\n"; 9 10 #include <petscmat.h> 11 12 int main(int argc,char **args) 13 { 14 Mat A1,A2,A3,A4,A5,B,C,C1,nest; 15 Mat mata[4]; 16 Mat aij; 17 MPI_Comm comm; 18 PetscInt m,M,n,istart,iend,ii,i,J,j,K=10; 19 PetscScalar v; 20 PetscMPIInt size; 21 PetscBool equal; 22 23 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 24 comm = PETSC_COMM_WORLD; 25 PetscCallMPI(MPI_Comm_size(comm,&size)); 26 27 /* 28 Assemble the matrix for the five point stencil, YET AGAIN 29 */ 30 PetscCall(MatCreate(comm,&A1)); 31 m=2,n=2; 32 PetscCall(MatSetSizes(A1,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n)); 33 PetscCall(MatSetFromOptions(A1)); 34 PetscCall(MatSetUp(A1)); 35 PetscCall(MatGetOwnershipRange(A1,&istart,&iend)); 36 for (ii=istart; ii<iend; ii++) { 37 v = -1.0; i = ii/n; j = ii - i*n; 38 if (i>0) {J = ii - n; PetscCall(MatSetValues(A1,1,&ii,1,&J,&v,INSERT_VALUES));} 39 if (i<m-1) {J = ii + n; PetscCall(MatSetValues(A1,1,&ii,1,&J,&v,INSERT_VALUES));} 40 if (j>0) {J = ii - 1; PetscCall(MatSetValues(A1,1,&ii,1,&J,&v,INSERT_VALUES));} 41 if (j<n-1) {J = ii + 1; PetscCall(MatSetValues(A1,1,&ii,1,&J,&v,INSERT_VALUES));} 42 v = 4.0; PetscCall(MatSetValues(A1,1,&ii,1,&ii,&v,INSERT_VALUES)); 43 } 44 PetscCall(MatAssemblyBegin(A1,MAT_FINAL_ASSEMBLY)); 45 PetscCall(MatAssemblyEnd(A1,MAT_FINAL_ASSEMBLY)); 46 PetscCall(MatView(A1,PETSC_VIEWER_STDOUT_WORLD)); 47 48 PetscCall(MatDuplicate(A1,MAT_COPY_VALUES,&A2)); 49 PetscCall(MatDuplicate(A1,MAT_COPY_VALUES,&A3)); 50 PetscCall(MatDuplicate(A1,MAT_COPY_VALUES,&A4)); 51 52 /*create a nest matrix */ 53 PetscCall(MatCreate(comm,&nest)); 54 PetscCall(MatSetType(nest,MATNEST)); 55 mata[0]=A1,mata[1]=A2,mata[2]=A3,mata[3]=A4; 56 PetscCall(MatNestSetSubMats(nest,2,NULL,2,NULL,mata)); 57 PetscCall(MatSetUp(nest)); 58 PetscCall(MatConvert(nest,MATAIJ,MAT_INITIAL_MATRIX,&aij)); 59 PetscCall(MatView(aij,PETSC_VIEWER_STDOUT_WORLD)); 60 61 /* create a dense matrix */ 62 PetscCall(MatGetSize(nest,&M,NULL)); 63 PetscCall(MatGetLocalSize(nest,&m,NULL)); 64 PetscCall(MatCreateDense(comm,m,PETSC_DECIDE,M,K,NULL,&B)); 65 PetscCall(MatSetRandom(B,PETSC_NULL)); 66 PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 67 PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 68 69 /* C = nest*B_dense */ 70 PetscCall(MatMatMult(nest,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&C)); 71 PetscCall(MatMatMult(nest,B,MAT_REUSE_MATRIX,PETSC_DEFAULT,&C)); 72 PetscCall(MatMatMultEqual(nest,B,C,10,&equal)); 73 PetscCheck(equal,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Error in C != nest*B_dense"); 74 75 /* Test B = nest*C, reuse C and B with MatProductCreateWithMat() */ 76 /* C has been obtained from nest*B. Clear internal data structures related to factors to prevent circular references */ 77 PetscCall(MatProductClear(C)); 78 PetscCall(MatProductCreateWithMat(nest,C,NULL,B)); 79 PetscCall(MatProductSetType(B,MATPRODUCT_AB)); 80 PetscCall(MatProductSetFromOptions(B)); 81 PetscCall(MatProductSymbolic(B)); 82 PetscCall(MatProductNumeric(B)); 83 PetscCall(MatMatMultEqual(nest,C,B,10,&equal)); 84 PetscCheck(equal,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Error in B != nest*C_dense"); 85 PetscCall(MatConvert(nest,MATAIJ,MAT_INPLACE_MATRIX,&nest)); 86 PetscCall(MatEqual(nest,aij,&equal)); 87 PetscCheck(equal,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Error in aij != nest"); 88 PetscCall(MatDestroy(&nest)); 89 90 if (size > 1) { /* Do not know why this test fails for size = 1 */ 91 PetscCall(MatCreateTranspose(A1,&A5)); /* A1 is symmetric */ 92 mata[0] = A5; 93 PetscCall(MatCreate(comm,&nest)); 94 PetscCall(MatSetType(nest,MATNEST)); 95 PetscCall(MatNestSetSubMats(nest,2,NULL,2,NULL,mata)); 96 PetscCall(MatSetUp(nest)); 97 PetscCall(MatMatMult(nest,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&C1)); 98 PetscCall(MatMatMult(nest,B,MAT_REUSE_MATRIX,PETSC_DEFAULT,&C1)); 99 100 PetscCall(MatMatMultEqual(nest,B,C1,10,&equal)); 101 PetscCheck(equal,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Error in C1 != C"); 102 PetscCall(MatDestroy(&C1)); 103 PetscCall(MatDestroy(&A5)); 104 PetscCall(MatDestroy(&nest)); 105 } 106 107 PetscCall(MatDestroy(&C)); 108 PetscCall(MatDestroy(&B)); 109 PetscCall(MatDestroy(&aij)); 110 PetscCall(MatDestroy(&A1)); 111 PetscCall(MatDestroy(&A2)); 112 PetscCall(MatDestroy(&A3)); 113 PetscCall(MatDestroy(&A4)); 114 PetscCall(PetscFinalize()); 115 return 0; 116 } 117 118 /*TEST 119 120 test: 121 nsize: 2 122 123 test: 124 suffix: 2 125 126 TEST*/ 127