forked from venkatarun95/genericCC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhisker.cc
144 lines (117 loc) · 3.75 KB
/
whisker.cc
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
#include <cassert>
#include <cmath>
#include <algorithm>
#include <boost/functional/hash.hpp>
#include "whisker.hh"
using namespace std;
vector< Whisker > Whisker::bisect( void ) const
{
vector< Whisker > ret;
for ( auto &x : _domain.bisect() ) {
Whisker new_whisker( *this );
new_whisker._domain = x;
ret.push_back( new_whisker );
}
return ret;
}
Whisker::Whisker( const unsigned int s_window_increment, const double s_window_multiple, const double s_intersend, const MemoryRange & s_domain )
: _generation( 0 ),
_window_increment( s_window_increment ),
_window_multiple( s_window_multiple ),
_intersend( s_intersend ),
_domain( s_domain )
{
}
Whisker::Whisker( const Whisker & other )
: _generation( other._generation ),
_window_increment( other._window_increment ),
_window_multiple( other._window_multiple ),
_intersend( other._intersend ),
_domain( other._domain )
{
}
template < typename T >
bool Whisker::OptimizationSetting< T >::eligible_value( const T & value ) const
{
return value >= min_value and value <= max_value;
}
template < typename T >
vector< T > Whisker::OptimizationSetting< T >::alternatives( const T & value ) const
{
assert( eligible_value( value ) );
vector< T > ret( 1, value );
for ( T proposed_change = min_change;
proposed_change <= max_change;
proposed_change *= multiplier ) {
/* explore positive change */
const T proposed_value_up = value + proposed_change;
const T proposed_value_down = value - proposed_change;
if ( eligible_value( proposed_value_up ) ) {
ret.push_back( proposed_value_up );
}
if ( eligible_value( proposed_value_down ) ) {
ret.push_back( proposed_value_down );
}
}
return ret;
}
vector< Whisker > Whisker::next_generation( void ) const
{
vector< Whisker > ret;
for ( const auto & alt_window : get_optimizer().window_increment.alternatives( _window_increment ) ) {
for ( const auto & alt_multiple : get_optimizer().window_multiple.alternatives( _window_multiple ) ) {
for ( const auto & alt_intersend : get_optimizer().intersend.alternatives( _intersend ) ) {
Whisker new_whisker { *this };
new_whisker._generation++;
new_whisker._window_increment = alt_window;
new_whisker._window_multiple = alt_multiple;
new_whisker._intersend = alt_intersend;
new_whisker.round();
ret.push_back( new_whisker );
}
}
}
return ret;
}
void Whisker::promote( const unsigned int generation )
{
_generation = max( _generation, generation );
}
string Whisker::str( const unsigned int total ) const
{
char tmp[ 256 ];
snprintf( tmp, 256, "{%s} gen=%u usage=%.4f => (win=%d + %f * win, intersend=%f)",
_domain.str().c_str(), _generation, double( _domain.count() ) / double( total ), _window_increment, _window_multiple, _intersend );
return tmp;
}
RemyBuffers::Whisker Whisker::DNA( void ) const
{
RemyBuffers::Whisker ret;
ret.set_window_increment( _window_increment );
ret.set_window_multiple( _window_multiple );
ret.set_intersend( _intersend );
ret.mutable_domain()->CopyFrom( _domain.DNA() );
return ret;
}
Whisker::Whisker( const RemyBuffers::Whisker & dna )
: _generation( 0 ),
_window_increment( dna.window_increment() ),
_window_multiple( dna.window_multiple() ),
_intersend( dna.intersend() ),
_domain( dna.domain() )
{
}
void Whisker::round( void )
{
_window_multiple = (1.0/10000.0) * int( 10000 * _window_multiple );
_intersend = (1.0/10000.0) * int( 10000 * _intersend );
}
size_t hash_value( const Whisker & whisker )
{
size_t seed = 0;
boost::hash_combine( seed, whisker._window_increment );
boost::hash_combine( seed, whisker._window_multiple );
boost::hash_combine( seed, whisker._intersend );
boost::hash_combine( seed, whisker._domain );
return seed;
}