Actual source code: fortranimpl.h

petsc-3.8.4 2018-03-24
Report Typos and Errors

  2: /* This file contains info for the use of PETSc Fortran interface stubs */
  3: #if !defined(_FORTRANIMPL_H)
  4: #define _FORTRANIMPL_H

  6:  #include <petsc/private/petscimpl.h>

  8: /* PETSC_STDCALL is defined on some Microsoft Windows systems and is used for functions compiled by the Fortran compiler */
  9: #if !defined(PETSC_STDCALL)
 10: #define PETSC_STDCALL
 11: #endif

 13: PETSC_EXTERN PetscErrorCode PetscScalarAddressToFortran(PetscObject,PetscInt,PetscScalar*,PetscScalar*,PetscInt,size_t*);
 14: PETSC_EXTERN PetscErrorCode PetscScalarAddressFromFortran(PetscObject,PetscScalar*,size_t,PetscInt,PetscScalar **);
 15: PETSC_EXTERN size_t         PetscIntAddressToFortran(const PetscInt*,const PetscInt*);
 16: PETSC_EXTERN PetscInt        *PetscIntAddressFromFortran(const PetscInt*,size_t);
 17: PETSC_EXTERN char   *PETSC_NULL_CHARACTER_Fortran;
 18: PETSC_EXTERN void    *PETSC_NULL_INTEGER_Fortran;
 19: PETSC_EXTERN void    *PETSC_NULL_SCALAR_Fortran;
 20: PETSC_EXTERN void    *PETSC_NULL_DOUBLE_Fortran;
 21: PETSC_EXTERN void    *PETSC_NULL_REAL_Fortran;
 22: PETSC_EXTERN void    *PETSC_NULL_BOOL_Fortran;
 23: PETSC_EXTERN void (*PETSC_NULL_FUNCTION_Fortran)(void);
 24: /*  ----------------------------------------------------------------------*/
 25: /*
 26:    PETSc object C pointers are stored directly as
 27:    Fortran integer*4 or *8 depending on the size of pointers.
 28: */


 31: /* --------------------------------------------------------------------*/
 32: /*
 33:     This lets us map the str-len argument either, immediately following
 34:     the char argument (DVF on Win32) or at the end of the argument list
 35:     (general unix compilers)
 36: */
 37: #if defined(PETSC_HAVE_FORTRAN_MIXED_STR_ARG)
 38: #define PETSC_MIXED_LEN(len) ,int len
 39: #define PETSC_END_LEN(len)
 40: #define PETSC_MIXED_LEN_CALL(len) ,len
 41: #define PETSC_END_LEN_CALL(len)
 42: #define PETSC_MIXED_LEN_PROTO ,int
 43: #define PETSC_END_LEN_PROTO
 44: #else
 45: #define PETSC_MIXED_LEN(len)
 46: #define PETSC_END_LEN(len)   ,int len
 47: #define PETSC_MIXED_LEN_CALL(len)
 48: #define PETSC_END_LEN_CALL(len)   ,len
 49: #define PETSC_MIXED_LEN_PROTO
 50: #define PETSC_END_LEN_PROTO   ,int
 51: #endif

 53: /* --------------------------------------------------------------------*/
 54: /*
 55:     Since Fortran does not null terminate strings we need to insure the string is null terminated before passing it
 56:     to C. This may require a memory allocation which is then freed with FREECHAR().
 57: */
 58: #define FIXCHAR(a,n,b) \
 59: {\
 60:   if (a == PETSC_NULL_CHARACTER_Fortran) { \
 61:     b = a = 0; \
 62:   } else { \
 63:     while((n > 0) && (a[n-1] == ' ')) n--; \
 64:     *PetscMalloc1(n+1,&b); \
 65:     if (*ierr) return; \
 66:     *PetscStrncpy(b,a,n+1); \
 67:     if (*ierr) return; \
 68:   } \
 69: }
 70: #define FREECHAR(a,b) if (a != b) *PetscFree(b);

 72: /*
 73:     Fortran expects any unneeded characters at the end of its strings to be filled with the blank character.
 74: */
 75: #define FIXRETURNCHAR(flg,a,n)               \
 76: if (flg) {                                   \
 77:   int __i;                                   \
 78:   for (__i=0; __i<n && a[__i] != 0; __i++) {};  \
 79:   for (; __i<n; __i++) a[__i] = ' ' ; \
 80: }

 82: /*
 83:     The cast through PETSC_UINTPTR_T is so that compilers that warn about casting to/from void * to void(*)(void)
 84:     will not complain about these comparisons. It is not know if this works for all compilers
 85: */
 86: #define FORTRANNULLINTEGER(a)   (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_INTEGER_Fortran)
 87: #define FORTRANNULLSCALAR(a)    (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_SCALAR_Fortran)
 88: #define FORTRANNULLDOUBLE(a)    (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_DOUBLE_Fortran)
 89: #define FORTRANNULLREAL(a)      (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_REAL_Fortran)
 90: #define FORTRANNULLBOOL(a)      (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_BOOL_Fortran)
 91: #define FORTRANNULLCHARACTER(a) (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_CHARACTER_Fortran)
 92: #define FORTRANNULLFUNCTION(a)  (((void(*)(void))(PETSC_UINTPTR_T)a) == PETSC_NULL_FUNCTION_Fortran)
 93: #define FORTRANNULLOBJECT(a)    (*(void**)(PETSC_UINTPTR_T)a == (void*)-1)

 95: #define CHKFORTRANNULLINTEGER(a)  \
 96:   if (FORTRANNULLDOUBLE(a) || FORTRANNULLSCALAR(a) || FORTRANNULLREAL(a)  || FORTRANNULLBOOL(a) || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
 97:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
 98:     "Use PETSC_NULL_INTEGER"); *1; return; } \
 99:   else if (FORTRANNULLINTEGER(a)) { a = NULL; }

101: #define CHKFORTRANNULLSCALAR(a)   \
102:   if (FORTRANNULLINTEGER(a) || FORTRANNULLDOUBLE(a) || FORTRANNULLREAL(a)  || FORTRANNULLBOOL(a) || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
103:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
104:     "Use PETSC_NULL_SCALAR"); *1; return; } \
105:   else if (FORTRANNULLSCALAR(a)) { a = NULL; }

107: #define CHKFORTRANNULLDOUBLE(a)  \
108:   if (FORTRANNULLINTEGER(a) || FORTRANNULLSCALAR(a) || FORTRANNULLREAL(a)  || FORTRANNULLBOOL(a) || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
109:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
110:     "Use PETSC_NULL_DOUBLE"); *1; return; } \
111:   else if (FORTRANNULLDOUBLE(a)) { a = NULL; }

113: #define CHKFORTRANNULLREAL(a)  \
114:   if (FORTRANNULLINTEGER(a) || FORTRANNULLDOUBLE(a) || FORTRANNULLSCALAR(a)  || FORTRANNULLBOOL(a) || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
115:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
116:     "Use PETSC_NULL_REAL"); *1; return; } \
117:   else if (FORTRANNULLREAL(a)) { a = NULL; }

119: /*
120:    The next two macros can generate false positives for Valgrind if the object passed
121:    in has never been set before because the location (void**)a has never had a value
122:    set to it. To prevent the false positive in the Fortran code one can initialize the
123:    object with a = tXXX(0); for example a = tVec(0)
124: */
125: #define CHKFORTRANNULLOBJECT(a)  \
126:   if (FORTRANNULLINTEGER(a) || FORTRANNULLDOUBLE(a) || FORTRANNULLSCALAR(a) || FORTRANNULLREAL(a) || FORTRANNULLBOOL(a) || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
127:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
128:     "Use PETSC_NULL_XXX where XXX is the name of a particular object class"); *1; return; } \
129:   else if (*(void**)a == (void*)-1) { a = NULL; }

131: PETSC_EXTERN void  *PETSCNULLPOINTERADDRESS;

133: #define CHKFORTRANNULLOBJECTDEREFERENCE(a)  \
134:   if (FORTRANNULLSCALAR(a) || FORTRANNULLDOUBLE(a) || FORTRANNULLREAL(a) || FORTRANNULLINTEGER(a) || FORTRANNULLBOOL(a) || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
135:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
136:     "Use PETSC_NULL_XXX where XXX is the name of a particular object class"); *1; return; } \
137:   else if (*(void**)a == (void*)-1) { *((void***)&a) = &PETSCNULLPOINTERADDRESS; }


140: #define CHKFORTRANNULLBOOL(a)  \
141:   if (FORTRANNULLSCALAR(a) || FORTRANNULLINTEGER(a) || FORTRANNULLDOUBLE(a) || FORTRANNULLSCALAR(a) || FORTRANNULLREAL(a)  || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
142:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
143:     "Use PETSC_NULL_BOOL"); *1; return; } \
144:   else if (FORTRANNULLBOOL(a)) { a = NULL; }

146: #define CHKFORTRANNULLFUNCTION(a)  \
147:   if (FORTRANNULLOBJECT(a) || FORTRANNULLSCALAR(a) || FORTRANNULLDOUBLE(a) || FORTRANNULLREAL(a) || FORTRANNULLINTEGER(a) || FORTRANNULLBOOL(a) || FORTRANNULLCHARACTER(a)) { \
148:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
149:     "Use PETSC_NULL_FUNCTION"); *1; return; } \
150:   else if (FORTRANNULLFUNCTION(a)) { a = NULL; }



154: /*
155:     Variable type where we stash PETSc object pointers in Fortran.
156: */
157: typedef PETSC_UINTPTR_T PetscFortranAddr;

159: /*
160:     These are used to support the default viewers that are
161:   created at run time, in C using the , trick.

163:     The numbers here must match the numbers in include/petsc/finclude/petscsys.h
164: */
165: #define PETSC_VIEWER_DRAW_WORLD_FORTRAN     4
166: #define PETSC_VIEWER_DRAW_SELF_FORTRAN      5
167: #define PETSC_VIEWER_SOCKET_WORLD_FORTRAN   6
168: #define PETSC_VIEWER_SOCKET_SELF_FORTRAN    7
169: #define PETSC_VIEWER_STDOUT_WORLD_FORTRAN   8
170: #define PETSC_VIEWER_STDOUT_SELF_FORTRAN    9
171: #define PETSC_VIEWER_STDERR_WORLD_FORTRAN   10
172: #define PETSC_VIEWER_STDERR_SELF_FORTRAN    11
173: #define PETSC_VIEWER_BINARY_WORLD_FORTRAN   12
174: #define PETSC_VIEWER_BINARY_SELF_FORTRAN    13
175: #define PETSC_VIEWER_MATLAB_WORLD_FORTRAN   14
176: #define PETSC_VIEWER_MATLAB_SELF_FORTRAN    15

178: #if defined (PETSC_USE_SOCKET_VIEWER)
179: #define PetscPatchDefaultViewers_Fortran_Socket(vin,v) \
180:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_SOCKET_WORLD_FORTRAN) { \
181:       v = PETSC_VIEWER_SOCKET_WORLD; \
182:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_SOCKET_SELF_FORTRAN) { \
183:       v = PETSC_VIEWER_SOCKET_SELF
184: #else
185: #define PetscPatchDefaultViewers_Fortran_Socket(vin,v)
186: #endif

188: #define PetscPatchDefaultViewers_Fortran(vin,v) \
189: { \
190:     CHKFORTRANNULLOBJECTDEREFERENCE(vin);\
191:     if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_DRAW_WORLD_FORTRAN) { \
192:       v = PETSC_VIEWER_DRAW_WORLD; \
193:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_DRAW_SELF_FORTRAN) { \
194:       v = PETSC_VIEWER_DRAW_SELF; \
195:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_STDOUT_WORLD_FORTRAN) { \
196:       v = PETSC_VIEWER_STDOUT_WORLD; \
197:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_STDOUT_SELF_FORTRAN) { \
198:       v = PETSC_VIEWER_STDOUT_SELF; \
199:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_STDERR_WORLD_FORTRAN) { \
200:       v = PETSC_VIEWER_STDERR_WORLD; \
201:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_STDERR_SELF_FORTRAN) { \
202:       v = PETSC_VIEWER_STDERR_SELF; \
203:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_BINARY_WORLD_FORTRAN) { \
204:       v = PETSC_VIEWER_BINARY_WORLD; \
205:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_BINARY_SELF_FORTRAN) { \
206:       v = PETSC_VIEWER_BINARY_SELF; \
207:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_MATLAB_WORLD_FORTRAN) { \
208:       v = PETSC_VIEWER_BINARY_WORLD; \
209:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_MATLAB_SELF_FORTRAN) { \
210:       v = PETSC_VIEWER_BINARY_SELF; \
211:     PetscPatchDefaultViewers_Fortran_Socket(vin,v); \
212:     } else { \
213:       v = *vin; \
214:     } \
215: }

217: /*
218:       Allocates enough space to store Fortran function pointers in PETSc object
219:    that are needed by the Fortran interface.
220: */
221: #define PetscObjectAllocateFortranPointers(obj,N) do {                  \
222:     if (!((PetscObject)(obj))->fortran_func_pointers) {                 \
223:       *PetscMalloc((N)*sizeof(void(*)(void)),&((PetscObject)(obj))->fortran_func_pointers);if (*ierr) return; \
224:       *PetscMemzero(((PetscObject)(obj))->fortran_func_pointers,(N)*sizeof(void(*)(void)));if (*ierr) return; \
225:       ((PetscObject)obj)->num_fortran_func_pointers = (N);              \
226:     }                                                                   \
227:   } while (0)

229: /* Entire function body, _ctx is a "special" variable that can be passed along */
230: #define PetscObjectUseFortranCallback_Private(obj,cid,types,args,cbclass) { \
232:     void (PETSC_STDCALL *func) types,*_ctx;                             \
234:     PetscObjectGetFortranCallback((PetscObject)(obj),(cbclass),(cid),(PetscVoidFunction*)&func,&_ctx); \
235:     if (func) {(*func)args;}                              \
236:     return(0);                                             \
237:   }
238: #define PetscObjectUseFortranCallback(obj,cid,types,args) PetscObjectUseFortranCallback_Private(obj,cid,types,args,PETSC_FORTRAN_CALLBACK_CLASS)
239: #define PetscObjectUseFortranCallbackSubType(obj,cid,types,args) PetscObjectUseFortranCallback_Private(obj,cid,types,args,PETSC_FORTRAN_CALLBACK_SUBTYPE)

241: /* Disable deprecation warnings while building Fortran wrappers */
242: #undef  PETSC_DEPRECATED
243: #define PETSC_DEPRECATED(arg)

245: #endif