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
#ifndef BMSPARSEVEC_PARALLEL__H__INCLUDED__
#define BMSPARSEVEC_PARALLEL__H__INCLUDED__
/*
Copyright(c) 2020 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
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.
For more information please visit: http://bitmagic.io
*/
/*! \file bmsparsevec_parallel.h
\brief Parallel planner for operations with sparse vectors
*/
#include "bmsparsevec_serial.h"
namespace bm
{
/**
Builder class to prepare a batch of tasks for parallel optimization of
a sparse vector
@ingroup bmtasks
*/
template<typename SVect, typename Lock>
class optimize_plan_builder
{
public:
typedef SVect sparse_vector_type;
typedef Lock lock_type;
typedef typename sparse_vector_type::bvector_type bvector_type;
typedef typename bvector_type::allocator_type allocator_type;
typedef typename bvector_type::optmode optmode_type;
typedef typename sparse_vector_type::statistics sv_statistics_type;
struct task_batch : public bm::task_batch<allocator_type>
{
typedef bm::task_batch<allocator_type> parent_type;
typedef typename parent_type::task_vector_type task_vector_type;
};
/**
Build paralell optimization batch (of tasks) for sparse vector
@param batch - target batch (can be re-used for multiple vectors)
@param sv - sparse vector for optimization
@param opt_mode - optimization mode (see bvector<>::optmode)
@param st - sparse vector statistics to compute with optimization pass (optional)
*/
static
void build_plan(task_batch& batch,
sparse_vector_type& sv,
typename bvector_type::optmode
opt_mode = bvector_type::opt_compress,
typename sparse_vector_type::statistics* st = 0)
{
typename task_batch::task_vector_type& tv = batch.get_task_vector();
auto rsize = sv.get_bmatrix().rows();
tv.reserve(rsize);
if (st)
st->reset();
for (unsigned k = 0; k < rsize; ++k)
{
if (bvector_type* bv = sv.get_bmatrix().get_row(k))
{
bm::task_function_t task([bv, opt_mode, st] (void* /*argp*/) {
typename bvector_type::statistics stbv;
stbv.reset();
BM_DECLARE_TEMP_BLOCK(tb)
bv->optimize(tb, opt_mode, &stbv);
if (st)
{
static lock_type lk;
bm::lock_guard<lock_type> lg(lk);
st->add(stbv);
}
return 0;
});
batch.add(task, 0);
}
} // for
}
};
/**
Parallel plan builder for the XOR filter scanner
@ingroup bmtasks
*/
template<typename BV>
class compute_sim_matrix_plan_builder
{
public:
typedef BV bvector_type;
typedef typename BV::size_type size_type;
typedef typename bvector_type::allocator_type allocator_type;
typedef bm::bv_ref_vector<BV> bv_ref_vector_type;
struct task_batch : public bm::task_batch<allocator_type>
{
typedef bm::task_batch<allocator_type> parent_type;
typedef typename parent_type::task_vector_type task_vector_type;
};
void build_plan(task_batch& batch,
bm::xor_sim_model<BV>& sim_model,
const bv_ref_vector_type& ref_vect,
const bm::xor_sim_params& xs_params)
{
sim_model.bv_blocks.clear(true);
ref_vect.build_nb_digest_and_xor_matrix(sim_model.matr,
sim_model.bv_blocks);
typename bvector_type::size_type nb_count = sim_model.bv_blocks.count();
typename task_batch::task_vector_type& tv = batch.get_task_vector();
tv.reserve(nb_count);
typename xor_sim_model<BV>::matrix_chain_type &sm_matr = sim_model.matr;
typename bvector_type::enumerator en(sim_model.bv_blocks);
for (size_type col = 0; en.valid(); ++en, ++col)
{
size_type nb = *en;
bm::task_function_t task(
[nb, col, &xs_params, &sm_matr, &ref_vect] (void* /*argp*/) {
thread_local bm::xor_scanner<BV> xor_scan;
xor_scan.set_ref_vector(&ref_vect);
xor_scan.sync_nb_vect();
xor_scan.compute_sim_model(sm_matr, nb, col, xs_params);
return 0;
});
batch.add(task, 0);
} // for en
}
};
/**
Parallel plan builder for succinct sparse vector serialization
@sa sparse_vector_serializer
@ingroup bmtasks
*/
template<typename SV>
class sv_serialization_plan_builder
{
public:
typedef SV sparse_vector_type;
typedef typename SV::bvector_type bvector_type;
typedef typename SV::size_type size_type;
typedef typename bvector_type::allocator_type allocator_type;
typedef bm::bv_ref_vector<bvector_type> bv_ref_vector_type;
typedef bm::xor_sim_model<bvector_type> xor_sim_model_type;
struct serialization_params
{
serialization_params()
: sb_bookmarks_(false),
sb_range_(0),
compression_level_(bm::set_compression_default),
bv_ref_ptr_(0), sim_model_ptr_(0)
{}
bool sb_bookmarks_; ///< Bookmarks flag
unsigned sb_range_; ///< Desired bookmarks interval
unsigned compression_level_;
const bv_ref_vector_type* bv_ref_ptr_;
const xor_sim_model_type* sim_model_ptr_;
};
struct task_batch : public bm::task_batch<allocator_type>
{
typedef bm::task_batch<allocator_type> parent_type;
typedef typename parent_type::task_vector_type task_vector_type;
serialization_params s_params;
};
public:
sv_serialization_plan_builder()
{}
void set_bookmarks(bool enable, unsigned bm_interval = 256) BMNOEXCEPT
{ s_params_.sb_bookmarks_ = enable; s_params_.sb_range_ = bm_interval; }
void set_xor_ref(const bv_ref_vector_type* bv_ref_ptr) BMNOEXCEPT
{ s_params_.bv_ref_ptr_ = bv_ref_ptr; }
void set_sim_model(const xor_sim_model_type* sim_model) BMNOEXCEPT
{ s_params_.sim_model_ptr_ = sim_model; }
void build_plan(task_batch& batch,
sparse_vector_serial_layout<SV>& sv_layout,
const sparse_vector_type& sv)
{
typename task_batch::task_vector_type& tv = batch.get_task_vector();
unsigned planes = sv.stored_slices();
tv.reserve(planes + 1); // +1 for finalization task
batch.s_params = s_params_;
for (unsigned i = 0; i < planes; ++i)
{
typename SV::bvector_type_const_ptr bv = sv.get_slice(i);
if (!bv) // empty plane
{
sv_layout.set_plane(i, 0, 0);
continue;
}
unsigned bv_idx = (unsigned)s_params_.bv_ref_ptr_->find_bv(bv);
BM_ASSERT(bv_idx != s_params_.bv_ref_ptr_->not_found());
bm::task_function_t task(
[bv_idx, &sv_layout] (void* /*argp*/) {
//TODO: full implementation
BM_ASSERT(0);
return 0;
});
bm::task_descr& tdescr = tv.add();
tdescr.init(task, (void*)&tdescr);
// tdescr.ret = (void*)&sv_layout;
if (s_params_.bv_ref_ptr_)
{
BM_ASSERT(batch.s_params.sim_model_ptr_);
/*
tdescr.payload0.u32 =
(unsigned)s_params_.bv_ref_ptr_->find_bv(bv);
BM_ASSERT(tdescr.payload0.u32
!= s_params_.bv_ref_ptr_->not_found());
*/
}
else
{
// ref vector not set: see set_xor_ref()
BM_ASSERT(!batch.s_params.sim_model_ptr_);
}
} // for i
// Add barrier task at the end to finalize the compression
bm::task_function_t task_final(
[&sv_layout] (void* /*argp*/) {
//TODO: full implementation
BM_ASSERT(0);
return 0;
});
bm::task_descr& tdescr = tv.add();
tdescr.init(task_final, (void*)&tdescr);
tdescr.flags = bm::task_descr::barrier_ok;
//tdescr.ret = (void*)&sv_layout;
}
protected:
/// Task execution Entry Point
/// @internal
static void* task_run(void* argp)
{
if (!argp)
return 0;
//TODO: full implementation
return 0;
}
static void* task_run_final(void* argp)
{
if (!argp)
return 0;
//TODO: full implementation
return 0;
}
protected:
serialization_params s_params_;
};
} // namespace bm
#endif