c_cpp:boost:boost_python

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
c_cpp:boost:boost_python [2020/08/27 18:15] ともやんc_cpp:boost:boost_python [2021/07/05 01:59] (現在) ともやん
行 1: 行 1:
-<html> 
-  <style> 
-    #result pre, #mincode pre { 
-      overflow: hidden; 
-      font-size: 10px; 
-    } 
-    #result_long pre, #mincode_long pre  { 
-      height: 250px; 
-      overflow: scroll; 
-      overflow-x: hidden; 
-      font-size: 10px; 
-    } 
-    #mintbl table { 
-      font-size: 12px; 
-    } 
-    .dokuwiki .plugin_wrap table { 
-      width: auto; 
-    } 
-    #logo { 
-      background-color: white; 
-      padding: 10px; 
-      width: fit-content; 
-    } 
-    #logo p { 
-      margin: 0; 
-    } 
-  </style> 
-</html> 
 ====== Boost.Python - Boost Python Library ====== ====== Boost.Python - Boost Python Library ======
 +本家: [[https://www.boost.org/doc/libs/release/libs/python/|Boost.Python - Boost Library Documentation]]\\
 +
 **Boost Python Library** は、Python と C++ を連携させるためのフレームワークである。Dave Abrahams の C++ コンパイラだけで、特別なツールを使用することなく、C++ クラスの関数やオブジェクトを Python に素早くシームレスに公開できる。\\ **Boost Python Library** は、Python と C++ を連携させるためのフレームワークである。Dave Abrahams の C++ コンパイラだけで、特別なツールを使用することなく、C++ クラスの関数やオブジェクトを Python に素早くシームレスに公開できる。\\
  
行 42: 行 16:
 {{c_cpp:visualstudio2019_boost.python_001.png?600|Visual Studio 2019 Boost.Python 001}}\\ {{c_cpp:visualstudio2019_boost.python_001.png?600|Visual Studio 2019 Boost.Python 001}}\\
  
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code> <code>
 言語: Python 言語: Python
行 53: 行 27:
  
 以下の内容で新しいプロジェクトを構成して **[作成]** をクリックする。\\ 以下の内容で新しいプロジェクトを構成して **[作成]** をクリックする。\\
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code> <code>
 プロジェクト名: PyHelloBoostPython プロジェクト名: PyHelloBoostPython
行 95: 行 69:
  
 **PyHelloBoostPython** プロジェクトの **PyHelloBoostPython.py** ファイルに以下のコードを貼り付ける。\\ **PyHelloBoostPython** プロジェクトの **PyHelloBoostPython.py** ファイルに以下のコードを貼り付ける。\\
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code python PyHelloBoostPython.py> <code python PyHelloBoostPython.py>
 #!/usr/bin/env python #!/usr/bin/env python
行 156: 行 130:
 {{c_cpp:visualstudio2019_boost.python_005.png?600|Visual Studio 2019 Boost.Python 005}}\\ {{c_cpp:visualstudio2019_boost.python_005.png?600|Visual Studio 2019 Boost.Python 005}}\\
  
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code> <code>
 言語: C++ 言語: C++
行 167: 行 141:
  
 以下の内容で新しいプロジェクトを構成して **[作成]** をクリックする。\\ 以下の内容で新しいプロジェクトを構成して **[作成]** をクリックする。\\
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code> <code>
 プロジェクト名: superfastcode_cpython プロジェクト名: superfastcode_cpython
行 177: 行 151:
 **superfastcode_cpython** プロジェクトを右クリックして、**[追加] - [新しい項目...]** をクリックする。\\ **superfastcode_cpython** プロジェクトを右クリックして、**[追加] - [新しい項目...]** をクリックする。\\
 以下の内容で新しい項目を追加する。\\ 以下の内容で新しい項目を追加する。\\
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code> <code>
 種類: C++ ファイル (.cpp) 種類: C++ ファイル (.cpp)
行 193: 行 167:
 これにより、**Debug、Release、Win32、x64** のすべての組み合わせについての設定を変更する。\\ これにより、**Debug、Release、Win32、x64** のすべての組み合わせについての設定を変更する。\\
 **CPython プロジェクト** (Python 用 C++ 拡張機能) のビルドに必要な設定の概要は以下の通りである。\\ **CPython プロジェクト** (Python 用 C++ 拡張機能) のビルドに必要な設定の概要は以下の通りである。\\
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code> <code>
 [構成プロパティ] - [全般] - [構成の種類]: ダイナミック ライブラリ (.dll) [構成プロパティ] - [全般] - [構成の種類]: ダイナミック ライブラリ (.dll)
行 211: 行 185:
 ※ここでは [[windows:scoop|Scoop]] でインストールした **Python 3.8** の設定を行っているが、各自の環境に合わせて設定すること。\\ ※ここでは [[windows:scoop|Scoop]] でインストールした **Python 3.8** の設定を行っているが、各自の環境に合わせて設定すること。\\
 <code> <code>
-%USERPROFILE%\scoop\apps\python\current\include+$(USERPROFILE)\scoop\apps\python\current\include
 </code> </code>
 +**※構成プロパティでは<wrap hi>$(USERPROFILE)</wrap>と指定しなければならない。コマンドプロンプトのように<wrap hi>%USERPROFILE%</wrap>と指定すると IntelliSense が正常動作しないようである。**
 {{c_cpp:visualstudio2019_boost.python_016.png?600|Visual Studio 2019 Boost.Python 016}}\\ {{c_cpp:visualstudio2019_boost.python_016.png?600|Visual Studio 2019 Boost.Python 016}}\\
  
行 218: 行 193:
 ※ここでは [[windows:scoop|Scoop]] でインストールした **Python 3.8** の設定を行っているが、各自の環境に合わせて設定すること。\\ ※ここでは [[windows:scoop|Scoop]] でインストールした **Python 3.8** の設定を行っているが、各自の環境に合わせて設定すること。\\
 <code> <code>
-%USERPROFILE%\scoop\apps\python\current\libs+$(USERPROFILE)\scoop\apps\python\current\libs
 </code> </code>
 +**※構成プロパティでは<wrap hi>$(USERPROFILE)</wrap>と指定しなければならない。コマンドプロンプトのように<wrap hi>%USERPROFILE%</wrap>と指定すると IntelliSense が正常動作しないようである。**
 {{c_cpp:visualstudio2019_boost.python_017.png?600|Visual Studio 2019 Boost.Python 017}}\\ {{c_cpp:visualstudio2019_boost.python_017.png?600|Visual Studio 2019 Boost.Python 017}}\\
 上記のすべての設定が終わったら、プロジェクトのプロパティ ページの **[OK]** をクリックする。\\ 上記のすべての設定が終わったら、プロジェクトのプロパティ ページの **[OK]** をクリックする。\\
  
 **superfastcode_cpython** プロジェクトの **cpython_module.cpp** ファイルに以下の内容を貼り付ける。\\ **superfastcode_cpython** プロジェクトの **cpython_module.cpp** ファイルに以下の内容を貼り付ける。\\
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code cpp cpython_module.cpp> <code cpp cpython_module.cpp>
 #include <Windows.h> #include <Windows.h>
行 261: 行 237:
 static PyModuleDef superfastcode_module = { static PyModuleDef superfastcode_module = {
     PyModuleDef_HEAD_INIT,     PyModuleDef_HEAD_INIT,
-    "superfastcode_cpython",                        // Module name to use with Python import statements+    "superfastcode_cpython",                // Module name to use with Python import statements
     "Provides some functions, but faster",  // Module description     "Provides some functions, but faster",  // Module description
     0,     0,
行 267: 行 243:
 }; };
  
-PyMODINIT_FUNC PyInit_superfastcode() {+PyMODINIT_FUNC PyInit_superfastcode_cpython() {
     return PyModule_Create(&superfastcode_module);     return PyModule_Create(&superfastcode_module);
 } }
行 277: 行 253:
 {{c_cpp:visualstudio2019_boost.python_005.png?600|Visual Studio 2019 Boost.Python 005}}\\ {{c_cpp:visualstudio2019_boost.python_005.png?600|Visual Studio 2019 Boost.Python 005}}\\
  
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code> <code>
 言語: C++ 言語: C++
行 288: 行 264:
  
 以下の内容で新しいプロジェクトを構成して **[作成]** をクリックする。\\ 以下の内容で新しいプロジェクトを構成して **[作成]** をクリックする。\\
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code> <code>
 プロジェクト名: superfastcode_boost プロジェクト名: superfastcode_boost
行 298: 行 274:
 **superfastcode_boost** プロジェクトを右クリックして、**[追加] - [新しい項目...]** をクリックする。\\ **superfastcode_boost** プロジェクトを右クリックして、**[追加] - [新しい項目...]** をクリックする。\\
 以下の内容で新しい項目を追加する。\\ 以下の内容で新しい項目を追加する。\\
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code> <code>
 種類: C++ ファイル (.cpp) 種類: C++ ファイル (.cpp)
行 311: 行 287:
 これにより、**Debug、Release、Win32、x64** のすべての組み合わせについての設定を変更する。\\ これにより、**Debug、Release、Win32、x64** のすべての組み合わせについての設定を変更する。\\
 **Boost.Python プロジェクト** (Python 用 C++ 拡張機能) のビルドに必要な設定の概要は以下の通りである。\\ **Boost.Python プロジェクト** (Python 用 C++ 拡張機能) のビルドに必要な設定の概要は以下の通りである。\\
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code> <code>
 [構成プロパティ] - [全般] - [構成の種類]: ダイナミック ライブラリ (.dll) [構成プロパティ] - [全般] - [構成の種類]: ダイナミック ライブラリ (.dll)
行 329: 行 305:
 ※ここでは [[windows:scoop|Scoop]] でインストールした **Python 3.8** の設定を行っているが、各自の環境に合わせて設定すること。\\ ※ここでは [[windows:scoop|Scoop]] でインストールした **Python 3.8** の設定を行っているが、各自の環境に合わせて設定すること。\\
 <code> <code>
-%USERPROFILE%\scoop\apps\python\current\include+$(USERPROFILE)\scoop\apps\python\current\include
 </code> </code>
 +**※構成プロパティでは<wrap hi>$(USERPROFILE)</wrap>と指定しなければならない。コマンドプロンプトのように<wrap hi>%USERPROFILE%</wrap>と指定すると IntelliSense が正常動作しないようである。**
 {{c_cpp:visualstudio2019_boost.python_022.png?600|Visual Studio 2019 Boost.Python 022}}\\ {{c_cpp:visualstudio2019_boost.python_022.png?600|Visual Studio 2019 Boost.Python 022}}\\
  
行 336: 行 313:
 ※ここでは [[windows:scoop|Scoop]] でインストールした **Python 3.8** の設定を行っているが、各自の環境に合わせて設定すること。\\ ※ここでは [[windows:scoop|Scoop]] でインストールした **Python 3.8** の設定を行っているが、各自の環境に合わせて設定すること。\\
 <code> <code>
-%USERPROFILE%\scoop\apps\python\current\libs+$(USERPROFILE)\scoop\apps\python\current\libs
 </code> </code>
 +**※構成プロパティでは<wrap hi>$(USERPROFILE)</wrap>と指定しなければならない。コマンドプロンプトのように<wrap hi>%USERPROFILE%</wrap>と指定すると IntelliSense が正常動作しないようである。**
 {{c_cpp:visualstudio2019_boost.python_023.png?600|Visual Studio 2019 Boost.Python 023}}\\ {{c_cpp:visualstudio2019_boost.python_023.png?600|Visual Studio 2019 Boost.Python 023}}\\
 上記のすべての設定が終わったら、プロジェクトのプロパティ ページの **[OK]** をクリックする。\\ 上記のすべての設定が終わったら、プロジェクトのプロパティ ページの **[OK]** をクリックする。\\
  
 **superfastcode_boost** プロジェクトの **boost_module.cpp** ファイルに以下の内容を貼り付ける。\\ **superfastcode_boost** プロジェクトの **boost_module.cpp** ファイルに以下の内容を貼り付ける。\\
-<WRAP prewrap 100% #mincode>+<WRAP prewrap 100% mincode>
 <code cpp boost_module.cpp> <code cpp boost_module.cpp>
 #include <Windows.h> #include <Windows.h>
行 375: 行 353:
  
 **アクティブ ソリューション構成: Debug** に変更して、以下のプロジェクトが **Release ビルド**、**x64 プラットフォーム** でビルドされるように設定する。(標準では Python の **python38.lib (Release 版)** のみのため)\\ **アクティブ ソリューション構成: Debug** に変更して、以下のプロジェクトが **Release ビルド**、**x64 プラットフォーム** でビルドされるように設定する。(標準では Python の **python38.lib (Release 版)** のみのため)\\
-<WRAP prewrap 100% #mintbl> +<WRAP prewrap 100% mintbl> 
-^  プロジェクト  ^  構成  ^  プラットフォーム +^  プロジェクト  ^  構成  ^  プラットフォーム   ビルド  ^ 
-|superfastcode_boost  |Release  |x64  | +|superfastcode_boost  |Release  |x64   [v]  | 
-|superfastcode_cpython  |Release  |x64  |+|superfastcode_cpython  |Release  |x64  |  [v] 
 +<wrap hi>**※必ず**ビルド**のチェックボックスをオンにすること。**</wrap>\\
 </WRAP>  </WRAP> 
 {{c_cpp:visualstudio2019_boost.python_025.png?600|Visual Studio 2019 Boost.Python 025}}\\ {{c_cpp:visualstudio2019_boost.python_025.png?600|Visual Studio 2019 Boost.Python 025}}\\
  
 **アクティブ ソリューション構成: Release** に変更して、以下のプロジェクトが **Release ビルド**、**x64 プラットフォーム** でビルドされるように設定する。(標準では Python の **python38.lib (Release 版)** のみのため)\\ **アクティブ ソリューション構成: Release** に変更して、以下のプロジェクトが **Release ビルド**、**x64 プラットフォーム** でビルドされるように設定する。(標準では Python の **python38.lib (Release 版)** のみのため)\\
-<WRAP prewrap 100% #mintbl> +<WRAP prewrap 100% mintbl> 
-^  プロジェクト  ^  構成  ^  プラットフォーム +^  プロジェクト  ^  構成  ^  プラットフォーム   ビルド  ^ 
-|superfastcode_boost  |Release  |x64  | +|superfastcode_boost  |Release  |x64   [v]  | 
-|superfastcode_cpython  |Release  |x64  |+|superfastcode_cpython  |Release  |x64  |  [v] 
 +<wrap hi>**※必ず**ビルド**のチェックボックスをオンにすること。**</wrap>\\
 </WRAP>  </WRAP> 
 {{c_cpp:visualstudio2019_boost.python_026.png?600|Visual Studio 2019 Boost.Python 026}}\\ {{c_cpp:visualstudio2019_boost.python_026.png?600|Visual Studio 2019 Boost.Python 026}}\\
  
-===== ソリューション内の Python プロジェクトで C++ 拡張機能 プロジェクトを参照する =====+**ソリューション エクスプローラー** より **PyHelloBoostPython** ソリューションを右クリックして、**[プロパティ]**をクリックする。\\ 
 +**シングル スタートアップ プロジェクト**が **PyHelloBoostPython** であることを確認する。\\ 
 +{{c_cpp:visualstudio2019_boost.python_032.png?600|Visual Studio 2019 Boost.Python 032}}\\ 
 + 
 +==== ソリューション内の Python プロジェクトで C++ 拡張機能 プロジェクトを参照する ====
 **superfastcode_cpython**、および、**superfastcode_boost** プロジェクトの Python 用 C++ 拡張機能は、同一ソリューション内であってもそのままでは Python から import できない。\\ **superfastcode_cpython**、および、**superfastcode_boost** プロジェクトの Python 用 C++ 拡張機能は、同一ソリューション内であってもそのままでは Python から import できない。\\
-{{c_cpp:visualstudio2019_boost.python_027.png?600|Visual Studio 2019 Boost.Python 027}}\\+{{c_cpp:visualstudio2019_boost.python_027.png?610|Visual Studio 2019 Boost.Python 027}}\\ 
 + 
 +**PyHelloBoostPython** プロジェクトの**[参照]**を右クリックして、**[参照の追加...]**をクリックする。\\
 {{c_cpp:visualstudio2019_boost.python_028.png?250|Visual Studio 2019 Boost.Python 028}}\\ {{c_cpp:visualstudio2019_boost.python_028.png?250|Visual Studio 2019 Boost.Python 028}}\\
 +
 +**superfastcode_cpython**、および、**superfastcode_boost** プロジェクトのチェックボックスをオンにして **[OK]** をクリックする。\\
 {{c_cpp:visualstudio2019_boost.python_029.png?600|Visual Studio 2019 Boost.Python 029}}\\ {{c_cpp:visualstudio2019_boost.python_029.png?600|Visual Studio 2019 Boost.Python 029}}\\
 +
 +以下のように **PyHelloBoostPython** プロジェクトの参照に C++ 拡張機能 プロジェクトが追加されて、検索パス (**x64\Release**) にビルド出力の ***.pyd** ファイルが配置されるので Python プロジェクトから **import** することが可能になる。\\ 
 {{c_cpp:visualstudio2019_boost.python_030.png?250|Visual Studio 2019 Boost.Python 030}}\\ {{c_cpp:visualstudio2019_boost.python_030.png?250|Visual Studio 2019 Boost.Python 030}}\\
 +
 +==== Python プロジェクトの実行 ====
 +Visual Studio の画面で **Ctrl + F5**(デバッグなしで開始) キーを押して実行する。\\
 +Python コードと C++ コードの実行速度のベンチマーク結果が表示される。\\
 +<WRAP prewrap 100% #result>
 +<code powershell>
 +Running benchmarks with COUNT = 500000 x NUM_OF_EXEC = 30
 +[tanh(x) for x in d] (Python implementation) took 1.930 seconds
 +[fast_tanh(x) for x in d] (CPython C++ implementation) took 0.248 seconds
 +[boost_tanh(x) for x in d] (Boost.Python C++ implementation) took 0.289 seconds
 +続行するには何かキーを押してください . . .
 +</code>
 +</WRAP>
  
 ===== 参考文献 ===== ===== 参考文献 =====
 [[https://docs.microsoft.com/ja-jp/visualstudio/python/working-with-c-cpp-python-in-visual-studio?view=vs-2019|Python 用の C++ 拡張機能の記述 - Visual Studio | Microsoft Docs]]\\ [[https://docs.microsoft.com/ja-jp/visualstudio/python/working-with-c-cpp-python-in-visual-studio?view=vs-2019|Python 用の C++ 拡張機能の記述 - Visual Studio | Microsoft Docs]]\\
 [[http://moriyoshi.hatenablog.com/entry/20091214/1260779899|Boost.Python の機能をざっと紹介してみる - moriyoshiの日記]]\\ [[http://moriyoshi.hatenablog.com/entry/20091214/1260779899|Boost.Python の機能をざっと紹介してみる - moriyoshiの日記]]\\
 +
 +[[http://alpha.osdn.jp/devel/boost.python_ja.pdf|Boost.Python - Alpha]]\\
 +[[https://qiita.com/maiueo/items/b2093ba78cde988bb111|ctypesの変数についてのメモ - Qiita]]\\
 +[[https://docs.python.org/ja/3/library/ctypes.html|ctypes --- Pythonのための外部関数ライブラリ — Python 3.8.6rc1 ドキュメント]]\\
 +[[https://stackoverflow.com/questions/16105539/boost-python-custom-converter|c++ - Boost.Python custom converter - Stack Overflow]]\\
  
  • c_cpp/boost/boost_python.1598519719.txt.gz
  • 最終更新: 2020/08/27 18:15
  • by ともやん