introduction to cython

11
INTRODUCTION TO CYTHON John Tuesday, August 23, 2022

Upload: john-zhang

Post on 23-Jun-2015

591 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Introduction to cython

INTRODUCTION TO CYTHON

JohnThursday, April 13, 2023

Page 2: Introduction to cython

Overview

• a superset of the Python language which give high-level, OO functional, and dynamic programming.

• The source code translate to optimized C/C++ code and compiled as Python extension modules.

• One word, Cython is Python with C data types.

Page 3: Introduction to cython

Installing Cython

• Pythonxy already include Cython.• Use easy_install or pip install Cython from

PYPI.$ python easy_install.py Cython$ pip install Cython

Page 4: Introduction to cython

First example: Building a Cython module “hello”

hello.pyxdef say_hello_to(name):

print (“Hello %s!” % name)

setup.pyfrom distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_ext

ext_modules = [Extension("hello", ["hello.pyx"])]

setup( name = 'Hello world app', cmdclass = {'build_ext': build_ext}, ext_modules = ext_modules)

Run command line build the module$ python setup.py build_ext --inplace --compiler=mingw32 --inplace

# How to run the function:>>> from hello import say_hello_to>>> say_hello_to(“John”)Hello John

Page 5: Introduction to cython

Static type declarationsCython can compile pure python code. To improve performance, use cdef add static type declarations

# test1.pyx, 35% speedupdef f(x): return x**2-xdef integrate_f(a, b, N): s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s*dx

# 4 time speedup over python versiondef f(double x): return x**2-xdef integrate_f(double a, double b, int N): cdef int i cdef double s, dx s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s*dx

Page 6: Introduction to cython

Typing function#declare c-style function150 times speedup

cdef double f(double x) except ? -2:

return x**2 - x

annotation tell you why your code take time

$ cython.py -a hello.pyxa html (hello.html) is created,Click the yellow, you will get why the Python API is called here

>>> import hello>>> hasattr(hello,'f')False# if use cpdef instead of cdef, a Python wrapper is also created

Page 7: Introduction to cython

Calling C function

A complete list of these cimport file see Lib\site-packages\Cython\Includes

from libc.math cimport sincdef double f(double x): return sin(x*x)

# instruct Cython generate C code that include math.h header file# C compiler will see it at compile timecdef extern from “math.h”:

double sin(double)cdef double f(double x):

return sin(x*x)

If Cython do not provide read-to-use declaration, access C code by cdef

Page 8: Introduction to cython

Using C libraries

Step 1: redefine .pxd head file# file: cqueue.pxd# copy most part of C head file herecdef extern from "libcalg/queue.h": ctypedef struct Queue: pass ctypedef void* QueueValue Queue* queue_new() void queue_free(Queue* queue)

Step 2: create a pyx define Queue class in Python

# file: queue.pyxcimport cqueuecdef class Queue: cdef cqueue.Queue *_c_queue def __cinit__(self): self._c_queue = cqueue.queue_new()

Page 9: Introduction to cython

Using C libraries - cont

step 3.1: change the setup.pychangeext_modules = [Extension("queue", ["queue.pyx"])]

toext_modules = [ Extension("queue", ["queue.pyx"], libraries=["calg"]) ]

or step 3.2: include the lib in the option

$ CFLAGS="-I/usr/local/otherdir/calg/include" \ LDFLAGS="-L/usr/local/otherdir/calg/lib" \ python setup.py build_ext -i

calg lib see Simon Howard, C Algorithms library, http://c-algorithms.sourceforge.net/

Page 10: Introduction to cython

Using C++ in Cython• Brief overview of C++ support in Cython(Cython

v0.13)– C++ objects can now be dynamically allocated

with new and del keywords.– C++ objects can be stack-allocated.– C++ classes can be declared with the new

keyword cppclass.– Templated classes are supported.– Overloaded functions are supported.– Overloading of C++ operators (such as operator+,

operator[],...) is supported.

Page 11: Introduction to cython

Review of this Slides

1. introduce the Cython2. How to install Cython3. An example show how to compile a Cython

project4. Optimize the pure Python code with Cython5. Call C function in Python code6. Use C library in Python code