Description
### File: `vectadd.cu`
**Summary:**
Implemented vector addition by writing a simple CUDA program. Explored how to launch a kernel to perform a parallelized addition of two arrays, where each thread computes the sum of a pair of values.
**Learned:**
- Basics of writing a CUDA kernel.
- Understanding of grid, block, and thread hierarchy in CUDA.
- How to allocate and manage device (GPU) memory using `cudaMalloc`, `cudaMemcpy`, and `cudaFree`.
### Reading:
- Read **Chapter 1** of the PMPP book.
- Learned about the fundamentals of parallel programming, CUDA architecture, and the GPU execution model.
---
vectAdd.cu
#include <iostream>
__global__ void vectorAdd(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];
}
}
int main() {
const int N = 10;
float A[N], B[N], C[N];
float *d_a, *d_b,*d_c;
cudaMalloc(&d_a,N*sizeof(float));
cudaMalloc(&d_b,N*sizeof(float));
cudaMalloc(&d_c,N*sizeof(float));
cudaMemcpy(d_a,A,N*sizeof(float),cudaMemcpyHostToDevice);
cudaMemcpy(d_b,B,N*sizeof(float),cudaMemcpyHostToDevice);
int blocksize=256;
int gridsize=ceil(N/blocksize);
vectorAdd<<<gridsize,blocksize>>>(d_a,d_b,d_c,N);
cudaMemcpy(C,d_c,N*sizeof(float),cudaMemcpyDeviceToHost);
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);
}