0Pricing
Mojo Academy · Lektion

SIMD mit Schleifen kombinieren

Vektorisieren Sie den Kern des Kernels.

SIMD mit Schleifen kombinieren 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.

From Scalar to SIMD

To accelerate a kernel, swap the one-at-a-time loop for one that handles a whole pack of values per step with SIMD.

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

Pick a Width

Choose how many elements fit in one vector. The width is the number of lanes each step processes together.

alias width = 4

Load a Chunk

Grab several elements at once into a SIMD value with a vector load instead of reading them one by one.

var va = a.load[width=width](i)

Compute the Pack

Run the kernel's math on both chunks at once. The whole pack is added element-wise in a single operation.

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

Store the Pack

Write the full result back with a vector store, covering every lane you just computed in one move.

out.store[width=width](i, vsum)

Step by the Width

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

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

Let vectorize Help

Mojo's vectorize helper sweeps a closure across the range in vector steps so you skip the manual bookkeeping.

from algorithm import vectorize

Write the Chunk Closure

You define a small parameterized function that handles one chunk. Mojo calls it with the right width as it sweeps.

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

Run vectorize

Call vectorize with your closure, the width, and the size. It loops and even handles the leftover tail for you.

vectorize[body, width](n)

Mind the Tail

When n is not a multiple of the width, a few elements remain. vectorize cleans up that tail so nothing is missed.

Same Output, More Speed

The vectorized kernel produces identical results but moves through data in big steps, so it finishes much sooner.

Quick Check

You vectorize a kernel with width 4 but n is 10. What handles the last two elements?

Recap

Vectorize a kernel by loading and storing packs, stepping by the width, and letting vectorize handle the sweep and the tail. 🚀

Häufig gestellte Fragen

Ist die Lektion „SIMD mit Schleifen kombinieren“ kostenlos?

Ja — der vollständige Text von „SIMD mit Schleifen kombinieren“ 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 „SIMD mit Schleifen kombinieren“?

Vektorisieren Sie den Kern des Kernels. 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 „SIMD mit Schleifen kombinieren“?

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. Anatomie eines Compute-Kernels
  2. SIMD mit Schleifen kombinieren
  3. Speicherverkehr reduzieren
  4. Tiling für Cache-Lokalität
← Zurück zu Mojo Academy