Edit
[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                         settings.add(setting);
99                         usedSettings.add(setting);
100                 }
101                 myfile.close();
102         } else{
103                 model_print("Warning: Tuner %s couldn't be loaded ... Using default tuner instead ....\n", filename);
104         }
105 }
106
107 void SearchTuner::addUsed(const char *filename) {
108         ifstream myfile;
109         myfile.open (filename, ios::in);
110         if (myfile.is_open()) {
111                 bool hasVar;
112                 VarType type1;
113                 VarType type2;
114                 TunableParam param;
115                 int lowValue;
116                 int highValue;
117                 int defaultValue;
118                 int selectedValue;
119                 while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
120                         TunableSetting *setting;
121
122                         if (hasVar) {
123                                 setting = new TunableSetting(type1, type2, param);
124                         } else {
125                                 setting = new TunableSetting(param);
126                         }
127                         setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
128                         if (!settings.contains(setting)) {
129                                 settings.add(setting);
130                                 usedSettings.add(setting);
131                         } else {
132                                 TunableSetting *tmp = settings.get(setting);
133                                 settings.remove(tmp);
134                                 usedSettings.remove(tmp);
135                                 delete tmp;
136                                 settings.add(setting);
137                                 usedSettings.add(setting);
138                         }
139                 }
140                 myfile.close();
141         }
142 }
143
144 SearchTuner *SearchTuner::copyUsed() {
145         SearchTuner *tuner = new SearchTuner();
146         SetIteratorTunableSetting *iterator = usedSettings.iterator();
147         while (iterator->hasNext()) {
148                 TunableSetting *setting = iterator->next();
149                 TunableSetting *copy = new TunableSetting(setting);
150                 tuner->settings.add(copy);
151         }
152         delete iterator;
153         return tuner;
154 }
155
156 SearchTuner::~SearchTuner() {
157         SetIteratorTunableSetting *iterator = settings.iterator();
158         while (iterator->hasNext()) {
159                 TunableSetting *setting = iterator->next();
160                 delete setting;
161         }
162         delete iterator;
163 }
164
165 void SearchTuner::setTunable(TunableParam param, TunableDesc *descriptor, uint value) {
166         TunableSetting *result = new TunableSetting(param);
167         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
168         settings.add(result);
169         usedSettings.add(result);
170 }
171
172 void SearchTuner::setVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor, uint value) {
173         setVarTunable(vartype, 0, param, descriptor, value);
174 }
175
176 void SearchTuner::setVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor, uint value) {
177         TunableSetting *result = new TunableSetting(vartype1, vartype2, param);
178         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
179         settings.add(result);
180         usedSettings.add(result);
181 }
182
183 int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) {
184         TunableSetting setting(param);
185         TunableSetting *result = usedSettings.get(&setting);
186         if (result == NULL) {
187                 result = settings.get(&setting);
188                 if ( result == NULL) {
189                         result = new TunableSetting(param);
190                         uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
191                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
192                         settings.add(result);
193                 }
194                 usedSettings.add(result);
195         }
196         return result->selectedValue;
197 }
198
199 int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) {
200         return getVarTunable(vartype, 0, param, descriptor);
201 }
202
203 int SearchTuner::getVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor) {
204         TunableSetting setting(vartype1, vartype2, param);
205         TunableSetting *result = usedSettings.get(&setting);
206         if (result == NULL) {
207                 result = settings.get(&setting);
208                 if ( result == NULL) {
209                         result = new
210                                                          TunableSetting(vartype1, vartype2, param);
211                         uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
212                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
213                         settings.add(result);
214                 }
215                 usedSettings.add(result);
216         }
217         return result->selectedValue;
218 }
219
220 void SearchTuner::randomMutate() {
221         TunableSetting *randomSetting = settings.getRandomElement();
222         int range = randomSetting->highValue - randomSetting->lowValue;
223         int randomchoice = (random() % range) + randomSetting->lowValue;
224         if (randomchoice < randomSetting->selectedValue)
225                 randomSetting->selectedValue = randomchoice;
226         else
227                 randomSetting->selectedValue = randomchoice + 1;
228         model_print("&&&&&&&&Mutating&&&&&&&\n");
229         randomSetting->print();
230         model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
231 }
232
233 void SearchTuner::print() {
234         SetIteratorTunableSetting *iterator = settings.iterator();
235         while (iterator->hasNext()) {
236                 TunableSetting *setting = iterator->next();
237                 setting->print();
238         }
239         delete iterator;
240
241 }
242
243 void SearchTuner::serialize(const char *filename) {
244         ofstream myfile;
245         myfile.open (filename, ios::out | ios::trunc);
246         SetIteratorTunableSetting *iterator = settings.iterator();
247         while (iterator->hasNext()) {
248                 TunableSetting *setting = iterator->next();
249                 myfile << *setting << endl;
250         }
251         myfile.close();
252         delete iterator;
253 }
254
255 void SearchTuner::serializeUsed(const char *filename) {
256         ofstream myfile;
257         myfile.open (filename, ios::out | ios::trunc);
258         SetIteratorTunableSetting *iterator = usedSettings.iterator();
259         while (iterator->hasNext()) {
260                 TunableSetting *setting = iterator->next();
261                 myfile << *setting << endl;
262         }
263         myfile.close();
264         delete iterator;
265 }
266
267 void SearchTuner::printUsed() {
268         SetIteratorTunableSetting *iterator = usedSettings.iterator();
269         while (iterator->hasNext()) {
270                 TunableSetting *setting = iterator->next();
271                 setting->print();
272         }
273         delete iterator;
274 }