å°æ£ã§ãã£ã¼ãã©ã¼ãã³ã°ã試ãã¦ããããPythonã§å ¥åãã¼ã¿ã®å å·¥ãè¡ãã¨å¦çé度ãåé¡ã«ãªã£ã¦ããã
ããã§ãPythonããC++ã§ä½æããã¢ã¸ã¥ã¼ã«ãå¼ã³åºãã¦ããã®ä¸ã§numpyã®ãªãã¸ã§ã¯ãã®å å·¥ãè¡ãããã
PythonããC++ã®å¼ã³åºãã¯ãªã¼ãã¼ãããããããããé »ç¹ã«å¼ã³åºãã¨ããã£ã¦é
ããªãã
ãã®ããããããããã®ãã¼ã¿ãã¾ã¨ãã¦ä½æããããã«ããäºå®ã
以ä¸ã®æé ã§ãPythonã§C++ã®ã¢ã¸ã¥ã¼ã«ã使ããã¨ãã§ããã
ç°å¢
- Windows Home 64bit
- Python 3.5.2(Anaconda 4.2.0)
- Boost 1.64.0
- Visual Studio 2015
Boostã®ãã¦ã³ãã¼ã
Boostã®1.63以éã§Boost.Numpyãæ¨æºã§å©ç¨ã§ããããã«ãªã£ã¦ããã
å
¬å¼ãµã¤ãããææ°ã®1.64.0ããã¦ã³ãã¼ãããã
ãã¦ã³ãã¼ããããã¡ã¤ã«ãä»»æã®ãã£ã¬ã¯ããªã«å±éããã
以ä¸ãC:\boost_1_64_0ã«å±éãããã®ã¨ãã¦è¨è¿°ããã
Boost.Pythonã®ãã«ããæå¹ã«ãã
ãã¼ã ãã£ã¬ã¯ããªC:\Users\xxxxã«ãuser-config.jamããä½æãã以ä¸ã®éãç·¨éããã
using python : 3.5 : C:\\Anaconda3\\python ;
â»C:\\Anaconda3ã®ãã¹ã¯ç°å¢ã«åããã¦ç·¨éããã
Boostã®ãã«ã
cd /d C:\boost_1_64_0 bootstrap.bat b2 toolset=msvc threading=multi variant=debug,release link=static runtime-link=static address-model=64 --stagedir=stage/x64 -j 8
64ãããã®ã¹ã¿ãã£ãã¯ãªã³ã¯ã©ã¤ãã©ãªãçæãããªãã·ã§ã³ãæå®ãã¦ããã
ãã¤ãããã¯ãªã³ã¯ã©ã¤ãã©ãªã§ã¯ãnumpyã®dtypeãã°ãã¼ãã«å¤æ°ã«å®ç¾©ããã¦ãããããDLLã®å
ã®å¤æ°ããªã³ã¯ã§ããªããããnp::dtype::get_builtin
stage\x64\libã«.libãçæãããã
pythoné¢é£ã®.libãã¡ã¤ã«ã®ååã以ä¸ã®éãå¤æ´ããã
libboost_python3-vc140-mt-s-1_64.lib â libboost_python-vc140-mt-s-1_64.lib libboost_python3-vc140-mt-sgd-1_64.lib â libboost_python-vc140-mt-sgd-1_64.lib libboost_numpy3-vc140-mt-s-1_64.lib â libboost_numpy-vc140-mt-s-1_64.lib libboost_numpy3-vc140-mt-sgd-1_64.lib â libboost_numpy-vc140-mt-sgd-1_64.lib
â»2017/10/27 追è¨
Version 1.65.1ã§ã¯ååã®å¤æ´ã¯ä¸è¦ã«ãªã£ã¦ãã¾ããã
C++ã®DLLããã¸ã§ã¯ãä½æ
Visual Studio 2015ã§Win32ã®DLLããã¸ã§ã¯ããä½æããã
ã¢ã¯ãã£ããªæ§æãx64/Releaseã«ããã
ããã¸ã§ã¯ãã®ããããã£ã§ã¤ã³ã¯ã«ã¼ããã£ã¬ã¯ããªã«ä»¥ä¸ã追å ããã
- C:\Anaconda3\include
- C:\boost_1_64_0
ã©ã¤ãã©ãªãã£ã¬ã¯ããªã«ä»¥ä¸ã追å ããã
- C:\Anaconda3\libs
- C:\boost_1_64_0\stage\x64\lib
C/C++->ã³ã¼ãçæã®ã©ã³ã¿ã¤ã ã©ã¤ãã©ãªããã«ãã¹ã¬ãã(/MT)ã«å¤æ´ããã
boostãã¹ã¿ãã£ãã¯ãªã³ã¯ã©ã¤ãã©ãªã¨ãã¦çæãã¦ããã®ã§åãããå¿
è¦ãããã
C++ã®ã³ã¼ãä½æ
以ä¸ã®ãããªã³ã¼ããä½æããã
ï¼ã³ã¼ãã¯ãã¡ãã®ãµã¤ããåèã«ãããï¼
mymod1.cpp
#define BOOST_PYTHON_STATIC_LIB #define BOOST_NUMPY_STATIC_LIB #include <boost/python/numpy.hpp> #include <stdexcept> #include <algorithm> namespace p = boost::python; namespace np = boost::python::numpy; /* 2åã«ãã */ void mult_two(np::ndarray a) { int nd = a.get_nd(); if (nd != 1) throw std::runtime_error("a must be 1-dimensional"); size_t N = a.shape(0); if (a.get_dtype() != np::dtype::get_builtin<float>()) throw std::runtime_error("a must be float32 array"); float *p = reinterpret_cast<float *>(a.get_data()); std::transform(p, p + N, p, [](float x) { return 2 * x; }); } BOOST_PYTHON_MODULE(mymod1) { Py_Initialize(); np::initialize(); p::def("mult_two", mult_two); }
ã¹ã¿ãã£ãã¯ãªã³ã¯ãããã«ã¯ããããã¼ãèªã¿è¾¼ãåã«ä»¥ä¸ã®å®ç¾©ãå¿
è¦
#define BOOST_PYTHON_STATIC_LIB
#define BOOST_NUMPY_STATIC_LIB
ãã«ãããã
ããã¸ã§ã¯ãã®x64/Releaseã«.dllãä½æãããã
Pythonã³ã¼ãä½æ
以ä¸ã®ãããªã³ã¼ããä½æããã
mymod.py
import mymod1 import numpy as np if __name__ == '__main__': a = np.array([1,2,3], dtype=np.float32) mymod1.mult_two(a) print(a)
ä¸è¨ã§ãã«ããã.dllãã¡ã¤ã«ãpythonã®ã³ã¼ãã¨åããã£ã¬ã¯ããªã«ã³ãã¼ãããã¡ã¤ã«åãmymod1.pydã«å¤æ´ããã
æ¡å¼µåã¯.pydã§ããå¿
è¦ãããã
Visual Studioã®ãã«ãã¤ãã³ã->ãã«ãå¾ã®ã¤ãã³ãã§ã
copy $(TargetPath) $(SolutionDir)\PythonApplication1\mymod1.pyd
ãªã©è¨å®ããã°ããã
Pythonã®ã³ã¼ããå®è¡ããã
python mymod.py
å®è¡çµæ
[ 2. 4. 6.]