def test_simple(): network: model.Network = model.Network() msg_node1 = model.SwitchNode(f'msg_core1') msg_node2 = model.SwitchNode(f'msg_core2') app_node1 = model.EndNode('app_core1') app_node2 = model.EndNode('app_core2') network.add_node(msg_node1) network.add_node(msg_node2) network.add_node(app_node1) network.add_node(app_node2) network.add_link(app_node1.name, msg_node1.name) network.add_link(app_node2.name, msg_node2.name) network.add_link(msg_node1.name, msg_node2.name) app1 = model.Application(network, 'app1', 'app_core1') app2 = model.Application(network, 'app2', 'app_core2') app1.set_virtual_link([app2]).set_frame(6) app2.set_virtual_link([app1]).set_frame(6).depend_on(app1) sc = model.Scheduler(network) sc.add_apps([app1, app2]) hook = constrains.Z3Hook() sc.add_constrains(hook) df = hook.to_dataframe() an = analyzer.Analyzer(df, network, sc.app_lcm) an.print_by_time() an.animate()
def test_director(): # 星形拓扑 network: model.Network = model.Network() for x in range(ord('a'), ord('d')): node = model.EndNode('node_' + chr(x)) network.add_node(node) network.add_node(model.SwitchNode('switch_a')) network.get_link_helper()('switch_a', ['node_a', 'node_b', 'node_c']) # 开始构建约束 app1 = model.Application(network, 'app1', 'node_a') app2 = model.Application(network, 'app2', 'node_b') app3 = model.Application(network, 'app3', 'node_c') app1.set_virtual_link([app2, app3]) app2.set_virtual_link([app3]) app1.set_frame(4, 4) app2.set_frame(4, 2) app2.depend_on(app1) sc = model.Scheduler(network) sc.add_apps([app1, app2, app3]) hook = constrains.Z3Hook() sc.add_constrains(hook) df = hook.to_dataframe() an = analyzer.Analyzer(df, network, sc.app_lcm) an.print_by_time()
def test_paper(): network: model.Network = model.Network() # 创建多核处理器 for proc_idx in range(1, 4): msg_node = model.SwitchNode(f'msg_core_{proc_idx}') network.add_node(msg_node) app_nodes = [ model.EndNode(f'app_core_{proc_idx}_{core_idx}') for core_idx in range(1, 4) ] for n in app_nodes: network.add_node(n) network.add_link(msg_node.name, n.name) switch = model.SwitchNode('msg_switch', 1) network.add_node(switch) for i in range(1, 4): network.add_link(switch.name, f'msg_core_{i}') # 生成APP apps = {} for proc_idx in range(1, 4): for core_idx in range(1, 4): app = model.Application(network, f'app{proc_idx}_{core_idx}', f'app_core_{proc_idx}_{core_idx}') apps[app.name] = app # 建立虚链路 apps['app1_1'].set_virtual_link([apps['app3_2']]).set_frame(4) apps['app1_2'].set_virtual_link([apps['app2_2'], apps['app3_1']]).set_frame(8) apps['app1_3'].set_virtual_link([apps['app2_3']]).set_frame(8) apps['app2_1'].set_virtual_link([apps['app1_1']]).set_frame(8) apps['app2_2'].set_virtual_link([apps['app2_1'] ]).set_frame(8).depend_on(apps['app1_2']) apps['app2_3'].set_virtual_link([apps['app3_3'], apps['app3_2']]).set_frame(8) apps['app3_1'].set_virtual_link([apps['app3_2'] ]).set_frame(8).depend_on(apps['app1_2']) apps['app3_2'].set_virtual_link([apps['app3_3']]).set_frame(4) apps['app3_3'].set_virtual_link([apps['app1_1']]).set_frame(8) sc = model.Scheduler(network) sc.add_apps(list(apps.values())) hook = constrains.Z3Hook() sc.add_constrains(hook) hook.print() df = hook.to_dataframe() an = analyzer.Analyzer(df, network, sc.app_lcm) an.print_by_time() an.animate() an.export(('192.168.11.224', '192.168.11.209')) return
def test_random(): g: networkx.Graph = ntx_gen.random_tree(random.randint(5, 20), seed=random.randint(1, 99999999)) network: model.Network = model.Network() node_name_map = {} for n in g.nodes(): nei = list(g.neighbors(n)) if len(nei) == 1: new_node = model.EndNode(f'app_{n}') else: new_node = model.SwitchNode(f'msg_{n}') network.add_node(new_node) node_name_map[n] = new_node.name for e in g.edges: network.add_link(node_name_map[e[0]], node_name_map[e[1]]) # 生成app apps = set() for en in network.end_nodes: app = model.Application(network, f'{en.name}', en.name) apps.add(app) for app in apps: target_apps = random.sample( apps, random.randint(1, max(2, len(network.end_nodes) // 2))) if app in target_apps: target_apps.remove(app) app.set_virtual_link(list(target_apps)).set_frame(int(len(apps) * 1.5)) sc = model.Scheduler(network) sc.add_apps(list(apps)) hook = constrains.Z3Hook() sc.add_constrains(hook) hook.print() if hook.solve() is None: test_random() return df = hook.to_dataframe() an = analyzer.Analyzer(df, network, sc.app_lcm) an.print_by_time() an.animate()
def do_test(split_output_path: str, s_delta=0.5): #return times split_time = 0 task_sche_time = 0 msg_sche_time = 0 # call gen_model network, task_dict, commu_pair = gb.gen_example(50000, s_delta) # draw the network # network.draw() free_utils = dict() for node in task_dict: free_utils[node] = 0.6 # test solver solver = tsolver.Solver(network, task_dict, free_utils, commu_pair) solver_result_dict, split_time = solver.solve(split_output_path) if not solver_result_dict: print("\x1b[31m#### Can't solve spliting\x1b[0m") time.sleep(3) return -1, -1, -1 print("-- 密度测试 --") for node in task_dict: tasks = task_dict[node] s = 0 for task in tasks: if isinstance(task, tmodel.FreeTask): s += task.wcet / task.deadline0 else: s += task.wcet / solver_result_dict["{}_deadline".format( task.name)] print("{}: s={}".format(node.name, s)) print("-- 任务调度 --") _temp_sum = 0 _count = 0 for node in task_dict: tasks = task_dict[node] print(node.name) # re-setup phi and deadline tasks4edf = [] for task in tasks: _tid = int(task.name.split('_')[2]) + 1 #print(_tid) if isinstance(task, tmodel.FreeTask): _C = int(task.wcet) _T = int(task.peroid) _offset = int(task.offset0) _D = int(task.deadline0) _task: edfsim.Task = edfsim.Task(C=_C, D=_D, T=_T, offset=_offset, tid=_tid) else: # re-setup _C = int(task.wcet) _T = int(task.peroid) _offset = solver_result_dict['{}_phi'.format(task.name)] _D = solver_result_dict['{}_deadline'.format(task.name)] _task: edfsim.Task = edfsim.Task(C=_C, D=_D, T=_T, offset=_offset, tid=_tid) tasks4edf.append(_task) # do edf sim # dump taskset #for task in tasks4edf: # print('Task(tid={}, T={}, C={}, D={}, offset={})'.format(task.tid, task.T, task.C, task.D, task.offset)) _t0 = time.time() edfsim.testEDFSim(tasks4edf, []) _t1 = time.time() _count += 1 _temp_sum += _t1 - _t0 task_sche_time = _temp_sum / _count print("-- 通信调度 --") gb.setup_frame_constraints(task_dict, solver_result_dict) sc = mmodel.Scheduler(network) for node in task_dict: sc.add_apps(task_dict[node]) hook = constrains.Z3Hook() _t0 = time.time() sc.add_constrains(hook) df = hook.to_dataframe() _t1 = time.time() msg_sche_time = _t1 - _t0 if df.empty: return -2, -2, -2 an = analyzer.Analyzer(df, network, sc.app_lcm) an.print_by_time() #an.animate() #print("Message Passing Solved in {} s!".format(_t1-_t0)) return split_time, task_sche_time, msg_sche_time
def do_test(peroids, util, gran, net_type, times): #return times split_time = 0 task_sche_time = 0 msg_sche_time = 0 # call gen_model network, task_dict, commu_pair = gb.gen_test_model(peroids, util, gran, net_type, times) # draw the network # network.draw() # check param with open("./output/bench_test.txt", 'w') as f: record_test_model(f, task_dict) f.close() free_utils = dict() for node in task_dict: free_utils[node] = util * 0.75 # test solver solver = tsolver.Solver(network, task_dict, free_utils, commu_pair) solver_result_dict, split_time = solver.solve("./output/gurobi_result.txt") if not solver_result_dict: print("\x1b[31m#### Can't solve spliting\x1b[0m") time.sleep(3) return -1, -1, -1 # 测试密度 for node in task_dict: tasks = task_dict[node] s = 0 for task in tasks: if isinstance(task, tmodel.FreeTask): s += task.wcet / task.deadline0 else: s += task.wcet / solver_result_dict["{}_deadline".format( task.name)] print("{}: s={}".format(node.name, s)) _temp_sum = 0 _count = 0 for node in task_dict: tasks = task_dict[node] print(node.name) # re-setup phi and deadline tasks4edf = [] for task in tasks: _tid = int(task.name.split('_')[2]) + 1 #print(_tid) if isinstance(task, tmodel.FreeTask): _C = int(task.wcet) _T = int(task.peroid) _offset = int(task.offset0) _D = int(task.deadline0) _task: edfsim.Task = edfsim.Task(C=_C, D=_D, T=_T, offset=_offset, tid=_tid) else: # re-setup _C = int(task.wcet) _T = int(task.peroid) _offset = solver_result_dict['{}_phi'.format(task.name)] _D = solver_result_dict['{}_deadline'.format(task.name)] _task: edfsim.Task = edfsim.Task(C=_C, D=_D, T=_T, offset=_offset, tid=_tid) tasks4edf.append(_task) # do edf sim # dump taskset #for task in tasks4edf: # print('Task(tid={}, T={}, C={}, D={}, offset={})'.format(task.tid, task.T, task.C, task.D, task.offset)) _t0 = time.time() edfsim.testEDFSim(tasks4edf, []) _t1 = time.time() _count += 1 _temp_sum += _t1 - _t0 task_sche_time = _temp_sum / _count gb.setup_frame_constraints(task_dict, solver_result_dict) sc = mmodel.Scheduler(network) for node in task_dict: sc.add_apps(task_dict[node]) _t0 = time.time() hook = constrains.Z3Hook() sc.add_constrains(hook) df = hook.to_dataframe() _t1 = time.time() msg_sche_time = _t1 - _t0 if df.empty: return -2, -2, -2 #an = analyzer.Analyzer(df, network, sc.app_lcm) #an.print_by_time() #an.animate() #print("Message Passing Solved in {} s!".format(_t1-_t0)) return split_time, task_sche_time, msg_sche_time
print("\n-- 任务调度 --") temp_sum = 0 count = 0 for node in task_dict: _tasks = task_dict[node] #print(node.name) _path = "{}{}.bin".format(task_sch_result_path_prefix, node.name) _cost_time = single_task_schedule(_tasks, solver_result_dict, _path) count += 1 temp_sum += _cost_time task_sche_time = temp_sum / count print("\n-- 通信调度 --") ips.setup_frame_constraints(task_dict, solver_result_dict) sc = mmodel.Scheduler(network) for node in task_dict: sc.add_apps(task_dict[node]) hook = constrains.Z3Hook() _t0 = time.time() sc.add_constrains(hook) df = hook.to_dataframe() _t1 = time.time() msg_sche_time = _t1 - _t0 if df.empty: print("\x1b[31m#### 通信调度无解\x1b[0m") exit(0) # gen msg schedule gen_msg_sch_table(df)