xref: /petsc/src/mat/impls/shell/ftn-custom/zshellf.c (revision 7911a5123f36558d95dece24220952b83053b598)
1 #include "private/fortranimpl.h"
2 #include "petscmat.h"
3 
4 #if defined(PETSC_HAVE_FORTRAN_CAPS)
5 #define matshellsetoperation_            MATSHELLSETOPERATION
6 #define matcreateshell_                  MATCREATESHELL
7 #define matshellgetcontext_              MATSHELLGETCONTEXT
8 #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
9 #define matcreateshell_                  matcreateshell
10 #define matshellsetoperation_            matshellsetoperation
11 #define matshellgetcontext_              matshellgetcontext
12 #endif
13 
14 EXTERN_C_BEGIN
15 
16 /*
17       The MatShell Matrix Vector product requires a C routine.
18    This C routine then calls the corresponding Fortran routine that was
19    set by the user.
20 */
21 void PETSC_STDCALL matcreateshell_(MPI_Comm *comm,PetscInt *m,PetscInt *n,PetscInt *M,PetscInt *N,void **ctx,Mat *mat,PetscErrorCode *ierr)
22 {
23   *ierr = MatCreateShell(MPI_Comm_f2c(*(MPI_Fint *)&*comm),*m,*n,*M,*N,*ctx,mat);
24 
25 }
26 
27 static PetscErrorCode ourmult(Mat mat,Vec x,Vec y)
28 {
29   PetscErrorCode ierr = 0;
30   (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[0]))(&mat,&x,&y,&ierr);
31   return ierr;
32 }
33 
34 static PetscErrorCode ourmulttranspose(Mat mat,Vec x,Vec y)
35 {
36   PetscErrorCode ierr = 0;
37   (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[2]))(&mat,&x,&y,&ierr);
38   return ierr;
39 }
40 
41 static PetscErrorCode ourmultadd(Mat mat,Vec x,Vec y,Vec z)
42 {
43   PetscErrorCode ierr = 0;
44   (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[1]))(&mat,&x,&y,&z,&ierr);
45   return ierr;
46 }
47 
48 static PetscErrorCode ourmulttransposeadd(Mat mat,Vec x,Vec y,Vec z)
49 {
50   PetscErrorCode ierr = 0;
51   (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[3]))(&mat,&x,&y,&z,&ierr);
52   return ierr;
53 }
54 
55 static PetscErrorCode ourgetdiagonal(Mat mat,Vec x)
56 {
57   PetscErrorCode ierr = 0;
58   (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[4]))(&mat,&x,&ierr);
59   return ierr;
60 }
61 
62 static PetscErrorCode ourdiagonalscale(Mat mat,Vec l,Vec r)
63 {
64   PetscErrorCode ierr = 0;
65   if (!l) {
66     (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[5]))(&mat,(Vec*)PETSC_NULL_OBJECT_Fortran,&r,&ierr);
67   } else if (!r) {
68     (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[5]))(&mat,&l,(Vec*)PETSC_NULL_OBJECT_Fortran,&ierr);
69   } else {
70     (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[5]))(&mat,&l,&r,&ierr);
71   }
72   return ierr;
73 }
74 
75 static PetscErrorCode ourgetvecs(Mat mat,Vec *l,Vec *r)
76 {
77   PetscErrorCode ierr = 0;
78   if (!l) {
79     (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[7]))(&mat,(Vec*)PETSC_NULL_OBJECT_Fortran,r,&ierr);
80   } else if (!r) {
81     (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[7]))(&mat,l,(Vec*)PETSC_NULL_OBJECT_Fortran,&ierr);
82   } else {
83     (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,Vec*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[7]))(&mat,l,r,&ierr);
84   }
85   return ierr;
86 }
87 
88 static PetscErrorCode ourdiagonalset(Mat mat,Vec x,InsertMode ins)
89 {
90   PetscErrorCode ierr = 0;
91   (*(PetscErrorCode (PETSC_STDCALL *)(Mat*,Vec*,InsertMode*,PetscErrorCode*))(((PetscObject)mat)->fortran_func_pointers[6]))(&mat,&x,&ins,&ierr);
92   return ierr;
93 }
94 
95 void PETSC_STDCALL matshellsetoperation_(Mat *mat,MatOperation *op,PetscErrorCode (PETSC_STDCALL *f)(Mat*,Vec*,Vec*,PetscErrorCode*),PetscErrorCode *ierr)
96 {
97   PetscObjectAllocateFortranPointers(*mat,8);
98   if (*op == MATOP_MULT) {
99     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourmult);
100     ((PetscObject)*mat)->fortran_func_pointers[0] = (PetscVoidFunction)f;
101   } else if (*op == MATOP_MULT_TRANSPOSE) {
102     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourmulttranspose);
103     ((PetscObject)*mat)->fortran_func_pointers[2] = (PetscVoidFunction)f;
104   } else if (*op == MATOP_MULT_ADD) {
105     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourmultadd);
106     ((PetscObject)*mat)->fortran_func_pointers[1] = (PetscVoidFunction)f;
107   } else if (*op == MATOP_MULT_TRANSPOSE_ADD) {
108     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourmulttransposeadd);
109     ((PetscObject)*mat)->fortran_func_pointers[3] = (PetscVoidFunction)f;
110   } else if (*op == MATOP_GET_DIAGONAL) {
111     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourgetdiagonal);
112     ((PetscObject)*mat)->fortran_func_pointers[4] = (PetscVoidFunction)f;
113   } else if (*op == MATOP_DIAGONAL_SCALE) {
114     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourdiagonalscale);
115     ((PetscObject)*mat)->fortran_func_pointers[5] = (PetscVoidFunction)f;
116   } else if (*op == MATOP_DIAGONAL_SET) {
117     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourdiagonalset);
118     ((PetscObject)*mat)->fortran_func_pointers[6] = (PetscVoidFunction)f;
119   } else if (*op == MATOP_GET_VECS) {
120     *ierr = MatShellSetOperation(*mat,*op,(PetscVoidFunction)ourgetvecs);
121     ((PetscObject)*mat)->fortran_func_pointers[7] = (PetscVoidFunction)f;
122   } else {
123     PetscError(__LINE__,"MatShellSetOperation_Fortran",__FILE__,__SDIR__,1,0,
124                "Cannot set that matrix operation");
125     *ierr = 1;
126   }
127 }
128 
129 void PETSC_STDCALL matshellgetcontext_(Mat *mat,void **ctx,PetscErrorCode *ierr)
130 {
131   *ierr = MatShellGetContext(*mat,ctx);
132 }
133 
134 
135 EXTERN_C_END
136