Fix tuner issues
[satune.git] / src / Tuner / searchtuner.cc
1 #include "searchtuner.h"
2 #include <iostream>
3 #include <fstream>
4 using namespace std;
5
6 TunableSetting::TunableSetting(VarType _type, TunableParam _param) :
7         hasVar(true),
8         type1(_type),
9         type2(0),
10         param(_param) {
11 }
12
13 TunableSetting::TunableSetting(VarType _type1, VarType _type2, TunableParam _param) :
14         hasVar(true),
15         type1(_type1),
16         type2(_type2),
17         param(_param) {
18 }
19
20 TunableSetting::TunableSetting(TunableParam _param) :
21         hasVar(false),
22         type1(0),
23         type2(0),
24         param(_param) {
25 }
26
27 TunableSetting::TunableSetting(TunableSetting *ts) :
28         hasVar(ts->hasVar),
29         type1(ts->type1),
30         type2(ts->type2),
31         param(ts->param),
32         lowValue(ts->lowValue),
33         highValue(ts->highValue),
34         defaultValue(ts->defaultValue),
35         selectedValue(ts->selectedValue)
36 {
37 }
38
39 void TunableSetting::setDecision(int _low, int _high, int _default, int _selection) {
40         lowValue = _low;
41         highValue = _high;
42         defaultValue = _default;
43         selectedValue = _selection;
44 }
45
46 void TunableSetting::print() {
47         model_print("Param %s = %u \t range=[%u,%u]", tunableParameterToString( (Tunables)param), selectedValue, lowValue, highValue);
48         if (hasVar) {
49                 model_print("\tVarType1 %" PRIu64 ", ", type1);
50                 model_print("VarType2 %" PRIu64 ", ", type2);
51         }
52         model_print("\n");
53 }
54
55 unsigned int tunableSettingHash(TunableSetting *setting) {
56         return setting->hasVar ^ setting->type1 ^ setting->type2 ^ setting->param;
57 }
58
59 bool tunableSettingEquals(TunableSetting *setting1, TunableSetting *setting2) {
60         return setting1->hasVar == setting2->hasVar &&
61                                  setting1->type1 == setting2->type1 &&
62                                  setting1->type2 == setting2->type2 &&
63                                  setting1->param == setting2->param;
64 }
65
66 ostream &operator<<(ostream &os, const TunableSetting &ts)
67 {
68         os << ts.hasVar << " " << ts.type1 << " " << ts.type2 << " " << ts.param << " " << ts.lowValue << " "
69                  << ts.highValue << " " << ts.defaultValue << " " << ts.selectedValue;
70         return os;
71 }
72
73
74 SearchTuner::SearchTuner() {
75 }
76
77 SearchTuner::SearchTuner(const char *filename) {
78         ifstream myfile;
79         myfile.open (filename, ios::in);
80         if (myfile.is_open()) {
81                 bool hasVar;
82                 VarType type1;
83                 VarType type2;
84                 TunableParam param;
85                 int lowValue;
86                 int highValue;
87                 int defaultValue;
88                 int selectedValue;
89                 while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
90                         TunableSetting *setting;
91
92                         if (hasVar) {
93                                 setting = new TunableSetting(type1, type2, param);
94                         } else {
95                                 setting = new TunableSetting(param);
96                         }
97                         setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
98                         usedSettings.add(setting);
99                 }
100                 myfile.close();
101         }
102 }
103
104 SearchTuner *SearchTuner::copyUsed() {
105         SearchTuner *tuner = new SearchTuner();
106         SetIteratorTunableSetting *iterator = usedSettings.iterator();
107         while (iterator->hasNext()) {
108                 TunableSetting *setting = iterator->next();
109                 TunableSetting *copy = new TunableSetting(setting);
110                 tuner->settings.add(copy);
111         }
112         delete iterator;
113         return tuner;
114 }
115
116 SearchTuner::~SearchTuner() {
117         SetIteratorTunableSetting *iterator = settings.iterator();
118         while (iterator->hasNext()) {
119                 TunableSetting *setting = iterator->next();
120                 delete setting;
121         }
122         delete iterator;
123 }
124
125 void SearchTuner::setTunable(TunableParam param, TunableDesc *descriptor, uint value) {
126         TunableSetting *result = new TunableSetting(param);
127         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
128         settings.add(result);
129         usedSettings.add(result);
130 }
131
132 void SearchTuner::setVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor, uint value) {
133         setVarTunable(vartype, 0, param, descriptor, value);
134 }
135
136 void SearchTuner::setVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor, uint value) {
137         TunableSetting *result = new TunableSetting(vartype1, vartype2, param);
138         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
139         settings.add(result);
140         usedSettings.add(result);
141 }
142
143 int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) {
144         TunableSetting setting(param);
145         TunableSetting *result = usedSettings.get(&setting);
146         if (result == NULL) {
147                 result = settings.get(&setting);
148                 if ( result == NULL) {
149                         result = new TunableSetting(param);
150                         uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
151                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
152                         settings.add(result);
153                 }
154                 usedSettings.add(result);
155         }
156         return result->selectedValue;
157 }
158
159 int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) {
160         return getVarTunable(vartype, 0, param, descriptor);
161 }
162
163 int SearchTuner::getVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor) {
164         TunableSetting setting(vartype1, vartype2, param);
165         TunableSetting *result = usedSettings.get(&setting);
166         if (result == NULL) {
167                 result = settings.get(&setting);
168                 if ( result == NULL) {
169                         result = new
170                                                          TunableSetting(vartype1, vartype2, param);
171                         uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
172                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
173                         settings.add(result);
174                 }
175                 usedSettings.add(result);
176         }
177         return result->selectedValue;
178 }
179
180 void SearchTuner::randomMutate() {
181         TunableSetting *randomSetting = settings.getRandomElement();
182         int range = randomSetting->highValue - randomSetting->lowValue;
183         int randomchoice = (random() % range) + randomSetting->lowValue;
184         if (randomchoice < randomSetting->selectedValue)
185                 randomSetting->selectedValue = randomchoice;
186         else
187                 randomSetting->selectedValue = randomchoice + 1;
188         model_print("&&&&&&&&Mutating&&&&&&&\n");
189         randomSetting->print();
190         model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
191 }
192
193 void SearchTuner::print() {
194         SetIteratorTunableSetting *iterator = settings.iterator();
195         while (iterator->hasNext()) {
196                 TunableSetting *setting = iterator->next();
197                 setting->print();
198         }
199         delete iterator;
200
201 }
202
203 void SearchTuner::serialize(const char *filename) {
204         ofstream myfile;
205         myfile.open (filename, ios::out | ios::trunc);
206         SetIteratorTunableSetting *iterator = settings.iterator();
207         while (iterator->hasNext()) {
208                 TunableSetting *setting = iterator->next();
209                 myfile << *setting << endl;
210         }
211         myfile.close();
212         delete iterator;
213 }
214
215 void SearchTuner::printUsed() {
216         SetIteratorTunableSetting *iterator = usedSettings.iterator();
217         while (iterator->hasNext()) {
218                 TunableSetting *setting = iterator->next();
219                 setting->print();
220         }
221         delete iterator;
222 }