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