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
1.1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Pybind11でC++とPythonのいいとこ取りをする
Slide for OSK2020 September Lightning Talk
yumcyawiz
September 13, 2020
More Decks by yumcyawiz
See All by yumcyawiz
rtcamp 10 (vk-illuminati)
yumcyawiz
1
560
ReSTIRの数理と実装 (rtcamp10)
yumcyawiz
1
2k
fredholm(rtcamp9)
yumcyawiz
0
180
フォトンマッピングをパス空間から考える
yumcyawiz
0
560
fredholm
yumcyawiz
0
350
Introduction to volume rendering
yumcyawiz
0
2.8k
OSK#p-ray2020年度講義: 古典的レイトレーサーの実装
yumcyawiz
0
310
Ray Tracing: Overview
yumcyawiz
7
1.4k
GLSLでパストレーシングしてコーネルボックスを出す
yumcyawiz
0
630
Other Decks in Technology
See All in Technology
AIエージェントの開発・提供におけるセキュリティリスクの論点と対策
flatt_security
1
520
作り直せるコードは迅速に 作り直せないDBは慎重に - AI時代のプロダクトエンジニアが「判断の不可逆性」で開発速度を変える話
kinosuke01
0
170
[RSJ26] Building a VLA Model Based on Self-Distilled Classification
keio_smilab
PRO
0
180
Azure App Service / Container Apps の組み込み認証
kuniteru
0
180
KAEN Company Deck
kaen
PRO
0
290
Point Cloud as a Foreign Language for Multi-modal Large Language Model
takmin
0
370
KPIだけでは評価できないプロダクトが考えるべき Evalsという第二の評価系 / Beyond KPIs: Evals as a Second Evaluation Framework for Products #PdEConf
aki_iinuma
3
3.4k
Bet AI Day 2026丨AIを「使う」から、AIが「働く」へ ― LayerXが進める「組織AI」の社会実装
layerx
PRO
2
1.9k
Hub & Spoke 環境のネットワークルーティングを分解してみる
tsuyataku
1
520
Guerilla InnerSource in enterprises, during the AI hype
onenashev
PRO
0
140
Instana&Bob トラブルシュートハンズオン
mayamasaki68
0
110
指示待ちから変化に応じるClaude Codeへ!~環境からAgentへの帰り道を作る~
gotalab555
9
1.8k
Featured
See All Featured
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
690
Practical Orchestrator
shlominoach
191
12k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.1k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
65k
A Tale of Four Properties
chriscoyier
163
24k
Google's AI Overviews - The New Search
badams
0
1.6k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
430
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
500
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
320
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
510
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!