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 usedSettings.add(setting);
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);
116 SearchTuner::~SearchTuner() {
117 SetIteratorTunableSetting *iterator = settings.iterator();
118 while (iterator->hasNext()) {
119 TunableSetting *setting = iterator->next();
125 void SearchTuner::setTunable(TunableParam param, TunableDesc *descriptor, uint value) {
126 TunableSetting *result = new TunableSetting(param);
127 result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
128 settings.add(result);
129 usedSettings.add(result);
132 void SearchTuner::setVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor, uint value) {
133 setVarTunable(vartype, 0, param, descriptor, value);
136 void SearchTuner::setVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor, uint value) {
137 TunableSetting *result = new TunableSetting(vartype1, vartype2, param);
138 result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
139 settings.add(result);
140 usedSettings.add(result);
143 int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) {
144 TunableSetting setting(param);
145 TunableSetting *result = usedSettings.get(&setting);
146 if (result == NULL) {
147 result = settings.get(&setting);
148 if ( result == NULL) {
149 result = new TunableSetting(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);
154 usedSettings.add(result);
156 return result->selectedValue;
159 int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) {
160 return getVarTunable(vartype, 0, param, descriptor);
163 int SearchTuner::getVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor) {
164 TunableSetting setting(vartype1, vartype2, param);
165 TunableSetting *result = usedSettings.get(&setting);
166 if (result == NULL) {
167 result = settings.get(&setting);
168 if ( result == NULL) {
170 TunableSetting(vartype1, vartype2, param);
171 uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
172 result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
173 settings.add(result);
175 usedSettings.add(result);
177 return result->selectedValue;
180 void SearchTuner::randomMutate() {
181 TunableSetting *randomSetting = settings.getRandomElement();
182 int range = randomSetting->highValue - randomSetting->lowValue;
183 int randomchoice = (random() % range) + randomSetting->lowValue;
184 if (randomchoice < randomSetting->selectedValue)
185 randomSetting->selectedValue = randomchoice;
187 randomSetting->selectedValue = randomchoice + 1;
188 model_print("&&&&&&&&Mutating&&&&&&&\n");
189 randomSetting->print();
190 model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
193 void SearchTuner::print() {
194 SetIteratorTunableSetting *iterator = settings.iterator();
195 while (iterator->hasNext()) {
196 TunableSetting *setting = iterator->next();
203 void SearchTuner::serialize(const char *filename) {
205 myfile.open (filename, ios::out | ios::trunc);
206 SetIteratorTunableSetting *iterator = settings.iterator();
207 while (iterator->hasNext()) {
208 TunableSetting *setting = iterator->next();
209 myfile << *setting << endl;
215 void SearchTuner::printUsed() {
216 SetIteratorTunableSetting *iterator = usedSettings.iterator();
217 while (iterator->hasNext()) {
218 TunableSetting *setting = iterator->next();