-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
145 lines (114 loc) · 5.24 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <stdint.h>
#include "support.h"
#include "kernel.cu"
int main(int argc, char* argv[])
{
Timer timer;
// Initialize host variables ----------------------------------------------
printf("\nSetting up the problem..."); fflush(stdout);
startTime(&timer);
unsigned int *in_h;
unsigned int* bins_h;
unsigned int* bins_h1;
unsigned int* bins_h2;
unsigned int *in_d1;
unsigned int* bins_d1;
unsigned int *in_d2;
unsigned int* bins_d2;
unsigned int num_elements, num_bins;
cudaError_t cuda_ret;
cudaStream_t stream0, stream1;
cudaStreamCreate(&stream0);
cudaStreamCreate(&stream1);
if(argc == 1) {
num_elements = 1000000;
num_bins = 4096;
} else if(argc == 2) {
num_elements = atoi(argv[1]);
num_bins = 4096;
} else if(argc == 3) {
num_elements = atoi(argv[1]);
num_bins = atoi(argv[2]);
} else {
printf("\n Invalid input parameters!"
"\n Usage: ./histogram # Input: 1,000,000, Bins: 4,096"
"\n Usage: ./histogram <m> # Input: m, Bins: 4,096"
"\n Usage: ./histogram <m> <n> # Input: m, Bins: n"
"\n");
exit(0);
}
initVector(&in_h, num_elements, num_bins);
bins_h = (unsigned int*) malloc(num_bins*sizeof(unsigned int));
bins_h1= (unsigned int*) malloc(num_bins*sizeof(unsigned int));
bins_h2= (unsigned int*) malloc(num_bins*sizeof(unsigned int));
stopTime(&timer); printf("%f s\n", elapsedTime(timer));
printf(" Input size = %u\n Number of bins = %u\n", num_elements,
num_bins);
int SegSize = num_elements/2;
// Allocate device variables ----------------------------------------------
printf("Allocating device variables..."); fflush(stdout);
startTime(&timer);
cuda_ret = cudaMalloc((void**)&in_d1, SegSize * sizeof(unsigned int));
if(cuda_ret != cudaSuccess) printf("Unable to allocate device memory");
cuda_ret = cudaMalloc((void**)&bins_d1, num_bins * sizeof(unsigned int));
if(cuda_ret != cudaSuccess) printf("Unable to allocate device memory");
cuda_ret = cudaMalloc((void**)&in_d2, SegSize * sizeof(unsigned int));
if(cuda_ret != cudaSuccess) printf("Unable to allocate device memory");
cuda_ret = cudaMalloc((void**)&bins_d2, num_bins * sizeof(unsigned int));
if(cuda_ret != cudaSuccess) printf("Unable to allocate device memory");
cudaDeviceSynchronize();
stopTime(&timer); printf("%f s\n", elapsedTime(timer));
// Copy host variables to device ------------------------------------------
printf("Copying data from host to device..."); fflush(stdout);
startTime(&timer);
//cuda_ret = cudaMemcpy(in_d, in_h, num_elements * sizeof(unsigned int),
// cudaMemcpyHostToDevice);
//if(cuda_ret != cudaSuccess) printf("Unable to copy memory to the device");
cuda_ret = cudaMemset(bins_d1, 0, num_bins * sizeof(unsigned int));
if(cuda_ret != cudaSuccess) printf("Unable to set device memory");
cuda_ret = cudaMemset(bins_d2, 0, num_bins * sizeof(unsigned int));
if(cuda_ret != cudaSuccess) printf("Unable to set device memory");
cudaDeviceSynchronize();
stopTime(&timer); printf("%f s\n", elapsedTime(timer));
// Launch kernel ----------------------------------------------------------
printf("Launching kernel..."); fflush(stdout);
startTime(&timer);
dim3 block(BLOCK_SIZE);
dim3 grid((BLOCK_SIZE + block.x - 1)/block.x);
for (int i = 0; i <num_elements ; i+=SegSize*2) {
cudaMemcpyAsync(in_d1, in_h+i, SegSize * sizeof(unsigned int),cudaMemcpyHostToDevice,stream0);
cudaMemcpyAsync(in_d2, in_h+i+SegSize, SegSize * sizeof(unsigned int),cudaMemcpyHostToDevice,stream1);
histo_kernel<<<grid,block,0,stream0>>>(in_d1,bins_d1,SegSize,num_bins);
histo_kernel<<<grid,block,0,stream1>>>(in_d2,bins_d2,SegSize,num_bins);
cudaMemcpyAsync(bins_h1, bins_d1, num_bins * sizeof(unsigned int),cudaMemcpyDeviceToHost,stream0);
cudaMemcpyAsync(bins_h2, bins_d2, num_bins * sizeof(unsigned int),cudaMemcpyDeviceToHost,stream1);
}
//histogram(in_d, bins_d, num_elements, num_bins);
cuda_ret = cudaDeviceSynchronize();
if(cuda_ret != cudaSuccess) printf("Unable to launch/execute kernel");
stopTime(&timer); printf("%f s\n", elapsedTime(timer));
// Copy device variables from host ----------------------------------------
printf("Copying data from device to host..."); fflush(stdout);
startTime(&timer);
//cuda_ret = cudaMemcpy(bins_h, bins_d1, num_bins * sizeof(unsigned int),
//cudaMemcpyDeviceToHost);
//if(cuda_ret != cudaSuccess) printf("Unable to copy memory to host");
cudaDeviceSynchronize();
stopTime(&timer); printf("%f s\n", elapsedTime(timer));
for(int i=0;i<num_bins;i++){
bins_h[i]=bins_h1[i]+bins_h2[i];
}
// Verify correctness -----------------------------------------------------
printf("Verifying results..."); fflush(stdout);
verify(in_h, bins_h, num_elements, num_bins);
// Free memory ------------------------------------------------------------
cudaFree(in_d1);
cudaFree(in_d2);
cudaFree(bins_d1);
cudaFree(bins_d2);
free(in_h); free(bins_h);
free(bins_h1);
free(bins_h2);
return 0;
}