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
750
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
61
フォトンマッピングをパス空間から考える
yumcyawiz
0
240
fredholm
yumcyawiz
0
160
Introduction to volume rendering
yumcyawiz
0
1.6k
OSK#p-ray2020年度講義: 古典的レイトレーサーの実装
yumcyawiz
0
230
Ray Tracing: Overview
yumcyawiz
7
1.2k
GLSLでパストレーシングしてコーネルボックスを出す
yumcyawiz
0
350
Path Tracing in 5 minutes
yumcyawiz
1
280
Lens Tracing
yumcyawiz
0
99
Other Decks in Technology
See All in Technology
地理空間データ可視化・解析・活用ソリューション Pacific Spatial Solutions (PSS)
pacificspatialsolutions
0
300
Azure Container Apps + Bicep 〜 こんな感じで運用しています
kaz29
2
480
生成AIの変革の時代に、直近1年で直面した課題とその解決策
ktc_wada
0
330
一生覚えておきたい「システム開発=コミュニケーション」〜初めての実務案件振り返りLT〜
maimyyym
1
170
アクセシビリティを考慮したUI/CSSフレームワーク・ライブラリ選定
yajihum
2
1k
LangSmith入門―トレース/評価/プロンプト管理などを担うLLMアプリ開発プラットフォーム
os1ma
3
360
オーナーシップを持つ領域を明確にする
konifar
13
3.2k
Databricks における 『MLOps』
databricksjapan
2
170
プロトタイピングによる不確実性の低減 / Reducing Uncertainty through Prototyping
ohbarye
5
390
長期間TiDBを使ってきた話 @ 私たちはなぜNewSQLを使うのかTiDB選定5社が語る選定理由と活用LT / Experiences with TiDB Over Time
chibiegg
2
900
ServiceNow Knowledge 24の歩き方 EYストラテジー・アンド・コンサルティング
manarobot
0
200
Google Cloud Next '24でブログを10本書いた方法と勉強会を沸かせた方法
yasumuusan
0
310
Featured
See All Featured
RailsConf 2023
tenderlove
4
540
Visualization
eitanlees
136
14k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
352
28k
Done Done
chrislema
178
15k
How GitHub Uses GitHub to Build GitHub
holman
468
290k
Thoughts on Productivity
jonyablonski
58
3.8k
Design by the Numbers
sachag
274
18k
From Idea to $5000 a Month in 5 Months
shpigford
377
45k
The Cost Of JavaScript in 2023
addyosmani
16
3.9k
Debugging Ruby Performance
tmm1
70
11k
Happy Clients
brianwarren
92
6.4k
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!