def print_matrix(matrix): for a in matrix: ln = "" for i in a: ln += str(i) + " " # print(ln) logger.debug(ln)
def all_path(adj_matrix): n = len(adj_matrix) paths = [] DFS(adj_matrix, n - 2, '', paths) for p in paths: logger.debug(p) return paths
def reorder_modules(self, modules, topo_order): # print(modules) logger.debug('modules : %s'.format(modules)) r = [] for t in topo_order: if t in modules: r.append(t) return r
def schedule_queue(self, v_tag, topo_order, anc_matrix): queue = [] modules = [] for i in range(len(v_tag)): if v_tag[i] == 1: modules.append(i) for i in range(self.core_num): queue.append([]) ms = self.reorder_modules(modules, topo_order) ancs = {} for i in ms: ancs.update({i:[]}) for j in ms: if anc_matrix[j][i] == 1: ancs[i].append(j) # print(ancs) logger.debug('ancs : %s'.format(ancs)) un_scheduled = ms list = [] core = [] for i in range(self.core_num): core.append(i) i = 0 while len(un_scheduled) != 0: if i % self.core_num == 0: for v in list: un_scheduled.remove(v) list = [] for un_v in un_scheduled: if len(list) == self.core_num: break # completion_queue(queue) # sq = shortest_queue(queue) # for s in sq: # queue[s].append(0) if self.check_anc_scheduled(ancs, un_v, un_scheduled): list.append(un_v) queue[core[i % self.core_num]].append(un_v) i += 1 if i % self.core_num != 0 and len(un_scheduled) != 0: self.completion_queue(queue) while i % self.core_num != 0: i += 1 # for i in range(len(ms)): # for un_v in un_scheduled: # if # for i in range(len(ms)): # queue[i % CORE_NUM].append(ms[i]) # print('queue : {0}'.format(queue)) logger.debug('queue : {0}'.format(queue)) return queue
def randomly_choice_a_path(adj_matrix, i, path): # path = path + str(i) path.append(i) logger.debug(path) if outdere(adj_matrix, i) == 0: return path v = adj_matrix[i][:] l = [] for j in range(len(v)): logger.debug(v[j]) if v[j] != 0: l.append(j) n = random.choice(l) return randomly_choice_a_path(adj_matrix, n, path)
def generate_DAG(n): node = list(range(1, n + 1)) sl(node) sl(node) m = rd(1, (n - 1) * n / 2) adj_matrix = [([0] * (n + 2)) for i in range(n + 2)] # lines = '' ln = 0 # for i in range(0, m): for i in range(0, min(10, m)): p1 = rd(1, n - 1) p2 = rd(p1 + 1, n) x = node[p1 - 1] y = node[p2 - 1] # print '{},{}'.format(x-1, y - 1) if adj_matrix[x - 1][y - 1] == 0: adj_matrix[x - 1][y - 1] = gauss_random(EDGE_WEIGHT_MEAN, EDGE_WEIGHT_VAR) ln += 1 # if lines.find('{0},{1}'.format(x, y)) == - 1: # ln = ln + 1 # lines = lines + '{0},{1};'.format(x, y) for i in range(0, n): # 出度为0的节点,需要连接到结束节点 if outdere(adj_matrix, i) == 0: adj_matrix[i][n + 1] = gauss_random(EDGE_WEIGHT_MEAN, EDGE_WEIGHT_VAR) w_vex = [] for i in range(0, n): # 头节点连向入度为0的节点 if indere(adj_matrix, i) == 0: adj_matrix[n][i] = gauss_random(EDGE_WEIGHT_MEAN, EDGE_WEIGHT_VAR) # w_vex.append(gauss_random(VEX_WEIGHT_MEAN, VEX_WEIGHT_VAR)) logger.debug('{0} nodes, {1} edges'.format(n, ln)) # logger.debug('lines : ' + lines) for i in range(n + 2): w_vex.append(gauss_random(VEX_WEIGHT_MEAN, VEX_WEIGHT_VAR)) # topo_order = generate_topo_order(node) return adj_matrix, w_vex, generate_topo_order(node)
def T(self, adj_matrix, path, entry_task, exit_task, w_vex, v_tag): if entry_task == -1 and exit_task == -1: entry_task = len(path) - 1 exit_task = len(path) - 1 T_local = 0 T_local += local_T(path, 1, entry_task, w_vex) T_local += local_T(path, exit_task+1, len(path) - 1, w_vex) T_cloud = 0 if entry_task == len(path) - 1 and exit_task == len(path) - 1: T_cloud += 0 else: T_cloud += self.cloud.T(path, entry_task, exit_task, w_vex) T_trans = self.trans_time(adj_matrix, path, entry_task, exit_task, v_tag) if v_tag[path[0]] == -1: T_local += w_vex[path[0]] / local.COMPUTE_ABILITY else: T_cloud += w_vex[path[0]] / self.cloud.compute_ability if v_tag[path[-1]] == -1: T_local += w_vex[path[-1]] / local.COMPUTE_ABILITY else: T_cloud += w_vex[path[-1]] / self.cloud.compute_ability logger.debug('{0},{1} : {2}'.format(entry_task, exit_task, T_local + T_cloud + T_trans)) return T_local + T_cloud + T_trans
def choose_trans_vex_update_tag(self, adj_matrix, path, w_vex, v_tag): # logger.debug('path : {0};'.format(path)) # logger.debug('v_tag : {0};'.format(v_tag)) # logger.debug('v_trans : {0};'.format(v_trans)) entry_task, exit_task = self.select_trans_seq(adj_matrix, path, w_vex, v_tag) self.tag_trans_vex(path, entry_task, exit_task, v_tag) logger.debug('{0}->{1}'.format(entry_task, exit_task)) logger.debug('path : {0};'.format(path)) logger.debug('v_tag : {0};'.format(v_tag))