Adding a directed search based config for the tuner
[satune.git] / src / Tuner / searchtuner.cc
index c1195977f2ab618fea8e6d0b7267468f38d82079..1aef3fcd566a4adf28e201aac1fe1ec91d19e7af 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,7 +63,43 @@ 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() {
+#ifdef STATICENCGEN
+        graphEncoding =false;
+        naiveEncoding = ELEM_UNASSIGNED;
+#endif
+       ifstream myfile;
+       myfile.open (TUNEFILE, 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);
+                       usedSettings.add(setting);
+               }
+               myfile.close();
+       }
 }
 
 SearchTuner *SearchTuner::copyUsed() {
@@ -70,6 +110,12 @@ SearchTuner *SearchTuner::copyUsed() {
                TunableSetting *copy = new TunableSetting(setting);
                tuner->settings.add(copy);
        }
+#ifdef STATICENCGEN
+       if(naiveEncoding != ELEM_UNASSIGNED){
+               tuner->graphEncoding = graphEncoding;
+               tuner->naiveEncoding = naiveEncoding;
+       }
+#endif
        delete iterator;
        return tuner;
 }
@@ -128,7 +174,54 @@ void SearchTuner::randomMutate() {
                randomSetting->selectedValue = randomchoice;
        else
                randomSetting->selectedValue = randomchoice + 1;
+       model_print("&&&&&&&&Mutating&&&&&&&\n");
+       randomSetting->print();
+       model_print("&&&&&&&&&&&&&&&&&&&&&&&\n");
+}
+
+#ifdef STATICENCGEN
+int SearchTuner::nextStaticTuner() {
+       if(naiveEncoding == ELEM_UNASSIGNED){
+               naiveEncoding = ONEHOT;
+               SetIteratorTunableSetting *iter = settings.iterator();
+               while(iter->hasNext()){
+                       TunableSetting *setting = iter->next();
+                       if (setting->param == NAIVEENCODER){
+                               setting->selectedValue = ONEHOT;
+                       } else if(setting->param == ENCODINGGRAPHOPT){
+                               setting->selectedValue = false;
+                       }
+               }
+               delete iter;
+               return EXIT_FAILURE;
+       }
+       int result=EXIT_FAILURE;
+       if(naiveEncoding == BINARYINDEX && graphEncoding){
+               model_print("Best tuner\n");
+               return EXIT_SUCCESS;
+       }else if (naiveEncoding == BINARYINDEX && !graphEncoding){
+               naiveEncoding = ONEHOT;
+               graphEncoding = true;
+       }else {
+               naiveEncoding = (ElementEncodingType)((int)naiveEncoding + 1);
+       }
+       SetIteratorTunableSetting *iter = settings.iterator();
+       uint count = 0;
+       while(iter->hasNext()){
+               TunableSetting * setting = iter->next();
+               if (setting->param == NAIVEENCODER){
+                       setting->selectedValue = naiveEncoding;
+                       count++;
+               } else if(setting->param == ENCODINGGRAPHOPT){
+                       setting->selectedValue = graphEncoding;
+                       count++;
+               }
+       }
+       model_print("Mutating %u settings\n", count);
+       delete iter;
+       return result;
 }
+#endif
 
 void SearchTuner::print() {
        SetIteratorTunableSetting *iterator = settings.iterator();
@@ -140,6 +233,18 @@ void SearchTuner::print() {
 
 }
 
+void SearchTuner::serialize() {
+       ofstream myfile;
+       myfile.open (TUNEFILE, ios::out | ios::trunc);
+       SetIteratorTunableSetting *iterator = settings.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()) {