0Pricing
Mojo Academy · Lektion

Matmul Schritt für Schritt erstellen

Von naiven Schleifen zu einem echten Kernel.

Matmul Schritt für Schritt erstellen ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.

What Matmul Computes

Matrix multiply, or matmul, combines an M by K matrix with a K by N matrix to produce an M by N result. It is the heart of AI math.

The Dot Product Rule

Each output cell is a dot product: walk across one row of A and down one column of B, multiplying and summing as you go.

Three Nested Loops

The naive version uses three loops over i, j, and k. The outer two pick the output cell, the inner one sums the products.

for i in range(M):
    for j in range(N):
        for k in range(K):
            C[i, j] += A[i, k] * B[k, j]

Start Each Cell at Zero

Before accumulating, set each result cell to zero. Otherwise old or garbage values pollute your sum.

C[i, j] = 0.0

Accumulate the Sum

The inner k loop keeps a running accumulator. Summing into a local variable is often faster than touching C every step.

var acc: Float32 = 0.0
for k in range(K):
    acc += A[i, k] * B[k, j]
C[i, j] = acc

Use fn for Speed

Write the kernel with fn and typed arguments. Strict types let Mojo compile tight machine code with no dynamic overhead.

fn matmul(A: Matrix, B: Matrix, C: Matrix):
    pass

Why Naive Is Slow

The basic triple loop does the right math but reads B by column, jumping through memory. Poor locality wastes cache and time.

Loop Order Matters

Reordering to i, k, j keeps the inner loop walking memory in straight lines. Better access patterns can speed matmul a lot.

for i in range(M):
    for k in range(K):
        for j in range(N):
            C[i, j] += A[i, k] * B[k, j]

The Inner Loop Is the Target

Nearly all the time lives in the innermost loop. That hot inner loop is exactly where vectorizing and tuning pay off.

Correctness First

Get the simple version right and save its output. It becomes the reference you compare every faster kernel against.

A Path to a Real Kernel

From here you add SIMD, tiling, and parallelism. Each step keeps the same result but raises throughput toward peak hardware speed.

Quick Check

Why is the textbook triple-loop matmul often slow in practice?

Recap

Matmul sums a dot product per output cell with three loops; start cells at zero, accumulate locally, and mind loop order for cache. 🔢

Häufig gestellte Fragen

Ist die Lektion „Matmul Schritt für Schritt erstellen“ kostenlos?

Ja — der vollständige Text von „Matmul Schritt für Schritt erstellen“ 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 „Matmul Schritt für Schritt erstellen“?

Von naiven Schleifen zu einem echten Kernel. 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 2 von 4.

Wie lange dauert die Lektion „Matmul Schritt für Schritt erstellen“?

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

  1. Einen Tensor in Mojo modellieren
  2. Matmul Schritt für Schritt erstellen
  3. Das Skalarprodukt optimieren
  4. Numerische Korrektheit überprüfen
← Zurück zu Mojo Academy