Autotune Framework
[satune.git] / src / Tuner / searchtuner.cc
1 #include "searchtuner.h"
2
3 TunableSetting::TunableSetting(VarType _type, TunableParam _param) :
4         hasVar(true),
5         type(_type),
6         param(_param) {
7 }
8
9 TunableSetting::TunableSetting(TunableParam _param) :
10         hasVar(false),
11         type(0),
12         param(_param) {
13 }
14
15 TunableSetting::TunableSetting(TunableSetting * ts) :
16         hasVar(ts->hasVar),
17         type(ts->type),
18         param(ts->param),
19   lowValue(ts->lowValue),
20         highValue(ts->highValue),
21         defaultValue(ts->defaultValue),
22         selectedValue(ts->selectedValue)
23 {
24 }
25
26 void TunableSetting::setDecision(int _low, int _high, int _default, int _selection) {
27         lowValue = _low;
28         highValue = _high;
29         defaultValue = _default;
30         selectedValue = _selection;
31 }
32
33 unsigned int tunableSettingHash(TunableSetting *setting) {
34         return setting->hasVar ^ setting->type ^ setting->param;
35 }
36
37 bool tunableSettingEquals(TunableSetting *setting1, TunableSetting *setting2) {
38         return setting1->hasVar == setting2->hasVar &&
39                 setting1->type == setting2->type &&
40                 setting1->param == setting2->param;
41 }
42
43 SearchTuner::SearchTuner() {
44 }
45
46 SearchTuner * SearchTuner::copyUsed() {
47         SearchTuner * tuner = new SearchTuner();
48         HSIteratorTunableSetting *iterator=usedSettings.iterator();
49         while(iterator->hasNext()) {
50                 TunableSetting *setting=iterator->next();
51                 TunableSetting *copy=new TunableSetting(setting);
52                 tuner->settings.add(copy);
53         }
54         delete iterator;
55         return tuner;
56 }
57
58 SearchTuner::~SearchTuner() {
59         HSIteratorTunableSetting *iterator=settings.iterator();
60         while(iterator->hasNext()) {
61                 TunableSetting *setting=iterator->next();
62                 delete setting;
63         }
64         delete iterator;
65 }
66
67 int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) {
68         TunableSetting setting(param);
69         TunableSetting * result = usedSettings.get(&setting);
70         if (result == NULL) {
71                 result = settings.get(&setting);
72                 if ( result == NULL) {
73                         result=new TunableSetting(param);
74                         uint value = descriptor->lowValue + (random() % (1+ descriptor->highValue - descriptor->lowValue));
75                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
76                         settings.add(result);
77                 }
78                 usedSettings.add(result);
79         }
80         return result->selectedValue;
81 }
82
83 int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) {
84         TunableSetting setting(vartype, param);
85         TunableSetting * result = usedSettings.get(&setting);
86         if (result == NULL) {
87                 result = settings.get(&setting);
88                 if ( result == NULL) {
89                         result=new TunableSetting(vartype, param);
90                         uint value = descriptor->lowValue + (random() % (1+ descriptor->highValue - descriptor->lowValue));
91                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
92                         settings.add(result);
93                 }
94                 usedSettings.add(result);
95         }
96         return result->selectedValue;
97 }
98
99 void SearchTuner::randomMutate() {
100         TunableSetting * randomSetting = settings.getRandomElement();
101         uint range=randomSetting->highValue-randomSetting->lowValue;
102         uint randomchoice=(random() % range) + randomSetting->lowValue;
103         if (randomchoice < randomSetting->selectedValue)
104                 randomSetting->selectedValue = randomchoice;
105         else
106                 randomSetting->selectedValue = randomchoice + 1;
107 }