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