from templates import * from bs4 import BeautifulSoup def get_opposs_dir(dir): if dir == DIRS.LOCAL: return DIRS.LOCAL elif dir == DIRS.EAST: return DIRS.WEST elif dir == DIRS.WEST: return DIRS.EAST elif dir == DIRS.NORTH: return DIRS.SOUTH elif dir == DIRS.SOUTH: return DIRS.NORTH elif dir == DIRS.UP: return DIRS.DOWN elif dir == DIRS.DOWN: return DIRS.UP else: return DIRS.INVALID class ConfigTaskWriter: def __init__(self, sourc, dest, routing_typ, start_task, start_type, #start_conf_type_id, start_pack_type_id, use_spec_task=False, spec_task_id=0): self.sourc = sourc self.dest = dest self.routing_typ = routing_typ self.start_task = start_task self.start_type = start_type #self.start_conf_type_id = start_conf_type_id self.start_pack_type_id = start_pack_type_id self.use_spec_task = use_spec_task self.spec_task_id = spec_task_id self.task_count = 0 self.xml_data = "" self.xml_map = "" self.xml_data_types = "" self.xml_stream_types = "" self.spec_task_count = 0 self.type_count_start = self.start_type self.type_count = self.start_type def write_one_dir_config_tasks(self, x0, xf, dir, prev_dir, last_dir): for x in range(x0, xf): self.xml_data += DEST_HEADER.format(self.task_count) + "\n" self.xml_data += DELAY_TAG + "\n" self.xml_data += INTERVAL_TAG + "\n" self.xml_data += COUNT_TAG + "\n" self.xml_data += TYPE_TAG.format(self.type_count) + "\n" self.xml_data += TASK_TAG.format(self.start_task+self.task_count+1)+"\n" if x == x0 and x == xf-1: link = prev_dir.value dest = last_dir.value elif x == x0: link = prev_dir.value dest = dir.value elif x == xf-1: link = get_opposs_dir(dir).value dest = last_dir.value else: link = get_opposs_dir(dir).value dest = dir.value self.xml_data += CONFIG_TAG.format(link, dest) + "\n" self.xml_data += DEST_H_END + "\n" self.task_count += 1 def write_req_task(self): for i in range(0, self.task_count): self.xml_data += "\n"+REQ_TASK_START.format(self.start_task+i+1)+"\n" self.xml_data += REQ_TASK_REQ.format(self.type_count, self.start_task) + "\n" if(not self.use_spec_task): last_task = self.start_task+(self.dest[0]-self.sourc[0] + \ self.dest[1]-self.sourc[1]) + 2 else: last_task = self.spec_task_id self.xml_data += REQ_TASK_GEN.format(self.start_pack_type_id, last_task) + "\n" self.task_count += 1 def write_last_task(self): self.xml_data += "\n" + LAST_TASK.format(self.spec_task_id, self.start_pack_type_id, self.start_task+self.task_count-1, self.spec_task_count, self.spec_task_count) + "\n" def write_config_tasks(self): self.xml_data+=GEN_TASK_START.format(self.start_task+self.task_count)+"\n" if self.routing_typ == 0: # XY routing dir = DIRS.EAST if self.dest[0] > self.sourc[0] else DIRS.WEST x0 = self.dest[0] if self.sourc[0]>self.dest[0] else self.sourc[0] xf = self.sourc[0] if self.sourc[0]>self.dest[0] else self.dest[0] self.write_one_dir_config_tasks(x0, xf, dir, DIRS.LOCAL, dir) prev_dir = get_opposs_dir(dir) if self.dest[0] != self.sourc[0] \ else DIRS.LOCAL dir = DIRS.NORTH if self.dest[1] > self.sourc[1] else DIRS.SOUTH x0 = self.dest[1] if self.sourc[1]>self.dest[1] else self.sourc[1] xf = self.sourc[1]+1 if self.sourc[1]>self.dest[1] else self.dest[1]+1 self.write_one_dir_config_tasks(x0, xf, dir, prev_dir, DIRS.LOCAL) self.xml_data += GEN_TASK_END + "\n" elif self.routing_typ==1: # YX routing dir = DIRS.NORTH if self.dest[1] > self.sourc[1] else DIRS.SOUTH x0 = self.dest[1] if self.sourc[1]>self.dest[1] else self.sourc[1] xf = self.sourc[1] if self.sourc[1]>self.dest[1] else self.dest[1] self.write_one_dir_config_tasks(x0, xf, dir, DIRS.LOCAL, dir) prev_dir = get_opposs_dir(dir) if self.dest[0] != self.sourc[0] \ else DIRS.LOCAL dir = DIRS.EAST if self.dest[0] > self.sourc[0] else DIRS.WEST x0 = self.dest[0] if self.sourc[0]>self.dest[0] else self.sourc[0] xf = self.sourc[0]+1 if self.sourc[0]>self.dest[0] else self.dest[0]+1 self.write_one_dir_config_tasks(x0, xf, dir, prev_dir, DIRS.LOCAL) self.xml_data += GEN_TASK_END + "\n" self.write_req_task() #last task is for the pc that send config msgs if(not self.use_spec_task): self.xml_data += "\n" + LAST_TASK.format( self.start_task+self.task_count, self.start_task+self.task_count, self.start_task, self.task_count-1, self.task_count-1) + "\n" else: self.spec_task_count += self.task_count - 1 self.type_count += 1 def write_map(self): x = self.sourc[0] y = self.sourc[1] if(not self.use_spec_task): last_map_task = self.task_count+1 else: last_map_task = self.task_count self.xml_map += BIND.format(self.start_task, x*MAX_ROUT_Y+y) + "\n" # first task for i in range (1, last_map_task): self.xml_map += BIND.format(self.start_task+i, x*MAX_ROUT_Y+y) + "\n" if self.routing_typ == 0: if(x == self.dest[0] and y == self.dest[1]): x = self.sourc[0] y = self.sourc[1] elif(x == self.dest[0]): y = y+1 if self.dest[1] > self.sourc[1] else y-1 else: x = x+1 if self.dest[0] > self.sourc[0] else x-1 elif self.routing_typ == 1: if(x == self.dest[0] and y == self.dest[1]): x = self.sourc[0] y = self.sourc[1] elif(y == self.dest[1]): x = x+1 if self.dest[0] > self.sourc[0] else x-1 else: y = y+1 if self.dest[1] > self.sourc[1] else y-1 def write_types(self): for i in range (self.type_count_start, self.type_count): self.xml_data_types += CONF_TYPE.format(i) + "\n" self.xml_data_types += PACK_TYPE.format( self.start_pack_type_id, self.start_pack_type_id) def write_stream_types(self, data_file, map_file): with open(data_file, 'r') as f: data = f.read() with open(map_file, 'r') as f: map_data = f.read() Bs_data = BeautifulSoup(data, "xml") Bs_map_data = BeautifulSoup(map_data, "xml") tasks = Bs_data.find_all('task') binds = Bs_map_data.find_all('bind') for task in tasks: id = task.get('id') if id is not None: dests = task.find_all('destination') if dests: num_dest = len(dests) dest_task = dests[0].find('task').get('value') for bind in binds: if bind.find('task').get('value') == id: sourc_node = bind.find('node').get('value') if bind.find('task').get('value') == dest_task: dest_node = bind.find('node').get('value') if(num_dest == 1 and sourc_node != dest_node): self.xml_stream_types += STREAM_TYPE.format(id, id) + "\n" else: self.xml_stream_types += PACK_TYPE.format(id, id) + "\n" def write_non_opt_stream_types(self, data_file, map_file): with open(data_file, 'r') as f: data = f.read() with open(map_file, 'r') as f: map_data = f.read() Bs_data = BeautifulSoup(data, "xml") Bs_map_data = BeautifulSoup(map_data, "xml") tasks = Bs_data.find_all('task') binds = Bs_map_data.find_all('bind') for task in tasks: id = task.get('id') if id is not None: dests = task.find_all('destination') if dests: num_dest = len(dests) dest_task = dests[0].find('task').get('value') for bind in binds: if bind.find('task').get('value') == id: sourc_node = int(bind.find('node').get('value')) if bind.find('task').get('value') == dest_task: dest_node = int(bind.find('node').get('value')) # use this for mobilenet and efficienet #if(num_dest == 1 and ((sourc_node == 3 and dest_node == 4) or # (sourc_node == 7 and dest_node == 8) or # (sourc_node == 11 and dest_node == 12))): # use this for mobilenet_fb if((sourc_node == 3 and dest_node == 4) or (sourc_node == 7 and dest_node == 8) or (sourc_node == 11 and dest_node == 12)): self.xml_stream_types += STREAM_TYPE.format(id, id) + "\n" else: self.xml_stream_types += PACK_TYPE.format(id, id) + "\n" def update_sourc_dest(self, sourc, dest): self.sourc = sourc self.dest = dest if(not self.use_spec_task): self.start_task += self.task_count + 1 self.start_type += self.task_count + 1 else: self.start_task += self.task_count self.start_type += self.task_count self.task_count = 0 def write_to_file(self): f = open("./data.txt", "w") f.write("") f.close() f = open("./data.txt", "a") f.write(self.xml_data) f.close() f = open("./map.txt", "w") f.write("") f.close() f = open("./map.txt", "a") f.write(self.xml_map) f.close() f = open("./data_types.txt", "w") f.write("") f.close() f = open("./data_types.txt", "a") f.write(self.xml_data_types) f.close() f = open("./stream_types.txt", "w") f.write("") f.close() f = open("./stream_types.txt", "a") f.write(self.xml_stream_types) f.close()