Wrap C ++ with Cython for use from Python

Note: The following articles are out of date.

Consider using pybind11.

[Python acceleration] c ++ built-in by pybind11


"Speeding up Python by fusing with Cython C" From O'Reilly I tried to trace on Ubuntu that a function written in C ++ can be imported and executed in Python. First of all, since the sample of this book is on github, download it in a zip file. https://github.com/cythonbook/examples Move to the extracted directory. examples-master/08-wrapping-cxx/01-simple-example-mt_rng-class

Source code written in C ++ mt19937.h mt19937.cpp

Declaration of external C code in Cython (extension .pxd) C ++ class wrap (extension .pyx)

RNG.pyx


# distutils: language = c++
# distutils: sources = mt19937.cpp

cdef extern from "mt19937.h" namespace "mtrandom":
    unsigned int N
    cdef cppclass MT_RNG:
        MT_RNG()
        MT_RNG(unsigned long s)
        MT_RNG(unsigned long init_key[], int key_length)
        void init_genrand(unsigned long s)
        unsigned long genrand_int32()
        double genrand_real1()
        double operator()()

cdef class RNG:

    cdef MT_RNG *_thisptr

    def __cinit__(self, unsigned long s):
        self._thisptr = new MT_RNG(s)
        if self._thisptr == NULL:
            raise MemoryError()

    def __dealloc__(self):
        if self._thisptr != NULL:
            del self._thisptr

    cpdef unsigned long randint(self):
        return self._thisptr.genrand_int32()

    cpdef double rand(self):
        return self._thisptr.genrand_real1()

Setup.py file that tells you how to compile

py.setup.py


from distutils.core import setup, Extension
from Cython.Build import cythonize

ext = Extension("RNG",
                sources=["RNG.pyx", "mt19937.cpp"],
                language="c++")

setup(name="RNG",
      ext_modules=cythonize(ext))

From here on, I'm trying to interpret Cython, but it may be wrong. Please refer to the same book.

Declaration of external C ++ code in Cython (extension .pxd)

How to write pxd file


cdef extern from "C++Header file in" namespace "C++Namespace in":
    C++Constants defined by const static in the header file of
    unsigned int N
    cdef cppclass C++Class identifier in:
Constactor()
Constactor(unsigned long s)
Constactor(unsigned long init_key[], int key_length)
C used in Cython among public methods++Method declaration
        void init_genrand(unsigned long s)
        unsigned long genrand_int32()
        double genrand_real1()
        double operator()()

In the file downloaded from github, the declaration of external C ++ code in Cython is You have migrated to a pyx file.

Added description to pyx file to specify that it is C ++ language # distutils: language = c++

cdef cppclass MT_RNG:

In the block description of, the definition of the class and public method in the reference source C ++ is described.

How to write a pyx file


cdef class Class identifier in Cython:

    cdef MT_RNG *_thisptr

    def __cinit__(self, unsigned long s):
Constructor description
C to be wrapped in this++Create an instance of the class of.
The first self of the argument is common to the case of Python class.
        self._thisptr = new MT_RNG(s)
        if self._thisptr == NULL:
            raise MemoryError()

    def __dealloc__(self):
Special method to clean up
        if self._thisptr != NULL:
            del self._thisptr

Type method name in cpdef Cython(self,argument):
Implementation of methods in Cython grammar
In this, C declared by cdef extern++Functions and methods are available.
return Return value
    cpdef unsigned long randint(self):
        return self._thisptr.genrand_int32()

    cpdef double rand(self):
        return self._thisptr.genrand_real1()

setup.How to write py


from distutils.core import setup, Extension
from Cython.Build import cythonize

ext = Extension("The name of the python extension",
                sources=["The name of the pyx file", "cpp file name"],
                language="c++")

setup(name="RNG",
      ext_modules=cythonize(ext))

Difference from C version: Added language = "c ++" to ext = Extension ().

Supplement: It is also possible to include C-level language element declarations in the pyx file instead of the pxd file. However, it is better to keep it as a separate pxd file for maintenance. as a trial, cdef extern from "mt19937.h" namespace "mtrandom": Even if you extract the block starting with from the pyx file to the pyd file and compile it Successful build and operation.

URL: Cython document (Japanese translation) tutorial http://omake.accense.com/static/doc-ja/cython/src/tutorial/index.html

Recommended Posts

Wrap C with Cython for use from Python
Wrap C ++ with Cython for use from Python
Use C ++ functions from python with pybind11
Call C from Python with DragonFFI
Tips for calling Python from C
Array buffer object for use with Cython
Wrap (part of) the AtCoder Library in Cython for use in Python
Use thingsspeak from python
Use fluentd from python
Use MySQL from Python
Use logger with Python for the time being
Use mecab with Python3
Use DynamoDB with Python
Use Python 3.8 with Anaconda
Use python with docker
Use MySQL from Python
Use BigQuery from python.
~ Tips for Python beginners from Pythonista with love ② ~
Use mecab-ipadic-neologd from python
Benchmark for C, Java and Python with prime factorization
Use Python installed with pyenv for PL / Python execution environment
Programming with your smartphone anywhere! (Recommended for C / Python)
Use Trello API with python
ABC163 C problem with python3
Use Twitter API with Python
Use TUN / TAP with Python
Use MySQL from Anaconda (python)
Use Cython with Jupyter Notebook
Use subsonic API with python3
ABC188 C problem with python3
With skype, notify with skype from python!
Use e-Stat API from Python
ABC187 C problem with python
Let's use a scripting language for a comfortable C ++ life C ++ implementation after verification with python
Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Debugging
Use IvyFEM (Finite Element Method Library for .NET) from Python
[python] A note when trying to use numpy with Cython
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
Get data from analytics API with Google API Client for python
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
Execute C ++ functions from Python with Pybind11 (for Windows & Visual Studio Code people) Environment construction
Python: How to use async with
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
Create Awaitable with Python / C API
Using Rstan from Python with PypeR
Install Python from source with Ansible
Create folders from '01' to '12' with python
Call popcount from Ruby / Python / C #
Use NAIF SPICE TOOLKIT with Python
Read and use Python files from Python
Next, use Python (Flask) for Heroku!
Forcibly use Google Translate from python
Python cheat sheet (for C ++ experienced)
Use rospy with virtualenv in Python3
Solve ABC168 A ~ C with Python
Run Aprili from Python with Orange
Execute Python code from C # GUI
Use Azure Blob Storage from Python
Use the Flickr API from Python
How to wrap C in Python