22
33[ ![ Build Status] ( https://travis-ci.org/paugier/piconumpy.svg?branch=master )] ( https://travis-ci.org/paugier/piconumpy )
44
5- ** An experiment about Numpy and pyhandle/hpy **
5+ ** An experiment about Numpy and HPy **
66
77The C API of CPython is one of the cause of the success of Python in scientific
88computing. In particular, Numpy (and all the Python scientific stack) is built
@@ -11,7 +11,7 @@ issue for the future of scientific Python (see [1], [2], [HPy]).
1111
1212[ 1 ] : https://faster-cpython.readthedocs.io/
1313[ 2 ] : https://morepypy.blogspot.com/2019/12/hpy-kick-off-sprint-report.html
14- [ HPy ] : https://github.com/pyhandle /hpy
14+ [ HPy ] : https://github.com/hpyproject /hpy
1515
1616[ HPy] is a very ambitious and promissing project to design a new and better C
1717API for interacting with Python interpreters. It should allow people to write
@@ -60,6 +60,11 @@ such codes using Numpy).
6060
6161## Install and run the benchmarks
6262
63+ ** Warning:** PicoNumpy now depends on HPy, which still has to be installed from
64+ the [ Git repository] ( https://github.com/hpyproject/hpy ) . For now, the
65+ installation is a bit more complex that what is described here (more about this
66+ [ here] ( #more-precise-notes-on-how-to-install-and-run-the-benchmarks-with-PyPy ) ).
67+
6368` make ` should install the package in editable mode. ` cd bench; make ` should run
6469the benchmarks. For the benchmarks, Julia is used for a good comparison point
6570so the command ` julia ` has to be available.
@@ -85,106 +90,97 @@ make profile METHOD="purepy"
8590make profile METHOD=" cython"
8691```
8792
93+ ### More precise notes on how to install and run the benchmarks with PyPy
94+
95+ Download and extract a nightly PyPy build
96+ < https://buildbot.pypy.org/nightly/ > . Add to the ` PATH ` environment variable
97+ the path of the directory containing the ` pypy ` executable (something like
98+ ` ~/opt/pypy-c-jit-101190-b661dc329618-linux64/bin ` ). Then, you should be able
99+ to run:
100+
101+ ``` bash
102+ pypy -m ensurepip
103+ pypy -m pip install pip -U
104+ pypy -m pip install numpy cython pytest transonic pythran
105+ ```
106+
107+ We need to install the correct version of HPy for the version of PyPy we are using:
108+
109+ ``` bash
110+ pypy -c " import hpy.universal as u; print(u.get_version())"
111+ ```
112+
113+ gives ` ('0.0.2rc2.dev12+gc9660c2', 'c9660c2') ` .
114+
115+ ``` bash
116+ cd ~ /Dev/hpy
117+ # update to the correct commit
118+ pypy setup.py develop
119+ ```
120+
121+ Now we can build-install PicoNumpy:
122+
123+ ``` bash
124+ cd ~ /Dev/piconumpy
125+ pypy setup.py --hpy-abi=universal develop
126+ ```
127+
128+ And run the benchmarks with:
129+
130+ ``` bash
131+ export PYTHON=" pypy"
132+ make clean
133+ make bench_hpy
134+ make
135+ ```
136+
88137## Few results
89138
90- As of today (5 March 2020), HPy is not yet ready!
139+ As of today (6 July 2021), HPy is not yet ready for high performance, but at
140+ least (with HPy 0.0.2) it runs !
91141
92- ### At work (meige8pcpa79, Intel(R) Xeon(R) CPU E5-1603 v3 @ 2.80GHz)
142+ ### At home ( Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz)
93143
94144- With CPython
95145
96- ``` raw
97- Julia : 1 * norm = 0.00534 s
98- Transonic-Pythran : 0.564 * norm
99- Numpy : 18.2 * norm
100- PicoNumpy (purepy) : 23.7 * norm
101- PicoNumpy (purepy_array) : 21.7 * norm
102- PicoNumpy (Cython) : 15.6 * norm
103- PicoNumpy (CPython C-API) : 4.7 * norm
146+ ```
147+ Julia : 1 * norm = 0.00196 s
148+ PicoNumpy (CPython C-API) : 9.42 * norm
149+ PicoNumpy (HPy CPy ABI) : 9.95 * norm
150+ PicoNumpy (HPy Universal) : 10.4 * norm
151+ Transonic-Pythran : 0.497 * norm
152+ Numpy : 27.5 * norm
153+ PicoNumpy (purepy) : 37.3 * norm
154+ PicoNumpy (purepy_array) : 37.7 * norm
155+ PicoNumpy (Cython) : 28.9 * norm
104156```
105157
106158- With PyPy3
107159
108- ``` raw
109- Julia : 1 * norm = 0.00534 s
110- Transonic-Pythran : 0.603 * norm
111- Numpy : 183 * norm
112- PicoNumpy (purepy) : 2.4 * norm
113- PicoNumpy (purepy_array) : 3.73 * norm
114- PicoNumpy (Cython) : 135 * norm
115- PicoNumpy (CPython C-API) : 17.8 * norm
160+ ```
161+ Julia : 1 * norm = 0.00196 s
162+ PicoNumpy (CPython C-API) : 34.1 * norm
163+ PicoNumpy (HPy Universal) : 12.8 * norm
164+ Transonic-Pythran : 0.539 * norm
165+ Numpy : 232 * norm
166+ PicoNumpy (purepy) : 4.39 * norm
167+ PicoNumpy (purepy_array) : 6.33 * norm
168+ PicoNumpy (Cython) : 274 * norm
116169```
117170
118- ### At home (Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz )
171+ #### Simpler benchmarks (bench/bench_cpy_vs_hpy.py )
119172
120173- With CPython
121174
122- ``` raw
123- Julia : 1 * norm = 0.00176 s
124- Transonic-Pythran : 0.73 * norm
125- Numpy : 37.6 * norm
126- PicoNumpy (purepy) : 46.4 * norm
127- PicoNumpy (purepy_array) : 41.9 * norm
128- PicoNumpy (Cython) : 31.4 * norm
129- PicoNumpy (CPython C-API) : 9.22 * norm
175+ ```
176+ CPython C-API: 1.92 seconds
177+ HPy [Universal]: 2.08 seconds
178+ HPy [CPy ABI]: 2.02 seconds
130179```
131180
132181- With PyPy3
133182
134- ``` raw
135- Julia : 1 * norm = 0.00176 s
136- Transonic-Pythran : 0.797 * norm
137- Numpy : 326 * norm
138- PicoNumpy (purepy) : 4.71 * norm
139- PicoNumpy (purepy_array) : 7.48 * norm
140- PicoNumpy (Cython) : 243 * norm
141- PicoNumpy (CPython C-API) : 36.6 * norm
142183```
143-
144- ## CPython C-API usage in PicoNumpy
145-
146- It is the first time that I wrote a Python extension by hand (without Cython or
147- Pythran) and moreover, there are nearly no checks and I didn't pay attention to
148- reference counting!
149-
150- List obtained from ` grep -rwoh 'Py[_A-Z].[a-zA-Z_]*' | sort | uniq ` :
151-
152- ``` raw
153- PyArg_ParseTupleAndKeywords
154- PyCFunction
155- Py_DECREF
156- PyErr_NoMemory
157- PyErr_SetString
158- PyExc_TypeError
159- PyFloat_AsDouble
160- PyFloat_FromDouble
161- PyFloatObject
162- Py_INCREF
163- PyList_Check
164- PyList_GET_ITEM
165- PyList_New
166- PyList_SetItem
167- PyList_Size
168- PyLong_AsLong
169- PyMemberDef
170- PyMethodDef
171- PyMODINIT_FUNC
172- PyModule_AddObject
173- PyModule_Create
174- PyModuleDef
175- PyModuleDef_HEAD_INIT
176- PyNumber_Check
177- PyNumberMethods
178- PyObject
179- PyObject_HEAD
180- PyObject_New
181- PySequenceMethods
182- Py_ssize_t
183- Py_TPFLAGS_DEFAULT
184- Py_TYPE
185- PyType_GenericNew
186- PyTypeObject
187- PyType_Ready
188- Py_UNUSED
189- PyVarObject_HEAD_INIT
184+ CPython C-API: 5.75 seconds
185+ HPy [Universal]: 2.11 seconds
190186```
0 commit comments