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 naiveEncoding = ELEM_UNASSIGNED;
80 myfile.open (TUNEFILE, ios::in);
81 if (myfile.is_open()) {
90 while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
91 TunableSetting *setting;
94 setting = new TunableSetting(type1, type2, param);
96 setting = new TunableSetting(param);
98 setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
99 usedSettings.add(setting);
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);
114 if(naiveEncoding != ELEM_UNASSIGNED){
115 tuner->graphEncoding = graphEncoding;
116 tuner->naiveEncoding = naiveEncoding;
123 SearchTuner::~SearchTuner() {
124 SetIteratorTunableSetting *iterator = settings.iterator();
125 while (iterator->hasNext()) {
126 TunableSetting *setting = iterator->next();
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);
143 usedSettings.add(result);
145 return result->selectedValue;
148 int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) {
149 return getVarTunable(vartype, 0, param, descriptor);
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) {
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);
164 usedSettings.add(result);
166 return result->selectedValue;
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;
176 randomSetting->selectedValue = randomchoice + 1;
177 model_print("&&&&&&&&Mutating&&&&&&&\n");
178 randomSetting->print();
179 model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
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;
198 int result=EXIT_FAILURE;
199 if(naiveEncoding == BINARYINDEX && graphEncoding){
200 model_print("Best tuner\n");
202 }else if (naiveEncoding == BINARYINDEX && !graphEncoding){
203 naiveEncoding = ONEHOT;
204 graphEncoding = true;
206 naiveEncoding = (ElementEncodingType)((int)naiveEncoding + 1);
208 SetIteratorTunableSetting *iter = settings.iterator();
210 while(iter->hasNext()){
211 TunableSetting * setting = iter->next();
212 if (setting->param == NAIVEENCODER){
213 setting->selectedValue = naiveEncoding;
215 } else if(setting->param == ENCODINGGRAPHOPT){
216 setting->selectedValue = graphEncoding;
220 model_print("Mutating %u settings\n", count);
226 void SearchTuner::print() {
227 SetIteratorTunableSetting *iterator = settings.iterator();
228 while (iterator->hasNext()) {
229 TunableSetting *setting = iterator->next();
236 void SearchTuner::serialize() {
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;
248 void SearchTuner::printUsed() {
249 SetIteratorTunableSetting *iterator = usedSettings.iterator();
250 while (iterator->hasNext()) {
251 TunableSetting *setting = iterator->next();