__shared__-Arrays deklarieren
Schneller Scratchpad-Speicher pro Block.
__shared__-Arrays deklarieren ist eine kostenlose CUDA Academy-Lektion auf CoddyKit. Dies ist Lektion 1 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 CUDA Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
A Scratchpad Per Block
Every block gets a tiny, fast pool of on-chip memory called shared memory. Think of it as a scratchpad the whole block can write to and read from together.
Why It Is So Fast
Shared memory sits right on the streaming multiprocessor, so it is roughly 100x faster than global memory. Use it to avoid hammering slow off-chip DRAM. 🚀
The __shared__ Keyword
You declare it with the __shared__ qualifier inside a kernel. This one array is then shared by every thread in the block.
__global__ void kern() {
__shared__ float tile[256];
}Fixed Size at Compile Time
When you give a size in brackets, that is static shared memory. The compiler must know the size, so it has to be a constant, not a runtime value.
__shared__ int counts[128];One Copy, Not One Per Thread
This is the key idea: a __shared__ array is created once per block, not once per thread. All threads see the exact same array.
Threads Cooperate Through It
Because every thread sees the same data, shared memory lets threads cooperate. One thread can stash a value and a neighbor can pick it up.
Block Scope, Not Beyond
Its lifetime matches the block. The array is born when the block starts and gone when it ends, so it has block scope only. 🧱
Each Thread Owns a Slot
A common pattern is one slot per thread, indexed by threadIdx.x. Each thread loads its element into shared memory in parallel.
__shared__ float s[256];
s[threadIdx.x] = input[i];A Tiny but Precious Resource
Shared memory is small, often just 48 to 100 KB per SM. Asking for too much per block reduces how many blocks can run at once.
The Classic Use: Staging Tiles
The most common job is staging a tile of global data so the block can reuse it many times without going back to slow DRAM.
Not Visible to Other Blocks
Remember the boundary: shared memory is private to its block. Two different blocks each get their own separate copy and cannot peek at each other.
Quick Check
Let us check how shared memory is scoped.
Recap
You learned that __shared__ gives each block a fast on-chip scratchpad, created once per block and ideal for staging reusable data. Next: keeping threads in step. 🎯
Häufig gestellte Fragen
Ist die Lektion „__shared__-Arrays deklarieren“ kostenlos?
Ja — der vollständige Text von „__shared__-Arrays deklarieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des CUDA Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „__shared__-Arrays deklarieren“?
Schneller Scratchpad-Speicher pro Block. Du übst CUDA 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 CUDA Academy zu starten?
Keine Vorkenntnisse erforderlich. CUDA 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 1 von 4.
Wie lange dauert die Lektion „__shared__-Arrays deklarieren“?
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 CUDA Academy-Lektion Code schreiben und ausführen?
Ja. Jede CUDA 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
- __shared__-Arrays deklarieren
- Mit __syncthreads synchronisieren
- Bankkonflikte vermeiden
- Dynamischer Shared Memory