-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared_time.h
More file actions
279 lines (236 loc) · 5.88 KB
/
shared_time.h
File metadata and controls
279 lines (236 loc) · 5.88 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
#ifndef CUZFP_SHARED_H
#define CUZFP_SHARED_H
#define CUDA_ZFP_RATE_PRINT 1
typedef unsigned long long Word;
#define Wsize ((uint)(CHAR_BIT * sizeof(Word)))
#include "type_info.cuh"
#include "zfp.h"
#include "constants.h"
#include <stdio.h>
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define bitsize(x) ((uint)(CHAR_BIT * sizeof(x)))
#define LDEXP(x, e) ldexp(x, e)
#define NBMASK 0xaaaaaaaaaaaaaaaaull
namespace cuZFP
{
template<typename T>
__device__ void print_bits(const T &bits)
{
const int bit_size = sizeof(T) * 8;
for(int i = bit_size - 1; i >= 0; --i)
{
T one = 1;
T mask = one << i;
T val = (bits & mask) >> i ;
printf("%d", (int) val);
}
printf("\n");
}
size_t calc_device_mem1d(const int dim,
const int maxbits)
{
const size_t vals_per_block = 4;
size_t total_blocks = dim / vals_per_block;
if(dim % vals_per_block != 0)
{
total_blocks++;
}
const size_t bits_per_block = maxbits;
const size_t bits_per_word = sizeof(Word) * 8;
const size_t total_bits = bits_per_block * total_blocks;
size_t alloc_size = total_bits / bits_per_word;
if(total_bits % bits_per_word != 0) alloc_size++;
// ensure we have zeros
return alloc_size * sizeof(Word);
}
size_t calc_device_mem2d(const uint2 dims,
const int maxbits)
{
const size_t vals_per_block = 16;
size_t total_blocks = (dims.x * dims.y) / vals_per_block;
if((dims.x * dims.y) % vals_per_block != 0) total_blocks++;
const size_t bits_per_block = maxbits;
const size_t bits_per_word = sizeof(Word) * 8;
const size_t total_bits = bits_per_block * total_blocks;
size_t alloc_size = total_bits / bits_per_word;
if(total_bits % bits_per_word != 0) alloc_size++;
return alloc_size * sizeof(Word);
}
size_t calc_device_mem3d(const uint3 encoded_dims,
const int bits_per_block)
{
const size_t vals_per_block = 64;
const size_t size = encoded_dims.x * encoded_dims.y * encoded_dims.z;
size_t total_blocks = size / vals_per_block;
const size_t bits_per_word = sizeof(Word) * 8;
const size_t total_bits = bits_per_block * total_blocks;
const size_t alloc_size = total_bits / bits_per_word;
return alloc_size * sizeof(Word);
}
dim3 get_max_grid_dims()
{
static cudaDeviceProp prop;
static bool firstTime = true;
if( firstTime )
{
firstTime = false;
int device = 0;
cudaGetDeviceProperties(&prop, device);
}
dim3 grid_dims;
grid_dims.x = prop.maxGridSize[0];
grid_dims.y = prop.maxGridSize[1];
grid_dims.z = prop.maxGridSize[2];
return grid_dims;
}
// size is assumed to have a pad to the nearest cuda block size
dim3 calculate_grid_size(size_t size, size_t cuda_block_size)
{
size_t grids = size / cuda_block_size; // because of pad this will be exact
dim3 max_grid_dims = get_max_grid_dims();
int dims = 1;
// check to see if we need to add more grids
if( grids > max_grid_dims.x)
{
dims = 2;
}
if(grids > max_grid_dims.x * max_grid_dims.y)
{
dims = 3;
}
dim3 grid_size;
grid_size.x = 1;
grid_size.y = 1;
grid_size.z = 1;
if(dims == 1)
{
grid_size.x = grids;
}
if(dims == 2)
{
float sq_r = sqrt((float)grids);
float intpart = 0;
modf(sq_r,&intpart);
uint base = intpart;
grid_size.x = base;
grid_size.y = base;
// figure out how many y to add
uint rem = (size - base * base);
uint y_rows = rem / base;
if(rem % base != 0) y_rows ++;
grid_size.y += y_rows;
}
if(dims == 3)
{
float cub_r = pow((float)grids, 1.f/3.f);;
float intpart = 0;
modf(cub_r,&intpart);
int base = intpart;
grid_size.x = base;
grid_size.y = base;
grid_size.z = base;
// figure out how many z to add
uint rem = (size - base * base * base);
uint z_rows = rem / (base * base);
if(rem % (base * base) != 0) z_rows ++;
grid_size.z += z_rows;
}
return grid_size;
}
// map two's complement signed integer to negabinary unsigned integer
inline __device__
unsigned long long int int2uint(const long long int x)
{
return (x + (unsigned long long int)0xaaaaaaaaaaaaaaaaull) ^
(unsigned long long int)0xaaaaaaaaaaaaaaaaull;
}
inline __device__
unsigned int int2uint(const int x)
{
return (x + (unsigned int)0xaaaaaaaau) ^
(unsigned int)0xaaaaaaaau;
}
template<typename Int, typename Scalar>
__device__
Scalar
dequantize(const Int &x, const int &e);
template<>
__device__
double
dequantize<long long int, double>(const long long int &x, const int &e)
{
return LDEXP((double)x, e - ((int)(CHAR_BIT * scalar_sizeof<double>()) - 2));
}
template<>
__device__
float
dequantize<int, float>(const int &x, const int &e)
{
return LDEXP((float)x, e - ((int)(CHAR_BIT * scalar_sizeof<float>()) - 2));
}
template<>
__device__
int
dequantize<int, int>(const int &x, const int &e)
{
return 1;
}
template<>
__device__
long long int
dequantize<long long int, long long int>(const long long int &x, const int &e)
{
return 1;
}
/* inverse lifting transform of 4-vector */
template<class Int, uint s>
__device__
static void
inv_lift(Int* p)
{
Int x, y, z, w;
x = *p; p += s;
y = *p; p += s;
z = *p; p += s;
w = *p; p += s;
/*
** non-orthogonal transform
** ( 4 6 -4 -1) (x)
** 1/4 * ( 4 2 4 5) (y)
** ( 4 -2 4 -5) (z)
** ( 4 -6 -4 1) (w)
*/
y += w >> 1; w -= y >> 1;
y += w; w <<= 1; w -= y;
z += x; x <<= 1; x -= z;
y += z; z <<= 1; z -= y;
w += x; x <<= 1; x -= w;
p -= s; *p = w;
p -= s; *p = z;
p -= s; *p = y;
p -= s; *p = x;
}
template<int BlockSize>
__device__ inline
const unsigned char* get_perm();
template<>
__device__ inline
const unsigned char* get_perm<64>()
{
return perm_3d;
}
template<>
__device__ inline
const unsigned char* get_perm<16>()
{
return perm_2;
}
template<>
__device__ inline
const unsigned char* get_perm<4>()
{
return perm_1;
}
} // namespace cuZFP
#endif