libfoedus-core
FOEDUS Core Library
prob_counter.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_ASSORTED_PROB_COUNTER_HPP_
19 #define FOEDUS_ASSORTED_PROB_COUNTER_HPP_
20 
21 #include "foedus/assert_nd.hpp"
23 
24 namespace foedus {
25 namespace assorted {
37 struct ProbCounter {
43  uint8_t value_;
44 
45  ProbCounter() : value_(0) {
46  ASSERT_ND(sizeof(uint8_t) == sizeof(ProbCounter));
47  }
48 
49  inline void reset() { value_ = 0; }
50 
51  inline void increment(UniformRandom* rnd) {
52  // We increment the value_ for exponentially smaller probability.
53  // value_==0: always
54  // value_==1: 50% chance
55  // value_==2: 25% chance... etc
56  // We can implement this by simple bit-shifts.
57 
58  // Extract middle 24-bits, which is typically done in hashing. This is kinda hashing too.
59  // This also means the following makes sense only for value_<=24. Should be a reasonable assmp.
60  const uint32_t int_val = rnd->next_uint32();
61  const uint32_t int24_val = (int_val >> 4) & 0xFFFFFFU;
62  const uint32_t threshold = 1U << (24 - value_);
63  if (int24_val < threshold) {
64  ++value_;
65  }
66  }
67 
68  // XXX(tzwang): how should we actually implement decrement()? blind --? follow prob.?
69  inline void decrement_force() {
70  --value_;
71  }
72 };
73 } // namespace assorted
74 } // namespace foedus
75 
76 #endif // FOEDUS_ASSORTED_PROB_COUNTER_HPP_
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
uint8_t value_
Log arithmic counter of aborts.
Implements a probabilistic counter [Morris 1978].
void increment(UniformRandom *rnd)
A very simple and deterministic random generator that is more aligned with standard benchmark such as...
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72