wyli / cpp-py-example

Example project that shows hybrid programming with C++ and Python (incl. NumPy and SciPy).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GitPitch

Summary

Example project that shows hybrid programming with C++ and Python. It also shows how to wrap C++ datatypes as NumPy arrays, so as to enable use of SciPy routines.

Requirements

  • Cmake version 3.2 or newer
  • C++11 compatible compiler
  • Boost.Python version 1.63
  • Python 3
  • NumPy and SciPy
  • Tested on an Ubuntu 16.04 LTS.

See towards the end of this file for instructions how to install these.

Usage

Building the libraries

  1. git clone git@github.com:dzhoshkun/cpp-py-example.git
  2. cd cpp-py-example/ex && export PYTHONPATH="$(pwd):$PYTHONPATH" && cd ../..
  3. mkdir cpp-py-example-build && cd cpp-py-example-build
  4. cmake ../cpp-py-example/src
  5. make -j
  6. export PYTHONPATH="$(pwd):$PYTHONPATH"

Running the examples

Most of the *.py files in the ex sub-folder contain main functions. Some of these use the image file located in the res sub-folder. So please change to this folder before running any example. You can then run any example by calling the file directly, e.g. ../ex/gil.py.

Running the benchmarks

The mgrad.py file in the ex folder is intended for benchmarking. You can benchmark the pure Python function in this file using e.g.:

python3 -m timeit "from mgrad import grad; import numpy as np; arr = np.zeros((128, 128, 3), dtype='uint8'); grad(arr)"

To benchmark its C++ implementation, replace mgrad with pymycpp, i.e:

python3 -m timeit "from pymycpp import grad; import numpy as np; arr = np.zeros((128, 128, 3), dtype='uint8'); grad(arr)"

Installing the requirements

Cmake

  • Ubuntu: apt install cmake
  • Mac OS X: brew install cmake (i.e. using Homebrew)

Boost.Python 1.63

  • Ubuntu
    1. Install Python 3 development libs (see below).
    2. wget https://sourceforge.net/projects/boost/files/boost/1.63.0/boost_1_63_0.tar.bz2
    3. tar xvfj boost_1_63_0.tar.bz2
    4. cd boost_1_63_0
    5. ./bootstrap.sh --with-python=$(which python3)
    6. ./b2 -j 7
    7. ./b2 -j 7 install
  • Mac OS X
    1. Install Python 3 and SciPy (see below)
    2. brew install boost-python --with-python3
    3. You might need to set your BOOST_ROOT, e.g. export BOOST_ROOT=/usr/local/Cellar/boost/1.63.0/include

Python 3

  • Ubuntu: apt install python3-dev
  • Mac OS X: brew install python3

NumPy and SciPy

  • Ubuntu
    1. apt install python3-numpy
    2. apt install python3-scipy
  • Mac OS X: brew install scipy --with-python3

About

Example project that shows hybrid programming with C++ and Python (incl. NumPy and SciPy).

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:C++ 68.1%Language:Python 18.5%Language:CMake 13.4%