protected override string ReceivedSolvePartialProblems(SolvePartialProblems solvePartialProblems) { /* Partial problems message is sent by the TM after dividing the problem into smaller partial problems. * The data in it consists of two parts – common for all partial problems and specific for the given task. * The same Partial Problems schema is used for the messages sent to be computed by the CN and to relay * information for synchronizing info with Backup CS. */ this.serverQueues.ProblemsToSolve.Enqueue(solvePartialProblems); //if (this.BackupMode == true) // return null; if (this.serverQueues.SolveRequests.Count > 0) { SolveRequest solveRequest = this.serverQueues.SolveRequests.Dequeue(); DivideProblem divideProblem = new DivideProblem(); divideProblem.Data = solveRequest.Data; divideProblem.Id = solveRequest.Id; Console.WriteLine("Sending DivideProblem as an ans to SolvePartiaProblems"); return(divideProblem.SerializeToXML()); } //TM is not going to join the solutions //if (this.serverQueues.Solutions.Count > 0) { // Solutions solutions = this.serverQueues.Solutions.Dequeue(); // return solutions.SerializeToXML(); //} Console.WriteLine("Sending NoOp as an ans to SolvePartialProblems"); return(this.GenerateNoOperation().SerializeToXML()); }
private void CreateAndSendPartialSolution(SolvePartialProblems message, SolvePartialProblemsPartialProblem problem) { var taskSolver = CreateSolverOrSendError(message.ProblemType, message.CommonData); if (taskSolver == null) return; var resultData = taskSolver.Solve(problem.Data, new TimeSpan()); var solution = new Solutions { Solutions1 = new[] {new SolutionsSolution { TaskId = problem.TaskId, TaskIdSpecified = true, Type = SolutionsSolutionType.Partial, TimeoutOccured = false, ComputationsTime = 1, Data = resultData }}, Id = message.Id, ProblemType = "DVRP", CommonData = problem.Data }; SendMessageNoResponse(solution); }
private void SendPartialProblemToNode(Problem problem, ComputationalNode computationalNode, int problemIndex) { SolvePartialProblems partialProblem = new SolvePartialProblems() { CommonData = problem.Data, Id = problem.Id, ProblemType = problem.ProblemType, SolvingTimeoutSpecified = problem.timeoutSpecified, PartialProblems = new SolvePartialProblemsPartialProblem[1] { new SolvePartialProblemsPartialProblem() { TaskId = problem.problems[problemIndex].TaskId, Data = problem.problems[problemIndex].Data } } }; if (problem.timeoutSpecified) { partialProblem.SolvingTimeout = problem.solvingTimeout; } Send(SerializeMessage(partialProblem), computationalNode.state.m_socket); }
public void SerializeSolvePartialProblems() { SolvePartialProblems d = new SolvePartialProblems(); string xml = d.SerializeToXML(); Assert.IsNotNull(xml); }
protected override string ReceivedDivideProblem(DivideProblem divideProblem) { /* Divide Problem is sent to TM to start the action of dividing the problem instance to smaller tasks. * TM is provided with information about the computational power of the cluster in terms of total number * of available threads. The same message is used to relay information for synchronizing info with Backup CS. */ //Debug.Assert(false, "Unimplemented"); //!!!!!!!!!!!!!!!!!!! ////we are not dividing yet - inserting everything into CommonData ////the same should be done in the ComputationalNode SolvePartialProblems solvePartialProblems = new SolvePartialProblems(); solvePartialProblems.CommonData = divideProblem.Data; solvePartialProblems.Id = divideProblem.Id; solvePartialProblems.SolvingTimeoutSpecified = false; if (divideProblem.ProblemType != null) { solvePartialProblems.ProblemType = divideProblem.ProblemType; } CMSocket.Instance.SendMessage(this.Port, this.IP, solvePartialProblems.SerializeToXML(), this); return(null); }
protected virtual void ProcessSolvePartialProblemMessage(SolvePartialProblems message, IDictionary <int, ProblemDataSet> dataSets, IDictionary <int, ActiveComponent> activeComponents) { WriteControlInformation(message); //update dataset for given problemId //message from TM and only from it, so set partialSets array (it will be enough) if (!dataSets.ContainsKey((int)message.Id)) { Log.DebugFormat("No problem ID {0} found for {1}", message.Id, message.MessageType); return; } var id = (int)message.Id; dataSets[id].PartialSets = new PartialSet[message.PartialProblems.Length]; for (var i = 0; i < message.PartialProblems.Length; i++) { dataSets[id].PartialSets[i] = new PartialSet() { NodeId = 0, PartialSolution = null, PartialProblem = message.PartialProblems[i], Status = PartialSetStatus.Fresh }; } }
private void SaveTaskManagerDataAndSendPartialProblemsToNodes(SolvePartialProblems _solvePartialProblems) { //_solvePartialProblems.SolvingTimeout?? m_memoryLock.WaitOne(); Problem problem = m_problems.Find(e => e.Id == _solvePartialProblems.Id); // if (_solvePartialProblems.CommonData == null) // problem.isAllSubproblems = true; problem.manager = null; problem.nodes = new ComputationalNode[_solvePartialProblems.PartialProblems.Length]; problem.problems = new SolvePartialProblemsPartialProblem[_solvePartialProblems.PartialProblems.Length]; problem.solutions = new SolutionsSolution[_solvePartialProblems.PartialProblems.Length]; //problem.Data = _solvePartialProblems.CommonData; //problem.solutions.AddRange(_solvePartialProblems = new SolutionsSolution[_solvePartialProblems.PartialProblems.Length]; for (int ii = 0; ii < problem.solutions.Length; ii++) { problem.problems[ii] = _solvePartialProblems.PartialProblems[ii]; problem.solutions[ii] = new SolutionsSolution() { Type = SolutionsSolutionType.Ongoing }; } SolvePartialProblems partialProblem = new SolvePartialProblems() { CommonData = problem.Data, Id = _solvePartialProblems.Id, ProblemType = _solvePartialProblems.ProblemType, SolvingTimeoutSpecified = problem.timeoutSpecified, }; if (partialProblem.SolvingTimeoutSpecified) { partialProblem.SolvingTimeout = problem.solvingTimeout; } int i = 0; foreach (var computationalNode in m_nodes) { if (computationalNode.solvableProblems.Contains(_solvePartialProblems.ProblemType) && computationalNode.statusThreads[0].State == StatusThreadState.Idle) { //for (int j = 0; j < computationalNode.statusThreads.Length; j++) // if (computationalNode.statusThreads[j].State == StatusThreadState.Idle) // freeThreads++; //freeThreads = Math.Min(freeThreads, _solvePartialProblems.PartialProblems.Length - i); partialProblem.PartialProblems = new SolvePartialProblemsPartialProblem[1]; // for (int j = 0; j < freeThreads; j++, i++) // { partialProblem.PartialProblems[0] = problem.problems[i]; problem.nodes[i++] = computationalNode; //} Send(SerializeMessage(partialProblem), computationalNode.state.m_socket); } if (i == problem.problems.Length) { break; } } m_memoryLock.Release(); }
public void DeserializeSolvePartialProblems() { SolvePartialProblems d = new SolvePartialProblems(); string xml = d.SerializeToXML(); d = (SolvePartialProblems)xml.DeserializeXML(); Assert.IsNotNull(d); }
public Message DivideProblem(DivideProblem divideProblem) { log.DebugFormat("Division of problem has started. ({0})", divideProblem.Id); if (!SolvableProblems.Contains(divideProblem.ProblemType)) { log.Debug("Not supported problem type."); return(new Error() { ErrorMessage = "not supported problem type", ErrorType = ErrorErrorType.InvalidOperation }); } var commonData = divideProblem.Data; var taskSolver = _resolver.GetInstanceByBaseTypeName(divideProblem.ProblemType, commonData); var bytes = taskSolver.DivideProblem(0); log.DebugFormat("Length of divide problem message: {0}", bytes?.Sum(x => x.Length)); //adding info about partial problems, their task ids, and partialProblem //some things can be temporary (partialProblems?) storage.AddIssue(divideProblem.Id, new ProblemInfo() { ProblemsCount = bytes?.GetLength(0) ?? 0, ProblemType = divideProblem.ProblemType, SolutionsCount = 0, CommonData = commonData }); var problemsList = new List <SolvePartialProblemsPartialProblem>(); //iterate through all partial problems and create proper messages for (var i = 0; i < (bytes?.GetLength(0) ?? 0); i++) { var partialProblem = new SolvePartialProblemsPartialProblem() { TaskId = (ulong)i, Data = bytes[i], NodeID = componentId }; problemsList.Add(partialProblem); //adding info about subtask to task manager memory storage.AddTaskToIssue(divideProblem.Id, partialProblem); } log.DebugFormat("Division finished. ({0})", divideProblem.Id); //creating msg var partialProblems = new SolvePartialProblems() { ProblemType = divideProblem.ProblemType, Id = divideProblem.Id, CommonData = divideProblem.Data, PartialProblems = problemsList.ToArray() }; return(partialProblems); }
protected override Message[] RespondSolvePartialProblemMessage(SolvePartialProblems message, IDictionary <int, ProblemDataSet> dataSets, IDictionary <int, ActiveComponent> activeComponents, List <BackupServerInfo> backups) { SynchronizationQueue.Enqueue(message); //sent by TM. send noOperation only. return(new Message[] { new NoOperation() { BackupServersInfo = backups.ToArray() } }); }
public void SolvePartialProblemsMessageSerializationTest() { SolvePartialProblems solveMessage = MessagesFactory.CreateEmptyMessage(MessageType.SolvePartialProblemsMessage).Cast <SolvePartialProblems>(); solveMessage.CommonData = new byte[150]; solveMessage.Id = 123; solveMessage.SolvingTimeout = 1344; solveMessage.SolvingTimeoutSpecified = true; //mandatory! string xml = _serializer.ToXmlString(solveMessage); SolvePartialProblems solveMessageDeserialized = _serializer.FromXmlString(xml).Cast <SolvePartialProblems>(); Assert.AreEqual(solveMessage.CommonData.Length, solveMessageDeserialized.CommonData.Length); Assert.AreEqual(solveMessage.Id, solveMessageDeserialized.Id); Assert.AreEqual(solveMessage.SolvingTimeout, solveMessageDeserialized.SolvingTimeout); }
public void RegisterPartialProblems() { DvrpProblem.PartialProblems.Clear(); SolvePartialProblems partialProblems = new SolvePartialProblems() { CommonData = new byte[1], Id = 1, PartialProblems = new SolvePartialProblemsPartialProblem[] {new SolvePartialProblemsPartialProblem() {Data = new byte[1]},}, ProblemType = "dvrp", }; string msg = MessageSerialization.Serialize(partialProblems); SolvePartialProblemsStrategy solvePartialProblemsStrategy = new SolvePartialProblemsStrategy(); solvePartialProblemsStrategy.HandleMessage(networkAdapterMock.Object, msg, MessageType.SolvePartialProblemsMessage, new TimeSpan(0,1,0)); Assert.AreEqual(DvrpProblem.PartialProblems.Count, 1); }
public Message ComputeSubtask(SolvePartialProblems solvePartialProblems) { log.DebugFormat("Computation started. ({0})", solvePartialProblems.Id); if (!SolvableProblems.Contains(solvePartialProblems.ProblemType)) { return new Error() { ErrorMessage = "Not supported problem type", ErrorType = ErrorErrorType.InvalidOperation } } ; //task solver stuff: var taskSolver = _resolver.GetInstanceByBaseTypeName(solvePartialProblems.ProblemType, solvePartialProblems.CommonData); var solutionsList = new List <SolutionsSolution>(); foreach (var partialProblem in solvePartialProblems.PartialProblems) { var newSolution = new SolutionsSolution() { TaskId = partialProblem.TaskId, TaskIdSpecified = true, //TimeoutOccured = false, Type = SolutionsSolutionType.Partial, //ComputationsTime = 0 }; newSolution.Data = taskSolver.Solve(partialProblem.Data, TimeSpan.Zero); solutionsList.Add(newSolution); } log.DebugFormat("Length of solution message: {0}", solutionsList.Sum(x => x.Data.Length)); log.DebugFormat("Computation finished. ({0})", solvePartialProblems.Id); return(new Solutions() { CommonData = solvePartialProblems.CommonData, Id = solvePartialProblems.Id, ProblemType = solvePartialProblems.ProblemType, SolutionsList = solutionsList.ToArray() }); }
protected override string ReceivedSolvePartialProblems(SolvePartialProblems solvePartialProblems) { ///// Hey here is the problem to solve! (undivided, everything is in CommonData) string problem = Utilities.Base64Decode(solvePartialProblems.CommonData); globalProblem = problem; //solvePartialProblems.Id; NodeWorker worker = new NodeWorker(solvePartialProblems.Id); worker.problemObject = ProblemLoader.loadnewExampleString(globalProblem); Workers.Add(worker); double sol = worker.calculateAlgorithm(); Solutions solutions = new Solutions(); solutions.Id = solvePartialProblems.Id; solutions.CommonData = Utilities.Base64Encode(sol.ToString()); Console.WriteLine("How Long = " + this.Workers.First().HowLong); CMSocket.Instance.SendMessage(this.Port, this.IP, solutions.SerializeToXML(), this); return(null); }
private void SendPartialProblem(DivideProblem _problem, byte[][] partitions) { SolvePartialProblems solvePartialProblems = new SolvePartialProblems() { Id = _problem.Id, CommonData = _problem.Data, ProblemType = taskSolver.Name, PartialProblems = new SolvePartialProblemsPartialProblem[partitions.Length] }; for (int i = 0; i < solvePartialProblems.PartialProblems.Length; i++) { solvePartialProblems.PartialProblems[i] = new SolvePartialProblemsPartialProblem() { TaskId = (ulong)i, Data = partitions[i] }; } Send(SerializeMessage <SolvePartialProblems>(solvePartialProblems)); Console.WriteLine("Sending Partial Problem from Problem ID={0} to server.", _problem.Id); }
/// <summary> /// gets some problem for comp. node (response to status msg) /// </summary> /// <param name="components">current active components memory</param> /// <param name="componentId">the ID of a comp node that sent status msg</param> /// <param name="dataSets">memory context</param> /// <returns>SolvePartialProblem message - null if there is nothing to do</returns> public static SolvePartialProblems GetMessageForCompNode (IDictionary <int, ActiveComponent> components, int componentId, IDictionary <int, ProblemDataSet> dataSets) { foreach (var dataSet in dataSets) { //checking only problems that this CN can handle if (!components[componentId].SolvableProblems.Contains(dataSet.Value.ProblemType)) { continue; } //no partial problems for this problem yet (not divided yet) if (dataSet.Value.PartialSets == null) { continue; } //check if there is some problem to send foreach (var partialSet in dataSet.Value.PartialSets) { //problem can be sent - because its fresh //we send only one partial problem to CN at a time if (partialSet.Status != PartialSetStatus.Fresh) { continue; } var response = new SolvePartialProblems() { CommonData = dataSet.Value.CommonData, Id = (ulong)dataSet.Key, PartialProblems = new[] { partialSet.PartialProblem }, ProblemType = dataSet.Value.ProblemType, SolvingTimeoutSpecified = false //we'll worry about this later }; partialSet.Status = PartialSetStatus.Ongoing; partialSet.NodeId = componentId; return(response); } } return(null); }
/// <summary> /// Sends partial problem to client (if any is available) /// </summary> public static void Work(ulong nodeId, ServerNetworkAdapter networkAdapter) { foreach (var list in DvrpProblem.PartialProblems.Values) foreach (var el in list) if (el.Value == nodeId) return; ulong problemId = 0; KeyValuePair<SolvePartialProblemsPartialProblem, ulong> partialProblem = new KeyValuePair<SolvePartialProblemsPartialProblem, ulong>(); foreach (var list in DvrpProblem.PartialProblems) { partialProblem = list.Value.FirstOrDefault(p => p.Value == 0); if (partialProblem.Key != null) { list.Value.Remove(partialProblem); problemId = list.Key; break; } } if (partialProblem.Key == null) return; var msg = new SolvePartialProblems(); msg.Id = problemId; msg.CommonData = DvrpProblem.Problems[problemId].Data; msg.ProblemType = DvrpProblem.Problems[problemId].ProblemType; msg.SolvingTimeout = DvrpProblem.Problems[problemId].SolvingTimeout; msg.SolvingTimeoutSpecified = DvrpProblem.Problems[problemId].SolvingTimeoutSpecified; msg.PartialProblems = new SolvePartialProblemsPartialProblem[] { partialProblem.Key }; if (networkAdapter.Send(msg)) DvrpProblem.PartialProblems[problemId].Add(new KeyValuePair<SolvePartialProblemsPartialProblem, ulong>(partialProblem.Key, nodeId)); }
protected abstract string ReceivedSolvePartialProblems(SolvePartialProblems solvePartialProblems);
public static void AddPartialProblemBackup(List<PartialProblemsBackup> list, int compID, SolvePartialProblems spp) { PartialProblemsBackup element = new PartialProblemsBackup() { ComponentID = compID, Problem = spp }; list.Add(element); }
protected virtual Message[] RespondSolvePartialProblemMessage(SolvePartialProblems message, IDictionary <int, ProblemDataSet> dataSets, IDictionary <int, ActiveComponent> activeComponents, List <BackupServerInfo> backups) { return(null); }
private bool PartialProblems() { Action action; int timeout; try { problem = networkAdapter.Receive<SolvePartialProblems>(false); if (problem != null && problem.PartialProblems != null) { timeout = (int)problem.SolvingTimeout; Console.WriteLine("Dostałem problem, zaczynam liczyć."); DVRPSolver = new DVRPTaskSolver(problem.CommonData); solutions = new Dictionary<ulong, byte[]>(); Stopwatch sw = Stopwatch.StartNew(); action = solve; ManualResetEvent evt = new ManualResetEvent(false); AsyncCallback cb = delegate { evt.Set(); }; IAsyncResult result = action.BeginInvoke(cb, null); if (evt.WaitOne(timeout)) { action.EndInvoke(result); timeOccured = false; } else { timeOccured = true; throw new TimeoutException(); } sw.Stop(); computationsTime =(ulong) sw.Elapsed.TotalMilliseconds; return true; } } catch (Exception e ) { Console.WriteLine(@"Cannot recive SolvePartialProblems {0}",e); return false; } return false; }
private void SendPartialProblems(ulong id) { try { taskSolver = new DVRPTaskSolver(problem.Data); var dividedProblems = taskSolver.DivideProblem((int) problem.ComputationalNodes); var solvePartialProblemsPartialProblem = new SolvePartialProblemsPartialProblem[dividedProblems.Length]; for (int i = 0; i < dividedProblems.Length; i++) { solvePartialProblemsPartialProblem[i] = new SolvePartialProblemsPartialProblem {Data = dividedProblems[i], TaskId = ++taskId}; } var partialProblems = new SolvePartialProblems { CommonData = problem.Data, Id = id, ProblemType = "DVRP", PartialProblems = solvePartialProblemsPartialProblem, SolvingTimeout = 100000, SolvingTimeoutSpecified = true }; networkAdapter.Send(partialProblems, true); Console.WriteLine("SendSolvePartialProblems"); } catch (Exception e) { Console.WriteLine("Cannot send partial problems to server: " + e.Message); } }
protected override string ReceivedSolvePartialProblems(SolvePartialProblems solvePartialProblems) { Debug.Assert(false, "Should not be here"); return(null); }
protected override string ReceivedStatus(Status status) { Debug.Assert(this.serverQueues != null, "null server queue"); Node node = this.RegisteredComponents.NodeWithID(status.Id); NoOperation noOperationResponse = this.GenerateNoOperation(); if (!this.ensureNode(node)) { return(noOperationResponse.SerializeToXML()); } switch (node.NodeType) { case NodeType.TaskManager: if (this.serverQueues.SolveRequests.Count > 0) { SolveRequest solveRequest = this.serverQueues.SolveRequests.Dequeue(); DivideProblem divideProblem = new DivideProblem(); divideProblem.Data = solveRequest.Data; divideProblem.Id = solveRequest.Id; Console.WriteLine("Sending DivideProblem to TM"); return(divideProblem.SerializeToXML()); } //TM is not going to join the solutions //if (this.serverQueues.Solutions.Count > 0) { // Solutions solutions = this.serverQueues.Solutions.Dequeue(); // return solutions.SerializeToXML(); //} break; case NodeType.ComputationalNode: //TODO: check!! bool busy = false; if (status.Threads != null) { Console.WriteLine("Threads field not null"); foreach (StatusThreadsThread stt in status.Threads) { if (stt.ProblemInstanceIdSpecified || stt.TaskIdSpecified) { busy = true; Console.WriteLine("Busy = true"); } } } if (this.serverQueues.ProblemsToSolve.Count > 0 && !busy) { Console.WriteLine("Busy = true"); SolvePartialProblems partialProblems = this.serverQueues.ProblemsToSolve.Dequeue(); Console.WriteLine("Sending PartialProblems to CN"); return(partialProblems.SerializeToXML()); } break; case NodeType.Server: { foreach (BackupServerQueue bsq in this.backupServerQueues) { if (bsq.backupServerId == status.Id) { Console.WriteLine("Sending queued message to BackupCS"); if (bsq.messages.Count > 0) { return(bsq.messages.Dequeue()); } } } } break; default: break; } Debug.Assert(node != null, "Received unregistered node status"); if (node == null) { Console.WriteLine("Received unregistered node status"); return(noOperationResponse.SerializeToXML()); } if (!this.BackupMode) { Console.WriteLine("Sending NoOp"); } return(noOperationResponse.SerializeToXML()); }
private SolvePartialProblems CreatePartialProblems(DivideProblem message) { var taskSolver = CreateSolverOrSendError(message.ProblemType, message.Data); if (taskSolver == null) return null; var problemsData = taskSolver.DivideProblem(0); var partialProblemsMessage = new SolvePartialProblems { Id = message.Id, ProblemType = "DVRP", CommonData = message.Data, PartialProblems = problemsData.Select((t, i) => new SolvePartialProblemsPartialProblem { TaskId = (ulong) i, NodeID = Id, Data = t }).ToArray() }; return partialProblemsMessage; }