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 67 /* C = nest*B_dense */ 68 PetscCall(MatMatMult(nest,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&C)); 69 PetscCall(MatMatMult(nest,B,MAT_REUSE_MATRIX,PETSC_DEFAULT,&C)); 70 PetscCall(MatMatMultEqual(nest,B,C,10,&equal)); 71 PetscCheck(equal,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Error in C != nest*B_dense"); 72 73 /* Test B = nest*C, reuse C and B with MatProductCreateWithMat() */ 74 /* C has been obtained from nest*B. Clear internal data structures related to factors to prevent circular references */ 75 PetscCall(MatProductClear(C)); 76 PetscCall(MatProductCreateWithMat(nest,C,NULL,B)); 77 PetscCall(MatProductSetType(B,MATPRODUCT_AB)); 78 PetscCall(MatProductSetFromOptions(B)); 79 PetscCall(MatProductSymbolic(B)); 80 PetscCall(MatProductNumeric(B)); 81 PetscCall(MatMatMultEqual(nest,C,B,10,&equal)); 82 PetscCheck(equal,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Error in B != nest*C_dense"); 83 PetscCall(MatConvert(nest,MATAIJ,MAT_INPLACE_MATRIX,&nest)); 84 PetscCall(MatEqual(nest,aij,&equal)); 85 PetscCheck(equal,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Error in aij != nest"); 86 PetscCall(MatDestroy(&nest)); 87 88 if (size > 1) { /* Do not know why this test fails for size = 1 */ 89 PetscCall(MatCreateTranspose(A1,&A5)); /* A1 is symmetric */ 90 mata[0] = A5; 91 PetscCall(MatCreate(comm,&nest)); 92 PetscCall(MatSetType(nest,MATNEST)); 93 PetscCall(MatNestSetSubMats(nest,2,NULL,2,NULL,mata)); 94 PetscCall(MatSetUp(nest)); 95 PetscCall(MatMatMult(nest,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&C1)); 96 PetscCall(MatMatMult(nest,B,MAT_REUSE_MATRIX,PETSC_DEFAULT,&C1)); 97 98 PetscCall(MatMatMultEqual(nest,B,C1,10,&equal)); 99 PetscCheck(equal,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Error in C1 != C"); 100 PetscCall(MatDestroy(&C1)); 101 PetscCall(MatDestroy(&A5)); 102 PetscCall(MatDestroy(&nest)); 103 } 104 105 PetscCall(MatDestroy(&C)); 106 PetscCall(MatDestroy(&B)); 107 PetscCall(MatDestroy(&aij)); 108 PetscCall(MatDestroy(&A1)); 109 PetscCall(MatDestroy(&A2)); 110 PetscCall(MatDestroy(&A3)); 111 PetscCall(MatDestroy(&A4)); 112 PetscCall(PetscFinalize()); 113 return 0; 114 } 115 116 /*TEST 117 118 test: 119 nsize: 2 120 121 test: 122 suffix: 2 123 124 TEST*/ 125