%autosave 10
Autosaving every 10 seconds
pyx
file.setup.py
.*.so
(Linux), *.pyd
(Windows).!!AI goes through Cython basics, same as tutorial on webpage.
In newer Cython versions
import cython
@cython.locals(a=cython.double, b=cython.double)
def add(a, b):
return a + b
Still valid Python, will of course need cython
module. Can still compile down.
This makes sense. Decorators are used for cross-cutting concerns, i.e. not part of core logic, and Cython is just that.
pyximport
tries to compile all pyz
and py
files, then:
import pyximport
pyximport.install()
import cy_101_pyximport
print(cy_101_pyximport.typed_python_func(3, 4))
a
and b
are 2000 x 2000, 4 million elements.(a + b) * 2 + (a * b)
.
- !!AI not matrix multiplication, element-wise multiplication.
- Hence classic "embarrassingly parallel" problem.
Key point:
@cython.boundscheck(False)
@cython.wraparound(False)
def func(object[double, ndim=2] buf1 not None,
object[double, ndim=2] buf2 not None,
object[double, ndim=2] output=one):
You want to loop over the NumPy arrray. Usually a no-no, but since this gets converted down to C fine.
Why does this speed up?
with nogil
when in C-only code.with gil
when need Python objects.with gil
below with nogil
.with nogil
to have genuine parallelism when using threads explicitly.prange