GPUカーネル関数の記述
Mojoでデバイスコードを表現します
「GPUカーネル関数の記述」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What a Kernel Is
A GPU kernel is the function each thread runs. You write the work for one element, and the hardware repeats it across many.
Think One Thread
The trick is to write a kernel from a single thread's view. Each thread does its own small slice of the work.
A Kernel Is Just an fn
In Mojo a kernel is a normal fn. Its strict, typed nature is exactly what device code needs to compile fast.
fn add_kernel():
passPassing in Buffers
A kernel takes pointers to device memory as parameters. These buffers hold the inputs and the output it will fill.
fn add_kernel(a: UnsafePointer[Float32], out: UnsafePointer[Float32]):
passFinding This Thread's Index
Inside the kernel, each thread computes its own position first. That index selects which element it must process.
var i = block_idx.x * block_dim.x + thread_idx.xGuarding the Bounds
Always check the index before touching memory. A bounds check keeps stray threads from reading past the array.
if i < n:
out[i] = a[i] + b[i]Doing the Element's Work
The body is tiny: read inputs, compute, write one result. The whole kernel often fits in a single line of math.
out[i] = a[i] * b[i]Launching the Kernel
You launch it by choosing a grid and block size. The launch fans your one-thread code out across the whole grid.
ctx.enqueue_function[add_kernel](grid_dim=blocks, block_dim=256)No Return Value
Kernels do not return results to the caller. They write into the output buffer, which you read back afterward.
Keep It Branch-Light
Threads run best in lockstep. Heavy branching makes lanes diverge and wait, so keep kernel logic simple and uniform.
Same Idea, Massive Scale
One short kernel plus a big grid equals millions of results. The scale comes from the launch, not from longer code.
Quick Check
You are writing the body of a GPU kernel for one thread.
Recap
A kernel is an fn for one thread: find your index, guard the bounds, do one element's math, then launch over a grid. ⚡
よくある質問
「GPUカーネル関数の記述」レッスンは無料ですか?
はい。「GPUカーネル関数の記述」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「GPUカーネル関数の記述」で何を学びますか?
Mojoでデバイスコードを表現します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「GPUカーネル関数の記述」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- AIワークロードにGPUを使う理由
- スレッド、ブロック、グリッド
- GPUカーネル関数の記述
- デバイスとのデータ転送