To understand vector operation on the GPU, we will start by writing a vector addition program on the CPU and then modify it to utilize the parallel structure of GPU. We will take two arrays of some numbers and store the answer of element-wise addition in the third array. The vector addition function on CPU is shown here:
#include "stdio.h"#include<iostream> //Defining Number of elements in Array#define N 5 //Defining vector addition function for CPUvoid cpuAdd(int *h_a, int *h_b, int *h_c) { int tid = 0; while (tid < N) { h_c[tid] = h_a[tid] + h_b[tid]; tid += 1; } }
The cpuAdd should be very simple to understand. One thing you might find difficult to understand is the use of tid. It is included to make the program ...