X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=src%2FTuner%2Fsearchtuner.cc;h=9ab6d45d8821318d7726a371f5ebb4acf85f7f6c;hb=b0f6ab9a232beae1ddad1ec0a38e040e2d6187cb;hp=52c7a704896d70ea2a4a985db05dfd4ad3ea6cf6;hpb=d46ee65a6767e2016cab629220a60c3e39b366f1;p=satune.git diff --git a/src/Tuner/searchtuner.cc b/src/Tuner/searchtuner.cc index 52c7a70..9ab6d45 100644 --- a/src/Tuner/searchtuner.cc +++ b/src/Tuner/searchtuner.cc @@ -1,20 +1,33 @@ #include "searchtuner.h" +#include +#include +using namespace std; TunableSetting::TunableSetting(VarType _type, TunableParam _param) : hasVar(true), - type(_type), + type1(_type), + type2(0), + param(_param) { +} + +TunableSetting::TunableSetting(VarType _type1, VarType _type2, TunableParam _param) : + hasVar(true), + type1(_type1), + type2(_type2), param(_param) { } TunableSetting::TunableSetting(TunableParam _param) : hasVar(false), - type(0), + type1(0), + type2(0), param(_param) { } TunableSetting::TunableSetting(TunableSetting *ts) : hasVar(ts->hasVar), - type(ts->type), + type1(ts->type1), + type2(ts->type2), param(ts->param), lowValue(ts->lowValue), highValue(ts->highValue), @@ -31,25 +44,135 @@ void TunableSetting::setDecision(int _low, int _high, int _default, int _selecti } void TunableSetting::print() { + model_print("Param %s = %u \t range=[%u,%u]", tunableParameterToString( (Tunables)param), selectedValue, lowValue, highValue); if (hasVar) { - model_print("Type %" PRIu64 ", ", type); + model_print("\tVarType1 %" PRIu64 ", ", type1); + model_print("VarType2 %" PRIu64 ", ", type2); } - model_print("Param %u = %u\n", param, selectedValue); + model_print("\n"); } -unsigned int tunableSettingHash(TunableSetting *setting) { - return setting->hasVar ^ setting->type ^ setting->param; -} -bool tunableSettingEquals(TunableSetting *setting1, TunableSetting *setting2) { - return setting1->hasVar == setting2->hasVar && - setting1->type == setting2->type && - setting1->param == setting2->param; +ostream &operator<<(ostream &os, const TunableSetting &ts) +{ + os << ts.hasVar << " " << ts.type1 << " " << ts.type2 << " " << ts.param << " " << ts.lowValue << " " + << ts.highValue << " " << ts.defaultValue << " " << ts.selectedValue; + return os; } + SearchTuner::SearchTuner() { } +SearchTuner::SearchTuner(const char *filename, bool addused) { + ifstream myfile; + myfile.open (filename, ios::in); + if (myfile.is_open()) { + bool hasVar; + VarType type1; + VarType type2; + TunableParam param; + int lowValue; + int highValue; + int defaultValue; + int selectedValue; + while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) { + TunableSetting *setting; + + if (hasVar) { + setting = new TunableSetting(type1, type2, param); + } else { + setting = new TunableSetting(param); + } + setting->setDecision(lowValue, highValue, defaultValue, selectedValue); + settings.add(setting); + if (addused) { + usedSettings.add(setting); + } + } + myfile.close(); + } else { + model_print("Warning: Tuner %s couldn't be loaded ... Using default tuner instead ....\n", filename); + } +} + +bool SearchTuner::equalUsed(SearchTuner *tuner) { + if (tuner->usedSettings.getSize() != usedSettings.getSize()) { + return false; + } + bool result = true; + SetIteratorTunableSetting *iterator = usedSettings.iterator(); + while (iterator->hasNext()) { + TunableSetting *setting = iterator->next(); + if (!tuner->usedSettings.contains(setting)) { + result = false; + break; + } else { + TunableSetting *tunerSetting = tuner->usedSettings.get(setting); + if (tunerSetting->selectedValue != setting->selectedValue) { + result = false; + break; + } + } + } + delete iterator; + return result; +} + +void SearchTuner::addUsed(const char *filename) { + ifstream myfile; + myfile.open (filename, ios::in); + if (myfile.is_open()) { + bool hasVar; + VarType type1; + VarType type2; + TunableParam param; + int lowValue; + int highValue; + int defaultValue; + int selectedValue; + while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) { + TunableSetting *setting; + + if (hasVar) { + setting = new TunableSetting(type1, type2, param); + } else { + setting = new TunableSetting(param); + } + setting->setDecision(lowValue, highValue, defaultValue, selectedValue); + if (!settings.contains(setting)) { + settings.add(setting); + usedSettings.add(setting); + } else { + TunableSetting *tmp = settings.get(setting); + settings.remove(tmp); + usedSettings.remove(tmp); + delete tmp; + settings.add(setting); + usedSettings.add(setting); + } + } + myfile.close(); + } +} + +bool SearchTuner::isSubTunerof(SearchTuner *newTuner) { + SetIteratorTunableSetting *iterator = usedSettings.iterator(); + while (iterator->hasNext()) { + TunableSetting *setting = iterator->next(); + if (!newTuner->settings.contains(setting)) { + return false; + } else { + TunableSetting *newSetting = newTuner->settings.get(setting); + if (newSetting->selectedValue != setting->selectedValue) { + return false; + } + } + } + delete iterator; + return true; +} + SearchTuner *SearchTuner::copyUsed() { SearchTuner *tuner = new SearchTuner(); SetIteratorTunableSetting *iterator = usedSettings.iterator(); @@ -71,6 +194,24 @@ SearchTuner::~SearchTuner() { delete iterator; } +void SearchTuner::setTunable(TunableParam param, TunableDesc *descriptor, uint value) { + TunableSetting *result = new TunableSetting(param); + result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value); + settings.add(result); + usedSettings.add(result); +} + +void SearchTuner::setVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor, uint value) { + setVarTunable(vartype, 0, param, descriptor, value); +} + +void SearchTuner::setVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor, uint value) { + TunableSetting *result = new TunableSetting(vartype1, vartype2, param); + result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value); + settings.add(result); + usedSettings.add(result); +} + int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) { TunableSetting setting(param); TunableSetting *result = usedSettings.get(&setting); @@ -88,12 +229,17 @@ int SearchTuner::getTunable(TunableParam param, TunableDesc *descriptor) { } int SearchTuner::getVarTunable(VarType vartype, TunableParam param, TunableDesc *descriptor) { - TunableSetting setting(vartype, param); + return getVarTunable(vartype, 0, param, descriptor); +} + +int SearchTuner::getVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor) { + TunableSetting setting(vartype1, vartype2, param); TunableSetting *result = usedSettings.get(&setting); if (result == NULL) { result = settings.get(&setting); if ( result == NULL) { - result = new TunableSetting(vartype, param); + result = new + TunableSetting(vartype1, vartype2, param); uint value = descriptor->lowValue + (random() % (1 + descriptor->highValue - descriptor->lowValue)); result->setDecision(descriptor->lowValue, descriptor->highValue, descriptor->defaultValue, value); settings.add(result); @@ -111,6 +257,9 @@ void SearchTuner::randomMutate() { randomSetting->selectedValue = randomchoice; else randomSetting->selectedValue = randomchoice + 1; + model_print("&&&&&&&&Mutating&&&&&&&\n"); + randomSetting->print(); + model_print("&&&&&&&&&&&&&&&&&&&&&&&\n"); } void SearchTuner::print() { @@ -123,6 +272,30 @@ void SearchTuner::print() { } +void SearchTuner::serialize(const char *filename) { + ofstream myfile; + myfile.open (filename, ios::out | ios::trunc); + SetIteratorTunableSetting *iterator = settings.iterator(); + while (iterator->hasNext()) { + TunableSetting *setting = iterator->next(); + myfile << *setting << endl; + } + myfile.close(); + delete iterator; +} + +void SearchTuner::serializeUsed(const char *filename) { + ofstream myfile; + myfile.open (filename, ios::out | ios::trunc); + SetIteratorTunableSetting *iterator = usedSettings.iterator(); + while (iterator->hasNext()) { + TunableSetting *setting = iterator->next(); + myfile << *setting << endl; + } + myfile.close(); + delete iterator; +} + void SearchTuner::printUsed() { SetIteratorTunableSetting *iterator = usedSettings.iterator(); while (iterator->hasNext()) {