-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_topk.cu
More file actions
303 lines (253 loc) · 10.8 KB
/
Copy pathtest_topk.cu
File metadata and controls
303 lines (253 loc) · 10.8 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <float.h>
#include <iostream>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <cmath>
#include <cuda_runtime.h>
__device__ inline void replace_smaller(float* array, int* idx_buffer, int k, float data, int data_idx) {
if(data < array[k-1]) return;
for(int j=k-2; j>=0; j--) {
if(data > array[j]) {
array[j+1] = array[j];
idx_buffer[j+1] = idx_buffer[j];
} else {
array[j+1] = data;
idx_buffer[j+1] = data_idx;
return;
}
}
array[0] = data;
idx_buffer[0] = data_idx;
}
__device__ inline void mergeTwoK(float* left, int* left_idx, float* right, int* right_idx, int k) {
int i;
for(i=0; i<k; i++) {
replace_smaller(left, left_idx, k, right[i], right_idx[i]);
}
}
__device__ inline void replace_smaller_warp(volatile float* array, volatile int* idx_buffer, int k, float data, int data_idx) {
if(data < array[k-1]) return;
for(int j=k-2; j>=0; j--) {
if(data > array[j]) {
array[j+1] = array[j];
idx_buffer[j+1] = idx_buffer[j];
} else {
array[j+1] = data;
idx_buffer[j+1] = data_idx;
return;
}
}
array[0] = data;
idx_buffer[0] = data_idx;
}
__device__ inline void mergeTwoK_warp(volatile float* left, volatile int* left_idx, volatile float* right, volatile int* right_idx, int k) {
int i;
for(i=0; i<k; i++) {
replace_smaller_warp(left, left_idx, k, right[i], right_idx[i]);
}
}
template<unsigned int numThreads>
__global__ void top_k_gpu_opt(float* input, int length, int k, float* output, int* out_idx) {
extern __shared__ float shared_buffer[];
int gtid = threadIdx.x + blockIdx.x * numThreads;
int tid = threadIdx.x;
float *buffer = shared_buffer + tid * k;
int *buffer_idx = reinterpret_cast<int*>(shared_buffer + numThreads * k) + tid * k;
int threadNum = gridDim.x * numThreads;
for (int i = 0;i < k; ++i) {
buffer[i] = -FLT_MAX;
buffer_idx[i] = -1;
}
for (int i = gtid; i < length; i += threadNum) {
replace_smaller(buffer, buffer_idx, k, input[i], i);
}
__syncthreads();
if (numThreads >= 1024) {
if (tid < 512) {
float* next_buffer = shared_buffer + (tid + 512) * k;
int* next_buffer_idx = reinterpret_cast<int*>(shared_buffer + numThreads * k) + (tid + 512) * k;
mergeTwoK(buffer, buffer_idx, next_buffer, next_buffer_idx, k);
}
__syncthreads();
}
if (numThreads >= 512) {
if (tid < 256) {
float* next_buffer = shared_buffer + (tid + 256) * k;
int* next_buffer_idx = reinterpret_cast<int*>(shared_buffer + numThreads * k) + (tid + 256) * k;
mergeTwoK(buffer, buffer_idx, next_buffer, next_buffer_idx, k);
}
__syncthreads();
}
if (numThreads >= 256) {
if (tid < 128) {
float* next_buffer = shared_buffer + (tid + 128) * k;
int* next_buffer_idx = reinterpret_cast<int*>(shared_buffer + numThreads * k) + (tid + 128) * k;
mergeTwoK(buffer, buffer_idx, next_buffer, next_buffer_idx, k);
}
__syncthreads();
}
if (numThreads >= 128) {
if (tid < 64) {
float* next_buffer = shared_buffer + (tid + 64) * k;
int* next_buffer_idx = reinterpret_cast<int*>(shared_buffer + numThreads * k) + (tid + 64) * k;
mergeTwoK(buffer, buffer_idx, next_buffer, next_buffer_idx, k);
}
__syncthreads();
}
if (tid < 32) {
if (numThreads >= 64) {
volatile float* next_buffer = shared_buffer + (tid + 32) * k;
volatile int* next_buffer_idx = reinterpret_cast<int*>(shared_buffer + numThreads * k) + (tid + 32) * k;
mergeTwoK_warp(buffer, buffer_idx, next_buffer, next_buffer_idx, k);
}
if (numThreads >= 32) {
volatile float* next_buffer = shared_buffer + (tid + 16) * k;
volatile int* next_buffer_idx = reinterpret_cast<int*>(shared_buffer + numThreads * k) + (tid + 16) * k;
mergeTwoK_warp(buffer, buffer_idx, next_buffer, next_buffer_idx, k);
}
if (numThreads >= 16) {
volatile float* next_buffer = shared_buffer + (tid + 8) * k;
volatile int* next_buffer_idx = reinterpret_cast<int*>(shared_buffer + numThreads * k) + (tid + 8) * k;
mergeTwoK_warp(buffer, buffer_idx, next_buffer, next_buffer_idx, k);
}
if (numThreads >= 8) {
volatile float* next_buffer = shared_buffer + (tid + 4) * k;
volatile int* next_buffer_idx = reinterpret_cast<int*>(shared_buffer + numThreads * k) + (tid + 4) * k;
mergeTwoK_warp(buffer, buffer_idx, next_buffer, next_buffer_idx, k);
}
if (numThreads >= 4) {
volatile float* next_buffer = shared_buffer + (tid + 2) * k;
volatile int* next_buffer_idx = reinterpret_cast<int*>(shared_buffer + numThreads * k) + (tid + 2) * k;
mergeTwoK_warp(buffer, buffer_idx, next_buffer, next_buffer_idx, k);
}
if (numThreads >= 2) {
volatile float* next_buffer = shared_buffer + (tid + 1) * k;
volatile int* next_buffer_idx = reinterpret_cast<int*>(shared_buffer + numThreads * k) + (tid + 1) * k;
mergeTwoK_warp(buffer, buffer_idx, next_buffer, next_buffer_idx, k);
}
if (tid == 0) {
for (int i = 0; i < k; ++i) {
output[blockIdx.x * k + i] = buffer[i];
out_idx[blockIdx.x * k + i] = buffer_idx[i];
}
}
}
}
__global__ void top_k_gpu(float* input, int length, int k, float* output, int* out_idx) {
extern __shared__ float shared_buffer[];
int gtid = threadIdx.x + blockIdx.x * blockDim.x;
int tid = threadIdx.x;
float *buffer = shared_buffer + tid * k;
int *buffer_idx = reinterpret_cast<int*>(shared_buffer + blockDim.x * k) + tid * k;
int threadNum = gridDim.x * blockDim.x;
for (int i = 0;i < k; ++i) {
buffer[i] = -FLT_MAX;
buffer_idx[i] = -1;
}
for (int i = gtid; i < length; i += threadNum) {
replace_smaller(buffer, buffer_idx, k, input[i], i);
}
__syncthreads();
for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) {
if (tid < stride) {
float* next_buffer = shared_buffer + (tid + stride) * k;
int* next_buffer_idx = reinterpret_cast<int*>(shared_buffer + blockDim.x * k) + (tid + stride) * k;
mergeTwoK(buffer, buffer_idx, next_buffer, next_buffer_idx, k);
}
__syncthreads();
}
if (tid == 0) {
for (int i = 0; i < k; ++i) {
output[blockIdx.x * k + i] = buffer[i];
out_idx[blockIdx.x * k + i] = buffer_idx[i];
}
}
}
__global__ void top_k_gpu_final(float* input, int* in_idx, int length, int k, float* output, int* out_idx) {
extern __shared__ float shared_buffer[];
int tid = threadIdx.x;
int buckets = length / k;
int *buffer_idx = reinterpret_cast<int*>(shared_buffer + length * sizeof(float));
for (int i = tid; i < length; i += blockDim.x) {
shared_buffer[i] = input[i];
buffer_idx[i] = in_idx[i];
}
__syncthreads();
for (int stride = buckets >> 1; stride > 0; stride >>= 1) {
if (tid < stride) {
float *bucketPonter = shared_buffer + tid * k;
float *bucketPonterNext = shared_buffer + (tid + stride) * k;
int *bucketPonter_idx = buffer_idx + tid * k;
int *bucketPonter_idxNext = buffer_idx + (tid + stride) * k;
mergeTwoK(bucketPonter, bucketPonter_idx, bucketPonterNext, bucketPonter_idxNext, k);
}
__syncthreads();
}
if (tid == 0) {
if ((buckets & 1) == 1 && buckets > 1) {
float *bucketPonter = shared_buffer + tid * k;
float *bucketPonterNext = shared_buffer + (tid + buckets - 1) * k;
int *bucketPonter_idx = buffer_idx + tid * k;
int *bucketPonter_idxNext = buffer_idx + (tid + buckets - 1) * k;
mergeTwoK(bucketPonter, bucketPonter_idx, bucketPonterNext, bucketPonter_idxNext, k);
__syncthreads();
}
for (int i = 0; i < k; ++i) {
output[i] = shared_buffer[i];
out_idx[i] = buffer_idx[i];
}
}
}
int main() {
int n = 100;
thrust::host_vector<float> h_vec(n);
for (int i = 0; i < n; ++i) {
h_vec[i] = static_cast<float>(rand() % 100); // Random values between 0 and 99
}
std::cout << "DATA:" << std::endl;
for (int i=0;i<n;++i) {
std::cout << h_vec[i] << ", ";
}
std::cout << std::endl;
thrust::device_vector<float> d_vec = h_vec;
int k = 4;
const int threadsPerBlock = 128;
int blocksPerGrid = (n + threadsPerBlock - 1) / threadsPerBlock;
int sharedMemSize = threadsPerBlock * k * sizeof(int) + threadsPerBlock * k * sizeof(float);
thrust::device_vector<float> d_res_first(k * blocksPerGrid);
thrust::device_vector<int> d_idx_first(k * blocksPerGrid);
thrust::device_vector<float> d_res_final(k);
thrust::device_vector<int> d_idx_final(k);
printf("N: %d, threads: %d, blocks: %d\n", n, threadsPerBlock, blocksPerGrid);
top_k_gpu<<<blocksPerGrid, threadsPerBlock, sharedMemSize>>>(d_vec.data().get(), n, k, d_res_first.data().get(), d_idx_first.data().get());
top_k_gpu_opt<128><<<blocksPerGrid, threadsPerBlock, sharedMemSize>>>(d_vec.data().get(), n, k, d_res_first.data().get(), d_idx_first.data().get());
top_k_gpu_final<<<1, threadsPerBlock, sharedMemSize>>>(d_res_first.data().get(), d_idx_first.data().get(), blocksPerGrid*k, k, d_res_final.data().get(), d_idx_final.data().get());
thrust::host_vector<float> h_res_first = d_res_first;
thrust::host_vector<int> h_idx_first = d_idx_first;
std::cout << "========FIRST" << std::endl;
for (int i=0;i<blocksPerGrid;++i) {
for (int j=0;j<k;++j) {
int idx = i*k + j;
std::cout<<"[" << h_idx_first[idx] << "]" << h_res_first[idx]<<", ";
}
std::cout<<std::endl;
}
thrust::host_vector<float> h_res_final = d_res_final;
thrust::host_vector<int> h_idx_final = d_idx_final;
std::cout << "========FINAL" << std::endl;
for (int i=0;i<k;++i) {
std::cout << "[" << h_idx_final[i] << "]" << h_res_final[i] << ", ";
}
std::cout << std::endl;
thrust::sort(d_vec.begin(), d_vec.end(), thrust::greater<float>());
thrust::host_vector<float> h_top_k(k);
thrust::copy(d_vec.begin(), d_vec.begin() + k, h_top_k.begin());
// Print the Top-K values
std::cout << "========GT" << std::endl;
for (auto val : h_top_k) {
std::cout << val << ", ";
}
std::cout << std::endl;
}