118cb821ab203af6b383754162b2b5c959e68a07
[satune.git] / src / Tuner / serializetuner.cc
1 #include "serializetuner.h"
2 #include <iostream>
3 #include <fstream>
4 using namespace std;
5
6 SerializeTuner::SerializeTuner(const char *filename) {
7         ifstream myfile;
8         myfile.open (filename, ios::in);
9         if (myfile.is_open()) {
10                 bool hasVar;
11                 VarType type1;
12                 VarType type2;
13                 TunableParam param;
14                 int lowValue;
15                 int highValue;
16                 int defaultValue;
17                 int selectedValue;
18                 while (myfile >> hasVar >> type1 >> type2 >> param >> lowValue >> highValue >> defaultValue >> selectedValue) {
19                         TunableSetting *setting;
20
21                         if (hasVar) {
22                                 setting = new TunableSetting(type1, type2, param);
23                         } else {
24                                 setting = new TunableSetting(param);
25                         }
26                         setting->setDecision(lowValue, highValue, defaultValue, selectedValue);
27                         settings.add(setting);
28                         usedSettings.add(setting);
29                 }
30                 myfile.close();
31         } else {
32                 model_print("Error: Tuner %s cannot be found ...\n", filename);
33                 exit(1);
34         }
35 }
36
37 /*SearchTuner::~SearchTuner() {
38    SetIteratorTunableSetting *iterator = settings.iterator();
39    while (iterator->hasNext()) {
40     TunableSetting *setting = iterator->next();
41     delete setting;
42    }
43    delete iterator;
44    }*/
45
46
47 int SerializeTuner::getTunable(TunableParam param, TunableDesc *descriptor) {
48         TunableSetting setting(param);
49         TunableSetting *result = usedSettings.get(&setting);
50         if (result == NULL) {
51                 result = settings.get(&setting);
52                 if ( result == NULL) {
53                         return descriptor->defaultValue;
54                 }
55                 usedSettings.add(result);
56         }
57         return result->selectedValue;
58 }
59
60 int SerializeTuner::getVarTunable(VarType vartype1, VarType vartype2, TunableParam param, TunableDesc *descriptor) {
61         TunableSetting setting(vartype1, vartype2, param);
62         TunableSetting *result = usedSettings.get(&setting);
63         if (result == NULL) {
64                 result = settings.get(&setting);
65                 if ( result == NULL) {
66                         return descriptor->defaultValue;
67                 }
68                 usedSettings.add(result);
69         }
70         return result->selectedValue;
71 }