Actual source code: fortranimpl.h

petsc-master 2020-11-24
Report Typos and Errors

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

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

  8: PETSC_EXTERN PetscErrorCode PetscMPIFortranDatatypeToC(MPI_Fint,MPI_Datatype*);

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

 27: /* --------------------------------------------------------------------*/
 28: /*
 29:     Since Fortran does not null terminate strings we need to insure the string is null terminated before passing it
 30:     to C. This may require a memory allocation which is then freed with FREECHAR().
 31: */
 32: #define FIXCHAR(a,n,b) \
 33: {\
 34:   if (a == PETSC_NULL_CHARACTER_Fortran) { \
 35:     b = a = NULL; \
 36:   } else { \
 37:     while ((n > 0) && (a[n-1] == ' ')) n--; \
 38:     *PetscMalloc1(n+1,&b); \
 39:     if (*ierr) return; \
 40:     *PetscStrncpy(b,a,n+1); \
 41:     if (*ierr) return; \
 42:   } \
 43: }
 44: #define FREECHAR(a,b) if (a != b) *PetscFree(b);

 46: /*
 47:     Fortran expects any unneeded characters at the end of its strings to be filled with the blank character.
 48: */
 49: #define FIXRETURNCHAR(flg,a,n)               \
 50: if (flg) {                                   \
 51:   PETSC_FORTRAN_CHARLEN_T __i;               \
 52:   for (__i=0; __i<n && a[__i] != 0; __i++) {};  \
 53:   for (; __i<n; __i++) a[__i] = ' ' ; \
 54: }

 56: /*
 57:     The cast through PETSC_UINTPTR_T is so that compilers that warn about casting to/from void * to void(*)(void)
 58:     will not complain about these comparisons. It is not know if this works for all compilers
 59: */
 60: #define FORTRANNULLINTEGER(a)   (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_INTEGER_Fortran)
 61: #define FORTRANNULLSCALAR(a)    (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_SCALAR_Fortran)
 62: #define FORTRANNULLDOUBLE(a)    (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_DOUBLE_Fortran)
 63: #define FORTRANNULLREAL(a)      (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_REAL_Fortran)
 64: #define FORTRANNULLBOOL(a)      (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_BOOL_Fortran)
 65: #define FORTRANNULLCHARACTER(a) (((void*)(PETSC_UINTPTR_T)a) == PETSC_NULL_CHARACTER_Fortran)
 66: #define FORTRANNULLFUNCTION(a)  (((void(*)(void))(PETSC_UINTPTR_T)a) == PETSC_NULL_FUNCTION_Fortran)
 67: #define FORTRANNULLOBJECT(a)    (*(void**)(PETSC_UINTPTR_T)a == (void*)0)

 69: #define CHKFORTRANNULLINTEGER(a)  \
 70:   if (FORTRANNULLDOUBLE(a) || FORTRANNULLSCALAR(a) || FORTRANNULLREAL(a)  || FORTRANNULLBOOL(a) || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
 71:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
 72:     "Use PETSC_NULL_INTEGER"); *1; return; } \
 73:   else if (FORTRANNULLINTEGER(a)) { a = NULL; }

 75: #define CHKFORTRANNULLSCALAR(a)   \
 76:   if (FORTRANNULLINTEGER(a) || FORTRANNULLDOUBLE(a) || FORTRANNULLREAL(a)  || FORTRANNULLBOOL(a) || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
 77:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
 78:     "Use PETSC_NULL_SCALAR"); *1; return; } \
 79:   else if (FORTRANNULLSCALAR(a)) { a = NULL; }

 81: #define CHKFORTRANNULLDOUBLE(a)  \
 82:   if (FORTRANNULLINTEGER(a) || FORTRANNULLSCALAR(a) || FORTRANNULLREAL(a)  || FORTRANNULLBOOL(a) || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
 83:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
 84:     "Use PETSC_NULL_DOUBLE"); *1; return; } \
 85:   else if (FORTRANNULLDOUBLE(a)) { a = NULL; }

 87: #define CHKFORTRANNULLREAL(a)  \
 88:   if (FORTRANNULLINTEGER(a) || FORTRANNULLDOUBLE(a) || FORTRANNULLSCALAR(a)  || FORTRANNULLBOOL(a) || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
 89:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
 90:     "Use PETSC_NULL_REAL"); *1; return; } \
 91:   else if (FORTRANNULLREAL(a)) { a = NULL; }

 93: #define CHKFORTRANNULLOBJECT(a)  \
 94:   if (FORTRANNULLINTEGER(a) || FORTRANNULLDOUBLE(a) || FORTRANNULLSCALAR(a) || FORTRANNULLREAL(a) || FORTRANNULLBOOL(a) || FORTRANNULLFUNCTION(a) || FORTRANNULLCHARACTER(a)) { \
 95:     PetscError(PETSC_COMM_SELF,__LINE__,"fortran_interface_unknown_file",__FILE__,PETSC_ERR_ARG_WRONG,PETSC_ERROR_INITIAL, \
 96:     "Use PETSC_NULL_XXX where XXX is the name of a particular object class"); *1; return; } \
 97:   else if (*(void**)a == (void*)0) { a = NULL; }

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

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



113: /*
114:     Variable type where we stash PETSc object pointers in Fortran.
115: */
116: typedef PETSC_UINTPTR_T PetscFortranAddr;

118: /*
119:     These are used to support the default viewers that are
120:   created at run time, in C using the , trick.

122:     The numbers here must match the numbers in include/petsc/finclude/petscsys.h
123: */
124: #define PETSC_VIEWER_DRAW_WORLD_FORTRAN     4
125: #define PETSC_VIEWER_DRAW_SELF_FORTRAN      5
126: #define PETSC_VIEWER_SOCKET_WORLD_FORTRAN   6
127: #define PETSC_VIEWER_SOCKET_SELF_FORTRAN    7
128: #define PETSC_VIEWER_STDOUT_WORLD_FORTRAN   8
129: #define PETSC_VIEWER_STDOUT_SELF_FORTRAN    9
130: #define PETSC_VIEWER_STDERR_WORLD_FORTRAN   10
131: #define PETSC_VIEWER_STDERR_SELF_FORTRAN    11
132: #define PETSC_VIEWER_BINARY_WORLD_FORTRAN   12
133: #define PETSC_VIEWER_BINARY_SELF_FORTRAN    13
134: #define PETSC_VIEWER_MATLAB_WORLD_FORTRAN   14
135: #define PETSC_VIEWER_MATLAB_SELF_FORTRAN    15

137: #if defined (PETSC_USE_SOCKET_VIEWER)
138: #define PetscPatchDefaultViewers_Fortran_Socket(vin,v) \
139:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_SOCKET_WORLD_FORTRAN) { \
140:       v = PETSC_VIEWER_SOCKET_WORLD; \
141:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_SOCKET_SELF_FORTRAN) { \
142:       v = PETSC_VIEWER_SOCKET_SELF
143: #else
144: #define PetscPatchDefaultViewers_Fortran_Socket(vin,v)
145: #endif

147: #define PetscPatchDefaultViewers_Fortran(vin,v) \
148: { \
149:     if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_DRAW_WORLD_FORTRAN) { \
150:       v = PETSC_VIEWER_DRAW_WORLD; \
151:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_DRAW_SELF_FORTRAN) { \
152:       v = PETSC_VIEWER_DRAW_SELF; \
153:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_STDOUT_WORLD_FORTRAN) { \
154:       v = PETSC_VIEWER_STDOUT_WORLD; \
155:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_STDOUT_SELF_FORTRAN) { \
156:       v = PETSC_VIEWER_STDOUT_SELF; \
157:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_STDERR_WORLD_FORTRAN) { \
158:       v = PETSC_VIEWER_STDERR_WORLD; \
159:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_STDERR_SELF_FORTRAN) { \
160:       v = PETSC_VIEWER_STDERR_SELF; \
161:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_BINARY_WORLD_FORTRAN) { \
162:       v = PETSC_VIEWER_BINARY_WORLD; \
163:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_BINARY_SELF_FORTRAN) { \
164:       v = PETSC_VIEWER_BINARY_SELF; \
165:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_MATLAB_WORLD_FORTRAN) { \
166:       v = PETSC_VIEWER_BINARY_WORLD; \
167:     } else if ((*(PetscFortranAddr*)vin) == PETSC_VIEWER_MATLAB_SELF_FORTRAN) { \
168:       v = PETSC_VIEWER_BINARY_SELF; \
169:     PetscPatchDefaultViewers_Fortran_Socket(vin,v); \
170:     } else { \
171:       v = *vin; \
172:     } \
173: }

175: /*
176:       Allocates enough space to store Fortran function pointers in PETSc object
177:    that are needed by the Fortran interface.
178: */
179: #define PetscObjectAllocateFortranPointers(obj,N) do {                  \
180:     if (!((PetscObject)(obj))->fortran_func_pointers) {                 \
181:       *PetscMalloc((N)*sizeof(void(*)(void)),&((PetscObject)(obj))->fortran_func_pointers);if (*ierr) return; \
182:       *PetscMemzero(((PetscObject)(obj))->fortran_func_pointers,(N)*sizeof(void(*)(void)));if (*ierr) return; \
183:       ((PetscObject)obj)->num_fortran_func_pointers = (N);              \
184:     }                                                                   \
185:   } while (0)

187: /* Entire function body, _ctx is a "special" variable that can be passed along */
188: #define PetscObjectUseFortranCallback_Private(obj,cid,types,args,cbclass) { \
190:     void (*func) types,*_ctx;                             \
192:     PetscObjectGetFortranCallback((PetscObject)(obj),(cbclass),(cid),(PetscVoidFunction*)&func,&_ctx); \
193:     if (func) {(*func)args;}                              \
194:     return(0);                                             \
195:   }
196: #define PetscObjectUseFortranCallback(obj,cid,types,args) PetscObjectUseFortranCallback_Private(obj,cid,types,args,PETSC_FORTRAN_CALLBACK_CLASS)
197: #define PetscObjectUseFortranCallbackSubType(obj,cid,types,args) PetscObjectUseFortranCallback_Private(obj,cid,types,args,PETSC_FORTRAN_CALLBACK_SUBTYPE)

199: /* Disable deprecation warnings while building Fortran wrappers */
200: #undef  PETSC_DEPRECATED_FUNCTION
201: #define PETSC_DEPRECATED_FUNCTION(arg)

203: #endif