public static async Task <NodeGrid> UpdateP2(this NodeGrid nodeGrid, float step, float noiseLevel) { var jobs = 4; var localOrders = ColUtils.SubSeqs(jobs, nodeGrid.Strides.Count() / jobs) .Select(t => t.ToList()) .ToList(); var trackedTasks = new List <Task <List <P1V <int, float> > > >(); for (var i = 0; i < jobs; i++) { var i1 = i; var ng = nodeGrid.Copy(); trackedTasks.Add( Task.Run( () => ng.UpdateSeveral(localOrders[i1], step, noiseLevel).ToList()) ); } await Task.WhenAll(trackedTasks); var results = new List <P1V <int, float> >(); foreach (var tt in trackedTasks) { results.AddRange(tt.Result); } return(nodeGrid.MakeNextGen(results)); }
public static NodeGrid UpdateP3(this NodeGrid nodeGrid, float step, float noiseLevel) { var jobs = 4; var localOrders = ColUtils.SubSeqs(jobs, nodeGrid.Strides.Count() / jobs) .Select(t => t.ToList()) .ToList(); Task <List <P1V <int, float> > > t0 = Task.Run(() => nodeGrid.UpdateSeveral(localOrders[0], step, noiseLevel).ToList()); Task <List <P1V <int, float> > > t1 = Task.Run(() => nodeGrid.UpdateSeveral(localOrders[1], step, noiseLevel).ToList()); Task <List <P1V <int, float> > > t2 = Task.Run(() => nodeGrid.UpdateSeveral(localOrders[2], step, noiseLevel).ToList()); Task <List <P1V <int, float> > > t3 = Task.Run(() => nodeGrid.UpdateSeveral(localOrders[3], step, noiseLevel).ToList()); Task.WaitAll(t0, t1, t2, t3); var results = new List <P1V <int, float> >(); results.AddRange(t0.Result); results.AddRange(t1.Result); results.AddRange(t2.Result); results.AddRange(t3.Result); return(nodeGrid.MakeNextGen(results)); }
public static NodeGrid UpdateP(this NodeGrid nodeGrid, float step, float noiseLevel) { var myLock = new object(); var jobs = 4; var localOrders = ColUtils.SubSeqs(jobs, nodeGrid.Strides.Count() / jobs) .Select(t => t.ToList()) .ToList(); var results = new List <P1V <int, float> >(); Parallel.ForEach( localOrders, () => new LocalData(nodeGrid), (indexes, loopstate, loco) => { //Console.WriteLine("{0}:{1}", Thread.CurrentThread.ManagedThreadId, url); //return ng.Update(index, step, noiseLevel); var retVal = new LocalData(nodeGrid, nodeGrid.UpdateSeveral(indexes, step, noiseLevel) .ToList()); return(retVal); }, (localRes) => { lock (myLock) { var res = localRes.Results; results.AddRange(res); } }); if (results.Count == nodeGrid.Strides.Count()) { nodeGrid = nodeGrid.MakeNextGen(results); } else { //nodeArray = nodeArray.MakeNextGen(results); } return(nodeGrid); }
async void RunBatch() { int Stride = 32; var jobs = 4; var GridStrides = new Sz2 <int>(Stride, Stride); var nodeGrid = NodeProcs.RandNodeGrid(GridStrides, 123, 456); var localOrders = ColUtils.SubSeqs(jobs, GridStrides.Count() / jobs) .Select(t => t.ToList()) .ToList(); var trackedTasks = new List <Task <int> >(); for (var i = 0; i < jobs; i++) { var i1 = i; trackedTasks.Add(Task.Run(() => { Thread.Sleep(2000); return(i1); })); } await Task.WhenAll(trackedTasks); var tot = trackedTasks.Sum(tt => tt.Result); }