Report from analyzing the tuner
[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
56 ostream &operator<<(ostream &os, const TunableSetting &ts)
57 {
58         os << ts.hasVar << " " << ts.type1 << " " << ts.type2 << " " << ts.param << " " << ts.lowValue << " "
59                  << ts.highValue << " " << ts.defaultValue << " " << ts.selectedValue;
60         return os;
61 }
62
63
64 SearchTuner::SearchTuner() {
65 }
66
67 SearchTuner::SearchTuner(const char *filename) {
68         ifstream myfile;
69         myfile.open (filename, ios::in);
70         if (myfile.is_open()) {
71                 bool hasVar;
72                 VarType type1;
73                 VarType type2;
74                 TunableParam param;
75                 int lowValue;
76                 int highValue;
77                 int defaultValue;
78                 int selectedValue;
79                 while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
80                         TunableSetting *setting;
81
82                         if (hasVar) {
83                                 setting = new TunableSetting(type1, type2, param);
84                         } else {
85                                 setting = new TunableSetting(param);
86                         }
87                         setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
88                         settings.add(setting);
89                         usedSettings.add(setting);
90                 }
91                 myfile.close();
92         } else {
93                 model_print("Warning: Tuner %s couldn't be loaded ... Using default tuner instead ....\n", filename);
94         }
95 }
96
97 bool SearchTuner::equalUsed(SearchTuner* tuner){
98         if(tuner->usedSettings.getSize() != usedSettings.getSize()){
99                 return false;
100         }
101         bool result = true;
102         SetIteratorTunableSetting *iterator = usedSettings.iterator();
103         while(iterator->hasNext()){
104                 TunableSetting *setting = iterator->next();
105                 if(!tuner->usedSettings.contains(setting)){
106                         result = false;
107                         break;
108                 }else{
109                         TunableSetting *tunerSetting = tuner->usedSettings.get(setting);
110                         if(tunerSetting->selectedValue != setting->selectedValue){
111                                 result = false;
112                                 break;
113                         }
114                 }
115         }
116         delete iterator;
117         return result;
118 }
119
120 void SearchTuner::addUsed(const char *filename) {
121         ifstream myfile;
122         myfile.open (filename, ios::in);
123         if (myfile.is_open()) {
124                 bool hasVar;
125                 VarType type1;
126                 VarType type2;
127                 TunableParam param;
128                 int lowValue;
129                 int highValue;
130                 int defaultValue;
131                 int selectedValue;
132                 while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
133                         TunableSetting *setting;
134
135                         if (hasVar) {
136                                 setting = new TunableSetting(type1, type2, param);
137                         } else {
138                                 setting = new TunableSetting(param);
139                         }
140                         setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
141                         if (!settings.contains(setting)) {
142                                 settings.add(setting);
143                                 usedSettings.add(setting);
144                         } else {
145                                 TunableSetting *tmp = settings.get(setting);
146                                 settings.remove(tmp);
147                                 usedSettings.remove(tmp);
148                                 delete tmp;
149                                 settings.add(setting);
150                                 usedSettings.add(setting);
151                         }
152                 }
153                 myfile.close();
154         }
155 }
156
157 bool SearchTuner::isSubTunerof(SearchTuner *newTuner){
158         SetIteratorTunableSetting *iterator = usedSettings.iterator();
159         while (iterator->hasNext()) {
160                 TunableSetting *setting = iterator->next();
161                 if(!newTuner->settings.contains(setting)){
162                         return false;
163                 } else{
164                         TunableSetting *newSetting = newTuner->settings.get(setting);
165                         if(newSetting->selectedValue != setting->selectedValue){
166                                 return false;
167                         }
168                 }
169         }
170         delete iterator;
171         return true;
172 }
173
174 SearchTuner *SearchTuner::copyUsed() {
175         SearchTuner *tuner = new SearchTuner();
176         SetIteratorTunableSetting *iterator = usedSettings.iterator();
177         while (iterator->hasNext()) {
178                 TunableSetting *setting = iterator->next();
179                 TunableSetting *copy = new TunableSetting(setting);
180                 tuner->settings.add(copy);
181         }
182         delete iterator;
183         return tuner;
184 }
185
186 SearchTuner::~SearchTuner() {
187         SetIteratorTunableSetting *iterator = settings.iterator();
188         while (iterator->hasNext()) {
189                 TunableSetting *setting = iterator->next();
190                 delete setting;
191         }
192         delete iterator;
193 }
194
195 void SearchTuner::setTunable(TunableParam param, TunableDesc *descriptor, uint value) {
196         TunableSetting *result = new TunableSetting(param);
197         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
198         settings.add(result);
199         usedSettings.add(result);
200 }
201
202 void SearchTuner::setVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor, uint value) {
203         setVarTunable(vartype, 0, param, descriptor, value);
204 }
205
206 void SearchTuner::setVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor, uint value) {
207         TunableSetting *result = new TunableSetting(vartype1, vartype2, param);
208         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
209         settings.add(result);
210         usedSettings.add(result);
211 }
212
213 int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) {
214         TunableSetting setting(param);
215         TunableSetting *result = usedSettings.get(&setting);
216         if (result == NULL) {
217                 result = settings.get(&setting);
218                 if ( result == NULL) {
219                         result = new TunableSetting(param);
220                         uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
221                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
222                         settings.add(result);
223                 }
224                 usedSettings.add(result);
225         }
226         return result->selectedValue;
227 }
228
229 int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) {
230         return getVarTunable(vartype, 0, param, descriptor);
231 }
232
233 int SearchTuner::getVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor) {
234         TunableSetting setting(vartype1, vartype2, param);
235         TunableSetting *result = usedSettings.get(&setting);
236         if (result == NULL) {
237                 result = settings.get(&setting);
238                 if ( result == NULL) {
239                         result = new
240                                                          TunableSetting(vartype1, vartype2, param);
241                         uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
242                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
243                         settings.add(result);
244                 }
245                 usedSettings.add(result);
246         }
247         return result->selectedValue;
248 }
249
250 void SearchTuner::randomMutate() {
251         TunableSetting *randomSetting = settings.getRandomElement();
252         int range = randomSetting->highValue - randomSetting->lowValue;
253         int randomchoice = (random() % range) + randomSetting->lowValue;
254         if (randomchoice < randomSetting->selectedValue)
255                 randomSetting->selectedValue = randomchoice;
256         else
257                 randomSetting->selectedValue = randomchoice + 1;
258         model_print("&&&&&&&&Mutating&&&&&&&\n");
259         randomSetting->print();
260         model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
261 }
262
263 void SearchTuner::print() {
264         SetIteratorTunableSetting *iterator = settings.iterator();
265         while (iterator->hasNext()) {
266                 TunableSetting *setting = iterator->next();
267                 setting->print();
268         }
269         delete iterator;
270
271 }
272
273 void SearchTuner::serialize(const char *filename) {
274         ofstream myfile;
275         myfile.open (filename, ios::out | ios::trunc);
276         SetIteratorTunableSetting *iterator = settings.iterator();
277         while (iterator->hasNext()) {
278                 TunableSetting *setting = iterator->next();
279                 myfile << *setting << endl;
280         }
281         myfile.close();
282         delete iterator;
283 }
284
285 void SearchTuner::serializeUsed(const char *filename) {
286         ofstream myfile;
287         myfile.open (filename, ios::out | ios::trunc);
288         SetIteratorTunableSetting *iterator = usedSettings.iterator();
289         while (iterator->hasNext()) {
290                 TunableSetting *setting = iterator->next();
291                 myfile << *setting << endl;
292         }
293         myfile.close();
294         delete iterator;
295 }
296
297 void SearchTuner::printUsed() {
298         SetIteratorTunableSetting *iterator = usedSettings.iterator();
299         while (iterator->hasNext()) {
300                 TunableSetting *setting = iterator->next();
301                 setting->print();
302         }
303         delete iterator;
304 }