1 #include "searchtuner.h"
6 TunableSetting::TunableSetting(VarType _type, TunableParam _param) :
13 TunableSetting::TunableSetting(VarType _type1, VarType _type2, TunableParam _param) :
20 TunableSetting::TunableSetting(TunableParam _param) :
27 TunableSetting::TunableSetting(TunableSetting *ts) :
32 lowValue(ts->lowValue),
33 highValue(ts->highValue),
34 defaultValue(ts->defaultValue),
35 selectedValue(ts->selectedValue)
39 void TunableSetting::setDecision(int _low, int _high, int _default, int _selection) {
42 defaultValue = _default;
43 selectedValue = _selection;
46 void TunableSetting::print() {
47 model_print("Param %s = %u \t range=[%u,%u]", tunableParameterToString( (Tunables)param), selectedValue, lowValue, highValue);
49 model_print("\tVarType1 %" PRIu64 ", ", type1);
50 model_print("VarType2 %" PRIu64 ", ", type2);
55 unsigned int tunableSettingHash(TunableSetting *setting) {
56 return setting->hasVar ^ setting->type1 ^ setting->type2 ^ setting->param;
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;
66 ostream &operator<<(ostream &os, const TunableSetting &ts)
68 os << ts.hasVar << " " << ts.type1 << " " << ts.type2 << " " << ts.param << " " << ts.lowValue << " "
69 << ts.highValue << " " << ts.defaultValue << " " << ts.selectedValue;
74 SearchTuner::SearchTuner() {
77 SearchTuner::SearchTuner(const char *filename) {
79 myfile.open (filename, ios::in);
80 if (myfile.is_open()) {
89 while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
90 TunableSetting *setting;
93 setting = new TunableSetting(type1, type2, param);
95 setting = new TunableSetting(param);
97 setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
98 settings.add(setting);
99 usedSettings.add(setting);
103 model_print("Warning: Tuner couldn't be loaded ... Using default tuner instead ....\n");
107 void SearchTuner::addUsed(const char *filename) {
109 myfile.open (filename, ios::in);
110 if (myfile.is_open()) {
119 while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
120 TunableSetting *setting;
123 setting = new TunableSetting(type1, type2, param);
125 setting = new TunableSetting(param);
127 setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
128 if (!settings.contains(setting)) {
129 settings.add(setting);
130 usedSettings.add(setting);
132 TunableSetting *tmp = settings.get(setting);
133 settings.remove(tmp);
134 usedSettings.remove(tmp);
136 settings.add(setting);
137 usedSettings.add(setting);
144 SearchTuner *SearchTuner::copyUsed() {
145 SearchTuner *tuner = new SearchTuner();
146 SetIteratorTunableSetting *iterator = usedSettings.iterator();
147 while (iterator->hasNext()) {
148 TunableSetting *setting = iterator->next();
149 TunableSetting *copy = new TunableSetting(setting);
150 tuner->settings.add(copy);
156 SearchTuner::~SearchTuner() {
157 SetIteratorTunableSetting *iterator = settings.iterator();
158 while (iterator->hasNext()) {
159 TunableSetting *setting = iterator->next();
165 void SearchTuner::setTunable(TunableParam param, TunableDesc *descriptor, uint value) {
166 TunableSetting *result = new TunableSetting(param);
167 result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
168 settings.add(result);
169 usedSettings.add(result);
172 void SearchTuner::setVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor, uint value) {
173 setVarTunable(vartype, 0, param, descriptor, value);
176 void SearchTuner::setVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor, uint value) {
177 TunableSetting *result = new TunableSetting(vartype1, vartype2, param);
178 result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
179 settings.add(result);
180 usedSettings.add(result);
183 int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) {
184 TunableSetting setting(param);
185 TunableSetting *result = usedSettings.get(&setting);
186 if (result == NULL) {
187 result = settings.get(&setting);
188 if ( result == NULL) {
189 result = new TunableSetting(param);
190 uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
191 result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
192 settings.add(result);
194 usedSettings.add(result);
196 return result->selectedValue;
199 int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) {
200 return getVarTunable(vartype, 0, param, descriptor);
203 int SearchTuner::getVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor) {
204 TunableSetting setting(vartype1, vartype2, param);
205 TunableSetting *result = usedSettings.get(&setting);
206 if (result == NULL) {
207 result = settings.get(&setting);
208 if ( result == NULL) {
210 TunableSetting(vartype1, vartype2, param);
211 uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
212 result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
213 settings.add(result);
215 usedSettings.add(result);
217 return result->selectedValue;
220 void SearchTuner::randomMutate() {
221 TunableSetting *randomSetting = settings.getRandomElement();
222 int range = randomSetting->highValue - randomSetting->lowValue;
223 int randomchoice = (random() % range) + randomSetting->lowValue;
224 if (randomchoice < randomSetting->selectedValue)
225 randomSetting->selectedValue = randomchoice;
227 randomSetting->selectedValue = randomchoice + 1;
228 model_print("&&&&&&&&Mutating&&&&&&&\n");
229 randomSetting->print();
230 model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
233 void SearchTuner::print() {
234 SetIteratorTunableSetting *iterator = settings.iterator();
235 while (iterator->hasNext()) {
236 TunableSetting *setting = iterator->next();
243 void SearchTuner::serialize(const char *filename) {
245 myfile.open (filename, ios::out | ios::trunc);
246 SetIteratorTunableSetting *iterator = settings.iterator();
247 while (iterator->hasNext()) {
248 TunableSetting *setting = iterator->next();
249 myfile << *setting << endl;
255 void SearchTuner::serializeUsed(const char *filename) {
257 myfile.open (filename, ios::out | ios::trunc);
258 SetIteratorTunableSetting *iterator = usedSettings.iterator();
259 while (iterator->hasNext()) {
260 TunableSetting *setting = iterator->next();
261 myfile << *setting << endl;
267 void SearchTuner::printUsed() {
268 SetIteratorTunableSetting *iterator = usedSettings.iterator();
269 while (iterator->hasNext()) {
270 TunableSetting *setting = iterator->next();