xref: /petsc/src/mat/tests/ex218.c (revision 8fb5bd83c3955fefcf33a54e3bb66920a9fa884b)
1 
2 static char help[] = "Tests MatShellTestMult()\n\n";
3 
4 #include <petscmat.h>
5 
6 typedef struct _n_User *User;
7 struct _n_User {
8   Mat B;
9 };
10 
11 static PetscErrorCode MatMult_User(Mat A,Vec X,Vec Y)
12 {
13   User           user;
14 
15   PetscFunctionBegin;
16   PetscCall(MatShellGetContext(A,&user));
17   PetscCall(MatMult(user->B,X,Y));
18   PetscFunctionReturn(0);
19 }
20 
21 static PetscErrorCode MatMultTranspose_User(Mat A,Vec X,Vec Y)
22 {
23   User           user;
24 
25   PetscFunctionBegin;
26   PetscCall(MatShellGetContext(A,&user));
27   PetscCall(MatMultTranspose(user->B,X,Y));
28   PetscFunctionReturn(0);
29 }
30 
31 static PetscErrorCode MyFunction(void *ctx,Vec x,Vec y)
32 {
33   User           user = (User) ctx;
34 
35   PetscFunctionBegin;
36   PetscCall(MatMult(user->B,x,y));
37   PetscFunctionReturn(0);
38 }
39 
40 int main(int argc,char **args)
41 {
42   const PetscInt    inds[]  = {0,1};
43   PetscScalar       avals[] = {2,3,5,7};
44   Mat               S;
45   User              user;
46   Vec               base;
47 
48   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
49   PetscCall(PetscNew(&user));
50   PetscCall(MatCreateSeqAIJ(PETSC_COMM_WORLD,2,2,2,NULL,&user->B));
51   PetscCall(MatSetUp(user->B));
52   PetscCall(MatSetValues(user->B,2,inds,2,inds,avals,INSERT_VALUES));
53   PetscCall(MatAssemblyBegin(user->B,MAT_FINAL_ASSEMBLY));
54   PetscCall(MatAssemblyEnd(user->B,MAT_FINAL_ASSEMBLY));
55   PetscCall(MatCreateVecs(user->B,&base,NULL));
56   PetscCall(MatCreateShell(PETSC_COMM_WORLD,2,2,2,2,user,&S));
57   PetscCall(MatSetUp(S));
58   PetscCall(MatShellSetOperation(S,MATOP_MULT,(void (*)(void))MatMult_User));
59   PetscCall(MatShellSetOperation(S,MATOP_MULT_TRANSPOSE,(void (*)(void))MatMultTranspose_User));
60 
61   PetscCall(MatShellTestMult(S,MyFunction,base,user,NULL));
62   PetscCall(MatShellTestMultTranspose(S,MyFunction,base,user,NULL));
63 
64   PetscCall(VecDestroy(&base));
65   PetscCall(MatDestroy(&user->B));
66   PetscCall(MatDestroy(&S));
67   PetscCall(PetscFree(user));
68   PetscCall(PetscFinalize());
69   return 0;
70 }
71 
72 /*TEST
73 
74    test:
75      args: -mat_shell_test_mult_view -mat_shell_test_mult_transpose_view
76 
77 TEST*/
78