BitMagic-C++
bmsparsevec_parallel.h
Go to the documentation of this file.
1#ifndef BMSPARSEVEC_PARALLEL__H__INCLUDED__
2#define BMSPARSEVEC_PARALLEL__H__INCLUDED__
3/*
4Copyright(c) 2020 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17
18For more information please visit: http://bitmagic.io
19*/
20
21/*! \file bmsparsevec_parallel.h
22 \brief Parallel planner for operations with sparse vectors
23*/
24
25#include "bmsparsevec_serial.h"
26
27namespace bm
28{
29
30/**
31 Builder class to prepare a batch of tasks for parallel optimization of
32 a sparse vector
33 @ingroup bmtasks
34 */
35template<typename SVect, typename Lock>
37{
38public:
39 typedef SVect sparse_vector_type;
40 typedef Lock lock_type;
41
42 typedef typename sparse_vector_type::bvector_type bvector_type;
45 typedef typename sparse_vector_type::statistics sv_statistics_type;
46
52
53 /**
54 Build paralell optimization batch (of tasks) for sparse vector
55 @param batch - target batch (can be re-used for multiple vectors)
56 @param sv - sparse vector for optimization
57 @param opt_mode - optimization mode (see bvector<>::optmode)
58 @param st - sparse vector statistics to compute with optimization pass (optional)
59 */
60 static
65 typename sparse_vector_type::statistics* st = 0)
66 {
67 typename task_batch::task_vector_type& tv = batch.get_task_vector();
68 auto rsize = sv.get_bmatrix().rows();
69 tv.reserve(rsize);
70 if (st)
71 st->reset();
72
73 for (unsigned k = 0; k < rsize; ++k)
74 {
75 if (bvector_type* bv = sv.get_bmatrix().get_row(k))
76 {
77 bm::task_function_t task([bv, opt_mode, st] (void* /*argp*/) {
78 typename bvector_type::statistics stbv;
79 stbv.reset();
81 bv->optimize(tb, opt_mode, &stbv);
82 if (st)
83 {
84 static lock_type lk;
86 st->add(stbv);
87 }
88 return 0;
89 });
90 batch.add(task, 0);
91 }
92 } // for
93 }
94
95};
96
97/**
98 Parallel plan builder for the XOR filter scanner.
99 Parallelization principle here is based on makig a task per 64K bit block (task per NP)
100 @ingroup bmtasks
101 */
102template<typename BV>
104{
105public:
106 typedef BV bvector_type;
107 typedef typename BV::size_type size_type;
110
111
117
118 /**
119 Construct task batch for parallel execution. (Every task is a lambda).
120 */
122 bm::xor_sim_model<BV>& sim_model,
123 const bv_ref_vector_type& ref_vect,
124 const bm::xor_sim_params& xs_params)
125 {
126 sim_model.bv_blocks.clear(true);
127 ref_vect.build_nb_digest_and_xor_matrix(sim_model.matr,
128 sim_model.bv_blocks);
129
130 typename bvector_type::size_type nb_count = sim_model.bv_blocks.count();
131 typename task_batch::task_vector_type& tv = batch.get_task_vector();
132 tv.reserve(nb_count);
133
134 typename xor_sim_model<BV>::matrix_chain_type &sm_matr = sim_model.matr;
135
136 typename bvector_type::enumerator en(sim_model.bv_blocks);
137 for (size_type col = 0; en.valid(); ++en, ++col)
138 {
139 size_type nb = *en;
141 [nb, col, &xs_params, &sm_matr, &ref_vect] (void* /*argp*/) {
142 thread_local bm::xor_scanner<BV> xor_scan;
143 xor_scan.set_ref_vector(&ref_vect);
144 xor_scan.sync_nb_vect();
145 xor_scan.compute_sim_model(sm_matr, nb, col, xs_params);
146 return 0;
147 });
148 batch.add(task, 0);
149 } // for en
150 }
151};
152
153
154
155/**
156 Parallel plan builder for succinct sparse vector serialization
157
158 @sa sparse_vector_serializer
159 @ingroup bmtasks
160 */
161template<typename SV>
163{
164public:
166 typedef typename SV::bvector_type bvector_type;
167 typedef typename SV::size_type size_type;
171
172
174 {
181
182 bool sb_bookmarks_; ///< Bookmarks flag
183 unsigned sb_range_; ///< Desired bookmarks interval
185
188 };
189
197
198public:
201
202 void set_bookmarks(bool enable, unsigned bm_interval = 256) BMNOEXCEPT
203 { s_params_.sb_bookmarks_ = enable; s_params_.sb_range_ = bm_interval; }
204
206 { s_params_.bv_ref_ptr_ = bv_ref_ptr; }
207
209 { s_params_.sim_model_ptr_ = sim_model; }
210
211
214 const sparse_vector_type& sv)
215 {
216 typename task_batch::task_vector_type& tv = batch.get_task_vector();
217 unsigned planes = sv.stored_slices();
218 tv.reserve(planes + 1); // +1 for finalization task
219
220 batch.s_params = s_params_;
221
222 for (unsigned i = 0; i < planes; ++i)
223 {
224 typename SV::bvector_type_const_ptr bv = sv.get_slice(i);
225 if (!bv) // empty plane
226 {
227 sv_layout.set_plane(i, 0, 0);
228 continue;
229 }
230 unsigned bv_idx = (unsigned)s_params_.bv_ref_ptr_->find_bv(bv);
231 BM_ASSERT(bv_idx != s_params_.bv_ref_ptr_->not_found());
232
234 [bv_idx, &sv_layout] (void* /*argp*/) {
235 //TODO: full implementation
236 BM_ASSERT(0);
237 return 0;
238 });
239
240 bm::task_descr& tdescr = tv.add();
241 tdescr.init(task, (void*)&tdescr);
242
243// tdescr.ret = (void*)&sv_layout;
244
245 if (s_params_.bv_ref_ptr_)
246 {
248 /*
249 tdescr.payload0.u32 =
250 (unsigned)s_params_.bv_ref_ptr_->find_bv(bv);
251 BM_ASSERT(tdescr.payload0.u32
252 != s_params_.bv_ref_ptr_->not_found());
253 */
254 }
255 else
256 {
257 // ref vector not set: see set_xor_ref()
259 }
260
261 } // for i
262
263 // Add barrier task at the end to finalize the compression
264 bm::task_function_t task_final(
265 [&sv_layout] (void* /*argp*/) {
266 //TODO: full implementation
267 BM_ASSERT(0);
268 return 0;
269 });
270
271 bm::task_descr& tdescr = tv.add();
272 tdescr.init(task_final, (void*)&tdescr);
273 tdescr.flags = bm::task_descr::barrier_ok;
274 //tdescr.ret = (void*)&sv_layout;
275 }
276protected:
277 /// Task execution Entry Point
278 /// @internal
279 static void* task_run(void* argp)
280 {
281 if (!argp)
282 return 0;
283 //TODO: full implementation
284 return 0;
285 }
286
287 static void* task_run_final(void* argp)
288 {
289 if (!argp)
290 return 0;
291 //TODO: full implementation
292 return 0;
293 }
294protected:
296};
297
298
299} // namespace bm
300
301#endif
#define BM_DECLARE_TEMP_BLOCK(x)
Definition bm.h:47
#define BMNOEXCEPT
Definition bmdef.h:82
#define BM_ASSERT
Definition bmdef.h:139
Serialization for sparse_vector<>.
List of reference bit-vectors with their true index associations.
Definition bmxor.h:624
bool build_nb_digest_and_xor_matrix(matrix_chain_type &matr, bvector_type &bv_blocks) const
Calculate blocks digest and resize XOR distance matrix based on total number of available blocks.
Definition bmxor.h:759
Constant iterator designed to enumerate "ON" bits.
Definition bm.h:603
bool valid() const BMNOEXCEPT
Checks if iterator is still valid.
Definition bm.h:283
optmode
Optimization mode Every next level means additional checks (better compression vs time).
Definition bm.h:133
@ opt_compress
compress blocks when possible (GAP/prefix sum)
Definition bm.h:137
bvector_size_type size_type
Definition bm.h:121
Alloc allocator_type
Definition bm.h:117
Parallel plan builder for the XOR filter scanner.
bvector_type::allocator_type allocator_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)
Construct task batch for parallel execution.
Simple scoped lock guard.
Definition bmtask.h:227
Builder class to prepare a batch of tasks for parallel optimization of a sparse vector.
bvector_type::allocator_type allocator_type
sparse_vector_type::statistics sv_statistics_type
sparse_vector_type::bvector_type bvector_type
bvector_type::optmode optmode_type
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)
Build paralell optimization batch (of tasks) for sparse vector.
void set_bookmarks(bool enable, unsigned bm_interval=256) BMNOEXCEPT
static void * task_run_final(void *argp)
void set_xor_ref(const bv_ref_vector_type *bv_ref_ptr) BMNOEXCEPT
bm::xor_sim_model< bvector_type > xor_sim_model_type
void build_plan(task_batch &batch, sparse_vector_serial_layout< SV > &sv_layout, const sparse_vector_type &sv)
bm::bv_ref_vector< bvector_type > bv_ref_vector_type
bvector_type::allocator_type allocator_type
void set_sim_model(const xor_sim_model_type *sim_model) BMNOEXCEPT
static void * task_run(void *argp)
Task execution Entry Point.
Basic implementation for collection of tasks for parallel execution.
Definition bmtask.h:140
task_vector_type & get_task_vector() BMNOEXCEPT
Get access to internal task vector.
Definition bmtask.h:168
void add(task_function_t f, void *argptr)
Definition bmtask.h:172
std::vector< bm::task_descr > task_vector_type
Definition bmtask.h:146
XOR scanner to search for complement-similarities in collections of bit-vectors.
Definition bmxor.h:820
void sync_nb_vect()
Sync TEMP vector size.
Definition bmxor.h:1732
bool compute_sim_model(xor_sim_model< BV > &sim_model, const bv_ref_vector_type &ref_vect, const bm::xor_sim_params &params)
Calculate matrix of best XOR match metrics per block for the attached collection of bit-vectors.
Definition bmxor.h:1440
void set_ref_vector(const bv_ref_vector_type *ref_vect) BMNOEXCEPT
Definition bmxor.h:850
std::function< int(void *)> task_function_t
Typedef for a call-back functional for lambda capture.
Definition bmtask.h:54
Definition bm.h:78
const unsigned set_compression_default
Default compression level.
Definition bmserial.h:60
void reset() BMNOEXCEPT
Reset statisctics.
Definition bmfunc.h:94
Statistical information about bitset's memory allocation details.
Definition bm.h:125
bm::task_batch< allocator_type > parent_type
parent_type::task_vector_type task_vector_type
layout class for serialization buffer structure
void set_plane(unsigned i, unsigned char *ptr, size_t buf_size) BMNOEXCEPT
Set plane output pointer and size.
BitMagic task with a captured function.
Definition bmtask.h:62
@ barrier_ok
barrier waits all prev.tasks done without error
Definition bmtask.h:66
void init(task_function_t f, void *argptr) noexcept
Definition bmtask.h:98
XOR similarity model.
Definition bmxor.h:791
bm::dynamic_heap_matrix< block_match_chain_type, bv_allocator_type > matrix_chain_type
Definition bmxor.h:801
matrix_chain_type matr
model matrix
Definition bmxor.h:804
bvector_type bv_blocks
blocks digest
Definition bmxor.h:805
Parameters for XOR similarity search.
Definition bmxor.h:59