def mapping(self, sorted_jobs, existing_plan, live_nodes, commcost, compcost): """def allocate(job, orders, jobson, prec, compcost, commcost):""" """ Allocate job to the machine with earliest finish time Operates in place """ ## TODO: add finished tasks jobson = dict() for (node, items) in existing_plan.items(): for item in items: if item.state == ScheduleItem.FINISHED or item.state == ScheduleItem.EXECUTING: jobson[item.job] = node new_plan = existing_plan def ft(machine): #cost = st(machine) runtime = compcost(task, machine) cost = st(machine, runtime) + runtime ##print("machine: %s job:%s cost: %s" % (machine.name, task.id, cost)) ##print("machine: " + str(machine.name) + " cost: " + str(cost)) return cost if len(live_nodes) != 0: ## in case if there is not any live nodes we just return the same cleaned schedule for wf, tasks in sorted_jobs: ##wf_dag = self.convert_to_parent_children_map(wf) wf_dag = HeftHelper.convert_to_parent_children_map(wf) prec = reverse_dict(wf_dag) for task in tasks: st = partial(self.start_time, wf, task, new_plan, jobson, prec, commcost) # ress = [(key, ft(key)) for key in new_plan.keys()] # agent_pair = min(ress, key=lambda x: x[1][0]) # agent = agent_pair[0] # start = agent_pair[1][0] # end = agent_pair[1][1] # agent = min(new_plan.keys(), key=ft) agent = min(live_nodes, key=ft) runtime = compcost(task, agent) start = st(agent, runtime) end = ft(agent) # new_plan[agent].append(ScheduleItem(task, start, end)) Schedule.insert_item(new_plan, agent, ScheduleItem(task, start, end)) jobson[task] = agent new_sched = Schedule(new_plan) return new_sched
def __call__(self, chromo, current_time): (schedule_mapping, finished_tasks, ready_tasks, chrmo_mapping, task_to_node) = self._create_helping_structures(chromo) chromo_copy = dict() for (nd_name, items) in chromo.items(): chromo_copy[nd_name] = [] for item in items: chromo_copy[nd_name].append(item) alive_nodes = [node for node in self.nodes if node.state != Node.Down] if len(alive_nodes) == 0: raise Exception("There are not alive nodes") while len(ready_tasks) > 0: for node in alive_nodes: if len(chromo_copy[node.name]) == 0: continue if node.state == Node.Down: continue ## TODO: Urgent! completely rethink this procedure tsk_id = None for i in range(len(chromo_copy[node.name])): if chromo_copy[node.name][i] in ready_tasks: tsk_id = chromo_copy[node.name][i] break if tsk_id is not None: task = self.task_map[tsk_id] #del chromo_copy[node.name][0] chromo_copy[node.name].remove(tsk_id) ready_tasks.remove(tsk_id) time_slots, runtime = self._get_possible_execution_times( schedule_mapping, task_to_node, chrmo_mapping, task, node, current_time) time_slot = next(time_slots) start_time = time_slot[0] end_time = start_time + runtime item = ScheduleItem(task, start_time, end_time) # need to account current time Schedule.insert_item(schedule_mapping, node, item) task_to_node[task.id] = (node, start_time, end_time) finished_tasks.add(task.id) #ready_children = [child for child in task.children if self._is_child_ready(finished_tasks, child)] ready_children = self._get_ready_tasks(task.children, finished_tasks) for child in ready_children: ready_tasks.append(child.id) schedule = Schedule(schedule_mapping) return schedule