BitMagic-C++
sample11.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16 For more information please visit: http://bitmagic.io
17 */
18 
19 /** \example sample11.cpp
20  Example of how to use various bit counting techniques
21 
22  \sa bm::bvector<>::count()
23  \sa bm::bvector<>::count_range()
24  \sa bm::bvector<>::count_to()
25  \sa bm::count_and()
26  \sa bm::bvector<>::counted_enumerator
27  */
28 
29 /*! \file sample11.cpp
30  \brief Example: bvector<> bit-counting techniques analysis
31 */
32 
33 #include <iostream>
34 #include <random>
35 #include <memory>
36 
37 #include "bm.h"
38 #include "bmalgo.h"
39 #include "bmtimer.h"
40 
41 using namespace std;
42 
43 // timing storage for benchmarking
45 
46 const unsigned benchmark_count = 10000;
47 unsigned vector_max = 400000000;
48 
49 std::random_device rand_dev;
50 std::mt19937 gen(rand_dev()); // mersenne_twister_engine
51 std::uniform_int_distribution<> rand_dis(1, int(vector_max)); // generate uniform numebrs for [1, vector_max]
52 
53 
54 /// generate pseudo-random bit-vector, mix of blocks
55 ///
56 static
58 {
60  for (i = 0; i < vector_max;)
61  {
62  // generate bit-blocks
63  for (j = 0; j < 65535*8; i += 10, j++)
64  {
65  bv.set(i);
66  }
67  if (i > vector_max)
68  break;
69  // generate GAP (compressed) blocks
70  for (j = 0; j < 65535; i += 120, j++)
71  {
72  unsigned len = rand() % 64;
73  bv.set_range(i, i + len);
74  i += len;
75  if (i > vector_max)
76  break;
77  }
78  }
79 
80  // compress vector
82  bv.optimize(tb);
83 
84  // compute bit-vector statistics
86  bv.calc_stat(&st);
87 
88  std::cout << "Bit-vector statistics: GAP (compressed blocks)=" << st.gap_blocks
89  << ", BIT (uncompressed blocks)=" << st.bit_blocks
90  << std::endl << std::endl;
91 }
92 
93 /// "pre-heat" CPU to minimize dynamic overclocking effects
94 ///
95 static
97 {
100  for (unsigned i = 0; i < benchmark_count; ++i)
101  {
102  cnt += bv.count();
103  m+=cnt*cnt;
104  }
105  return m;
106 }
107 
108 
109 
110 /// simple population count for the whole vector
111 ///
112 static
114 {
115  bm::bvector<>::size_type cnt = 0;
116 
117  {
118  bm::chrono_taker tt1("1. bvector<>::count()", benchmark_count / 2, &timing_map);
119  for (unsigned i = 0; i < benchmark_count / 2; ++i)
120  {
121  cnt += bv.count();
122  }
123  }
124  // this is mostly to prevent compiler to optimize loop away
125  std::cout << "Count test finished." << cnt << "\r";
126 }
127 
128 /// count_range() test
129 ///
130 static
132 {
133  bm::bvector<>::size_type cnt = 0;
134  {
135  bm::chrono_taker tt1("2. bvector<>::count_range()", benchmark_count, &timing_map);
136  for (unsigned i = 0; i < benchmark_count; ++i)
137  {
138  unsigned from = unsigned(rand_dis(gen));
139  unsigned to = unsigned(rand_dis(gen));
140  if (from > to)
141  swap(from, to);
142  cnt += bv.count_range(from, to);
143  }
144  }
145  // this is mostly to prevent compiler to optimize loop away
146  std::cout << "Count range test finished." << cnt << "\r";
147 }
148 
149 /// count_range() test using pre-calculated blocks bit count
150 ///
151 static
153 {
154  bm::bvector<>::size_type cnt = 0;
155 
156  std::unique_ptr<bm::bvector<>::rs_index_type> rs(new bm::bvector<>::rs_index_type());
157  bv.build_rs_index(rs.get());
158 
159  {
160  bm::chrono_taker tt1("3. bvector<>::count_range() with rs_index", benchmark_count, &timing_map);
161  cnt = 0;
162  for (unsigned i = 0; i < benchmark_count; ++i)
163  {
164  unsigned from = unsigned(rand_dis(gen));
165  unsigned to = unsigned(rand_dis(gen));
166  if (from > to)
167  swap(from, to);
168  cnt += bv.count_range(from, to, *rs); // use rs index for acceleration
169  } // for i
170  }
171  // this is mostly to prevent compiler to optimize loop away
172  std::cout << "Count range with blocks test finished." << cnt << "\r";
173 }
174 
175 /// count_to() test using pre-calculated rank-select index
176 ///
177 static
179 {
180  bm::bvector<>::size_type cnt = 0;
181 
182  // build a block population count list, used for count_to() acceleration
183  std::unique_ptr<bm::bvector<>::rs_index_type> rs(new bm::bvector<>::rs_index_type());
184  bv.build_rs_index(rs.get());
185 
186  {
187  bm::chrono_taker tt1("4. bvector<>::count_to() with rs_index", benchmark_count, &timing_map);
188 
189  for (unsigned i = 0; i < benchmark_count; ++i)
190  {
191  unsigned to = unsigned(rand_dis(gen));
192  cnt += bv.count_to(to, *rs); // use rank-select index for acceleration
193  }
194  }
195  // this is mostly to prevent compiler to optimize loop away
196  std::cout << "Count to with blocks test finished." << cnt << "\r";
197 }
198 
199 
200 /// count_range implemented via two count_to() calls using pre-calculated
201 /// rank-select index
202 ///
203 static
205 {
206  bm::bvector<>::size_type cnt = 0;
207 
208  // build a block population count list, used for count_to() acceleration
209  std::unique_ptr<bm::bvector<>::rs_index_type> rs(new bm::bvector<>::rs_index_type());
210  bv.build_rs_index(rs.get());
211 
212  {
213  bm::chrono_taker tt1("5. bvector<>::count_to to simulate count_range()", benchmark_count, &timing_map);
214 
215  for (unsigned i = 0; i < benchmark_count; ++i)
216  {
217  unsigned from = unsigned(rand_dis(gen));
218  unsigned to = unsigned(rand_dis(gen));
219  if (from > to)
220  swap(from, to);
221 
222  bm::bvector<>::size_type cnt_to = bv.count_to(to, *rs);
223  bm::bvector<>::size_type cnt_from = bv.count_to(from - 1, *rs);
224  bm::bvector<>::size_type cnt_r = cnt_to - cnt_from;
225  cnt += cnt_r;
226  }
227  }
228  // this is mostly to prevent compiler to optimize loop away
229  std::cout << "Count range via count_to test finished." << cnt << "\r";
230 }
231 
232 /// count_range implemented via bm::count_and
233 ///
234 /// this method can be used, when we need co compute multiple ranges in one call
235 ///
236 static
238 {
239  bm::bvector<>::size_type cnt = 0;
240  {
241  bm::chrono_taker tt1("6. bm::count_and with mask vector", benchmark_count, &timing_map);
242 
243  bm::bvector<> mask_bv(bm::BM_GAP); // use compressed mask, better seluts on long ranges
244  for (unsigned i = 0; i < benchmark_count; ++i)
245  {
246  unsigned from = unsigned(rand_dis(gen));
247  unsigned to = unsigned(rand_dis(gen));
248  if (from > to)
249  swap(from, to);
250 
251  mask_bv.set_range(from, to, true); // set mask vector
252 
253  cnt += bm::count_and(bv, mask_bv);
254  mask_bv.clear(true); // clear and free memory (faster)
255  }
256  }
257  // this is mostly to prevent compiler to optimize loop away
258  std::cout << "count AND finished." << cnt << "\r";
259 }
260 
261 /// count_to implemented via bm::bvector<>::counted_enumerator
262 ///
263 /// Counted enumerator is an iterator automata, which counts the running population count
264 /// along the iteration sequence
265 ///
266 static
268 {
269  bm::bvector<>::size_type cnt = 0;
270  {
271  // This is a slow method so we use less iterators
272  bm::chrono_taker tt1("7. bm::bvector<>::counted_enumerator", benchmark_count/20, &timing_map);
273 
274  for (unsigned i = 0; i < benchmark_count/20; ++i)
275  {
276  unsigned to = unsigned(rand_dis(gen));
278  for (; en.valid(); ++en)
279  {
280  if (*en > to)
281  break;
282  }
283  cnt += en.count();
284  }
285  }
286  std::cout << "counted_enumerator finished." << cnt << "\r";
287 }
288 
289 
290 
291 
292 int main(void)
293 {
294  try
295  {
296  bm::bvector<> bv;
297  generate_bvector(bv);
298 
299  /// pre-heat CPU to minimize dynamic overclocking
300  unsigned s = pre_heat(bv);
301  std::cout << s << "\r";
302 
303 
304  // Test 1.
305  // Uses plain bvector<>::count() to compute global population count
306  // This function would benefit from SIMD (SSE42 / AVX2) acceleration
307  //
308  bv_count_test(bv);
309 
310  // Test 2.
311  // Uses bvector<>::count_range() to compute population count in a randomly generated
312  // region of a bit-vector.
313  // This is should be naturally faster than Test 1, because it range is less than the whole
314  //
315  bv_count_range(bv);
316 
317  // Test 3.
318  // Uses bvector<>::count_range() together with bvector<>::count_blocks()
319  // (pre-calculated bit-count for each block).
320  // It make sense to use this method if bit-vector is constant (or chnages infrequently)
321  // and we need to do many range counting calculations
322  //
323  bv_count_range_acc(bv);
324 
325  // Test 4.
326  // Uses bvector<>::count_to() to compute population count to a specified element.
327  // Equivalent of count_range(0, to);
328  // This method uses acceleration structure using bvector<>::running_count_blocks()
329  // It is similar to count_range acceleration, but uses a different (faster) algorithm
330  //
331  bv_count_to_acc(bv);
332 
333  // Test 5.
334  // Uses bvector<>::count_to() twice to simulate count_range()
335  // using counting difference:
336  // count_r = count_to(0, from) - count_to(0, to-1)
337  // This method can actually be faster than count_range()
338  //
340 
341  // Test 6.
342  // Compute range population count via a mask vector and logical AND operation.
343  // Not the fastest method, but can be useful, when multiple ranges needs to be computed
344  //
345  bv_count_and(bv);
346 
347  // Test 7.
348  // Compute cout using counted_enumerator iterator
349  // method combines iteratrion over bit vector and sliding population count
351 
352 
353  // print all test timing results
354  //
355  std::cout << " "
356  << std::endl;
357 
359  }
360  catch(std::exception& ex)
361  {
362  std::cerr << ex.what() << std::endl;
363  return 1;
364  }
365 
366  return 0;
367 }
368 
pre_heat
static bm::bvector ::size_type pre_heat(const bm::bvector<> &bv)
"pre-heat" CPU to minimize dynamic overclocking effects
Definition: sample11.cpp:96
bm::bvector::set_range
bvector< Alloc > & set_range(size_type left, size_type right, bool value=true)
Sets all bits in the specified closed interval [left,right] Interval must be inside the bvector's siz...
Definition: bm.h:2184
bm::bvector::calc_stat
void calc_stat(struct bm::bvector< Alloc >::statistics *st) const BMNOEXCEPT
Calculates bitvector statistics.
Definition: bm.h:3419
generate_bvector
static void generate_bvector(bm::bvector<> &bv)
generate pseudo-random bit-vector, mix of blocks
Definition: sample11.cpp:57
bm::bvector::rs_index_type
rs_index< allocator_type > rs_index_type
Definition: bm.h:845
benchmark_count
const unsigned benchmark_count
Definition: sample11.cpp:46
timing_map
bm::chrono_taker::duration_map_type timing_map
Definition: sample11.cpp:44
bm::chrono_taker
Utility class to collect performance measurements and statistics.
Definition: bmtimer.h:39
bv_counted_enumerator
static void bv_counted_enumerator(const bm::bvector<> &bv)
count_to implemented via bm::bvector<>::counted_enumerator
Definition: sample11.cpp:267
bm::bv_statistics::bit_blocks
size_t bit_blocks
Number of bit blocks.
Definition: bmfunc.h:56
BM_DECLARE_TEMP_BLOCK
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
bm::bvector::count_range
size_type count_range(size_type left, size_type right, const rs_index_type &rs_idx) const BMNOEXCEPT
Returns count of 1 bits in the given range [left..right] Uses rank-select index to accelerate the sea...
Definition: bm.h:2985
bv_count_to_acc
static void bv_count_to_acc(const bm::bvector<> &bv)
count_to() test using pre-calculated rank-select index
Definition: sample11.cpp:178
bv_count_test
static void bv_count_test(const bm::bvector<> &bv)
simple population count for the whole vector
Definition: sample11.cpp:113
bm::bvector::counted_enumerator::count
size_type count() const BMNOEXCEPT
Number of bits ON starting from the .
Definition: bm.h:771
vector_max
unsigned vector_max
Definition: sample11.cpp:47
bmalgo.h
Algorithms for bvector<> (main include)
bv_count_and
static void bv_count_and(const bm::bvector<> &bv)
count_range implemented via bm::count_and
Definition: sample11.cpp:237
bm::bv_statistics::gap_blocks
size_t gap_blocks
Number of GAP blocks.
Definition: bmfunc.h:57
bv_count_range_acc
static void bv_count_range_acc(const bm::bvector<> &bv)
count_range() test using pre-calculated blocks bit count
Definition: sample11.cpp:152
bm::bvector<>
bm::bvector::set
bvector< Alloc > & set(size_type n, bool val=true)
Sets bit n if val is true, clears bit n if val is false.
Definition: bm.h:3607
bm::bvector::count
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition: bm.h:2220
bm::bvector::iterator_base::valid
bool valid() const BMNOEXCEPT
Checks if iterator is still valid. Analog of != 0 comparison for pointers.
Definition: bm.h:280
gen
std::mt19937 gen(rand_dev())
bmtimer.h
Timing utilities for benchmarking (internal)
bm::chrono_taker::ct_ops_per_sec
@ ct_ops_per_sec
Definition: bmtimer.h:59
bm::BM_GAP
@ BM_GAP
GAP compression is ON.
Definition: bmconst.h:144
main
int main(void)
Definition: sample11.cpp:292
bm::bvector::clear
void clear(const size_type *ids, size_type ids_size, bm::sort_order so=bm::BM_UNKNOWN)
clear list of bits in this bitset
Definition: bm.h:3572
bm::chrono_taker::duration_map_type
std::map< std::string, statistics > duration_map_type
test name to duration map
Definition: bmtimer.h:65
bm::count_and
BV::size_type count_and(const BV &bv1, const BV &bv2) BMNOEXCEPT
Computes bitcount of AND operation of two bitsets.
Definition: bmalgo.h:49
bv_count_range
static void bv_count_range(const bm::bvector<> &bv)
count_range() test
Definition: sample11.cpp:131
bm::chrono_taker::print_duration_map
static void print_duration_map(const duration_map_type &dmap, format fmt=ct_time)
Definition: bmtimer.h:127
bm::bvector::build_rs_index
void build_rs_index(rs_index_type *rs_idx, bvector< Alloc > *bv_blocks=0) const
compute running total of all blocks in bit vector (rank-select index)
Definition: bm.h:2316
bm::bvector::first
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1796
bm::bvector::size_type
bm::id_t size_type
Definition: bm.h:117
bm::bvector::counted_enumerator
Constant iterator designed to enumerate "ON" bits counted_enumerator keeps bitcount,...
Definition: bm.h:729
bv_count_to_range_acc
static void bv_count_to_range_acc(const bm::bvector<> &bv)
count_range implemented via two count_to() calls using pre-calculated rank-select index
Definition: sample11.cpp:204
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
bm::bvector::optimize
void optimize(bm::word_t *temp_block=0, optmode opt_mode=opt_compress, statistics *stat=0)
Optimize memory bitvector's memory allocation.
Definition: bm.h:3097
rand_dis
std::uniform_int_distribution rand_dis(1, int(vector_max))
bm::bvector::count_to
size_type count_to(size_type n, const rs_index_type &rs_idx) const BMNOEXCEPT
Returns count of 1 bits (population) in [0..right] range.
Definition: bm.h:2559
bm::bvector::statistics
Statistical information about bitset's memory allocation details.
Definition: bm.h:121
rand_dev
std::random_device rand_dev
Definition: sample11.cpp:49