0Pricing
Mojo Academy · レッスン

SIMDとループの組み合わせ

カーネルの中核をベクトル化します

「SIMDとループの組み合わせ」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

From Scalar to SIMD

To accelerate a kernel, swap the one-at-a-time loop for one that handles a whole pack of values per step with SIMD.

for i in range(n):
    out[i] = a[i] + b[i]

Pick a Width

Choose how many elements fit in one vector. The width is the number of lanes each step processes together.

alias width = 4

Load a Chunk

Grab several elements at once into a SIMD value with a vector load instead of reading them one by one.

var va = a.load[width=width](i)

Compute the Pack

Run the kernel's math on both chunks at once. The whole pack is added element-wise in a single operation.

var vsum = a.load[width=width](i) + b.load[width=width](i)

Store the Pack

Write the full result back with a vector store, covering every lane you just computed in one move.

out.store[width=width](i, vsum)

Step by the Width

The vectorized loop advances by the width, not by one. Each pass covers a full pack of elements.

for i in range(0, n, width):
    pass

Let vectorize Help

Mojo's vectorize helper sweeps a closure across the range in vector steps so you skip the manual bookkeeping.

from algorithm import vectorize

Write the Chunk Closure

You define a small parameterized function that handles one chunk. Mojo calls it with the right width as it sweeps.

fn body[w: Int](i: Int):
    out.store[width=w](i, a.load[width=w](i) + b.load[width=w](i))

Run vectorize

Call vectorize with your closure, the width, and the size. It loops and even handles the leftover tail for you.

vectorize[body, width](n)

Mind the Tail

When n is not a multiple of the width, a few elements remain. vectorize cleans up that tail so nothing is missed.

Same Output, More Speed

The vectorized kernel produces identical results but moves through data in big steps, so it finishes much sooner.

Quick Check

You vectorize a kernel with width 4 but n is 10. What handles the last two elements?

Recap

Vectorize a kernel by loading and storing packs, stepping by the width, and letting vectorize handle the sweep and the tail. 🚀

よくある質問

「SIMDとループの組み合わせ」レッスンは無料ですか?

はい。「SIMDとループの組み合わせ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。

「SIMDとループの組み合わせ」で何を学びますか?

カーネルの中核をベクトル化します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Mojo Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「SIMDとループの組み合わせ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMojo Academyレッスンでコードを書いて実行できますか?

はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. コンピュートカーネルの構造
  2. SIMDとループの組み合わせ
  3. メモリトラフィックの削減
  4. キャッシュ局所性のためのタイリング
← Mojo Academyに戻る