Carregamentos vetorizados com float4
Transações mais largas para maior largura de banda.
Carregamentos vetorizados com float4 é uma aula grátis de CUDA Academy no CoddyKit. Esta é a aula 3 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.
Move More Per Instruction
A normal load fetches one value at a time. A vectorized load grabs several adjacent values in a single wider instruction, doing more work per request.
Meet float4
CUDA ships built-in vector types. A float4 packs four floats into one 16-byte bundle you can load or store together.
float4 v = make_float4(1, 2, 3, 4);
float first = v.x; // also .y .z .wOne Load, Four Floats
Reading a float4 from memory pulls four floats in a single transaction. That cuts the number of memory instructions a thread must issue by four.
float4 a = reinterpret_cast<float4*>(in)[i];Reinterpret the Pointer
To use vector loads on a plain float array, you cast the pointer. reinterpret_cast lets you read the same bytes as float4 elements.
float4* in4 = reinterpret_cast<float4*>(in);
float4 chunk = in4[idx];Alignment Is Required
A float4 must sit on a 16-byte boundary. If the data is not properly aligned, the load is illegal and your kernel can crash or read garbage.
cudaMalloc Aligns for You
Good news: buffers from cudaMalloc are aligned to at least 256 bytes. So freshly allocated device arrays are safe to read as float4 from the start.
Fewer Requests, More Bandwidth
Wider loads mean fewer outstanding requests for the same data. That can raise achieved bandwidth when a kernel is limited by memory, not math.
Index by Vectors, Not Floats
With float4 each thread now covers four elements. Your global index steps through float4 slots, so the total thread count drops by four.
int i = blockIdx.x * blockDim.x + threadIdx.x;
float4 d = in4[i]; // covers 4 floatsOther Vector Widths
float4 is not the only choice. Types like float2 and int4 give you 8- or 16-byte loads, so you can pick a width that fits your data.
Watch the Leftovers
If your array length is not a multiple of four, handle the extra remainder elements with a normal scalar loop after the vectorized part.
It Costs Registers
A float4 holds four values, so it uses more registers per thread. As always, confirm the bandwidth gain outweighs any drop in occupancy.
Quick Check
You reinterpret a float array as float4 but the kernel crashes. Why?
Recap: Wider Is Faster
You learned that float4 loads four floats at once, cutting instructions and lifting bandwidth. Keep data aligned and handle leftover elements. 📦
Perguntas Frequentes
A aula “Carregamentos vetorizados com float4” é grátis?
Sim — o texto completo de “Carregamentos vetorizados com float4” é 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 “Carregamentos vetorizados com float4”?
Transações mais largas para maior largura de banda. 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 3 de 4.
Quanto tempo leva a aula “Carregamentos vetorizados com float4”?
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
- Paralelismo em nível de instrução
- Desenrolamento de laços com #pragma unroll
- Carregamentos vetorizados com float4
- Pressão de registradores e derramamentos