protected void ParallelMerge() { int processor = Environment.ProcessorCount; Thread[] threads = new Thread[processor]; for (int I = 0; I < processor; I++) { threads[I] = new Thread( () => { while (true) { IMHComputeNode <T> n = null; if (!topologicalQueue.TryGet(out n)) { break; } ; MergeThatNode(n); } } ); threads[I].Name = $"MHComputeTree Branching: {I}"; threads[I].Start(); } foreach (Thread t in threads) { t.Join(TimeSpan.FromMilliseconds(sectionTimeOut)); } }
public MergeHCNodePQ(IMHComputeNode <T> root) { Queue <IMHComputeNode <T> > prevLevel = new Queue <IMHComputeNode <T> >(); prevLevel.Enqueue(root); Queue <IMHComputeNode <T> > nextLevel = new Queue <IMHComputeNode <T> >(); topologicalOrder_ = new ParallelStack <IMHComputeNode <T> >(); flags_ = new Dictionary <IMHComputeNode <T>, Task>(); // Topological ordering while (true) { foreach (IMHComputeNode <T> parent in prevLevel) { foreach (IMHComputeNode <T> n in parent.GetChildren()) { nextLevel.Enqueue(n); } topologicalOrder_.Put(parent); // this is for topological order. flags_[parent] = new Task(() => { }); // Empty task for waiting. } if (nextLevel.Count == 0) { break; } prevLevel = nextLevel; nextLevel = new Queue <IMHComputeNode <T> >(); } }
protected void BranchNode(IMHComputeNode <T> n) { n.Branch(); foreach (IMHComputeNode <T> child in n.GetChildren()) { forBranching_.Put(child); } }
protected void MergeThatNode(IMHComputeNode <T> node) { // prereq foreach (IMHComputeNode <T> n in node.GetChildren()) { Task f = topologicalQueue.GetFlagOf(n); if (f.IsCompleted) { continue; } f.Wait(); } node.Merge(); topologicalQueue.GetFlagOf(node).Start(); }
protected void ParallelBranch() { int processor = Environment.ProcessorCount; int loopCount = processor; while (loopCount-- > 0) { IMHComputeNode <T> n = null; if (!forBranching_.TryGet(out n)) { return; } ; BranchNode(n); } Thread[] threads = new Thread[processor]; for (int I = 0; I < processor; I++) { threads[I] = new Thread( () => { while (true) { IMHComputeNode <T> n = null; if (!forBranching_.TryGet(out n)) { break; } ; BranchNode(n); } } ); threads[I].Name = $"MHComputeTree Branching: {I}"; threads[I].Start(); } foreach (Thread t in threads) { t.Join(TimeSpan.FromMilliseconds(sectionTimeOut)); } }
int sectionTimeOut; // minutes waits for joining the thread running branch and merge process. public MHComputeNodeEvaluator(MHComputeNode <T> root, int sectionTimeOut = 30) { root_ = root; forBranching_ = new ParallelStack <IMHComputeNode <T> >(); forBranching_.Put(root); }
public void RegisterParent(IMHComputeNode <T> parent) { parent_ = parent; }
public void AddChild(IMHComputeNode <T> child) { children_.Enqueue(child); child.RegisterParent(this); }
/// <summary> /// Get the flag and modify it in the compute tree. /// </summary> /// <param name="node"> /// The node. /// </param> /// <returns> /// A task, just for the purpose of letting a thread to wait for /// it to finish. /// </returns> public Task GetFlagOf(IMHComputeNode <T> node) { return(flags_[node]); }
/// <summary> /// Get an IMHCompuetNode from the priority queue. /// </summary> /// <param name="placeHolder"> /// null, it should always be null. /// </param> /// <returns></returns> public bool TryGet(out IMHComputeNode <T> placeHolder) { return(topologicalOrder_.TryGet(out placeHolder)); }