Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
名古屋Ruby会議05_LT.pdf
Search
Yuji Teshima
September 18, 2026
61
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
名古屋Ruby会議05_LT.pdf
Yuji Teshima
September 18, 2026
More Decks by Yuji Teshima
See All by Yuji Teshima
rubykaigi2026LT_The Joy of Taking to Hardware in Ruby
yujiteshima
1
520
俺の勉強会:oss-contribution-graphで草駆動理論
yujiteshima
0
1k
Nuxtで作ったBlogサイトを高速化しようと頑張った話
yujiteshima
0
1.3k
Featured
See All Featured
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
830
Mobile First: as difficult as doing things right
swwweet
225
10k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
710
Exploring anti-patterns in Rails
aemeredith
4
510
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
260
We Are The Robots
honzajavorek
0
380
Into the Great Unknown - MozCon
thekraken
41
2.7k
How to build a perfect <img>
jonoalderson
1
6k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.6k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
2k
First, design no harm
axbom
PRO
2
1.3k
Transcript
Ruby のブロックが、 GPU のカーネルになる mruby で GPU 計算をする話 ─ 株式会社スタジアム(懇親会スポンサー)
名古屋Ruby会議05 二〇二六年九月十九日 大須演芸場 mruby 4.0.0 / Vulkan Compute / Raspberry Pi 5・Apple M5 名古屋Ruby会議05 開口一番 二席目
なぜ作ったか 一、試作は Python、本番は C++ に書き直し 二、mruby には、数をまとめて計算する道具が無い 三、GPU は、同じ計算を大人数で一斉にやる部品 名古屋Ruby会議05
開口一番 二席目
Ruby で書けば、一行 mruby-gpu-narray a = GPU::SFloat.new(1024).seq b = a *
2 + 1 b.to_a 見た目は普通の配列、中身は GPU 名古屋Ruby会議05 開口一番 二席目
八つでも、百万でも、同じ時間 名古屋Ruby会議05 開口一番 二席目
一行に見えて、演算の数だけ往復 命令書を書く、届ける、終わるのを待つ、受け取る ─ この往復が、計算より長い 名古屋Ruby会議05 開口一番 二席目
なぜブロックか 四つの命令を、一通にまとめて渡したい 一、演算子で書くと、Ruby は掛け算を先に実行して結果を返す 二、ライブラリには式の全体が見えない。一つずつ来るから、一つ ずつ往復 三、ブロックなら、式をまるごと受け取れる 名古屋Ruby会議05 開口一番 二席目
mruby は、ブロックを読めない ブロックをシェーダにするには、中身が要る 一、動かせるのに、何が書いてあるかは読めない 二、読み込んだ時点で実行しやすい形に変え、手順書を捨てる 三、作れる人はいるのに、レシピの紙が無い (実行できるのに、逐次実行手順はもうない) 名古屋Ruby会議05 開口一番 二席目
ブロックに、嘘をつく 一、ブロックは、x が本物の数か確かめない 二、数の代わりに、帳面を持った書記を渡す 三、料理人は一皿こしらえたつもりで、出てきたのはレシピ 名古屋Ruby会議05 開口一番 二席目
mruby-gpu-kernel の使い方 gem を一つ足す。本体の mruby-gpu-narray は自動で付いてくる conf.gem mruby-gpu-kernel # ビルド設定に、この一行
a.map { |x| x * 2 + sin(x) } # 普段は、これだけ k = GPU.kernel { |x| x * 2 + sin(x) } # シェーダを作って、取って おく k.call(a) # 何度でも走らせる GPU.kernel_source { |x| x * 2 + sin(x) } # 中身(GLSL)を覗く 名古屋Ruby会議05 開口一番 二席目
ブロックが、シェーダになる a.map { |x| x * 2 + sin(x) }
ブロックを一回だけ動かして、 書記が帳面に写す。 帳面を GPU の言葉に翻訳して、渡す。 往復は、一度きり。 名古屋Ruby会議05 開口一番 二席目 layout(local_size_x = 256) in; layout(set = 0, binding = 0) buffer BufA { float a[]; }; layout(set = 0, binding = 1) buffer BufB { float b[]; }; layout(push_constant) uniform PushConstants { uint n; } pc; // generated by GPU.kernel void main() { uint idx = gl_GlobalInvocationID.x; if (idx >= pc.n) { return; } float x = a[idx]; b[idx] = ((x * 2.0) + sin(x)); }
右肩上がりが、まっ平らに 演算をいくつ足しても、時間が増えない ─ およそ 4 倍 名古屋Ruby会議05 開口一番 二席目
実演(3分間クッキング) ここからターミナルで。四枚の絵が出たら、スライドの七へ /usr/bin/ruby demo/deck.rb 絵 GPU の本職は絵 → map の中身
→ 一行のシェーダ → 四枚、一度に 名古屋Ruby会議05 開口一番 二席目
GPU の本職は、絵 配列の番号から、座標が出る x = mod(i, 256) # 番号 ÷
256 の余り → 横 y = floor(i / 256) # 番号 ÷ 256 の商 i.map { |i| ... } # この座標なら、この明るさ 名古屋Ruby会議05 開口一番 二席目 → 縦
map の中身 四枚ぶんを計算してから、座標で一枚を選ぶ i.map { |i| x = mod(i, 360.0);
y = floor(i / 360.0) tx = floor(x / 180.0); ty = floor(y / 180.0) lx = x - tx * 180.0; ly = y - ty * 180.0 # どのタイルか(0 か 1) # タイルの中の座標 sea = 青海波(半径 24 の円を 4 段、Ruby のループで重ねる) crest = 丸に三つ引き(輪と、横棒 3 本) ichi = mod(floor(lx / 30.0) + floor(ly / 30.0), 2.0) * 200.0 + 30.0 heart = (x² + y² − 1)³ − x²y³ < 0 の内側 top = mix(sea, crest, step(0.5, tx)) bottom = mix(ichi, heart, step(0.5, tx)) mix(top, bottom, step(0.5, ty)) } 名古屋Ruby会議05 開口一番 二席目 # 上段: 左か右か # 下段 # 上段か下段か
出てきたシェーダは、一行 ブロック一つから、改行なしの 8,500 文字 b[idx] = mix(mix(mix(mix(mix(mix(((mod(x, 360.0) - (floor((mod(x,
360.0) / 180.0)) * 180.0)) * 0.0), ((step(0.5, fract((sqrt((pow(((mod(x, 360.0) - (floor((mod(x, 360.0) / 180.0)) * 180.0)) - ((floor(((((mod(x, 360.0) - (floor((mod(x, 360.0) / 180.0)) * 180.0)) (mod((floor(((floor((x / 360.0)) - (floor((floor((x / 360.0)) / 180.0)) * 180.0)) / 12.0)) + -1.0), 2.0) * 24.0)) / 48.0) + 0.5)) * 48.0) + (mod((floor(((floor((x / 360.0)) (floor((floor((x / 360.0)) / 180.0)) * 180.0)) / 12.0)) + -1.0), 2.0) * 24.0))), 2.0) + pow(((floor((x / 360.0)) - (floor … 名古屋Ruby会議05 開口一番 二席目
四枚、一度に 十三万人の作業員が、めいめい自分の座標を見て、担当の絵を描く─ GPU の中では、一回の計算 名古屋Ruby会議05 開口一番 二席目
一緒にGPUで遊びませんか github.com/yujiteshima/mruby-gpu-narray github.com/yujiteshima/mruby-gpu-kernel 二入力・合計・ループ・待たない ─ 先週、ブランチで出来ました Pull Request 三本、レビュー中 名古屋Ruby会議05
開口一番 二席目
嘘から出たシェーダ 嘘つきはシェーダのはじまり お後がよろしいようで