-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathvector_add.cu
executable file
·78 lines (65 loc) · 1.89 KB
/
vector_add.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <cuda.h>
#include <cuda_runtime.h>
#define N 10
#define MAX_ERR 1e-6
__global__ void vector_add(float *out, float *a, float *b, int n)
{
for (int i = 0; i < n; i++)
{
out[i] = a[i] + b[i];
}
}
int main()
{
float *a, *b, *out;
float *d_a, *d_b, *d_out;
// Allocate host memory
printf("Allocating host memory\n");
a = (float *)malloc(sizeof(float) * N);
b = (float *)malloc(sizeof(float) * N);
out = (float *)malloc(sizeof(float) * N);
// Initialize host arrays
printf("Initializing host arrays\n");
for (int i = 0; i < N; i++)
{
a[i] = 1.0f;
b[i] = 2.0f;
}
// Allocate device memory
printf("Allocating device memory\n");
cudaMalloc((void **)&d_a, sizeof(float) * N);
cudaMalloc((void **)&d_b, sizeof(float) * N);
cudaMalloc((void **)&d_out, sizeof(float) * N);
// Transfer data from host to device memory
printf("Transferring data from host to device memory\n");
cudaMemcpy(d_a, a, sizeof(float) * N, cudaMemcpyHostToDevice);
cudaMemcpy(d_b, b, sizeof(float) * N, cudaMemcpyHostToDevice);
// Executing kernel
printf("Executing kernel\n");
vector_add<<<1, 1>>>(d_out, d_a, d_b, N);
// Transfer data back to host memory
printf("Transferring data back to host memory\n");
cudaMemcpy(out, d_out, sizeof(float) * N, cudaMemcpyDeviceToHost);
// Verification
printf("Verifying\n");
for (int i = 0; i < N; i++)
{
assert(fabs(out[i] - a[i] - b[i]) < MAX_ERR);
}
printf("out[0] = %f\n", out[0]);
// Deallocate device memory
printf("Deallocating device memory\n");
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_out);
// Deallocate host memory
printf("Deallocating host memory\n");
free(a);
free(b);
free(out);
printf("PASSED\n");
}