ParallelismBeginner~20 min

Vector Addition

The 'Hello World' of GPU programming. Add two arrays element-wise on the GPU, then measure memory bandwidth to understand the roofline model.

vectorcudaMalloccudaMemcpybandwidthroofline

// TUTORIAL

GPU execution
1 / 2

Step 1 — Write the addition kernel

Vector addition is the simplest GPU program: add two arrays element-by-element.

Each thread is responsible for exactly one index i:
C[i] = A[i] + B[i];


The global index formula you learned in Hello CUDA applies here too:
int i = blockIdx.x * blockDim.x + threadIdx.x;


Task: Complete the kernel body, then launch it and verify the result on the CPU.
main.cu
loading editor…

// expected output

Result: CORRECT

// YOUR PROGRESS

Log in to save progress

// DETAILS

Category
Parallelism
Difficulty
Beginner
Est. time
~20 min
Prerequisites
None

// MORE IN PARALLELISM

// LEARN THE THEORY

Parallelism concepts