Das Skalarprodukt optimieren
Vektorisieren und entrollen Sie das Skalarprodukt.
Das Skalarprodukt optimieren ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. ⚡
Häufig gestellte Fragen
Ist die Lektion „Das Skalarprodukt optimieren“ kostenlos?
Ja — der vollständige Text von „Das Skalarprodukt optimieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Das Skalarprodukt optimieren“?
Vektorisieren und entrollen Sie das Skalarprodukt. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Mojo Academy zu starten?
Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Das Skalarprodukt optimieren“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?
Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Einen Tensor in Mojo modellieren
- Matmul Schritt für Schritt erstellen
- Das Skalarprodukt optimieren
- Numerische Korrektheit überprüfen