-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample2.cu
More file actions
64 lines (50 loc) · 1.76 KB
/
Copy pathsample2.cu
File metadata and controls
64 lines (50 loc) · 1.76 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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <thrust/functional.h>
#include <thrust/transform.h>
const int SIZE=2048*512;
int main(int argc, char**argv){
clock_t start,stop;
printf("GPU:\n");
int i;
const int ishow=16;
int n;
const int nite=1000;
cudaSetDevice(0);
/* variables in device */
thrust::device_vector<float> d_InA(SIZE);
thrust::device_vector<float> d_InB(SIZE);
thrust::device_vector<float> d_Out(SIZE);
/* variables in host */
thrust:: host_vector<float> h_InA(SIZE);
thrust:: host_vector<float> h_InB(SIZE);
thrust:: host_vector<float> h_Out(SIZE);
srand(0);
/* initialize */
for(i=0;i<SIZE;i++) h_InA[i] = (float)(rand()%10)/10.0f;
for(i=0;i<SIZE;i++) h_InB[i] = (float)(rand()%10)/10.0f;
/* confirm */
printf("InA: "); for(i=0;i<ishow;i++) printf(" %.2f",h_InA[i]); printf("\n");
printf("InB: "); for(i=0;i<ishow;i++) printf(" %.2f",h_InB[i]); printf("\n");
/* transfer from host to device */
thrust::copy(h_InA.begin(),h_InA.end(),d_InA.begin());/* d_InA = h_InA */
thrust::copy(h_InB.begin(),h_InB.end(),d_InB.begin());/* d_InB = h_InB */
cudaDeviceSynchronize();
/* call kernel functions, specify grid and block as <<< grid, block >>> */
start=clock();
for(n=0;n<nite;n++){
thrust::transform(d_InA.begin(), d_InA.end(), d_InB.begin(), d_Out.begin(), thrust::plus<float>());
}
cudaDeviceSynchronize();
stop=clock();
/* transfer from device to host */
thrust::copy(d_Out.begin(),d_Out.end(),h_Out.begin());
/* confirm */
printf("Out: "); for(i=0;i<ishow;i++) printf(" %.2f",h_Out[i]); printf("\n");
printf("Time: %.2f [ms]\n",(double)(stop-start));
return 0;
}