-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathst.cu
437 lines (337 loc) · 18.4 KB
/
st.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*-----------------------------------------------------------*/
/* Block Sorting, Lossless Data Compression Library. */
/* Sort Transform (GPU version) */
/*-----------------------------------------------------------*/
/*--
This file is a part of bsc and/or libbsc, a program and a library for
lossless, block-sorting data compression.
Copyright (c) 2009-2012 Ilya Grebnov <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Please see the file LICENSE for full copyright information and file AUTHORS
for full list of contributors.
See also the bsc and libbsc web site:
http://libbsc.com/ for more information.
--*/
/*--
Sort Transform is patented by Michael Schindler under US patent 6,199,064.
However for research purposes this algorithm is included in this software.
So if you are of the type who should worry about this (making money) worry away.
The author shall have no liability with respect to the infringement of
copyrights, trade secrets or any patents by this software. In no event will
the author be liable for any lost revenue or profits or other special,
indirect and consequential damages.
Sort Transform is disabled by default and can be enabled by defining the
preprocessor macro LIBBSC_SORT_TRANSFORM_SUPPORT at compile time.
--*/
#if defined(LIBBSC_SORT_TRANSFORM_SUPPORT) && defined(LIBBSC_CUDA_SUPPORT)
#if defined(_MSC_VER)
#pragma warning(disable : 4267)
#endif
#include <stdlib.h>
#include <memory.h>
#include <cuda_runtime_api.h>
#include <device_functions.h>
#include <cub/cub.cuh>
#include "st.cuh"
#ifdef LIBBSC_OPENMP
omp_lock_t cuda_lock;
int bsc_st_cuda_init(int features)
{
omp_init_lock(&cuda_lock);
return LIBBSC_NO_ERROR;
}
#else
int bsc_st_cuda_init(int features)
{
return LIBBSC_NO_ERROR;
}
#endif
#ifndef __CUDA_ARCH__
#define CUDA_DEVICE_ARCH 0
#else
#define CUDA_DEVICE_ARCH __CUDA_ARCH__
#endif
#define CUDA_DEVICE_PADDING 1024
#define CUDA_NUM_THREADS_IN_BLOCK 192
#define CUDA_CTA_OCCUPANCY_SM30 10
#define CUDA_CTA_OCCUPANCY_SM20 8
#define CUDA_CTA_OCCUPANCY_SM12 5
#define CUDA_CTA_OCCUPANCY_SM10 4
#define CUDA_CTA_OCCUPANCY(v) (v >= 300 ? CUDA_CTA_OCCUPANCY_SM30 : v >= 200 ? CUDA_CTA_OCCUPANCY_SM20 : v >= 120 ? CUDA_CTA_OCCUPANCY_SM12 : CUDA_CTA_OCCUPANCY_SM10)
cudaError_t bsc_cuda_safe_call(const char * filename, int line, cudaError_t result, cudaError_t status = cudaSuccess)
{
if (result != cudaSuccess)
{
fprintf(stderr, "\n%s(%d): bsc_cuda_safe_call failed %d: '%s'.", filename, line, result, cudaGetErrorString(result));
fflush(stderr);
}
return result != cudaSuccess ? result : status;
}
__global__ __launch_bounds__(CUDA_NUM_THREADS_IN_BLOCK, CUDA_CTA_OCCUPANCY(CUDA_DEVICE_ARCH))
void bsc_st567_encode_cuda_presort(unsigned char * RESTRICT T_device, unsigned long long * RESTRICT K_device, int n)
{
__shared__ unsigned int staging[1 + CUDA_NUM_THREADS_IN_BLOCK + 7];
unsigned int * RESTRICT thread_staging = &staging[threadIdx.x];
for (int grid_size = gridDim.x * CUDA_NUM_THREADS_IN_BLOCK, block_start = blockIdx.x * CUDA_NUM_THREADS_IN_BLOCK; block_start < n; block_start += grid_size)
{
int index = block_start + threadIdx.x;
{
thread_staging[1 ] = T_device[index ];
if (threadIdx.x < 7 ) thread_staging[1 + CUDA_NUM_THREADS_IN_BLOCK] = T_device[index + CUDA_NUM_THREADS_IN_BLOCK]; else
if (threadIdx.x == 7) thread_staging[-7 ] = T_device[index - 8 ];
__syncthreads();
}
{
#if CUDA_DEVICE_ARCH >= 200
unsigned int lo = __byte_perm(thread_staging[4], thread_staging[5], 0x0411) | __byte_perm(thread_staging[6], thread_staging[7], 0x1104);
unsigned int hi = __byte_perm(thread_staging[0], thread_staging[1], 0x0411) | __byte_perm(thread_staging[2], thread_staging[3], 0x1104);
#else
unsigned int lo = (thread_staging[4] << 24) | (thread_staging[5] << 16) | (thread_staging[6] << 8) | thread_staging[7];
unsigned int hi = (thread_staging[0] << 24) | (thread_staging[1] << 16) | (thread_staging[2] << 8) | thread_staging[3];
#endif
K_device[index] = (((unsigned long long)hi) << 32) | ((unsigned long long)lo);
__syncthreads();
}
}
}
__global__ __launch_bounds__(CUDA_NUM_THREADS_IN_BLOCK, CUDA_CTA_OCCUPANCY(CUDA_DEVICE_ARCH))
void bsc_st8_encode_cuda_presort(unsigned char * RESTRICT T_device, unsigned long long * RESTRICT K_device, unsigned char * RESTRICT V_device, int n)
{
__shared__ unsigned int staging[1 + CUDA_NUM_THREADS_IN_BLOCK + 8];
unsigned int * RESTRICT thread_staging = &staging[threadIdx.x];
for (int grid_size = gridDim.x * CUDA_NUM_THREADS_IN_BLOCK, block_start = blockIdx.x * CUDA_NUM_THREADS_IN_BLOCK; block_start < n; block_start += grid_size)
{
int index = block_start + threadIdx.x;
{
thread_staging[1 ] = T_device[index ];
if (threadIdx.x < 8 ) thread_staging[1 + CUDA_NUM_THREADS_IN_BLOCK] = T_device[index + CUDA_NUM_THREADS_IN_BLOCK]; else
if (threadIdx.x == 8) thread_staging[-8 ] = T_device[index - 9 ];
__syncthreads();
}
{
#if CUDA_DEVICE_ARCH >= 200
unsigned int lo = __byte_perm(thread_staging[5], thread_staging[6], 0x0411) | __byte_perm(thread_staging[7], thread_staging[8], 0x1104);
unsigned int hi = __byte_perm(thread_staging[1], thread_staging[2], 0x0411) | __byte_perm(thread_staging[3], thread_staging[4], 0x1104);
#else
unsigned int lo = (thread_staging[5] << 24) | (thread_staging[6] << 16) | (thread_staging[7] << 8) | thread_staging[8];
unsigned int hi = (thread_staging[1] << 24) | (thread_staging[2] << 16) | (thread_staging[3] << 8) | thread_staging[4];
#endif
K_device[index] = (((unsigned long long)hi) << 32) | ((unsigned long long)lo); V_device[index] = thread_staging[0];
__syncthreads();
}
}
}
__global__ __launch_bounds__(CUDA_NUM_THREADS_IN_BLOCK, CUDA_CTA_OCCUPANCY(CUDA_DEVICE_ARCH))
void bsc_st567_encode_cuda_postsort(unsigned char * RESTRICT T_device, unsigned long long * RESTRICT K_device, int n, unsigned long long lookup, int * RESTRICT I_device)
{
int min_index = n;
for (int grid_size = gridDim.x * CUDA_NUM_THREADS_IN_BLOCK, block_start = blockIdx.x * CUDA_NUM_THREADS_IN_BLOCK; block_start < n; block_start += grid_size)
{
int index = block_start + threadIdx.x;
{
unsigned long long value = K_device[index];
{
if (value == lookup && index < min_index) min_index = index;
T_device[index] = (unsigned char)(value >> 56);
}
}
}
__syncthreads(); if (min_index != n) atomicMin(I_device, min_index);
}
__global__ __launch_bounds__(CUDA_NUM_THREADS_IN_BLOCK, CUDA_CTA_OCCUPANCY(CUDA_DEVICE_ARCH))
void bsc_st8_encode_cuda_postsort(unsigned long long * RESTRICT K_device, int n, unsigned long long lookup, int * RESTRICT I_device)
{
int min_index = n;
for (int grid_size = gridDim.x * CUDA_NUM_THREADS_IN_BLOCK, block_start = blockIdx.x * CUDA_NUM_THREADS_IN_BLOCK; block_start < n; block_start += grid_size)
{
int index = block_start + threadIdx.x;
{
if (K_device[index] == lookup && index < min_index) min_index = index;
}
}
__syncthreads(); if (min_index != n) atomicMin(I_device, min_index);
}
int bsc_st567_encode_cuda(unsigned char * T, unsigned char * T_device, int n, int num_blocks, int k)
{
int index = LIBBSC_GPU_NOT_ENOUGH_MEMORY;
{
unsigned long long * K_device = NULL;
unsigned long long * K_device_sorted = NULL;
if (bsc_cuda_safe_call(__FILE__, __LINE__, cudaMalloc((void **)&K_device, 2 * (n + 2 * CUDA_DEVICE_PADDING) * sizeof(unsigned long long))) == cudaSuccess)
{
index = LIBBSC_GPU_ERROR;
bsc_st567_encode_cuda_presort<<<num_blocks, CUDA_NUM_THREADS_IN_BLOCK>>>(T_device, K_device, n);
cudaError_t status = cudaSuccess;
{
// Bit subrange [begin_bit, end_bit) of differentiating key bits
int begin_bit = 56-8*k, end_bit = 56;
// Create a DoubleBuffer to wrap the pair of device pointers
cub::DoubleBuffer<unsigned long long> d_keys ( K_device
, K_device + ((n + 2 * CUDA_DEVICE_PADDING) / CUDA_DEVICE_PADDING) * CUDA_DEVICE_PADDING);
// Determine temporary device storage requirements
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
cub::DeviceRadixSort::SortKeys(d_temp_storage, temp_storage_bytes, d_keys, n, begin_bit, end_bit);
// Allocate temporary storage
cudaMalloc(&d_temp_storage, temp_storage_bytes);
// Run sorting operation
status = cub::DeviceRadixSort::SortKeys(d_temp_storage, temp_storage_bytes, d_keys, n, begin_bit, end_bit);
// Release temporary storage
cudaFree(d_temp_storage);
// Extract the result
K_device_sorted = d_keys.Current();
}
if (bsc_cuda_safe_call(__FILE__, __LINE__, status) == cudaSuccess)
{
unsigned long long lookup;
{
unsigned int lo = (T[3 ] << 24) | (T[4] << 16) | (T[5] << 8) | T[6];
unsigned int hi = (T[n - 1] << 24) | (T[0] << 16) | (T[1] << 8) | T[2];
lookup = (((unsigned long long)hi) << 32) | ((unsigned long long)lo);
status = bsc_cuda_safe_call(__FILE__, __LINE__, cudaMemcpy(T_device - sizeof(int), &n, sizeof(int), cudaMemcpyHostToDevice), status);
}
if (bsc_cuda_safe_call(__FILE__, __LINE__, status) == cudaSuccess)
{
bsc_st567_encode_cuda_postsort<<<num_blocks, CUDA_NUM_THREADS_IN_BLOCK>>>(T_device, K_device_sorted, n, lookup, (int *)(T_device - sizeof(int)));
status = bsc_cuda_safe_call(__FILE__, __LINE__, cudaFree(K_device), status);
if (bsc_cuda_safe_call(__FILE__, __LINE__, status) == cudaSuccess)
{
status = bsc_cuda_safe_call(__FILE__, __LINE__, cudaMemcpy(T_device + n, T_device - sizeof(int), sizeof(int), cudaMemcpyDeviceToDevice), status);
status = bsc_cuda_safe_call(__FILE__, __LINE__, cudaMemcpy(T, T_device, n + sizeof(int), cudaMemcpyDeviceToHost), status);
}
if (bsc_cuda_safe_call(__FILE__, __LINE__, status) == cudaSuccess)
{
index = *(int *)(T + n);
}
return index;
}
}
cudaFree(K_device);
}
}
return index;
}
int bsc_st8_encode_cuda(unsigned char * T, unsigned char * T_device, int n, int num_blocks)
{
int index = LIBBSC_GPU_NOT_ENOUGH_MEMORY;
{
unsigned char * V_device = NULL;
unsigned char * V_device_sorted = NULL;
if (bsc_cuda_safe_call(__FILE__, __LINE__, cudaMalloc((void **)&V_device, 2 * (n + 2 * CUDA_DEVICE_PADDING) * sizeof(unsigned char))) == cudaSuccess)
{
unsigned long long * K_device = NULL;
unsigned long long * K_device_sorted = NULL;
if (bsc_cuda_safe_call(__FILE__, __LINE__, cudaMalloc((void **)&K_device, 2 * (n + 2 * CUDA_DEVICE_PADDING) * sizeof(unsigned long long))) == cudaSuccess)
{
index = LIBBSC_GPU_ERROR;
bsc_st8_encode_cuda_presort<<<num_blocks, CUDA_NUM_THREADS_IN_BLOCK>>>(T_device, K_device, V_device, n);
cudaError_t status = cudaSuccess;
{
// Create a set of DoubleBuffers to wrap pairs of device pointers
cub::DoubleBuffer<unsigned long long> d_keys ( K_device
, K_device + ((n + 2 * CUDA_DEVICE_PADDING) / CUDA_DEVICE_PADDING) * CUDA_DEVICE_PADDING);
cub::DoubleBuffer<unsigned char> d_values ( V_device
, V_device + ((n + 2 * CUDA_DEVICE_PADDING) / CUDA_DEVICE_PADDING) * CUDA_DEVICE_PADDING);
// Determine temporary device storage requirements
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
cub::DeviceRadixSort::SortPairs(d_temp_storage, temp_storage_bytes, d_keys, d_values, n);
// Allocate temporary storage
cudaMalloc(&d_temp_storage, temp_storage_bytes);
// Run sorting operation
status = cub::DeviceRadixSort::SortPairs(d_temp_storage, temp_storage_bytes, d_keys, d_values, n);
// Release temporary storage
cudaFree(d_temp_storage);
// Extract the result
K_device_sorted = d_keys.Current();
V_device_sorted = d_values.Current();
}
if (bsc_cuda_safe_call(__FILE__, __LINE__, status) == cudaSuccess)
{
unsigned long long lookup;
{
unsigned int lo = (T[4] << 24) | (T[5] << 16) | (T[6] << 8) | T[7];
unsigned int hi = (T[0] << 24) | (T[1] << 16) | (T[2] << 8) | T[3];
lookup = (((unsigned long long)hi) << 32) | ((unsigned long long)lo);
status = bsc_cuda_safe_call(__FILE__, __LINE__, cudaMemcpy(V_device_sorted + ((n + sizeof(int) - 1) / sizeof(int)) * sizeof(int), &n, sizeof(int), cudaMemcpyHostToDevice), status);
}
if (bsc_cuda_safe_call(__FILE__, __LINE__, status) == cudaSuccess)
{
bsc_st8_encode_cuda_postsort<<<num_blocks, CUDA_NUM_THREADS_IN_BLOCK>>>(K_device_sorted, n, lookup, (int *)(V_device_sorted + ((n + sizeof(int) - 1) / sizeof(int)) * sizeof(int)));
if (bsc_cuda_safe_call(__FILE__, __LINE__, status) == cudaSuccess)
{
status = bsc_cuda_safe_call(__FILE__, __LINE__, cudaMemcpy(T, V_device_sorted, n + 2 * sizeof(int), cudaMemcpyDeviceToHost), status);
}
status = bsc_cuda_safe_call(__FILE__, __LINE__, cudaFree(K_device), status);
status = bsc_cuda_safe_call(__FILE__, __LINE__, cudaFree(V_device), status);
if (bsc_cuda_safe_call(__FILE__, __LINE__, status) == cudaSuccess)
{
index = *(int *)(T + ((n + sizeof(int) - 1) / sizeof(int)) * sizeof(int));
}
return index;
}
}
cudaFree(K_device);
}
cudaFree(V_device);
}
}
return index;
}
int bsc_st_encode_cuda(unsigned char * T, int n, int k, int features)
{
if ((T == NULL) || (n < 0)) return LIBBSC_BAD_PARAMETER;
if ((k < 5) || (k > 8)) return LIBBSC_BAD_PARAMETER;
if (n <= 1) return 0;
int num_blocks = 1;
{
cudaDeviceProp deviceProperties;
{
int deviceId; if (cudaGetDevice(&deviceId) != cudaSuccess || cudaGetDeviceProperties(&deviceProperties, deviceId) != cudaSuccess)
{
return LIBBSC_GPU_NOT_SUPPORTED;
}
}
if (deviceProperties.major * 10 + deviceProperties.minor <= 10) return LIBBSC_GPU_NOT_SUPPORTED;
num_blocks = CUDA_CTA_OCCUPANCY(deviceProperties.major * 100 + deviceProperties.minor * 10) * deviceProperties.multiProcessorCount;
if (num_blocks > ((n + CUDA_NUM_THREADS_IN_BLOCK - 1) / CUDA_NUM_THREADS_IN_BLOCK)) num_blocks = (n + CUDA_NUM_THREADS_IN_BLOCK - 1) / CUDA_NUM_THREADS_IN_BLOCK;
if (num_blocks <= 0) num_blocks = 1;
}
#ifdef LIBBSC_OPENMP
omp_set_lock(&cuda_lock);
#endif
int index = LIBBSC_GPU_NOT_ENOUGH_MEMORY;
{
unsigned char * T_device = NULL;
if (cudaMalloc((void **)&T_device, n + 2 * CUDA_DEVICE_PADDING) == cudaSuccess)
{
index = LIBBSC_GPU_ERROR;
cudaError_t status = cudaSuccess;
status = bsc_cuda_safe_call(__FILE__, __LINE__, cudaMemcpy(T_device + CUDA_DEVICE_PADDING , T , n , cudaMemcpyHostToDevice ), status);
status = bsc_cuda_safe_call(__FILE__, __LINE__, cudaMemcpy(T_device + CUDA_DEVICE_PADDING + n, T_device + CUDA_DEVICE_PADDING, CUDA_DEVICE_PADDING, cudaMemcpyDeviceToDevice), status);
status = bsc_cuda_safe_call(__FILE__, __LINE__, cudaMemcpy(T_device , T_device + n , CUDA_DEVICE_PADDING, cudaMemcpyDeviceToDevice), status);
if (status == cudaSuccess)
{
if (k >= 5 && k <= 7) index = bsc_st567_encode_cuda(T, T_device + CUDA_DEVICE_PADDING, n, num_blocks, k);
if (k == 8) index = bsc_st8_encode_cuda (T, T_device + CUDA_DEVICE_PADDING, n, num_blocks );
}
cudaFree(T_device);
}
}
#ifdef LIBBSC_OPENMP
omp_unset_lock(&cuda_lock);
#endif
return index;
}
#endif
/*-----------------------------------------------------------*/
/* End st.cu */
/*-----------------------------------------------------------*/