def messageSolveRequest(conn, msg): id = problems.add(msg) print("-> AddProblem :: #{id:d} Type: {type:s} Timeout: {timeout:d}".format(type=msg.ProblemType, timeout=msg.SolvingTimeout, id=id)) problemStartTime[id] = current_time_ms() response = messages.SolveRequestResponse(id) conn.send(response) backup_msg = copy.deepcopy(msg) backup_msg.Id = id backup_servers.backupQueueAppend(backup_msg)
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)
def messageSolutions(conn, msg): problem = problems.list[msg.Id] # Received partial solution from CN if problem.status == Problem.Divided: print("-> Solutions :: #{id:d} Type: {type:s}".format(type=msg.ProblemType, id=msg.Id)) for solution in msg.Solutions: print("--- Solution :: #{id:d} Type: {type:s} Time: {time:d} Timeout: {timeout:}".format(type=solution.Type, time=solution.ComputationsTime, id=solution.TaskId, timeout=solution.TimeoutOccured)) # Received final merged solution from TM elif problem.status == Problem.Computed: print("-> Merged Solutions :: #{id:d} Type: {type:s}".format(type=msg.ProblemType, id=msg.Id)) time = current_time_ms() - problemStartTime[msg.Id] print("-> Solve time: {time:d}".format(time=time)) with open("Output {}.txt".format(msg.Id), "w") as text_file: print("{}\n{}\n".format(time, msg.Solutions[0].Data), file=text_file) problem.updateWithSolutions(msg) backup_msg = copy.deepcopy(msg) backup_servers.backupQueueAppend(backup_msg)
def sendStatusMessage(conn): msgThreads = [] for thread in threads: msgThread = { "State": "Busy", "ProblemType": thread.message.ProblemType, "HowLong": current_time_ms() - thread.started, "ProblemInstanceId": thread.message.Id } if not args.type == "TM": msgThread["TaskId"] = thread.assignedId msgThreads.append(msgThread) for i in range(config.threads - len(threads)): msgThreads.append({ "State": "Idle" }) msg = messages.Status(config.id, msgThreads) conn.send(msg)
def __init__(self, msg, assignedId): self.started = current_time_ms() self.message = msg self.assignedId = assignedId