bfe66bb4dd77cdcb21e15230a09cb9e5a6da5882
[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         ifstream myfile;
76         myfile.open (TUNEFILE, ios::in);
77         if (myfile.is_open()) {
78                 bool hasVar;
79                 VarType type1;
80                 VarType type2;
81                 TunableParam param;
82                 int lowValue;
83                 int highValue;
84                 int defaultValue;
85                 int selectedValue;
86                 while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
87                         TunableSetting *setting;
88
89                         if (hasVar) {
90                                 setting = new TunableSetting(type1, type2, param);
91                         } else {
92                                 setting = new TunableSetting(param);
93                         }
94                         setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
95                         usedSettings.add(setting);
96                 }
97                 myfile.close();
98         }
99 }
100
101 SearchTuner *SearchTuner::copyUsed() {
102         SearchTuner *tuner = new SearchTuner();
103         SetIteratorTunableSetting *iterator = usedSettings.iterator();
104         while (iterator->hasNext()) {
105                 TunableSetting *setting = iterator->next();
106                 TunableSetting *copy = new TunableSetting(setting);
107                 tuner->settings.add(copy);
108         }
109         delete iterator;
110         return tuner;
111 }
112
113 SearchTuner::~SearchTuner() {
114         SetIteratorTunableSetting *iterator = settings.iterator();
115         while (iterator->hasNext()) {
116                 TunableSetting *setting = iterator->next();
117                 delete setting;
118         }
119         delete iterator;
120 }
121
122 int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) {
123         TunableSetting setting(param);
124         TunableSetting *result = usedSettings.get(&setting);
125         if (result == NULL) {
126                 result = settings.get(&setting);
127                 if ( result == NULL) {
128                         result = new TunableSetting(param);
129                         uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
130                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
131                         settings.add(result);
132                 }
133                 usedSettings.add(result);
134         }
135         return result->selectedValue;
136 }
137
138 int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) {
139         return getVarTunable(vartype, 0, param, descriptor);
140 }
141
142 int SearchTuner::getVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor) {
143         TunableSetting setting(vartype1, vartype2, param);
144         TunableSetting *result = usedSettings.get(&setting);
145         if (result == NULL) {
146                 result = settings.get(&setting);
147                 if ( result == NULL) {
148                         result = new
149                                                          TunableSetting(vartype1, vartype2, param);
150                         uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
151                         result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
152                         settings.add(result);
153                 }
154                 usedSettings.add(result);
155         }
156         return result->selectedValue;
157 }
158
159 void SearchTuner::randomMutate() {
160         TunableSetting *randomSetting = settings.getRandomElement();
161         int range = randomSetting->highValue - randomSetting->lowValue;
162         int randomchoice = (random() % range) + randomSetting->lowValue;
163         if (randomchoice < randomSetting->selectedValue)
164                 randomSetting->selectedValue = randomchoice;
165         else
166                 randomSetting->selectedValue = randomchoice + 1;
167         model_print("&&&&&&&&Mutating&&&&&&&\n");
168         randomSetting->print();
169         model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
170 }
171
172 void SearchTuner::print() {
173         SetIteratorTunableSetting *iterator = settings.iterator();
174         while (iterator->hasNext()) {
175                 TunableSetting *setting = iterator->next();
176                 setting->print();
177         }
178         delete iterator;
179
180 }
181
182 void SearchTuner::serialize() {
183         ofstream myfile;
184         myfile.open (TUNEFILE, ios::out | ios::trunc);
185         SetIteratorTunableSetting *iterator = settings.iterator();
186         while (iterator->hasNext()) {
187                 TunableSetting *setting = iterator->next();
188                 myfile << *setting << endl;
189         }
190         myfile.close();
191         delete iterator;
192 }
193
194 void SearchTuner::printUsed() {
195         SetIteratorTunableSetting *iterator = usedSettings.iterator();
196         while (iterator->hasNext()) {
197                 TunableSetting *setting = iterator->next();
198                 setting->print();
199         }
200         delete iterator;
201 }