def divideProblem(thread): msg = thread.message problemType = msg.ProblemType solver = TaskSolver(msg.Data) partialProblems = [{ "TaskId": i, "Data": str(problem), "NodeID": config.id } for i, problem in enumerate(solver.divide(msg.ComputationalNodes))] message = messages.SolvePartialProblems(msg.Id, msg.ProblemType, msg.Data, partialProblems) pending_messages.add(message) threads.remove(thread)
def solvePartialProblem(thread): msg = thread.message problemType = msg.ProblemType partialProblem = [item for item in msg.PartialProblems if item.TaskId == thread.assignedId][0] solver = TaskSolver(msg.CommonData) data = solver.solve(partialProblem.Data, msg.SolvingTimeout) solutions = [] # Type=Partial solution = messages.Solution(Type="Final", ComputationsTime=(current_time_ms() - thread.started), TimeoutOccured=False, TaskId=partialProblem.TaskId, Data=data) solutions.append(solution) message = messages.Solutions(msg.Id, msg.ProblemType, solutions, msg.CommonData) pending_messages.add(message) threads.remove(thread)
def mergeSolutions(thread): msg = thread.message problemType = msg.ProblemType solver = TaskSolver(msg.CommonData) value = solver.merge(list(solution.Data for solution in msg.Solutions)) sumTime = sum(int(solution.ComputationsTime) for solution in msg.Solutions) solutions = [{ "TimeoutOccured": False, "Type": "Final", # Partial "ComputationsTime": current_time_ms() - thread.started + sumTime, "Data": str(value) }] message = messages.Solutions(msg.Id, msg.ProblemType, solutions, msg.CommonData) pending_messages.add(message) threads.remove(thread)