public Task Compute() { if (Simulated) { return(Task.Run(() => { foreach (var bond in bonds) { bond.OnResultComputed(PriceLib.ComputePrice(bond), null); computingTime += PriceLib.EstimedTime(bond); } })); } else { return(Task.Run(() => { var watch = new Stopwatch(); watch.Start(); foreach (var bond in bonds) { Thread.Sleep(PriceLib.EstimedTime(bond) * 1000); bond.OnResultComputed(PriceLib.ComputePrice(bond), null); } watch.Stop(); computingTime += (int)watch.ElapsedMilliseconds / 1000; })); } }
public int EstimedTime(Bond bond) { if (GetAvailableNodeIndex(bond) != -1) { return(0); } return(PriceLib.EstimedTime(bond) + 8); }
public int GetAvailableNodeIndex(Bond bond) { /* Check if we can add the bond to a node without increasing the computation time. * Return the index of the node or -1 if we can't find one */ if (nodes.Count == 0) { return(-1); } var longestComputation = nodes.Max(node => node.EstimedTime(bond)) - PriceLib.EstimedTime(bond); int index = 0; foreach (var node in nodes) { if (node.EstimedTime(bond) <= longestComputation) { return(index); } index++; } return(-1); }
public int EstimedTime(Bond bond) { return(bonds.Select(PriceLib.EstimedTime).Sum() + PriceLib.EstimedTime(bond)); }