内積の最適化
内積をベクトル化してアンロールします
「内積の最適化」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The Dot Product Core
The inner product multiplies two sequences element by element and adds the results. Speeding it up speeds your whole matmul.
var acc: Float32 = 0.0
for k in range(K):
acc += a[k] * b[k]One Lane at a Time Is Slow
A plain loop handles a single pair per step. Modern CPUs can do many at once, so scalar code leaves lanes idle.
Enter SIMD
A SIMD value packs several numbers and applies one operation to all of them together, doing real work in parallel per instruction.
var v = SIMD[DType.float32, 4](1, 2, 3, 4)Load a Chunk at Once
Instead of one float, load a whole SIMD-width slice of each array in a single move, ready for vector math.
var va = a.load[width=4](k)
var vb = b.load[width=4](k)Multiply Whole Vectors
Multiplying two SIMD values yields a vector of products in one step. The element-wise work happens across all lanes together.
var prod = va * vb # 4 products at onceAccumulate Into a Vector
Keep a SIMD accumulator and add each product vector into it. You delay the final sum until the loop is done.
var acc = SIMD[DType.float32, 4](0)
acc += va * vbStep by the Width
The loop now advances by the SIMD width, not by one. Four lanes wide means a quarter as many iterations.
for k in range(0, K, 4):
acc += a.load[width=4](k) * b.load[width=4](k)Reduce at the End
After the loop, collapse the lane accumulator into one number with a horizontal reduce, giving the final dot product.
var total = acc.reduce_add()Unrolling the Loop
Unrolling processes several widths per iteration, cutting loop overhead and exposing more independent work to the CPU.
Handle the Remainder
If K is not a multiple of the width, a few elements are left over. A small tail loop finishes them one at a time.
for k in range(K - K % 4, K):
total += a[k] * b[k]Let Mojo Help
Mojo offers the vectorize helper to apply a width-parameterized body across a range, handling stepping and the tail for you.
Quick Check
After a SIMD-width dot product loop, why do you call reduce_add at the end?
Recap
Vectorize the inner product by loading SIMD chunks, multiplying lanes together, accumulating, then reduce; unroll and clean up the tail. ⚡
よくある質問
「内積の最適化」レッスンは無料ですか?
はい。「内積の最適化」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「内積の最適化」で何を学びますか?
内積をベクトル化してアンロールします ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「内積の最適化」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。