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() {
48 model_print("VarType1 %" PRIu64 ", ", type1);
49 model_print("VarType2 %" PRIu64 ", ", type2);
51 model_print("Param %s = %u \t range=[%u,%u]\n", tunableParameterToString( (Tunables)param), selectedValue, lowValue, highValue);
54 unsigned int tunableSettingHash(TunableSetting *setting) {
55 return setting->hasVar ^ setting->type1 ^ setting->type2 ^ setting->param;
58 bool tunableSettingEquals(TunableSetting *setting1, TunableSetting *setting2) {
59 return setting1->hasVar == setting2->hasVar &&
60 setting1->type1 == setting2->type1 &&
61 setting1->type2 == setting2->type2 &&
62 setting1->param == setting2->param;
65 ostream &operator<<(ostream &os, const TunableSetting &ts)
67 os << ts.hasVar << " " << ts.type1 << " " << ts.type2 << " " << ts.param << " " << ts.lowValue << " "
68 << ts.highValue << " " << ts.defaultValue << " " << ts.selectedValue;
73 SearchTuner::SearchTuner() {
75 myfile.open (TUNEFILE, ios::in);
76 if (myfile.is_open()) {
85 while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
86 TunableSetting *setting;
89 setting = new TunableSetting(type1, type2, param);
91 setting = new TunableSetting(param);
93 setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
94 usedSettings.add(setting);
100 SearchTuner *SearchTuner::copyUsed() {
101 SearchTuner *tuner = new SearchTuner();
102 SetIteratorTunableSetting *iterator = usedSettings.iterator();
103 while (iterator->hasNext()) {
104 TunableSetting *setting = iterator->next();
105 TunableSetting *copy = new TunableSetting(setting);
106 tuner->settings.add(copy);
112 SearchTuner::~SearchTuner() {
113 SetIteratorTunableSetting *iterator = settings.iterator();
114 while (iterator->hasNext()) {
115 TunableSetting *setting = iterator->next();
121 int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) {
122 TunableSetting setting(param);
123 TunableSetting *result = usedSettings.get(&setting);
124 if (result == NULL) {
125 result = settings.get(&setting);
126 if ( result == NULL) {
127 result = new TunableSetting(param);
128 uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
129 result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
130 settings.add(result);
132 usedSettings.add(result);
134 return result->selectedValue;
137 int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) {
138 return getVarTunable(vartype, 0, param, descriptor);
141 int SearchTuner::getVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor) {
142 TunableSetting setting(vartype1, vartype2, param);
143 TunableSetting *result = usedSettings.get(&setting);
144 if (result == NULL) {
145 result = settings.get(&setting);
146 if ( result == NULL) {
148 TunableSetting(vartype1, vartype2, param);
149 uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue));
150 result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value);
151 settings.add(result);
153 usedSettings.add(result);
155 return result->selectedValue;
158 void SearchTuner::randomMutate() {
159 TunableSetting *randomSetting = settings.getRandomElement();
160 int range = randomSetting->highValue - randomSetting->lowValue;
161 int randomchoice = (random() % range) + randomSetting->lowValue;
162 if (randomchoice < randomSetting->selectedValue)
163 randomSetting->selectedValue = randomchoice;
165 randomSetting->selectedValue = randomchoice + 1;
166 model_print("&&&&&&&&Mutating&&&&&&&\n");
167 randomSetting->print();
168 model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
171 void SearchTuner::print() {
172 SetIteratorTunableSetting *iterator = settings.iterator();
173 while (iterator->hasNext()) {
174 TunableSetting *setting = iterator->next();
181 void SearchTuner::serialize() {
183 myfile.open (TUNEFILE, ios::out | ios::trunc);
184 SetIteratorTunableSetting *iterator = settings.iterator();
185 while (iterator->hasNext()) {
186 TunableSetting *setting = iterator->next();
187 myfile << *setting << endl;
193 void SearchTuner::printUsed() {
194 SetIteratorTunableSetting *iterator = usedSettings.iterator();
195 while (iterator->hasNext()) {
196 TunableSetting *setting = iterator->next();