Skip to content

Commit ce1b5a3

Browse files
authored
Transform C API md to rst (#374)
1 parent e3bd0a1 commit ce1b5a3

File tree

2 files changed

+94
-93
lines changed

2 files changed

+94
-93
lines changed

spec/design_topics/C_API.md

-93
This file was deleted.

spec/design_topics/C_API.rst

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
.. _C-API:
2+
3+
C API
4+
=====
5+
6+
Use of a C API is out of scope for this array API, as mentioned in :ref:`Scope`.
7+
There are a lot of libraries that do use such an API - in particular via Cython code
8+
or via direct usage of the NumPy C API. When the maintainers of such libraries
9+
want to use this array API standard to support multiple types of arrays, they
10+
need a way to deal with that issue. This section aims to provide some guidance.
11+
12+
The assumption in the rest of this section is that performance matters for the library,
13+
and hence the goal is to make other array types work without converting to a
14+
``numpy.ndarray`` or another particular array type. If that's not the case (e.g. for a
15+
visualization package), then other array types can simply be handled by converting
16+
to the supported array type.
17+
18+
.. note::
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+
31+
Situation 1: a Python package that is mostly pure Python, with a limited number of Cython extensions
32+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33+
34+
.. note::
35+
Projects in this situation include Statsmodels, scikit-bio and QuTiP
36+
37+
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.
38+
39+
40+
Situation 2: a Python package that contains a lot of Cython code
41+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42+
43+
.. note::
44+
Projects in this situation include scikit-learn and scikit-image
45+
46+
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.
47+
48+
Longer term: specific support for particular array types (e.g. ``cupy.ndarray`` can be supported with Python-only code via ``cupy.ElementwiseKernel``).
49+
50+
51+
Situation 3: a Python package that uses the NumPy or Python C API directly
52+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53+
54+
.. note::
55+
Projects in this situation include SciPy and Astropy
56+
57+
Strategy: similar to *situation 2*, but the number of submodules that can support all array types may be limited.
58+
59+
60+
Device support
61+
--------------
62+
63+
Supporting non-CPU array types in code using the C API or Cython seems problematic,
64+
this almost inevitably will require custom device-specific code (e.g., CUDA, ROCm) or
65+
something like JIT compilation with Numba.
66+
67+
68+
Other longer-term approaches
69+
----------------------------
70+
71+
Further Python API standardization
72+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73+
74+
There may be cases where it makes sense to standardize additional sets of
75+
functions, because they're important enough that array libraries tend to
76+
reimplement them. An example of this may be *special functions*, as provided
77+
by ``scipy.special``. Bessel and gamma functions for example are commonly
78+
reimplemented by array libraries. This may avoid having to drop into a
79+
particular implementation that does use a C API (e.g., one can then rely on
80+
``arraylib.special.gamma`` rather than having to use ``scipy.special.gamma``).
81+
82+
HPy
83+
~~~
84+
85+
`HPy <https://github.com/hpyproject/hpy>`_ is a new project that will provide a higher-level
86+
C API and ABI than CPython offers. A Cython backend targeting HPy will be provided as well.
87+
88+
- Better PyPy support
89+
- Universal ABI - single binary for all supported Python versions
90+
- Cython backend generating HPy rather than CPython code
91+
92+
HPy isn't quite ready for mainstream usage today, but once it does it may
93+
help make supporting multiple array libraries or adding non-CPU device
94+
support to Cython more feasible.

0 commit comments

Comments
 (0)