Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Pybind11でC++とPythonのいいとこ取りをする
Search
yumcyawiz
September 13, 2020
Technology
0
810
Pybind11でC++とPythonのいいとこ取りをする
Slide for OSK2020 September Lightning Talk
yumcyawiz
September 13, 2020
Tweet
Share
More Decks by yumcyawiz
See All by yumcyawiz
fredholm(rtcamp9)
yumcyawiz
0
74
フォトンマッピングをパス空間から考える
yumcyawiz
0
290
fredholm
yumcyawiz
0
190
Introduction to volume rendering
yumcyawiz
0
1.7k
OSK#p-ray2020年度講義: 古典的レイトレーサーの実装
yumcyawiz
0
230
Ray Tracing: Overview
yumcyawiz
7
1.2k
GLSLでパストレーシングしてコーネルボックスを出す
yumcyawiz
0
380
Path Tracing in 5 minutes
yumcyawiz
1
290
Lens Tracing
yumcyawiz
0
110
Other Decks in Technology
See All in Technology
[NIKKEI Tech Talk] KDDI/KAG Scrum & Community for Engineering Training
curanosuke
2
220
[NIKKEI Tech Talk]Bias for Action!! 実践から学ぶための仕組とコミュニティ / Community for Practice and Learning
kanamasa
0
270
AOAI Dev Day LLMシステム開発 Tips集
hirosatogamo
15
3.7k
頼られるのが大好きな 皆さんへ - 支援相手との期待の合わせ方、突き放し方 -/For_people_who_like_to_be_relied_on
naitosatoshi
1
290
ペパボのオブザーバビリティ研修2024 説明資料
kesompochy
0
1.1k
セキュリティ研修 Day1【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
160
ゆめみのアクセシビリティの現在地と今後
ryokatsuse
3
290
公共領域から学ぶ クラウド移行についてエンジニアが意識していること
kawakawa2222
0
140
How to Think Like a Performance Engineer
csswizardry
4
590
さらに高品質・高速化を目指すAI時代のテスト設計支援と、めざす先 / AI Test Lab vol.1
shift_evolve
0
190
dxd2024-生成AIに振り回された3か月間の成功と失敗/dxd2024-link-and-motivation
lmi
2
260
テスト・設計研修【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
170
Featured
See All Featured
Making Projects Easy
brettharned
111
5.7k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
24
1.8k
KATA
mclloyd
20
13k
Scaling GitHub
holman
458
140k
Ruby is Unlike a Banana
tanoku
96
10k
Code Review Best Practice
trishagee
58
16k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
29
2.5k
Fontdeck: Realign not Redesign
paulrobertlloyd
79
5.1k
Faster Mobile Websites
deanohume
303
30k
How to Ace a Technical Interview
jacobian
274
23k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
105
6.8k
How GitHub (no longer) Works
holman
305
140k
Transcript
Pybind11でC++とPythonの いいとこ取りをする 2020/09/13 OSK 9月LT会 @yumcyawiz
Motivation Pythonはnumpyとかmatplotlibとか色々あって便利!
Motivation だけど複雑な計算を実装すると遅い・・
Motivation 一方でC++は計算早いけど、可視化とか不便・・
Motivation C++で計算を実装して、Pythonで可視化できないか?
None
• C++・Python間で互いのデータ型・関数を扱えるように するC++のHeader Onlyライブラリ • STLのデータ型もサポート • numpyのデータ型も扱える
Usage - Setup
Project Setup • 今回はC++側でMyLibという名 前の静的ライブラリを作成 • Python側でMyLibを呼び出せる ようにしたい
Project Setup - CMake • CMakeを使ってpybind11を導入 • Python Bindingをbinding.cppに 記述
• pybind11_add_moduleで binding.cppをソースに指定
Project Setup - Build • ビルドするとMyLib.*.soが生成 される • Python Scriptと同じディレクト
リに置けばimport Mylibできる
Usage - Function
Function mylib.cpp binding.cpp Binding.cppにPython Bindingを記述していく
Function binding.cpp Python側から呼び出せる test.py
Function – default argument binding.cpp 名前付き引数&デフォルト引数も設定可能 mylib.h
Function – reference test.py 参照型も基本的には問題なく使える ただしstr, bool, intなどPython側でImmutableな型 は関数内の変更が反映されない mylib.cpp
0
Usage - Class
Class Vec3クラスをPythonから使えるようにする mylib.h binding.cpp test.py <MyLib.Vec3 object at 0x7fb66ccd8f30>
Class - member メンバ変数へのアクセス 関数の時と同様にバインドを作成できる binding.cpp test.py
Class - print printでVec3の中身を表示 __repr__を定義する binding.cpp test.py (1.000000, 2.000000, 3.000000)
Class - element 要素アクセスしたい場合 __getitem__を定義する binding.cpp test.py 2
Class - numpy Vec3をnumpy arrayとして読み込めるようにしたい Buffer Protocolを利用することで可能 binding.cpp test.py [2.
4. 6.]
Reference 紹介したコードは以下のrepoで公開してます https://github.com/yumcyaWiz/pybind11_tutorial
Application
LensSim • Ray Tracingによって写真レンズの性能評価・映り方のシミュ レーションを行うツール
LensSim C++ • レンズの読み込み • レイトレーシング • 各種性能計算 • IBLによるレンダリング
Python • 可視化(matplotlib) 複雑な計算を実装し、ライブラリ化 可視化周り
LensSim – Optical Path Diagram Python matplotlibを用いて光路図を表示する C++
LensSim – Exit Pupil Plot Python matplotlibを用いて射出瞳を表示する C++
Reference LensSimは以下のrepoで公開してます https://github.com/yumcyaWiz/LensSim
Thank you for listening!