要素単位のSIMD演算
ベクトル全体を一度に加算・乗算します
「要素単位のSIMD演算」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Math on Whole Vectors
The best part of SIMD is that arithmetic works on the entire vector at once. One operation touches every lane together.
Adding Two Vectors
Add two SIMD values and each lane is added to its matching lane. This is called element-wise addition.
var a = SIMD[DType.int32, 4](1, 2, 3, 4)
var b = SIMD[DType.int32, 4](10, 20, 30, 40)
var c = a + bLane Lines Up with Lane
In a + b, lane 0 adds to lane 0 and lane 1 adds to lane 1. The lanes never mix, they stay aligned.
print(c[0])Multiplying Vectors
Multiplication is element-wise too. Each lane of one vector is multiplied by the same lane of the other vector.
var p = a * bSubtract and Divide
Subtraction and division follow the same rule. Every lane is handled independently and in parallel across the pack.
var d = b - aScalar Meets Vector
Add a single number to a vector and Mojo applies it to every lane. The scalar spreads across all positions at once.
var bumped = a + 100Widths Must Match
To combine two SIMD values they need the same DType and the same width. Mismatched lane counts will not line up.
Comparisons Go Lane by Lane
Comparing two vectors gives a SIMD of Bools, one True or False per lane. Each lane reports its own result.
var mask = a > SIMD[DType.int32, 4](2)Reducing to One Number
Sometimes you want a single answer. reduce_add sums all lanes into one scalar, perfect for totals.
var total = a.reduce_add()
print(total)Built-in Math Functions
Standard math like sqrt also works lane-wise on SIMD, computing the result for every element in one call.
from math import sqrt
var roots = sqrt(a.cast[DType.float32]())Same Code, Many Results
Writing a + b looks like scalar math but produces many results at once. That readability with speed is Mojo's promise.
Quick Check
You compute a + b where both are SIMD[DType.int32, 4]. How is the addition done?
Recap
Element-wise SIMD math runs +, -, *, and / on every lane at once, scalars spread to all lanes, and reduce_add folds a vector into one number. ✨
よくある質問
「要素単位の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フィードバックを取得できます。ローカル設定は不要です。