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