fortranobject.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #ifndef Py_FORTRANOBJECT_H
  2. #define Py_FORTRANOBJECT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <Python.h>
  7. #ifndef NPY_NO_DEPRECATED_API
  8. #define NPY_NO_DEPRECATED_API NPY_API_VERSION
  9. #endif
  10. #ifdef FORTRANOBJECT_C
  11. #define NO_IMPORT_ARRAY
  12. #endif
  13. #define PY_ARRAY_UNIQUE_SYMBOL _npy_f2py_ARRAY_API
  14. #include "numpy/arrayobject.h"
  15. #include "numpy/npy_3kcompat.h"
  16. #ifdef F2PY_REPORT_ATEXIT
  17. #include <sys/timeb.h>
  18. // clang-format off
  19. extern void f2py_start_clock(void);
  20. extern void f2py_stop_clock(void);
  21. extern void f2py_start_call_clock(void);
  22. extern void f2py_stop_call_clock(void);
  23. extern void f2py_cb_start_clock(void);
  24. extern void f2py_cb_stop_clock(void);
  25. extern void f2py_cb_start_call_clock(void);
  26. extern void f2py_cb_stop_call_clock(void);
  27. extern void f2py_report_on_exit(int, void *);
  28. // clang-format on
  29. #endif
  30. #ifdef DMALLOC
  31. #include "dmalloc.h"
  32. #endif
  33. /* Fortran object interface */
  34. /*
  35. 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
  36. PyFortranObject represents various Fortran objects:
  37. Fortran (module) routines, COMMON blocks, module data.
  38. Author: Pearu Peterson <pearu@cens.ioc.ee>
  39. */
  40. #define F2PY_MAX_DIMS 40
  41. #define F2PY_MESSAGE_BUFFER_SIZE 300 // Increase on "stack smashing detected"
  42. typedef void (*f2py_set_data_func)(char *, npy_intp *);
  43. typedef void (*f2py_void_func)(void);
  44. typedef void (*f2py_init_func)(int *, npy_intp *, f2py_set_data_func, int *);
  45. /*typedef void* (*f2py_c_func)(void*,...);*/
  46. typedef void *(*f2pycfunc)(void);
  47. typedef struct {
  48. char *name; /* attribute (array||routine) name */
  49. int rank; /* array rank, 0 for scalar, max is F2PY_MAX_DIMS,
  50. || rank=-1 for Fortran routine */
  51. struct {
  52. npy_intp d[F2PY_MAX_DIMS];
  53. } dims; /* dimensions of the array, || not used */
  54. int type; /* PyArray_<type> || not used */
  55. int elsize; /* Element size || not used */
  56. char *data; /* pointer to array || Fortran routine */
  57. f2py_init_func func; /* initialization function for
  58. allocatable arrays:
  59. func(&rank,dims,set_ptr_func,name,len(name))
  60. || C/API wrapper for Fortran routine */
  61. char *doc; /* documentation string; only recommended
  62. for routines. */
  63. } FortranDataDef;
  64. typedef struct {
  65. PyObject_HEAD
  66. int len; /* Number of attributes */
  67. FortranDataDef *defs; /* An array of FortranDataDef's */
  68. PyObject *dict; /* Fortran object attribute dictionary */
  69. } PyFortranObject;
  70. #define PyFortran_Check(op) (Py_TYPE(op) == &PyFortran_Type)
  71. #define PyFortran_Check1(op) (0 == strcmp(Py_TYPE(op)->tp_name, "fortran"))
  72. extern PyTypeObject PyFortran_Type;
  73. extern int
  74. F2PyDict_SetItemString(PyObject *dict, char *name, PyObject *obj);
  75. extern PyObject *
  76. PyFortranObject_New(FortranDataDef *defs, f2py_void_func init);
  77. extern PyObject *
  78. PyFortranObject_NewAsAttr(FortranDataDef *defs);
  79. PyObject *
  80. F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *));
  81. void *
  82. F2PyCapsule_AsVoidPtr(PyObject *obj);
  83. int
  84. F2PyCapsule_Check(PyObject *ptr);
  85. extern void *
  86. F2PySwapThreadLocalCallbackPtr(char *key, void *ptr);
  87. extern void *
  88. F2PyGetThreadLocalCallbackPtr(char *key);
  89. #define ISCONTIGUOUS(m) (PyArray_FLAGS(m) & NPY_ARRAY_C_CONTIGUOUS)
  90. #define F2PY_INTENT_IN 1
  91. #define F2PY_INTENT_INOUT 2
  92. #define F2PY_INTENT_OUT 4
  93. #define F2PY_INTENT_HIDE 8
  94. #define F2PY_INTENT_CACHE 16
  95. #define F2PY_INTENT_COPY 32
  96. #define F2PY_INTENT_C 64
  97. #define F2PY_OPTIONAL 128
  98. #define F2PY_INTENT_INPLACE 256
  99. #define F2PY_INTENT_ALIGNED4 512
  100. #define F2PY_INTENT_ALIGNED8 1024
  101. #define F2PY_INTENT_ALIGNED16 2048
  102. #define ARRAY_ISALIGNED(ARR, SIZE) ((size_t)(PyArray_DATA(ARR)) % (SIZE) == 0)
  103. #define F2PY_ALIGN4(intent) (intent & F2PY_INTENT_ALIGNED4)
  104. #define F2PY_ALIGN8(intent) (intent & F2PY_INTENT_ALIGNED8)
  105. #define F2PY_ALIGN16(intent) (intent & F2PY_INTENT_ALIGNED16)
  106. #define F2PY_GET_ALIGNMENT(intent) \
  107. (F2PY_ALIGN4(intent) \
  108. ? 4 \
  109. : (F2PY_ALIGN8(intent) ? 8 : (F2PY_ALIGN16(intent) ? 16 : 1)))
  110. #define F2PY_CHECK_ALIGNMENT(arr, intent) \
  111. ARRAY_ISALIGNED(arr, F2PY_GET_ALIGNMENT(intent))
  112. #define F2PY_ARRAY_IS_CHARACTER_COMPATIBLE(arr) ((PyArray_DESCR(arr)->type_num == NPY_STRING && PyArray_ITEMSIZE(arr) >= 1) \
  113. || PyArray_DESCR(arr)->type_num == NPY_UINT8)
  114. #define F2PY_IS_UNICODE_ARRAY(arr) (PyArray_DESCR(arr)->type_num == NPY_UNICODE)
  115. extern PyArrayObject *
  116. ndarray_from_pyobj(const int type_num, const int elsize_, npy_intp *dims,
  117. const int rank, const int intent, PyObject *obj,
  118. const char *errmess);
  119. extern PyArrayObject *
  120. array_from_pyobj(const int type_num, npy_intp *dims, const int rank,
  121. const int intent, PyObject *obj);
  122. extern int
  123. copy_ND_array(const PyArrayObject *in, PyArrayObject *out);
  124. #ifdef DEBUG_COPY_ND_ARRAY
  125. extern void
  126. dump_attrs(const PyArrayObject *arr);
  127. #endif
  128. extern int f2py_describe(PyObject *obj, char *buf);
  129. /* Utility CPP macros and functions that can be used in signature file
  130. expressions. See signature-file.rst for documentation.
  131. */
  132. #define f2py_itemsize(var) (PyArray_ITEMSIZE(capi_ ## var ## _as_array))
  133. #define f2py_size(var, ...) f2py_size_impl((PyArrayObject *)(capi_ ## var ## _as_array), ## __VA_ARGS__, -1)
  134. #define f2py_rank(var) var ## _Rank
  135. #define f2py_shape(var,dim) var ## _Dims[dim]
  136. #define f2py_len(var) f2py_shape(var,0)
  137. #define f2py_fshape(var,dim) f2py_shape(var,rank(var)-dim-1)
  138. #define f2py_flen(var) f2py_fshape(var,0)
  139. #define f2py_slen(var) capi_ ## var ## _len
  140. extern npy_intp f2py_size_impl(PyArrayObject* var, ...);
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #endif /* !Py_FORTRANOBJECT_H */