|
1 | 1 | .. _C-api:
|
2 | 2 |
|
3 | 3 | # C API
|
| 4 | + |
| 5 | +Use of a C API is out of scope for this array API, as mentioned in :ref:`Scope`. |
| 6 | +There are a lot of libraries that do use such an API - in particular via Cython code |
| 7 | +or via direct usage of the NumPy C API. When the maintainers of such libraries |
| 8 | +want to use this array API standard to support multiple types of arrays, they |
| 9 | +need a way to deal with that issue. This section aims to provide some guidance. |
| 10 | + |
| 11 | +The assumption in the rest of this section is that performance matters for the library, |
| 12 | +and hence the goal is to make other array types work without converting to a |
| 13 | +`numpy.ndarray` or another particular array type. If that's not the case (e.g. for a |
| 14 | +visualization package), then other array types can simply be handled by converting |
| 15 | +to the supported array type. |
| 16 | + |
| 17 | +.. note:: |
| 18 | + |
| 19 | + Often a zero-copy conversion to `numpy.ndarray` is possible, at least for CPU arrays. |
| 20 | + If that's the case, this may be a good way to support other array types. |
| 21 | + The main difficulty in that case will be getting the return array type right - however, |
| 22 | + this standard does provide a Python-level API for array construction that should allow |
| 23 | + doing this. A relevant question is if it's possible to know with |
| 24 | + certainty that a conversion will be zero-copy. This may indeed be |
| 25 | + possible, see :ref:`data-interchange`. |
| 26 | + |
| 27 | + |
| 28 | +## Example situations for C/Cython usage |
| 29 | + |
| 30 | +### Situation 1: a Python package that is mostly pure Python, with a limited number of Cython extensions |
| 31 | + |
| 32 | +.. note:: |
| 33 | + |
| 34 | + Projects in this situation include Statsmodels, scikit-bio and QuTiP |
| 35 | + |
| 36 | +Main strategy: documentation. The functionality using Cython code will not support other array types (or only with conversion to/from `numpy.ndarray`), which can be documented per function. |
| 37 | + |
| 38 | + |
| 39 | +### Situation 2: a Python package that contains a lot of Cython code |
| 40 | + |
| 41 | +.. note:: |
| 42 | + |
| 43 | + Projects in this situation include scikit-learn and scikit-image |
| 44 | + |
| 45 | +Main strategy: add support for other array types _per submodule_. This keeps it manageable to explain to the user which functionality does and doesn't have support. |
| 46 | + |
| 47 | +Longer term: specific support for particular array types (e.g. `cupy.ndarray` can be supported with Python-only code via `cupy.ElementwiseKernel`). |
| 48 | + |
| 49 | + |
| 50 | +### Situation 3: a Python package that uses the NumPy or Python C API directly |
| 51 | + |
| 52 | +.. note:: |
| 53 | + |
| 54 | + Projects in this situation include SciPy and Astropy |
| 55 | + |
| 56 | +Strategy: similar to _situation 2_, but the number of submodules that can support all array types may be limited. |
| 57 | + |
| 58 | + |
| 59 | +## Device support |
| 60 | + |
| 61 | +Supporting non-CPU array types in code using the C API or Cython seems problematic, |
| 62 | +this almost inevitably will require custom device-specific code (e.g., CUDA, ROCm) or |
| 63 | +something like JIT compilation with Numba. |
| 64 | + |
| 65 | + |
| 66 | +## Other longer-term approaches |
| 67 | + |
| 68 | +### Further Python API standardization |
| 69 | + |
| 70 | +There may be cases where it makes sense to standardize additional sets of |
| 71 | +functions, because they're important enough that array libraries tend to |
| 72 | +reimplement them. An example of this may be _special functions_, as provided |
| 73 | +by `scipy.special`. Bessel and gamma functions for example are commonly |
| 74 | +reimplemented by array libraries. This may avoid having to drop into a |
| 75 | +particular implementation that does use a C API (e.g., one can then rely on |
| 76 | +`arraylib.special.gamma` rather than having to use `scipy.special.gamma`). |
| 77 | + |
| 78 | + |
| 79 | +### HPy |
| 80 | + |
| 81 | +[HPy](https://github.com/hpyproject/hpy) is a new project that will provide a higher-level |
| 82 | +C API and ABI than CPython offers. A Cython backend targeting HPy will be provided as well. |
| 83 | + |
| 84 | +- Better PyPy support |
| 85 | +- Universal ABI - single binary for all supported Python versions |
| 86 | +- Cython backend generating HPy rather than CPython code |
| 87 | + |
| 88 | +HPy isn't quite ready for mainstream usage today, but once it does it may |
| 89 | +help make supporting multiple array libraries or adding non-CPU device |
| 90 | +support to Cython more feasible. |
0 commit comments