-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_blocks_and_threads.cu
More file actions
64 lines (51 loc) · 1.72 KB
/
5_blocks_and_threads.cu
File metadata and controls
64 lines (51 loc) · 1.72 KB
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
62
63
64
// Blocks and Threads
// blockIdx.x-> 0 | 1 | 2
// threadIdx.x-> [0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5]
//
// blockDim -> Block dimension = number of threads per block
//
// index = threadIdx + (blockIdx.x * M)
#include <stdio.h>
#include <time.h>
#define N 2048*2048
#define M 512 // THREADS_PER_BLOCK
__global__ void add(int *a, int *b, int *c, int n) {
int index = threadIdx.x + blockIdx.x * blockDim.x;
if(index < n) // avoid accessing beyond end of array, when not perfect multiples.
c[index] = a[index] + b[index];
}
void random_ints(int *a, int n){
int i;
for (i = 0; i < n; ++i)
a[i] = rand()%100;
}
int main(void) {
int *a, *b, *c; // host copy
int *d_a, *d_b, *d_c; // device copy
int size = N * sizeof(int);
// allocate mem for device copies
cudaMalloc((void**)&d_a, size);
cudaMalloc((void**)&d_b, size);
cudaMalloc((void**)&d_c, size);
a = (int *)malloc(size); random_ints(a, N);
b = (int *)malloc(size); random_ints(b, N);
c = (int *)malloc(size);
clock_t start, end;
double cpu_time_used;
start = clock();
// copy inputs to device
// cudaMemcpy(destination, source, size, direction);
cudaMemcpy(d_a, a, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_b, b, size, cudaMemcpyHostToDevice);
// launch add() kernel on GPU with N threads
add<<<(N + M-1)/M, M>>>(d_a, d_b, d_c, N);
// copy result back to host
cudaMemcpy(c, d_c, size, cudaMemcpyDeviceToHost);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("exec time: %f seconds\n", cpu_time_used);
// cleanup
free(a); free(b); free(c);
cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
return 0;
}