Adding a directed search based config for 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 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 #ifdef STATICENCGEN
76         graphEncoding =false;
77         naiveEncoding = ELEM_UNASSIGNED;
78 #endif
79         ifstream myfile;
80         myfile.open (TUNEFILE, ios::in);
81         if (myfile.is_open()) {
82                 bool hasVar;
83                 VarType type1;
84                 VarType type2;
85                 TunableParam param;
86                 int lowValue;
87                 int highValue;
88                 int defaultValue;
89                 int selectedValue;
90                 while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
91                         TunableSetting *setting;
92
93                         if (hasVar) {
94                                 setting = new TunableSetting(type1, type2, param);
95                         } else {
96                                 setting = new TunableSetting(param);
97                         }
98                         setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
99                         usedSettings.add(setting);
100                 }
101                 myfile.close();
102         }
103 }
104
105 SearchTuner *SearchTuner::copyUsed() {
106         SearchTuner *tuner = new SearchTuner();
107         SetIteratorTunableSetting *iterator = usedSettings.iterator();
108         while (iterator->hasNext()) {
109                 TunableSetting *setting = iterator->next();
110                 TunableSetting *copy = new TunableSetting(setting);
111                 tuner->settings.add(copy);
112         }
113 #ifdef STATICENCGEN
114         if(naiveEncoding != ELEM_UNASSIGNED){
115                 tuner->graphEncoding = graphEncoding;
116                 tuner->naiveEncoding = naiveEncoding;
117         }
118 #endif
119         delete iterator;
120         return tuner;
121 }
122
123 SearchTuner::~SearchTuner() {
124         SetIteratorTunableSetting *iterator = settings.iterator();
125         while (iterator->hasNext()) {
126                 TunableSetting *setting = iterator->next();
127                 delete setting;
128         }
129         delete iterator;
130 }
131
132 int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) {
133         TunableSetting setting(param);
134         TunableSetting *result = usedSettings.get(&setting);
135         if (result == NULL) {
136                 result = settings.get(&setting);
137                 if ( result == NULL) {
138                         result = new TunableSetting(param);
139                         uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
140                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
141                         settings.add(result);
142                 }
143                 usedSettings.add(result);
144         }
145         return result->selectedValue;
146 }
147
148 int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) {
149         return getVarTunable(vartype, 0, param, descriptor);
150 }
151
152 int SearchTuner::getVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor) {
153         TunableSetting setting(vartype1, vartype2, param);
154         TunableSetting *result = usedSettings.get(&setting);
155         if (result == NULL) {
156                 result = settings.get(&setting);
157                 if ( result == NULL) {
158                         result = new
159                                                          TunableSetting(vartype1, vartype2, param);
160                         uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
161                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
162                         settings.add(result);
163                 }
164                 usedSettings.add(result);
165         }
166         return result->selectedValue;
167 }
168
169 void SearchTuner::randomMutate() {
170         TunableSetting *randomSetting = settings.getRandomElement();
171         int range = randomSetting->highValue - randomSetting->lowValue;
172         int randomchoice = (random() % range) + randomSetting->lowValue;
173         if (randomchoice < randomSetting->selectedValue)
174                 randomSetting->selectedValue = randomchoice;
175         else
176                 randomSetting->selectedValue = randomchoice + 1;
177         model_print("&&&&&&&&Mutating&&&&&&&\n");
178         randomSetting->print();
179         model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
180 }
181
182 #ifdef STATICENCGEN
183 int SearchTuner::nextStaticTuner() {
184         if(naiveEncoding == ELEM_UNASSIGNED){
185                 naiveEncoding = ONEHOT;
186                 SetIteratorTunableSetting *iter = settings.iterator();
187                 while(iter->hasNext()){
188                         TunableSetting *setting = iter->next();
189                         if (setting->param == NAIVEENCODER){
190                                 setting->selectedValue = ONEHOT;
191                         } else if(setting->param == ENCODINGGRAPHOPT){
192                                 setting->selectedValue = false;
193                         }
194                 }
195                 delete iter;
196                 return EXIT_FAILURE;
197         }
198         int result=EXIT_FAILURE;
199         if(naiveEncoding == BINARYINDEX && graphEncoding){
200                 model_print("Best tuner\n");
201                 return EXIT_SUCCESS;
202         }else if (naiveEncoding == BINARYINDEX && !graphEncoding){
203                 naiveEncoding = ONEHOT;
204                 graphEncoding = true;
205         }else {
206                 naiveEncoding = (ElementEncodingType)((int)naiveEncoding + 1);
207         }
208         SetIteratorTunableSetting *iter = settings.iterator();
209         uint count = 0;
210         while(iter->hasNext()){
211                 TunableSetting * setting = iter->next();
212                 if (setting->param == NAIVEENCODER){
213                         setting->selectedValue = naiveEncoding;
214                         count++;
215                 } else if(setting->param == ENCODINGGRAPHOPT){
216                         setting->selectedValue = graphEncoding;
217                         count++;
218                 }
219         }
220         model_print("Mutating %u settings\n", count);
221         delete iter;
222         return result;
223 }
224 #endif
225
226 void SearchTuner::print() {
227         SetIteratorTunableSetting *iterator = settings.iterator();
228         while (iterator->hasNext()) {
229                 TunableSetting *setting = iterator->next();
230                 setting->print();
231         }
232         delete iterator;
233
234 }
235
236 void SearchTuner::serialize() {
237         ofstream myfile;
238         myfile.open (TUNEFILE, ios::out | ios::trunc);
239         SetIteratorTunableSetting *iterator = settings.iterator();
240         while (iterator->hasNext()) {
241                 TunableSetting *setting = iterator->next();
242                 myfile << *setting << endl;
243         }
244         myfile.close();
245         delete iterator;
246 }
247
248 void SearchTuner::printUsed() {
249         SetIteratorTunableSetting *iterator = usedSettings.iterator();
250         while (iterator->hasNext()) {
251                 TunableSetting *setting = iterator->next();
252                 setting->print();
253         }
254         delete iterator;
255 }