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