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
460
ReSTIRの数理と実装 (rtcamp10)
yumcyawiz
1
1.5k
fredholm(rtcamp9)
yumcyawiz
0
140
フォトンマッピングをパス空間から考える
yumcyawiz
0
470
fredholm
yumcyawiz
0
300
Introduction to volume rendering
yumcyawiz
0
2.5k
OSK#p-ray2020年度講義: 古典的レイトレーサーの実装
yumcyawiz
0
280
Ray Tracing: Overview
yumcyawiz
7
1.3k
GLSLでパストレーシングしてコーネルボックスを出す
yumcyawiz
0
560
Other Decks in Technology
See All in Technology
Android Studio Otter の最新 Gemini 機能 / Latest Gemini features in Android Studio Otter
yanzm
0
340
FFMとJVMの実装から学ぶJavaのインテグリティ
kazumura
0
160
新しい風。SolidFlutterで実現するシンプルな状態管理
zozotech
PRO
0
140
アジャイル社内普及ご近所さんマップを作ろう / Let's create an agile neighborhood map
psj59129
1
140
AS59105におけるFreeBSD EtherIPの運用と課題
x86taka
0
270
Dev Containers と Skaffold で実現する クラウドネイティブ開発環境 ローカルのみという制約に挑む / Cloud-Native Development with Dev Containers and Skaffold: Tackling the Local-Only Constraint
bitkey
PRO
0
130
大規模モノレポの秩序管理 失速しない多言語化フロントエンドの運用 / JSConf JP 2025
shoota
0
360
事業状況で変化する最適解。進化し続ける開発組織とアーキテクチャ
caddi_eng
1
6.9k
IPv6-mostly field report from RubyKaigi 2026
sorah
0
180
クラウドネイティブ時代の 開発プロセス再設計 〜速さと品質を両立するには〜
moritamasami
0
110
持続可能なアクセシビリティ開発
azukiazusa1
6
320
巨大モノリスのリプレイス──機能整理とハイブリッドアーキテクチャで挑んだ再構築戦略
zozotech
PRO
0
290
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
37
3.5k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Building an army of robots
kneath
306
46k
Typedesign – Prime Four
hannesfritz
42
2.9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
31
2.7k
How to Think Like a Performance Engineer
csswizardry
28
2.3k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Building Flexible Design Systems
yeseniaperezcruz
329
39k
A Modern Web Designer's Workflow
chriscoyier
697
190k
Bash Introduction
62gerente
615
210k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
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!