-
Notifications
You must be signed in to change notification settings - Fork 6
/
binary_search.cpp
332 lines (290 loc) · 9.6 KB
/
binary_search.cpp
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
#include "cuda_wrapper.h"
#include "binary_search.h"
inline bool CheckCUresult(CUresult res, const char* name_call)
{
if (res != CUDA_SUCCESS)
{
printf("%s failed with Error code: %u\n", name_call, res);
const char *name = nullptr;
const char *desc = nullptr;
cuGetErrorName(res, &name);
cuGetErrorString(res, &desc);
if (name != nullptr)
{
printf("Error Name: %s\n", name);
}
if (desc != nullptr)
{
printf("Error Description: %s\n", desc);
}
return false;
}
return true;
}
bool TRTC_Lower_Bound(const DVVectorLike& vec, const DeviceViewable& value, const Functor& comp, size_t& result)
{
if (vec.size()<=0)
{
result = 0;
return true;
}
static TRTC_Kernel s_kernel(
{ "num_grps", "vec", "begin", "value", "comp", "range_out", "size_grp", "div_id" },
" size_t id = threadIdx.x+blockIdx.x*blockDim.x;\n"
" if (id>=num_grps) return;"
" size_t begin_grp = size_grp*id + begin;\n"
" size_t end_grp = begin_grp + size_grp;\n"
" if (id>=div_id)\n"
" {\n"
" begin_grp += id - div_id;\n"
" end_grp = begin_grp + size_grp + 1;\n"
" }\n"
" if ( (id == 0 || comp(vec[begin_grp-1],value)) && !comp(vec[end_grp-1], value) )\n"
" {\n"
" range_out[0] = begin_grp;\n"
" range_out[1] = end_grp;\n"
" }\n"
);
size_t h_range_out[2];
DVVector dv_range_out("size_t", 2);
int numBlocks;
{
DVSizeT _dv_num_grps(vec.size());
DVSizeT _dv_begin(0);
DVSizeT _dv_size_grp(1);
DVSizeT _dv_div_id((size_t)(-1));
const DeviceViewable* _args[] = { &_dv_num_grps, &vec, &_dv_begin, &value, &comp, &dv_range_out, &_dv_size_grp, &_dv_div_id };
s_kernel.calc_number_blocks(_args, 128, numBlocks);
}
size_t s_begin = 0;
size_t s_end = vec.size();
do
{
size_t n = s_end - s_begin;
size_t size_grp = 1;
size_t div_id = (size_t)(-1);
size_t num_grps = 128 * numBlocks;
if (num_grps < n)
{
size_grp = n / num_grps;
div_id = (size_grp + 1) * num_grps - n;
}
else
{
num_grps = n;
numBlocks = (int)((num_grps + 127) / 128);
}
DVSizeT dv_num_grps(num_grps);
DVSizeT dv_begin(s_begin);
DVSizeT dv_size_grp(size_grp);
DVSizeT dv_div_id(div_id);
h_range_out[0] = vec.size();
h_range_out[1] = 0;
if (!CheckCUresult(cuMemcpyHtoD((CUdeviceptr)dv_range_out.data(), h_range_out, sizeof(size_t) * 2), "cuMemcpyHtoD()")) return false;
const DeviceViewable* args[] = { &dv_num_grps, &vec, &dv_begin, &value, &comp, &dv_range_out, &dv_size_grp, &dv_div_id };
if (!s_kernel.launch({ (unsigned)numBlocks, 1,1 }, { 128, 1, 1 }, args)) return false;
dv_range_out.to_host(h_range_out);
s_begin = h_range_out[0];
s_end = h_range_out[1];
}
while (s_end > 0 && s_end > s_begin + 1);
result = s_begin;
return true;
}
bool TRTC_Lower_Bound(const DVVectorLike& vec, const DeviceViewable& value, size_t& result)
{
return TRTC_Lower_Bound(vec, value, Functor("Less"), result);
}
bool TRTC_Upper_Bound(const DVVectorLike& vec, const DeviceViewable& value, const Functor& comp, size_t& result)
{
if (vec.size() <= 0)
{
result = 0;
return true;
}
static TRTC_Kernel s_kernel(
{ "num_grps", "vec", "begin", "value", "comp", "range_out", "size_grp", "div_id" },
" size_t id = threadIdx.x+blockIdx.x*blockDim.x;\n"
" if (id>=num_grps) return;"
" size_t begin_grp = size_grp*id + begin;\n"
" size_t end_grp = begin_grp + size_grp;\n"
" if (id>=div_id)\n"
" {\n"
" begin_grp += id - div_id;\n"
" end_grp = begin_grp + size_grp + 1;\n"
" }\n"
" if ( !comp(value, vec[begin_grp]) && (id == num_grps - 1 || comp(value, vec[end_grp]) ) )\n"
" {\n"
" range_out[0] = begin_grp;\n"
" range_out[1] = end_grp;\n"
" }\n"
);
size_t h_range_out[2];
DVVector dv_range_out("size_t", 2);
int numBlocks;
{
DVSizeT _dv_num_grps(vec.size());
DVSizeT _dv_begin(0);
DVSizeT _dv_size_grp(1);
DVSizeT _dv_div_id((size_t)(-1));
const DeviceViewable* _args[] = { &_dv_num_grps, &vec, &_dv_begin, &value, &comp, &dv_range_out, &_dv_size_grp, &_dv_div_id };
s_kernel.calc_number_blocks(_args, 128, numBlocks);
}
size_t s_begin = 0;
size_t s_end = vec.size();
do
{
size_t n = s_end - s_begin;
size_t size_grp = 1;
size_t div_id = (size_t)(-1);
size_t num_grps = 128 * numBlocks;
if (num_grps < n)
{
size_grp = n / num_grps;
div_id = (size_grp + 1) * num_grps - n;
}
else
{
num_grps = n;
numBlocks = (int)((num_grps + 127) / 128);
}
DVSizeT dv_num_grps(num_grps);
DVSizeT dv_begin(s_begin);
DVSizeT dv_size_grp(size_grp);
DVSizeT dv_div_id(div_id);
h_range_out[0] = vec.size();
h_range_out[1] = 0;
if (!CheckCUresult(cuMemcpyHtoD((CUdeviceptr)dv_range_out.data(), h_range_out, sizeof(size_t) * 2), "cuMemcpyHtoD()")) return false;
const DeviceViewable* args[] = { &dv_num_grps, &vec, &dv_begin, &value, &comp, &dv_range_out, &dv_size_grp, &dv_div_id };
if (!s_kernel.launch({ (unsigned)numBlocks, 1,1 }, { 128, 1, 1 }, args)) return false;
dv_range_out.to_host(h_range_out);
s_begin = h_range_out[0];
s_end = h_range_out[1];
}
while (s_end > 0 && s_end > s_begin + 1);
result = s_end;
return true;
}
bool TRTC_Upper_Bound(const DVVectorLike& vec, const DeviceViewable& value, size_t& result)
{
return TRTC_Upper_Bound(vec, value, Functor("Less"), result);
}
bool TRTC_Binary_Search(const DVVectorLike& vec, const DeviceViewable& value, const Functor& comp, bool& result)
{
if (vec.size() <= 0)
{
result = 0;
return true;
}
static TRTC_Kernel s_kernel(
{ "num_grps", "vec", "begin", "value", "comp", "range_out", "size_grp", "div_id" },
" size_t id = threadIdx.x+blockIdx.x*blockDim.x;\n"
" if (id>=num_grps) return;"
" size_t begin_grp = size_grp*id + begin;\n"
" size_t end_grp = begin_grp + size_grp;\n"
" if (id>=div_id)\n"
" {\n"
" begin_grp += id - div_id;\n"
" end_grp = begin_grp + size_grp + 1;\n"
" }\n"
" if ( !comp(value, vec[begin_grp]) && !comp(vec[end_grp-1], value) )\n"
" {\n"
" if (!comp(vec[begin_grp], value) || !comp(value, vec[end_grp-1]))\n"
" {\n"
" range_out[2] = 1;\n"
" return;"
" }\n"
" range_out[0] = begin_grp;\n"
" range_out[1] = end_grp;\n"
" }\n"
);
size_t h_range_out[3];
DVVector dv_range_out("size_t", 3);
int numBlocks;
{
DVSizeT _dv_num_grps(vec.size());
DVSizeT _dv_begin(0);
DVSizeT _dv_size_grp(1);
DVSizeT _dv_div_id((size_t)(-1));
const DeviceViewable* _args[] = { &_dv_num_grps, &vec, &_dv_begin, &value, &comp, &dv_range_out, &_dv_size_grp, &_dv_div_id };
s_kernel.calc_number_blocks(_args, 128, numBlocks);
}
size_t s_begin = 0;
size_t s_end = vec.size();
do
{
size_t n = s_end - s_begin;
size_t size_grp = 1;
size_t div_id = (size_t)(-1);
size_t num_grps = 128 * numBlocks;
if (num_grps < n)
{
size_grp = n / num_grps;
div_id = (size_grp + 1) * num_grps - n;
}
else
{
num_grps = n;
numBlocks = (int)((num_grps + 127) / 128);
}
DVSizeT dv_num_grps(num_grps);
DVSizeT dv_begin(s_begin);
DVSizeT dv_size_grp(size_grp);
DVSizeT dv_div_id(div_id);
h_range_out[0] = (size_t)(-1);
h_range_out[1] = 0;
h_range_out[2] = 0;
if (!CheckCUresult(cuMemcpyHtoD((CUdeviceptr)dv_range_out.data(), h_range_out, sizeof(size_t) * 3), "cuMemcpyHtoD()")) return false;
const DeviceViewable* args[] = { &dv_num_grps, &vec, &dv_begin, &value, &comp, &dv_range_out, &dv_size_grp, &dv_div_id };
if (!s_kernel.launch({ (unsigned)numBlocks, 1,1 }, { 128, 1, 1 }, args)) return false;
dv_range_out.to_host(h_range_out);
if (h_range_out[2] != 0) break;
s_begin = h_range_out[0];
s_end = h_range_out[1];
} while (s_end - s_begin > 1);
result = h_range_out[2] != 0;
return true;
}
bool TRTC_Binary_Search(const DVVectorLike& vec, const DeviceViewable& value, bool& result)
{
return TRTC_Binary_Search(vec, value, Functor("Less"), result);
}
bool TRTC_Lower_Bound_V(const DVVectorLike& vec, const DVVectorLike& values, DVVectorLike& result, const Functor& comp)
{
static TRTC_For s_for({ "vec", "values", "result", "comp" }, "idx",
" auto value = values[idx];\n"
" result[idx] = (decltype(result)::value_t) d_lower_bound(vec, value, comp);\n"
);
const DeviceViewable* args[] = { &vec, &values, &result, &comp };
return s_for.launch_n(values.size(), args);
}
bool TRTC_Lower_Bound_V(const DVVectorLike& vec, const DVVectorLike& values, DVVectorLike& result)
{
return TRTC_Lower_Bound_V(vec, values, result, Functor("Less"));
}
bool TRTC_Upper_Bound_V(const DVVectorLike& vec, const DVVectorLike& values, DVVectorLike& result, const Functor& comp)
{
static TRTC_For s_for({ "vec", "values", "result", "comp" }, "idx",
" auto value = values[idx];\n"
" result[idx] = (decltype(result)::value_t) d_upper_bound(vec, value, comp);\n"
);
const DeviceViewable* args[] = { &vec, &values, &result, &comp };
return s_for.launch_n(values.size(), args);
}
bool TRTC_Upper_Bound_V(const DVVectorLike& vec, const DVVectorLike& values, DVVectorLike& result)
{
return TRTC_Upper_Bound_V(vec, values, result, Functor("Less"));
}
bool TRTC_Binary_Search_V(const DVVectorLike& vec, const DVVectorLike& values, DVVectorLike& result, const Functor& comp)
{
static TRTC_For s_for({ "vec", "values", "result", "comp" }, "idx",
" auto value = values[idx ];\n"
" result[idx] = (decltype(result)::value_t) d_binary_search(vec, value, comp);\n"
);
const DeviceViewable* args[] = { &vec, &values, &result, &comp };
return s_for.launch_n(values.size(), args);
}
bool TRTC_Binary_Search_V(const DVVectorLike& vec, const DVVectorLike& values, DVVectorLike& result)
{
return TRTC_Binary_Search_V(vec, values, result, Functor("Less"));
}