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
45 typedef typename sparse_vector_type::statistics sv_statistics_type;
46
47 struct task_batch : public bm::task_batch<allocator_type>
48 {
51 };
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 @ingroup bmtasks
100 */
101template<typename BV>
103{
104public:
105 typedef BV bvector_type;
106 typedef typename BV::size_type size_type;
109
110
111 struct task_batch : public bm::task_batch<allocator_type>
112 {
115 };
116
118 bm::xor_sim_model<BV>& sim_model,
119 const bv_ref_vector_type& ref_vect,
120 const bm::xor_sim_params& xs_params)
121 {
122 sim_model.bv_blocks.clear(true);
123 ref_vect.build_nb_digest_and_xor_matrix(sim_model.matr,
124 sim_model.bv_blocks);
125
126 typename bvector_type::size_type nb_count = sim_model.bv_blocks.count();
127 typename task_batch::task_vector_type& tv = batch.get_task_vector();
128 tv.reserve(nb_count);
129
130 typename xor_sim_model<BV>::matrix_chain_type &sm_matr = sim_model.matr;
131
132 typename bvector_type::enumerator en(sim_model.bv_blocks);
133 for (size_type col = 0; en.valid(); ++en, ++col)
134 {
135 size_type nb = *en;
137 [nb, col, &xs_params, &sm_matr, &ref_vect] (void* /*argp*/) {
138 thread_local bm::xor_scanner<BV> xor_scan;
139 xor_scan.set_ref_vector(&ref_vect);
140 xor_scan.sync_nb_vect();
141 xor_scan.compute_sim_model(sm_matr, nb, col, xs_params);
142 return 0;
143 });
144 batch.add(task, 0);
145 } // for en
146 }
147};
148
149
150
151/**
152 Parallel plan builder for succinct sparse vector serialization
153
154 @sa sparse_vector_serializer
155 @ingroup bmtasks
156 */
157template<typename SV>
159{
160public:
163 typedef typename SV::size_type size_type;
167
168
170 {
172 : sb_bookmarks_(false),
173 sb_range_(0),
176 {}
177
178 bool sb_bookmarks_; ///< Bookmarks flag
179 unsigned sb_range_; ///< Desired bookmarks interval
181
184 };
185
186 struct task_batch : public bm::task_batch<allocator_type>
187 {
190
192 };
193
194public:
196 {}
197
198 void set_bookmarks(bool enable, unsigned bm_interval = 256) BMNOEXCEPT
199 { s_params_.sb_bookmarks_ = enable; s_params_.sb_range_ = bm_interval; }
200
202 { s_params_.bv_ref_ptr_ = bv_ref_ptr; }
203
205 { s_params_.sim_model_ptr_ = sim_model; }
206
207
210 const sparse_vector_type& sv)
211 {
212 typename task_batch::task_vector_type& tv = batch.get_task_vector();
213 unsigned planes = sv.stored_slices();
214 tv.reserve(planes + 1); // +1 for finalization task
215
216 batch.s_params = s_params_;
217
218 for (unsigned i = 0; i < planes; ++i)
219 {
220 typename SV::bvector_type_const_ptr bv = sv.get_slice(i);
221 if (!bv) // empty plane
222 {
223 sv_layout.set_plane(i, 0, 0);
224 continue;
225 }
226 unsigned bv_idx = (unsigned)s_params_.bv_ref_ptr_->find_bv(bv);
228
230 [bv_idx, &sv_layout] (void* /*argp*/) {
231 //TODO: full implementation
232 BM_ASSERT(0);
233 return 0;
234 });
235
236 bm::task_descr& tdescr = tv.add();
237 tdescr.init(task, (void*)&tdescr);
238
239// tdescr.ret = (void*)&sv_layout;
240
242 {
244 /*
245 tdescr.payload0.u32 =
246 (unsigned)s_params_.bv_ref_ptr_->find_bv(bv);
247 BM_ASSERT(tdescr.payload0.u32
248 != s_params_.bv_ref_ptr_->not_found());
249 */
250 }
251 else
252 {
253 // ref vector not set: see set_xor_ref()
255 }
256
257 } // for i
258
259 // Add barrier task at the end to finalize the compression
260 bm::task_function_t task_final(
261 [&sv_layout] (void* /*argp*/) {
262 //TODO: full implementation
263 BM_ASSERT(0);
264 return 0;
265 });
266
267 bm::task_descr& tdescr = tv.add();
268 tdescr.init(task_final, (void*)&tdescr);
269 tdescr.flags = bm::task_descr::barrier_ok;
270 //tdescr.ret = (void*)&sv_layout;
271 }
272protected:
273 /// Task execution Entry Point
274 /// @internal
275 static void* task_run(void* argp)
276 {
277 if (!argp)
278 return 0;
279 //TODO: full implementation
280 return 0;
281 }
282
283 static void* task_run_final(void* argp)
284 {
285 if (!argp)
286 return 0;
287 //TODO: full implementation
288 return 0;
289 }
290protected:
292};
293
294
295} // namespace bm
296
297#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
static size_type not_found() BMNOEXCEPT
not-found value for find methods
Definition: bmxor.h:672
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
size_type find_bv(const bvector_type *bv) const BMNOEXCEPT
Find vector index by the pointer.
Definition: bmxor.h:687
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
bm::bv_ref_vector< BV > bv_ref_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)
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.
Parallel plan builder for succinct sparse vector serialization.
void set_bookmarks(bool enable, unsigned bm_interval=256) BMNOEXCEPT
static void * task_run(void *argp)
Task execution Entry Point.
void set_xor_ref(const bv_ref_vector_type *bv_ref_ptr) BMNOEXCEPT
bm::xor_sim_model< bvector_type > xor_sim_model_type
static void * task_run_final(void *argp)
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
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 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:92
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.
bm::task_batch< allocator_type > parent_type
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
bm::bvector bvector_type
Definition: xsample07a.cpp:94