Vektör Toplama Çekirdeği
Her iş parçacığı bir çift öğeyi toplar.
Vektör Toplama Çekirdeği, CoddyKit'te ücretsiz bir CUDA Academy dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, CUDA Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. CUDA Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
The Big Idea
Vector addition is the perfect first kernel: each output is just C[i] = A[i] + B[i]. Every element is independent, so they can all run at once. 🚀
One Thread, One Element
The whole trick is simple: you assign one thread to one element. Instead of looping over the array, thousands of threads each do a single add in parallel.
Marking It as a Kernel
A function that runs on the GPU is a kernel, marked with the __global__ qualifier. That word tells nvcc this code launches on the device.
__global__ void vecAdd(const float* A, const float* B, float* C, int n) {
// body comes next
}Kernels Return void
A kernel always has a void return type. There is no return value to hand back to the CPU, so results must be written into device memory instead.
Finding This Thread's Index
Each thread computes its own global index so it knows which element to handle. The classic formula combines the block and thread coordinates.
int i = blockIdx.x * blockDim.x + threadIdx.x;The Single Line of Work
Once a thread knows its index i, the real work is one line. No loop, no branching, just one add per thread.
C[i] = A[i] + B[i];Why a Bounds Check Matters
You usually launch more threads than elements, so add an if (i < n) guard. Without it, extra threads read past the array and crash. 🛡️
if (i < n) {
C[i] = A[i] + B[i];
}The Full Kernel
Put it together and the entire vecAdd kernel is just a few lines. Tiny code, but it runs across thousands of threads at once.
__global__ void vecAdd(const float* A, const float* B, float* C, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) C[i] = A[i] + B[i];
}Pointers Live on the Device
The pointers A, B, and C must point to device memory. Hand a kernel a plain host pointer and it will read garbage or fault.
Mark Inputs as const
A and B are only read, so mark them const float*. This documents intent and lets the compiler optimize the read-only inputs more freely.
No Shared State Needed
Because every thread touches a different element, there are no races and no locks. This independence is exactly what makes the GPU shine here.
Quick Check
Why does the vector add kernel need an if (i < n) guard?
Recap
You wrote your first kernel: __global__ void vecAdd, one thread per element, a global index, and a bounds check. Simple code, massive parallelism. 🎉
Sıkça Sorulan Sorular
“Vektör Toplama Çekirdeği” dersi ücretsiz mi?
Evet — “Vektör Toplama Çekirdeği” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve CUDA Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. CUDA Academy kursu toplamda 4 dersten oluşur.
“Vektör Toplama Çekirdeği” dersinde ne öğreneceğim?
Her iş parçacığı bir çift öğeyi toplar. CUDA Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
CUDA Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te CUDA Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.
“Vektör Toplama Çekirdeği” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu CUDA Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her CUDA Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Vektör Toplama Çekirdeği
- Ana Bilgisayar Tarafını Bağlayın
- Sonucu CPU'da Doğrulayın
- İlk Hızlanmanızı Zamanlayın