-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumArrayOnHost.cu
More file actions
43 lines (33 loc) · 844 Bytes
/
sumArrayOnHost.cu
File metadata and controls
43 lines (33 loc) · 844 Bytes
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
#include <cstdlib>
#include <string>
#include <ctime>
#include <iostream>
void initialData(float *ip, int size){
time_t t;
srand((unsigned int)time(&t));
for(int i=0;i<size;++i){
ip[i] = (float)( rand() & 0xFF ) / 10.0F;
}
}
void sumArrayOnHost(float *A, float *B, float *C, const int N){
for(int idx = 0;idx < N; ++idx){
C[idx] = A[idx] + B[idx];
}
}
int main(){
int nElem = 1024;
size_t nBytes = nElem * sizeof(float);
auto h_A = (float *) malloc(nBytes);
auto h_B = (float *) malloc(nBytes);
auto h_C = (float *) malloc(nBytes);
initialData(h_A, nElem);
initialData(h_B, nElem);
sumArrayOnHost(h_A, h_B, h_C, nElem);
for(int i=0;i<nElem;++i){
std::cout << "i: " << h_C[i] << "\n";
}
free(h_A);
free(h_B);
free(h_C);
return 0;
}