-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConfGenBase.py
150 lines (128 loc) · 5.36 KB
/
ConfGenBase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# This file is part of GenMap and released under the MIT License, see LICENSE.
# Author: Takuya Kojima
from GenMapShell import GenMapShell
from ConfDrawer import ConfDrawer
from Individual import Individual
from Application import Application
from PEArrayModel import PEArrayModel
from SimParameters import SimParameters
from abc import ABCMeta, abstractmethod
from argparse import ArgumentParser
from deap import base
from deap import creator
import pickle
import os
class ConfGenBase(metaclass=ABCMeta):
def __init__(self):
self.style_types = {}
self.style_choices = {}
self.style_default = {}
def style_args_parser(self, args):
"""argument parser for style option
Args:
args (list): list of arguments
Returns:
dict or None: without any error, it returns parsed arguments as dict
An option with an argument is given as "optionanme=argment"
To set option name and it default value,
override style_types (dict), style_choices, and style_default (dict)
style_types:
key: option name
value: type of the value
If the type is boolean, the option is treated as flag option
Otherwise, the option needs one argument.
It type of the argument is different from the default value,
it occurs error
style_choise:
key: option name
value: list of choises
If an option name is not contained in this dict,
the option allows any values
style_default:
key: option name
value: default value
"""
# init by default setting
parsed_args = {k:v for k,v in self.style_default.items()}
# parse
for v in args:
temp = v.split("=")
if (len(temp)) == 1:
if v in self.style_types.keys():
if self.style_types[v] == bool:
parsed_args[v] = True
else:
print("-s: {0} is not flag option".format(v))
return None
else:
print("-s: unknown option {0}".format(v))
return None
elif (len(temp) == 2):
if temp[0] in self.style_types.keys():
if type(temp[1]) == self.style_types[temp[0]]:
if temp[0] in self.style_choices.keys():
if not temp[1] in self.style_choices[temp[0]]:
print("-s: arguemnt \"{1}\" of {0} is not allowed.\nSelect from {2}".format(\
temp[0], temp[1], self.style_choices[temp[0]]))
return None
parsed_args[temp[0]] = temp[1]
else:
if self.style_types[temp[0]] == bool:
print("-s: {0} option is flag option, no need of arguement".format(temp[0]))
else:
print("-s: argument \"{1}\" of {0} is not {2}".format(temp[0], temp[1], \
self.style_types[temp[0]]))
return None
else:
print("-s: unknown option {0}".format(v))
return None
else:
print("-s: invalid argument", v)
return None
return parsed_args
def main(self):
args = self.parser()
filename = args.result
if not os.path.exists(filename):
print(filename, " does not exist")
return
with open(filename, "rb") as f:
# load header
header = pickle.load(f)
# prepare for loading result data
creator.create("Fitness", base.Fitness, weights=header["fitness_weights"])
creator.create("Individual", Individual, fitness=creator.Fitness)
# load result
data = pickle.load(f)
shell = GenMapShell(header, data, self)
# start Shell
while (1):
try:
shell.cmdloop()
break
except KeyboardInterrupt:
shell.intro = ""
print()
continue
def parser(self):
usage = 'Usage: python3 {0} optimization_result'.format(self.__class__.__name__ + ".py")
argparser = ArgumentParser(usage=usage)
argparser.add_argument("result", type=str, help="optimization result")
args = argparser.parse_args()
return args
@abstractmethod
def generate(self, header, data, individual_id, args):
"""Generates configuration data for the target architecture
Args:
header (dict) : header of dumpfile
data (dict) : data of dumpfile
individual_id (int) : selected solution ID to be generated
args (list) : options from command line
Raises:
TypeError:
The dumpfile is incompatible for the target archtecture
"""
pass
if __name__ == '__main__':
generator = ConfGenBase()
generator.main()