Scegliere la larghezza SIMD
Abbini la larghezza alle lane dell'hardware.
Scegliere la larghezza SIMD è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Mojo Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Mojo Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Width Decides Throughput
The SIMD width is how many lanes you process per instruction. Picking it well is key to real performance.
Hardware Has Fixed Lanes
Your CPU has vector registers of a fixed size. A good SIMD width matches the lanes that hardware can run at once.
Too Narrow Wastes Power
A width of 1 ignores the CPU's vector units entirely. You leave most of the chip's throughput sitting idle.
var slow = SIMD[DType.float32, 1](2.0)Too Wide Spills Over
If a width is larger than the hardware supports, Mojo splits it into smaller chunks. That still works but adds overhead.
Let Mojo Tell You
Mojo can report the ideal width for a type on this machine through simdwidthof, so you do not have to guess.
from sys import simdwidthof
alias w = simdwidthof[DType.float32]()Use It as a Parameter
Because simdwidthof is known at compile time, you can feed it straight into a SIMD type as its width.
alias Vec = SIMD[DType.float32, simdwidthof[DType.float32]()]Width Depends on DType
Smaller elements fit more lanes in the same register. So float32 usually gets a bigger native width than float64.
Portable by Design
Using simdwidthof means your code adapts to each machine automatically. The same source stays fast and portable.
Multiples of Width Are Easy
Data that comes in clean multiples of your width vectorizes neatly, with no leftover lanes to handle as a remainder.
Mind the Leftovers
When a count is not a multiple of the width, a few tail elements remain. You finish those with a small scalar step.
Start with the Native Width
A safe default is simdwidthof for your DType. Measure later, but the native width is almost always a strong start.
Quick Check
You want a SIMD width that fits this CPU's float32 lanes without hardcoding a guess. What helps?
Recap
Match the SIMD width to the hardware: simdwidthof gives the native lane count per DType, and a scalar tail cleans up leftover elements. 🎯
Domande Frequenti
La lezione «Scegliere la larghezza SIMD» è gratuita?
Sì — il testo completo di «Scegliere la larghezza SIMD» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Mojo Academy, passa a CoddyKit PRO. Il corso Mojo Academy include 4 lezioni in totale.
Cosa imparerò in «Scegliere la larghezza SIMD»?
Abbini la larghezza alle lane dell'hardware. Eserciti Mojo Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Mojo Academy?
Non è richiesta alcuna esperienza precedente. Mojo Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Scegliere la larghezza SIMD»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Mojo Academy?
Sì. Ogni lezione Mojo Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Conoscere il tipo SIMD
- Calcolo SIMD elemento per elemento
- Scegliere la larghezza SIMD
- Vettorializzare un ciclo