-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaxReduction.cu
225 lines (179 loc) · 5.31 KB
/
MaxReduction.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// This program performs sum reduction with an optimization
// removing warp bank conflicts
// By: Nick from CoffeeBeforeArch
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <iostream>
#define CHECK_CUDA(cond) check_cuda(cond, __LINE__)
#define SIZE 256
#define SHMEM_SIZE 256
void check_cuda(cudaError_t status, std::size_t line)
{
if(status != cudaSuccess)
{
std::cout << cudaGetErrorString(status) << '\n';
std::cout << "Line: " << line << '\n';
throw 0;
}
}
__global__ void max_reduction_resnet(float *v, float *v_r) {
__shared__ int partial_sum[1000];
// Calculate thread ID
int tid = blockIdx.x * blockDim.x + threadIdx.x;
// Load elements into shared memory
partial_sum[threadIdx.x] = v[tid];
__syncthreads();
// Start at 1/2 block stride and divide by two each iteration
for (int s = blockDim.x / 2; s > 0; s >>= 1) {
// Each thread does work unless it is further than the stride
if (threadIdx.x < s) {
partial_sum[threadIdx.x] = max(partial_sum[threadIdx.x], partial_sum[threadIdx.x + s]);
}
__syncthreads();
}
// Let the thread 0 for this block write it's result to main memory
// Result is inexed by this block
if (threadIdx.x == 0 && partial_sum[0] > 0.8f) {
v_r[blockIdx.x] = 1;
}
}
__global__ void max_reduction_posenet(float *v, float *v_r) {
// int tid = blockIdx.x * blockDim.x + threadIdx.x;
float max_p = 0;
for (int s = 0; s < 96*96; s++){
float tmp = v[9216 * blockIdx.x + threadIdx.x + 16 * s];
if (tmp > max_p) {
max_p = tmp;
}
}
// v_r[tid] = max_p;
__shared__ int result[32];
// Initalize the shared memory to 0
if (threadIdx.x == 0) {
result[blockIdx.x] = 0;
}
__syncthreads();
if (max_p > 0.8f) {
atomicAdd(&result[blockIdx.x], 1);
}
__syncthreads();
if (threadIdx.x == 0 && result[blockIdx.x] > 8) {
v_r[blockIdx.x] = 1;
}
}
__global__ void max_reduction_openseg(float *v, float *v_r) {
// int tid = blockIdx.x * blockDim.x + threadIdx.x;
int bid = blockIdx.y * gridDim.x + blockIdx.x;
int tid = bid * blockDim.x + threadIdx.x;
float max_p = 0;
for (int s = 0; s < 19; s++){
float tmp = v[tid*16*19 + s];
if (tmp > max_p) {
max_p = tmp;
}
}
// v_r[tid] = max_p;
__shared__ int result[8];
// Initalize the shared memory to 0
if (threadIdx.x == 0 && blockIdx.y == 0) {
// printf("%d", blockIdx.y);
result[blockIdx.x] = 0;
}
__syncthreads();
if (max_p > 0.8f) {
atomicAdd(&result[blockIdx.x], 1);
}
__syncthreads();
if (threadIdx.x == 0 && blockIdx.y == 0 && result[blockIdx.x] > 1) {
v_r[blockIdx.x] = 1;
}
}
__global__ void check_posenet(float *v, int *v_r, int *result) {
int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (v[tid] > 0.8f) {
atomicAdd(&result[blockIdx.x], 1);
}
if (threadIdx.x == 0 && result[blockIdx.x] > 8) {
v_r[blockIdx.x] = 1;
}
}
__global__ void sum_reduction(float *v, float *v_r) {
// Allocate shared memory
__shared__ float partial_sum[SHMEM_SIZE];
// Calculate thread ID
// int tid = blockIdx.x * blockDim.x + threadIdx.x;
int bid = blockIdx.y * gridDim.x + blockIdx.x;
int tid = bid * blockDim.x + threadIdx.x;
// Load elements into shared memory
partial_sum[threadIdx.x] = v[tid * 16];
__syncthreads();
// Start at 1/2 block stride and divide by two each iteration
for (int s = blockDim.x / 2; s > 0; s >>= 1) {
// Each thread does work unless it is further than the stride
if (threadIdx.x < s) {
// partial_sum[threadIdx.x] += partial_sum[threadIdx.x + s];
partial_sum[threadIdx.x] = max(partial_sum[threadIdx.x], partial_sum[threadIdx.x + s]);
}
__syncthreads();
}
// Let the thread 0 for this block write it's result to main memory
// Result is inexed by this block
if (threadIdx.x == 0) {
v_r[blockIdx.x] = partial_sum[0];
}
}
void initialize_vector(float *v, int n) {
for (int i = 0; i < n; i++) {
// v[i] = rand() % 10;
v[i] = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
}
}
int main() {
// Vector size
int n = 32 * 1000;
// int n = 32 * 96 * 96 * 16;
// int n = 8 * 2048 * 1024 * 19;
size_t bytes = n * sizeof(float);
// Original vector and result vector
float *h_v, *h_v_r;
float *d_v, *d_v_r;
// Allocate memory
h_v = (float*)malloc(bytes);
h_v_r = (float*)malloc(bytes);
cudaMalloc(&d_v, bytes);
cudaMalloc(&d_v_r, bytes);
// Initialize vector
initialize_vector(h_v, n);
// Copy to device
cudaMemcpy(d_v, h_v, bytes, cudaMemcpyHostToDevice);
// TB Size
int TB_SIZE = 256;
// Grid Size (No padding)
// int GRID_SIZE = 32;
// dim3 threads(256);
dim3 blocks(8, 512);
// Call kernel
cudaEvent_t start, stop;
CHECK_CUDA(cudaEventCreate(&start));
CHECK_CUDA(cudaEventCreate(&stop));
CHECK_CUDA(cudaEventRecord(start));
max_reduction_posenet<<<32, 16>>> (d_v, d_v_r);
CHECK_CUDA(cudaEventRecord(stop));
CHECK_CUDA(cudaEventSynchronize(stop));
float current_time = 0;
CHECK_CUDA(cudaEventElapsedTime(¤t_time, start, stop));
std::cout << "Elapsed Time: " << current_time << "ms\n";
// sum_reduction<<<1, TB_SIZE>>> (d_v_r, d_v_r);
// Copy to host;
cudaMemcpy(h_v_r, d_v_r, bytes, cudaMemcpyDeviceToHost);
// Print the result
printf("Accumulated result is %f \n", h_v_r[0]);
// scanf("Press enter to continue: ");
// assert(h_v_r[0] == 65536);
printf("COMPLETED SUCCESSFULLY\n");
return 0;
}