private void ScheduleBatchJobs() { for (int i = 0; i < 2; i++) { if ((this.InvalidCells[i].Count > 0) && (this.RunningBatchTask[i] == null)) { MyPrecalcJobPhysicsBatch.Start(this, ref this.InvalidCells[i], i); } } }
public override void UpdateBeforeSimulation() { if (m_highPriorityJobs.Count > 0) { // no upper bound on these, as there should be just a few high priority jobs for (int i = 0; i < Parallel.Scheduler.ThreadCount; ++i) { var work = m_workPool.Allocate(); work.Queue = m_highPriorityJobs; work.Priority = WorkPriority.Low; work.MaxPrecalcTime = (long)MyFakes.MAX_PRECALC_TIME_IN_MILLIS; ++m_worksInUse; Parallel.Start(work, work.CompletionCallback); } } if (m_lowPriorityJobs.Count > 0 && m_worksInUse < 2 * Parallel.Scheduler.ThreadCount) { for (int i = 0; i < Parallel.Scheduler.ThreadCount; ++i) { var work = m_workPool.Allocate(); work.Queue = m_lowPriorityJobs; work.Priority = WorkPriority.VeryLow; work.MaxPrecalcTime = (long)MyFakes.MAX_PRECALC_TIME_IN_MILLIS; ++m_worksInUse; Parallel.Start(work, work.CompletionCallback); } } foreach (var physics in PhysicsWithInvalidCells) { MyPrecalcJobPhysicsBatch.Start(physics, ref physics.InvalidCells); } PhysicsWithInvalidCells.Clear(); Stats.Generic.Write("Precalc jobs in queue (low)", m_lowPriorityJobs.Count, VRage.Stats.MyStatTypeEnum.CurrentValue, 100, 0); Stats.Generic.Write("Precalc jobs in queue (high)", m_highPriorityJobs.Count, VRage.Stats.MyStatTypeEnum.CurrentValue, 100, 0); if (!MySandboxGame.IsGameReady) { var work = m_workPool.Allocate(); work.Queue = m_lowPriorityJobs; work.MaxPrecalcTime = (long)MyFakes.MAX_PRECALC_TIME_IN_MILLIS; (work as IWork).DoWork(); work.CompletionCallback(); } base.UpdateAfterSimulation(); }
public override void UpdateBeforeSimulation() { m_sortedJobs.Lock(); m_sortedJobs.List.AddList(m_addedJobs); m_sortedJobs.Unlock(); m_addedJobs.Clear(); m_counter++; if (m_counter % 30 == 0) { SortJobs(); //m_sortedJobs.Sort(m_comparer); } if (MyDebugDrawSettings.DEBUG_DRAW_SORTED_JOBS) { try { const float max = 255; float shade = m_sortedJobs.Count > 0 ? m_sortedJobs.ListUnsafe.ItemAt((int)Math.Min(m_sortedJobs.ListUnsafe.Count - 1, max)).Priority : 1; float minPriority = m_sortedJobs.Count > 0 ? m_sortedJobs.ListUnsafe.ItemAt(0).Priority : 1; shade -= minPriority; for (int xi = 0; xi < max; xi++) { if (xi + 5 > m_sortedJobs.Count) { break; } var job = m_sortedJobs.ListUnsafe.ItemAt(xi); var p = job.Priority - minPriority; job.DebugDraw(new Color((shade - p) / shade, 0.0f, p / shade, (max - xi) / max)); } } catch (Exception e) { } } if (m_sortedJobs.Count > 0) { while (m_workPool.Count > 0) { var work = m_workPool.Allocate(); work.Queue = m_sortedJobs; work.Priority = WorkPriority.Low; work.MaxPrecalcTime = (long)MyFakes.MAX_PRECALC_TIME_IN_MILLIS; if (MULTITHREADED) { Parallel.Start(work, work.CompletionCallback); } else { ((IWork)work).DoWork(); work.CompletionCallback(); } } } foreach (var physics in PhysicsWithInvalidCells) { for (int lod = 0; lod < physics.InvalidCells.Length; lod++) { if (physics.InvalidCells[lod].Count > 0 && physics.RunningBatchTask[lod] == null) { MyPrecalcJobPhysicsBatch.Start(physics, ref physics.InvalidCells[lod], lod); } } } PhysicsWithInvalidCells.Clear(); Stats.Generic.Write("Precalc jobs in sorted", m_sortedJobs.Count, VRage.Stats.MyStatTypeEnum.CurrentValue, 100, 0); Stats.Generic.Write("Clipmap triangle cache", MyClipmap.CellsCache.Usage, VRage.Stats.MyStatTypeEnum.CurrentValue, 100, 2); base.UpdateAfterSimulation(); }
public override void UpdateBeforeSimulation() { m_sortedJobs.Lock(); m_sortedJobs.List.AddList(m_addedJobs); m_sortedJobs.Unlock(); m_addedJobs.Clear(); m_counter++; if (m_counter % 60 == 0) { SortJobs(); //m_sortedJobs.Sort(m_comparer); } if (MyDebugDrawSettings.DEBUG_DRAW_SORTED_JOBS) { try { const float max = 255; float shade = m_sortedJobs.Count > 0 ? m_sortedJobs.ListUnsafe.ItemAt((int)Math.Min(m_sortedJobs.ListUnsafe.Count - 1, max)).Priority : 1; float minPriority = m_sortedJobs.Count > 0 ? m_sortedJobs.ListUnsafe.ItemAt(0).Priority : 1; shade -= minPriority; for (int xi = 0; xi < max; xi++) { if (xi + 5 > m_sortedJobs.Count) { break; } var job = m_sortedJobs.ListUnsafe.ItemAt(xi); var p = job.Priority - minPriority; job.DebugDraw(new Color((shade - p) / shade, 0.0f, p / shade, (max - xi) / max)); } } catch (Exception e) { } } if (m_sortedJobs.Count > 0 && m_worksInUse < 2 * Parallel.Scheduler.ThreadCount) { // no upper bound on these, as there should be just a few high priority jobs for (int i = 0; i < Parallel.Scheduler.ThreadCount; ++i) { var work = m_workPool.Allocate(); work.Queue = m_sortedJobs; work.Priority = WorkPriority.Low; work.MaxPrecalcTime = (long)MyFakes.MAX_PRECALC_TIME_IN_MILLIS; ++m_worksInUse; if (MULTITHREADED) { Parallel.Start(work, work.CompletionCallback); } else { ((IWork)work).DoWork(); work.CompletionCallback(); } } } if (m_highPriorityJobs.Count > 0) { // no upper bound on these, as there should be just a few high priority jobs for (int i = 0; i < Parallel.Scheduler.ThreadCount; ++i) { var work = m_workPool.Allocate(); work.Queue = m_highPriorityJobs; work.Priority = WorkPriority.Low; work.MaxPrecalcTime = (long)MyFakes.MAX_PRECALC_TIME_IN_MILLIS; ++m_worksInUse; if (MULTITHREADED) { Parallel.Start(work, work.CompletionCallback); } else { ((IWork)work).DoWork(); work.CompletionCallback(); } } } if (m_lowPriorityJobs.Count > 0 && m_worksInUse < 2 * Parallel.Scheduler.ThreadCount) { for (int i = 0; i < Parallel.Scheduler.ThreadCount; ++i) { var work = m_workPool.Allocate(); work.Queue = m_lowPriorityJobs; work.Priority = WorkPriority.VeryLow; work.MaxPrecalcTime = (long)MyFakes.MAX_PRECALC_TIME_IN_MILLIS; ++m_worksInUse; if (MULTITHREADED) { Parallel.Start(work, work.CompletionCallback); } else { ((IWork)work).DoWork(); work.CompletionCallback(); } } } foreach (var physics in PhysicsWithInvalidCells) { MyPrecalcJobPhysicsBatch.Start(physics, ref physics.InvalidCells); } PhysicsWithInvalidCells.Clear(); Stats.Generic.Write("Precalc jobs in sorted", m_sortedJobs.Count, VRage.Stats.MyStatTypeEnum.CurrentValue, 100, 0); Stats.Generic.Write("Precalc jobs in queue (low)", m_lowPriorityJobs.Count, VRage.Stats.MyStatTypeEnum.CurrentValue, 100, 0); Stats.Generic.Write("Precalc jobs in queue (high)", m_highPriorityJobs.Count, VRage.Stats.MyStatTypeEnum.CurrentValue, 100, 0); if (!MySandboxGame.IsGameReady) { var work = m_workPool.Allocate(); work.Queue = m_lowPriorityJobs; work.MaxPrecalcTime = (long)MyFakes.MAX_PRECALC_TIME_IN_MILLIS; (work as IWork).DoWork(); work.CompletionCallback(); } base.UpdateAfterSimulation(); }