0Pricing
CUDA Academy · Aula

Declare vetores __shared__

Memória rápida de trabalho temporário por bloco.

Declare vetores __shared__ é uma aula grátis de CUDA Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de CUDA Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de CUDA Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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. 🎯

Perguntas Frequentes

A aula “Declare vetores __shared__” é grátis?

Sim — o texto completo de “Declare vetores __shared__” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de CUDA Academy, atualize para CoddyKit PRO. O curso de CUDA Academy inclui 4 aulas no total.

O que vou aprender em “Declare vetores __shared__”?

Memória rápida de trabalho temporário por bloco. Você pratica CUDA Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar CUDA Academy?

Nenhuma experiência prévia é necessária. CUDA Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.

Quanto tempo leva a aula “Declare vetores __shared__”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de CUDA Academy?

Sim. Cada aula de CUDA Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Declare vetores __shared__
  2. Sincronize com __syncthreads
  3. Evite conflitos de bancos
  4. Memória compartilhada dinâmica
← Voltar para CUDA Academy