fix up serialization a bit more
[satune.git] / src / Tuner / searchtuner.cc
index c1195977f2ab618fea8e6d0b7267468f38d82079..f4f4cfcd00c2912179958c4827fab126e8cf8be3 100644 (file)
@@ -1,4 +1,7 @@
 #include "searchtuner.h"
+#include <iostream>
+#include <fstream>
+using namespace std;
 
 TunableSetting::TunableSetting(VarType _type, TunableParam _param) :
        hasVar(true),
@@ -41,11 +44,12 @@ 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("Type1 %" PRIu64 ", ", type1);
-               model_print("Type2 %" PRIu64 ", ", type2);
+               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) {
@@ -59,9 +63,82 @@ bool tunableSettingEquals(TunableSetting *setting1, TunableSetting *setting2) {
                                 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) {
+       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);
+                       usedSettings.add(setting);
+               }
+               myfile.close();
+       }
+}
+
+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();
+       }
+}
+
 SearchTuner *SearchTuner::copyUsed() {
        SearchTuner *tuner = new SearchTuner();
        SetIteratorTunableSetting *iterator = usedSettings.iterator();
@@ -83,6 +160,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);
@@ -128,6 +223,9 @@ void SearchTuner::randomMutate() {
                randomSetting->selectedValue = randomchoice;
        else
                randomSetting->selectedValue = randomchoice + 1;
+       model_print("&&&&&&&&Mutating&&&&&&&\n");
+       randomSetting->print();
+       model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
 }
 
 void SearchTuner::print() {
@@ -140,6 +238,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()) {