0Pricing
Mojo Academy · レッスン

ループのベクトル化

スカラーのループをベクトル処理に変換します

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

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

The Scalar Loop

A plain loop touches one element per step. It is clear but leaves your CPU's vector lanes mostly unused.

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

The Vector Idea

Vectorizing means each loop step handles a whole pack of elements instead of one. You do more work per iteration.

Load a Chunk

Instead of one value, you load several at once into a SIMD vector. That single load grabs a slice of your data.

var chunk = a.load[width=4](i)

Compute the Whole Chunk

With both chunks in SIMD form, add them in one element-wise step. The math covers every lane at once.

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

Store the Result

Write the whole SIMD result back in one move with a vector store. One write covers all the lanes you computed.

out.store[width=4](i, sum)

Step by the Width

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

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

Let Mojo Drive It

Mojo's vectorize helper runs a closure across the range in vector steps and handles the bookkeeping for you.

from algorithm import vectorize

A vectorize Closure

You write a small parameterized function that processes one chunk. Mojo calls it with the right width as it sweeps the range.

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

Run the Vectorize

Call vectorize with your chunk function, the width, and the total size. It does the looping and the tail for you.

vectorize[work, 4](n)

Same Result, Far Faster

The vectorized loop gives identical output to the scalar one but moves through data in big steps, so it finishes sooner.

Measure to Confirm

Always time both versions. Vectorization usually wins big, but a benchmark proves the gain on your real data.

Quick Check

You replace a one-element-per-step loop with SIMD loads and stores. How should the loop advance?

Recap

Vectorizing loads and stores whole chunks, steps by the width, and lets vectorize handle the sweep and tail, giving the same result far faster. 🚀

よくある質問

「ループのベクトル化」レッスンは無料ですか?

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

「ループのベクトル化」で何を学びますか?

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

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

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

「ループのベクトル化」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. SIMD型を知る
  2. 要素単位のSIMD演算
  3. SIMD幅の選択
  4. ループのベクトル化
← Mojo Academyに戻る