Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| procedures:boost_python [2016/11/11 02:20] – xif | procedures:boost_python [2016/11/12 03:21] (current) – [NumPy arrays] xif | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Boost.Python on OS X with NumPy ====== | ||
| + | Ur python program too slow at calculating the boiling point of tungsten under a 600bar atmosphere of francium fluoride ? Too baaad...\\ | ||
| + | Well, let's implement some part of it in C++ using [[http:// | ||
| + | This tuto uses Python 3.4, installed from [[https:// | ||
| + | |||
| + | ===== Build Boost.Python ===== | ||
| + | |||
| + | - Download Boost (dev version) : '' | ||
| + | - '' | ||
| + | - '' | ||
| + | - If it works, you should see ''/ | ||
| + | |||
| + | ===== Example Python module ===== | ||
| + | |||
| + | Exemple Makefile : | ||
| + | <code make> | ||
| + | NAME = gauss_seidel | ||
| + | CXX = clang++ | ||
| + | CXXFLAGS += -Wall -std=c++1y | ||
| + | PYTHON_VER = 3.4 | ||
| + | |||
| + | NUMPY_ROOT = $(shell pip$(PYTHON_VER) show numpy | grep " | ||
| + | CXXFLAGS += -fPIC $(shell python$(PYTHON_VER)-config --cflags) -I$(NUMPY_ROOT)/ | ||
| + | LDFLAGS += -fPIC $(shell python$(PYTHON_VER)-config --ldflags) | ||
| + | #LDFLAGS += -lboost_python3 -lboost_numpy3 | ||
| + | LDFLAGS += / | ||
| + | |||
| + | all: $(NAME).o | ||
| + | $(CXX) -shared $^ $(LDFLAGS) -o $(NAME).so | ||
| + | |||
| + | $(NAME).o: $(NAME).cpp | ||
| + | |||
| + | %.o: %.cpp | ||
| + | $(CXX) -o $@ -c $< $(CXXFLAGS) | ||
| + | </ | ||
| + | |||
| + | Exemple code ('' | ||
| + | <code cpp> | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | namespace py = boost:: | ||
| + | namespace np = boost:: | ||
| + | |||
| + | np::ndarray shift_col (int k, np::ndarray M) { | ||
| + | size_t n = M.shape(0), p = M.shape(1); | ||
| + | np:: | ||
| + | for (size_t i = 0; i < n; ++i) { | ||
| + | for (size_t j = 0; j < p; ++j) { | ||
| + | R[i][(j+k)%p] = M[i][j]; | ||
| + | } | ||
| + | } | ||
| + | return R; | ||
| + | } | ||
| + | |||
| + | BOOST_PYTHON_MODULE(my_little_poney) { | ||
| + | np:: | ||
| + | py:: | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Then build with '' | ||
| + | |||
| + | ==== NumPy arrays ==== | ||
| + | |||
| + | Here is the issue : all operation made with '' | ||
| + | You can use the '' | ||
| + | template < | ||
| + | struct nparray_accessor : public xif:: | ||
| + | nparray_accessor (np:: | ||
| + | (T*)ndarr.get_data(), | ||
| + | [& | ||
| + | ) {} | ||
| + | }; | ||
| + | </ | ||
| + | '' | ||
| + | |||
| + | Documentation about Boost.Python.NumPy : [[http:// | ||