xref: /petsc/src/mat/impls/shell/ftn-custom/zshellf.c (revision c8cd8b03a8e69285ecfea39db41c0ff684948fa5)
1 #include "private/zpetsc.h"
2 #include "petscmat.h"
3 
4 #if defined(PETSC_HAVE_FORTRAN_CAPS)
5 #define matshellsetoperation_            MATSHELLSETOPERATION
6 #define matcreateshell_                  MATCREATESHELL
7 #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
8 #define matcreateshell_                  matcreateshell
9 #define matshellsetoperation_            matshellsetoperation
10 #endif
11 
12 EXTERN_C_BEGIN
13 
14 /*
15       The MatShell Matrix Vector product requires a C routine.
16    This C routine then calls the corresponding Fortran routine that was
17    set by the user.
18 */
19 void PETSC_STDCALL matcreateshell_(MPI_Comm *comm,PetscInt *m,PetscInt *n,PetscInt *M,PetscInt *N,void **ctx,Mat *mat,PetscErrorCode *ierr)
20 {
21   *ierr = MatCreateShell((MPI_Comm)PetscToPointerComm(*comm),*m,*n,*M,*N,*ctx,mat);
22   if (*ierr) return;
23   *ierr = PetscMalloc(5*sizeof(void*),&((PetscObject)*mat)->fortran_func_pointers);
24 }
25 
26 static PetscErrorCode ourmult(Mat mat,Vec x,Vec y)
27 {
28   PetscErrorCode ierr = 0;
29   (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[0]))(&mat,&x,&y,&ierr);
30   return ierr;
31 }
32 
33 static PetscErrorCode ourmulttranspose(Mat mat,Vec x,Vec y)
34 {
35   PetscErrorCode ierr = 0;
36   (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[2]))(&mat,&x,&y,&ierr);
37   return ierr;
38 }
39 
40 static PetscErrorCode ourmultadd(Mat mat,Vec x,Vec y,Vec z)
41 {
42   PetscErrorCode ierr = 0;
43   (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[1]))(&mat,&x,&y,&z,&ierr);
44   return ierr;
45 }
46 
47 static PetscErrorCode ourmulttransposeadd(Mat mat,Vec x,Vec y,Vec z)
48 {
49   PetscErrorCode ierr = 0;
50   (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[3]))(&mat,&x,&y,&z,&ierr);
51   return ierr;
52 }
53 
54 static PetscErrorCode ourgetdiagonal(Mat mat,Vec x)
55 {
56   PetscErrorCode ierr = 0;
57   (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[4]))(&mat,&x,&ierr);
58   return ierr;
59 }
60 
61 void PETSC_STDCALL matshellsetoperation_(Mat *mat,MatOperation *op,PetscErrorCode (PETSC_STDCALL *f)(Mat*,Vec*,Vec*,PetscErrorCode*),PetscErrorCode *ierr)
62 {
63   if (*op == MATOP_MULT) {
64     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourmult);
65     ((PetscObject)*mat)->fortran_func_pointers[0] = (PetscVoidFunction)f;
66   } else if (*op == MATOP_MULT_TRANSPOSE) {
67     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourmulttranspose);
68     ((PetscObject)*mat)->fortran_func_pointers[2] = (PetscVoidFunction)f;
69   } else if (*op == MATOP_MULT_ADD) {
70     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourmultadd);
71     ((PetscObject)*mat)->fortran_func_pointers[1] = (PetscVoidFunction)f;
72   } else if (*op == MATOP_MULT_TRANSPOSE_ADD) {
73     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourmulttransposeadd);
74     ((PetscObject)*mat)->fortran_func_pointers[3] = (PetscVoidFunction)f;
75   } else if (*op == MATOP_GET_DIAGONAL) {
76     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourgetdiagonal);
77     ((PetscObject)*mat)->fortran_func_pointers[4] = (PetscVoidFunction)f;
78   } else {
79     PetscError(__LINE__,"MatShellSetOperation_Fortran",__FILE__,__SDIR__,1,0,
80                "Cannot set that matrix operation");
81     *ierr = 1;
82   }
83 }
84 
85 EXTERN_C_END
86