-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path01-vector-add (2).cu
56 lines (46 loc) · 900 Bytes
/
01-vector-add (2).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
#include <stdio.h>
void initWith(float num, float *a, int N)
{
for(int i = 0; i < N; ++i)
{
a[i] = num;
}
}
void addVectorsInto(float *result, float *a, float *b, int N)
{
for(int i = 0; i < N; ++i)
{
result[i] = a[i] + b[i];
}
}
void checkElementsAre(float target, float *array, int N)
{
for(int i = 0; i < N; i++)
{
if(array[i] != target)
{
printf("FAIL: array[%d] - %0.0f does not equal %0.0f\n", i, array[i], target);
exit(1);
}
}
printf("SUCCESS! All values added correctly.\n");
}
int main()
{
const int N = 2<<20;
size_t size = N * sizeof(float);
float *a;
float *b;
float *c;
a = (float *)malloc(size);
b = (float *)malloc(size);
c = (float *)malloc(size);
initWith(3, a, N);
initWith(4, b, N);
initWith(0, c, N);
addVectorsInto(c, a, b, N);
checkElementsAre(7, c, N);
free(a);
free(b);
free(c);
}