working on per-PoolThread context loading and global context loading

This commit is contained in:
Youmu Liu
2026-02-24 18:05:16 +08:00
parent d62e59cb93
commit 7ee9c749e5
12 changed files with 535 additions and 305 deletions
+8 -20
View File
@@ -26,6 +26,8 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Collections;
using StgSharp.HighPerformance.Memory;
using StgSharp.Internal.Intrinsic;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -41,45 +43,31 @@ namespace StgSharp.Mathematics.Numeric
private static CapacityFixedStack<MatrixParallelThread> _threads;
private static readonly
#if NET10_0_OR_GREATER
Lock
#else
object
#endif
_lock = new();
private static SlabAllocator<IntrinsicContext> contextAllocator;
// private static ThreadPriority _AfterWork = ThreadPriority.Lowest;
public static bool SupportParallel { get; } = Environment.ProcessorCount > 6;
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6;
public static bool IsParallelMeaningful => Environment.ProcessorCount > 6 && IsInitialized;
private static bool IsInitialized { get; set; } = false;
private static Queue<IntPtr> LeaderTask { get; set; }
public static void EndParallel()
{
Monitor.Exit(_lock);
}
internal static void Init()
{
if (IsInitialized) {
return;
}
MatrixParallelFactory.Init();
if (!SupportParallel)
if (!IsParallelSupported)
{
IsInitialized = true;
return;
}
MatrixParallelFactory.Init();
contextAllocator = SlabAllocator<IntrinsicContext>.Create(4, SlabBufferLayout.Chunked, false);
int count = Environment.ProcessorCount - 4;
_threads = new(count);
for (int i = 0; i < count; i++)
{
MatrixParallelThread thread = new MatrixParallelThread();
MatrixParallelThread thread = new MatrixParallelThread(i);
_threads.Push(thread);
}
_awaitingLeaderHandle = new AwaitingQueue();
@@ -62,6 +62,7 @@ namespace StgSharp.Mathematics.Numeric
public enum MatrixIndexStyle : short
{
SPECIAL = 0,
BUFFER_INDEX = 1,
}
@@ -31,7 +31,7 @@ using System.Runtime.CompilerServices;
namespace StgSharp.Mathematics.Numeric
{
internal static class MatrixComputeModel
public partial class MatrixParallelThread
{
internal static unsafe void BufferComputeBinary(
@@ -26,6 +26,7 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Internal;
using StgSharp.Internal.Intrinsic;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
@@ -46,34 +47,30 @@ namespace StgSharp.Mathematics.Numeric
private readonly ManualResetEventSlim _resetEvent;
private readonly Thread _managedThread;
public MatrixParallelThread()
internal MatrixParallelThread(
int poolId
)
{
_resetEvent = new ManualResetEventSlim(false);
_managedThread = new Thread(MatrixParallelWorker);
_managedThread = new Thread(MatrixParallelWorkLoad);
_managedThread.Start();
PoolId = poolId;
}
// ~MatrixComputeThread()
// {
// Dispose(disposing: false);
// }
public int PoolId { get; init; }
public ThreadPriority AfterWork { get; set; }
internal ManualResetEventSlim? LeaderEvent { get; set; }
[DllImport(
dllName:Native.LibName,
EntryPoint = "sn_set_thread_affinity",
CallingConvention = CallingConvention.Cdecl)]
internal static extern void SetThreadAffinity(
ulong affinityMask
);
#region wrap belong
internal IntrinsicContext* Intrinsic { get; set; }
internal MatrixParallelWrap BindedWrap { get; set; }
internal RoleType Role { get; set; }
internal SIMDID Simd { get; set; }
public void Dispose()
{
Dispose(disposing:true);
@@ -146,6 +143,14 @@ namespace StgSharp.Mathematics.Numeric
current.Priority = BindedWrap.AfterWork;
}
[DllImport(
dllName:Native.LibName,
EntryPoint = "sn_set_thread_affinity",
CallingConvention = CallingConvention.Cdecl)]
internal static extern void SetThreadAffinity(
int coreId
);
protected virtual void Dispose(
bool disposing
)
@@ -162,17 +167,19 @@ namespace StgSharp.Mathematics.Numeric
}
}
private unsafe void MatrixParallelWorker()
private unsafe void MatrixParallelWorkLoad()
{
// Bind OS-thread affinity once when the worker starts running
SetThreadAffinity(PoolId);
Thread.Sleep(0);
while (true)
{
// Thread self = Thread.CurrentThread;
AfterWork = BindedWrap.AfterWork;
_managedThread.Priority = ThreadPriority.Highest;
_resetEvent.Wait();
_resetEvent.Reset();
AfterWork = BindedWrap.AfterWork;
_managedThread.Priority = ThreadPriority.Highest;
switch (Role)
{
case RoleType.Worker://worker
@@ -183,7 +190,9 @@ namespace StgSharp.Mathematics.Numeric
// Console.WriteLine($"worker {Environment.CurrentManagedThreadId} end");
break;
case RoleType.Leader://leader, error because leader should be at outer thread but not here
case RoleType.SingleExternal:
// TODO 将外部线程绑定到当前线程上,并执行任务包
break;
default:
break;
@@ -192,16 +201,14 @@ namespace StgSharp.Mathematics.Numeric
}
}
public RoleType Role { get; internal set; }
public enum RoleType : int
internal enum RoleType : int
{
Worker = 0,
Leader = 2,
Leader = 1,
SingleExternal = 2,
}
#endregion
}
}
@@ -10,6 +10,11 @@
<Configurations>Debug;Release;PerformanceDebug</Configurations>
</PropertyGroup>
<ItemGroup>
<None Remove="Numeric\MatrixParallelHandle.cs.old" />
<None Remove="Numeric\MatrixParallelWrap.cs.old" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StgSharp.Common\StgSharp.Common.csproj" />
</ItemGroup>