wrap scheduler is ok after test

This commit is contained in:
Youmu Liu
2025-09-15 13:39:47 +08:00
parent 0a16f8317f
commit c655469f5f
4 changed files with 116 additions and 4 deletions
@@ -39,7 +39,7 @@ using System.Threading.Tasks;
namespace StgSharp.Mathematics
{
internal static partial class MatrixParallel
public static partial class MatrixParallel
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -33,6 +33,7 @@ using StgSharp.HighPerformance.Memory;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.IO.Pipes;
using System.Linq;
@@ -63,7 +64,7 @@ namespace StgSharp.Mathematics
* we allow each wrap to contain either 3 or 4 threads, providing flexibility while maintaining efficiency.
*/
internal static partial class MatrixParallel
public static partial class MatrixParallel
{
private static Thread[] _threads;
@@ -81,7 +82,9 @@ namespace StgSharp.Mathematics
public static bool IsParallelMeaningful { get; } = Environment.ProcessorCount > 6;
internal static MatrixParallelWrap.Handle LeaderHandle { get; private set; }
public static MatrixParallelWrap.Handle LeaderHandle { get; /*private/**/ set; }
private static bool IsInitialized { get; set; } = false;
public static void EndParallel()
{
@@ -105,6 +108,9 @@ namespace StgSharp.Mathematics
public static void Init()
{
if (IsInitialized) {
return;
}
int count = Environment.ProcessorCount - 4;
count = count == 5 ? 4 : count;
_threads = new Thread[count];
@@ -128,6 +134,7 @@ namespace StgSharp.Mathematics
}
}
LeaderHandle = new MatrixParallelWrap.Handle(0, _wraps[0]);
IsInitialized = true;
}
public static void LaunchParallel(SleepMode afterWork)
@@ -144,8 +151,15 @@ namespace StgSharp.Mathematics
public static void PublishTask() { }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ResetThreadCount()
{
RemainThreadCount = _threads.Length;
}
public static void SetAllWrap()
{
ResetThreadCount();
foreach (MatrixParallelWrap item in _wraps) {
_ = item.SchedulerReset.Set();
}
@@ -234,12 +248,15 @@ 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));
_ = Interlocked.Decrement(ref RemainThreadCount);
int remainCount = Interlocked.Decrement(ref RemainThreadCount);
Console.WriteLine($"[Worker] 线程 {self.Name} 完成任务,剩余线程数: {remainCount--}");
self.Priority = _AfterWork;
} while (true);
}
@@ -252,14 +269,19 @@ namespace StgSharp.Mathematics
Queue<IntPtr> 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--}");
self.Priority = _AfterWork;
} while (true);
}