1 static char help[] = "Tests MatCopy() for SHELL matrices\n\n"; 2 3 #include <petscmat.h> 4 5 typedef struct _n_User *User; 6 struct _n_User { 7 Mat A; 8 }; 9 10 static PetscErrorCode MatMult_User(Mat A,Vec X,Vec Y) 11 { 12 User user; 13 14 PetscFunctionBegin; 15 PetscCall(MatShellGetContext(A,&user)); 16 PetscCall(MatMult(user->A,X,Y)); 17 PetscFunctionReturn(0); 18 } 19 20 static PetscErrorCode MatCopy_User(Mat A,Mat B,MatStructure str) 21 { 22 User userA,userB; 23 24 PetscFunctionBegin; 25 PetscCall(MatShellGetContext(A,&userA)); 26 if (userA) { 27 PetscCall(PetscNew(&userB)); 28 PetscCall(MatDuplicate(userA->A,MAT_COPY_VALUES,&userB->A)); 29 PetscCall(MatShellSetContext(B, userB)); 30 } 31 PetscFunctionReturn(0); 32 } 33 34 static PetscErrorCode MatDestroy_User(Mat A) 35 { 36 User user; 37 38 PetscFunctionBegin; 39 PetscCall(MatShellGetContext(A, &user)); 40 if (user) { 41 PetscCall(MatDestroy(&user->A)); 42 PetscCall(PetscFree(user)); 43 } 44 PetscFunctionReturn(0); 45 } 46 47 int main(int argc,char **args) 48 { 49 const PetscScalar xvals[] = {11,13},yvals[] = {17,19}; 50 const PetscInt inds[] = {0,1}; 51 PetscScalar avals[] = {2,3,5,7}; 52 Mat S1,S2; 53 Vec X,Y; 54 User user; 55 56 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 57 58 PetscCall(PetscNew(&user)); 59 PetscCall(MatCreateSeqAIJ(PETSC_COMM_WORLD,2,2,2,NULL,&user->A)); 60 PetscCall(MatSetUp(user->A)); 61 PetscCall(MatSetValues(user->A,2,inds,2,inds,avals,INSERT_VALUES)); 62 PetscCall(MatAssemblyBegin(user->A,MAT_FINAL_ASSEMBLY)); 63 PetscCall(MatAssemblyEnd(user->A,MAT_FINAL_ASSEMBLY)); 64 PetscCall(VecCreateSeq(PETSC_COMM_WORLD,2,&X)); 65 PetscCall(VecSetValues(X,2,inds,xvals,INSERT_VALUES)); 66 PetscCall(VecAssemblyBegin(X)); 67 PetscCall(VecAssemblyEnd(X)); 68 PetscCall(VecDuplicate(X,&Y)); 69 PetscCall(VecSetValues(Y,2,inds,yvals,INSERT_VALUES)); 70 PetscCall(VecAssemblyBegin(Y)); 71 PetscCall(VecAssemblyEnd(Y)); 72 73 PetscCall(MatCreateShell(PETSC_COMM_WORLD,2,2,2,2,user,&S1)); 74 PetscCall(MatSetUp(S1)); 75 PetscCall(MatShellSetOperation(S1,MATOP_MULT,(void (*)(void))MatMult_User)); 76 PetscCall(MatShellSetOperation(S1,MATOP_COPY,(void (*)(void))MatCopy_User)); 77 PetscCall(MatShellSetOperation(S1,MATOP_DESTROY,(void (*)(void))MatDestroy_User)); 78 PetscCall(MatCreateShell(PETSC_COMM_WORLD,2,2,2,2,NULL,&S2)); 79 PetscCall(MatSetUp(S2)); 80 PetscCall(MatShellSetOperation(S2,MATOP_MULT,(void (*)(void))MatMult_User)); 81 PetscCall(MatShellSetOperation(S2,MATOP_COPY,(void (*)(void))MatCopy_User)); 82 PetscCall(MatShellSetOperation(S2,MATOP_DESTROY,(void (*)(void))MatDestroy_User)); 83 84 PetscCall(MatScale(S1,31)); 85 PetscCall(MatShift(S1,37)); 86 PetscCall(MatDiagonalScale(S1,X,Y)); 87 PetscCall(MatCopy(S1,S2,SAME_NONZERO_PATTERN)); 88 PetscCall(MatMult(S1,X,Y)); 89 PetscCall(VecView(Y,PETSC_VIEWER_STDOUT_WORLD)); 90 PetscCall(MatMult(S2,X,Y)); 91 PetscCall(VecView(Y,PETSC_VIEWER_STDOUT_WORLD)); 92 93 PetscCall(MatDestroy(&S1)); 94 PetscCall(MatDestroy(&S2)); 95 PetscCall(VecDestroy(&X)); 96 PetscCall(VecDestroy(&Y)); 97 PetscCall(PetscFinalize()); 98 return 0; 99 } 100 101 /*TEST 102 103 test: 104 args: -malloc_dump 105 106 TEST*/ 107