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
1k
Pybind11でC++とPythonのいいとこ取りをする
Slide for OSK2020 September Lightning Talk
yumcyawiz
September 13, 2020
Tweet
Share
More Decks by yumcyawiz
See All by yumcyawiz
rtcamp 10 (vk-illuminati)
yumcyawiz
1
490
ReSTIRの数理と実装 (rtcamp10)
yumcyawiz
1
1.6k
fredholm(rtcamp9)
yumcyawiz
0
140
フォトンマッピングをパス空間から考える
yumcyawiz
0
500
fredholm
yumcyawiz
0
320
Introduction to volume rendering
yumcyawiz
0
2.6k
OSK#p-ray2020年度講義: 古典的レイトレーサーの実装
yumcyawiz
0
290
Ray Tracing: Overview
yumcyawiz
7
1.3k
GLSLでパストレーシングしてコーネルボックスを出す
yumcyawiz
0
580
Other Decks in Technology
See All in Technology
田舎で20年スクラム(後編):一個人が企業で長期戦アジャイルに挑む意味
chinmo
1
1.5k
kintone開発のプラットフォームエンジニアの紹介
cybozuinsideout
PRO
0
530
「アウトプット脳からユーザー価値脳へ」がそんなに簡単にできたら苦労しない #RSGT2026
aki_iinuma
11
5.4k
Introduction to Bill One Development Engineer
sansan33
PRO
0
350
The Engineer with a Three-Year Cycle
e99h2121
0
140
Introduction to Sansan for Engineers / エンジニア向け会社紹介
sansan33
PRO
5
62k
AI時代のアジャイルチームを目指して ー スクラムというコンフォートゾーンからの脱却 ー / Toward Agile Teams in the Age of AI
takaking22
11
6.7k
CQRS/ESになぜアクターモデルが必要なのか
j5ik2o
0
1.2k
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
10k
OCI技術資料 : OS管理ハブ 概要
ocise
2
4.1k
WebDriver BiDi 2025年のふりかえり
yotahada3
1
140
形式手法特論:コンパイラの「正しさ」は証明できるか? #burikaigi / BuriKaigi 2026
ytaka23
17
6.2k
Featured
See All Featured
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
0
1.1k
Accessibility Awareness
sabderemane
0
35
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
Navigating Weather and Climate Data
rabernat
0
72
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.4k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.5k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.5k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.3k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Embracing the Ebb and Flow
colly
88
4.9k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.9k
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!