From fa54037a44cd304334356c1fdc194b0e0a33dbb7 Mon Sep 17 00:00:00 2001 From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com> Date: Mon, 15 Sep 2025 21:08:13 +0800 Subject: [PATCH] working on task publication --- .../Linear/MatrixParallel.Execute.cs | 2 +- ...ctory.cs => MatrixParallel.TaskFactory.cs} | 0 .../Mathematics/Linear/MatrixParallel.cs | 37 ++++++++++++------- 3 files changed, 24 insertions(+), 15 deletions(-) rename StgSharp.Common/Mathematics/Linear/{MatrixParallel.TaskWrapFactory.cs => MatrixParallel.TaskFactory.cs} (100%) diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs index 7f981ca9..5d30dc10 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs @@ -39,7 +39,7 @@ using System.Threading.Tasks; namespace StgSharp.Mathematics { - public static partial class MatrixParallel + internal static partial class MatrixParallel { [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskWrapFactory.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs similarity index 100% rename from StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskWrapFactory.cs rename to StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs index 64dd71d3..7a9f874b 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs @@ -64,7 +64,7 @@ namespace StgSharp.Mathematics * we allow each wrap to contain either 3 or 4 threads, providing flexibility while maintaining efficiency. */ - public static partial class MatrixParallel + internal static partial class MatrixParallel { private static Thread[] _threads; @@ -80,12 +80,14 @@ namespace StgSharp.Mathematics public static bool SupportParallel { get; } = Environment.ProcessorCount > 6; - public static bool IsParallelMeaningful { get; } = Environment.ProcessorCount > 6; + public static bool IsParallelMeaningful { get; } = Environment.ProcessorCount > 6 && IsInitialized; - public static MatrixParallelWrap.Handle LeaderHandle { get; /*private/**/ set; } + public static MatrixParallelWrap.Handle LeaderHandle { get; private/**/ set; } private static bool IsInitialized { get; set; } = false; + private static Queue LeaderTask { get; set; } + public static void EndParallel() { Monitor.Exit(_lock); @@ -134,6 +136,7 @@ namespace StgSharp.Mathematics } } LeaderHandle = new MatrixParallelWrap.Handle(0, _wraps[0]); + LeaderTask = LeaderHandle.Wrap.GetWorkerTaskQueue(0); IsInitialized = true; } @@ -149,7 +152,20 @@ namespace StgSharp.Mathematics Monitor.Enter(_lock); } - public static void PublishTask() { } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void MatrixParallelLeaderWork() + { + MatrixParallelWrap.ExecuteTaskPackage(LeaderTask); + _ = Interlocked.Decrement(ref RemainThreadCount); + } + + public static unsafe void PublishTask(MatrixParallelTaskPackage* package) + { + int primCountPerWrap = package->PrimCount / _threads.Length, extraPrimCount = package->PrimCount % _threads.Length; + int secCountPerWrap = package->SecCount / _threads.Length, extraSecCount = package->SecCount % _threads.Length; + + foreach (MatrixParallelWrap wrap in _wraps) { } + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ResetThreadCount() @@ -248,15 +264,12 @@ namespace StgSharp.Mathematics int inWrapIndex = handle.InWrapIndex; Thread self = Thread.CurrentThread; AutoResetEvent reset = wrap.GetWorkerResetEvent(inWrapIndex); - Console.WriteLine($"[Worker] 线程 {self.Name} 启动,Wrap ID: {wrap.ID}, 索引: {inWrapIndex}"); do { _ = reset.WaitOne(); - Console.WriteLine($"[Worker] 线程 {self.Name} 收到任务信号,开始执行"); self.Priority = ThreadPriority.Highest; ExecuteTaskPackage(wrap.GetWorkerTaskQueue(inWrapIndex)); - int remainCount = Interlocked.Decrement(ref RemainThreadCount); - Console.WriteLine($"[Worker] 线程 {self.Name} 完成任务,剩余线程数: {remainCount--}"); + _ = Interlocked.Decrement(ref RemainThreadCount); self.Priority = _AfterWork; } while (true); } @@ -269,25 +282,21 @@ namespace StgSharp.Mathematics Queue taskQueue = wrap.GetWorkerTaskQueue(inWrapIndex); AutoResetEvent resetEvent = wrap.SchedulerReset; Thread self = Thread.CurrentThread; - Console.WriteLine($"[Scheduler] 调度器线程 {self.Name} 启动,Wrap ID: {wrap.ID}, 索引: {inWrapIndex}"); do { _ = resetEvent.WaitOne(); - Console.WriteLine($"[Scheduler] 调度器线程 {self.Name} 收到调度信号,开始分发任务"); self.Priority = ThreadPriority.Highest; // TODO task publication to worker in wrap wrap.SetAllWorker(); - Console.WriteLine($"[Scheduler] 调度器线程 {self.Name} 已通知所有工作线程"); ExecuteTaskPackage(taskQueue); - int remain = Interlocked.Decrement(ref RemainThreadCount); - Console.WriteLine($"[Scheduler] 调度器线程 {self.Name} 完成调度任务,剩余线程数: {remain--}"); + _ = Interlocked.Decrement(ref RemainThreadCount); self.Priority = _AfterWork; } while (true); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static unsafe void ExecuteTaskPackage(Queue taskQueue) + public static unsafe void ExecuteTaskPackage(Queue taskQueue) { while (taskQueue.TryDequeue(out nint handle)) {