-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorAddition.cu
executable file
·63 lines (50 loc) · 1.57 KB
/
vectorAddition.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<chrono>
#define SIZE 10000000
#define Ts_per_B 192
void random_ints(int* a,int N)
{
for(int i=0;i<N;i++)
a[i]=rand()%200;
}
__global__ void add(int *a, int *b, int *c) {
int index=threadIdx.x+blockIdx.x*blockDim.x;
if(index<SIZE)
{
c[index] = a[index] + b[index];
}
}
int main() {
auto start = std::chrono::high_resolution_clock::now();
int *a, *b, *c;// host copies of variables a, b & c
int *d_a, *d_b, *d_c; // device copies of a, b, c
int size = SIZE * sizeof(int);
// Alloc space for device
cudaMalloc((void **)&d_a,size);
cudaMalloc((void **)&d_b,size);
cudaMalloc((void **)&d_c,size);
//allocate memory to arrays and initalize elemets of a & b
a=(int*)malloc(size);random_ints(a,SIZE);
b=(int*)malloc(size);random_ints(b,SIZE);
c=(int*)malloc(size);
// Copy inputs to device
cudaMemcpy(d_a, a, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_b, b, size, cudaMemcpyHostToDevice);
// Launch add() kernel on GPU with SIZE blocks
add<<<(SIZE+Ts_per_B-1)/Ts_per_B,Ts_per_B>>>(d_a, d_b, d_c);
// Copy result back to host
cudaMemcpy(c, d_c, size, cudaMemcpyDeviceToHost);
printf("tested by majid_kakavandi!\n");
// Cleanup
free(a); free(b); free(c);
cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
auto end = std::chrono::high_resolution_clock::now();
double time_taken = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
time_taken *= 1e-9;
printf("Time taken by program is : %f sec\n", time_taken);
return 0;
}