can create and retire matrix thread by numa infomation

This commit is contained in:
Youmu Liu
2026-04-15 18:08:08 +08:00
parent fdf11c729f
commit 2203a46eb7
11 changed files with 160 additions and 123 deletions
@@ -43,8 +43,6 @@ namespace StgSharp.Mathematics.Numeric
private static CapacityFixedStack<MatrixParallelThread> _retiredThreads;
private static CapacityFixedStack<MatrixParallelThread> _threads;
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6;
public static bool IsParallelMeaningful => IsParallelSupported &&
@@ -84,9 +82,9 @@ namespace StgSharp.Mathematics.Numeric
contextMap = [];
int count = Environment.ProcessorCount;
SIMDID globalSimd = new SIMDID(0);
_threads = new(count);
_retiredThreads = new(RESERVED_THREAD_COUNT);
// init thread on all logical cores
_numaThreads = Numa.CreateForeachThread<MatrixParallelThread>(
core => {
MatrixParallelThread thread = new(core);
@@ -101,37 +99,97 @@ namespace StgSharp.Mathematics.Numeric
return thread;
});
// make global simd context
IntrinsicContext globalContext = new();
LoadIntrinsicFunction(&globalContext, globalSimd.Mask);
NumericalModule.GlobalContext = globalContext;
NumericalModule.GlobalIntrinsicMask = globalSimd;
ContextCount = contextMap.Count;
// retire threads
if (ContextCount == 1)
{
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, _threads.Count));
for (int i = 0; i < retireCount; i++) {
RetireThread(_threads.Pop());
MatrixParallelThread[] threadArr = _numaThreads[0];
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, threadArr.Length));
for (int i = 1; i <= retireCount; i++) {
RetireThread(threadArr[^i]);
}
_numaThreads[0] = threadArr[0..^retireCount];
} else
{
// TODO 切换来源
// TODO _threads退休并移除
MatrixParallelThread[] allThreads = _threads.PopRange(_threads.Count);
Array.Sort(allThreads, static(
left,
right
) => CompareSIMDID(left.Simd, right.Simd));
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, allThreads.Length - 1));
for (int i = 0; i < retireCount; i++) {
RetireThread(allThreads[i]);
}
_threads.PushRange(allThreads.AsSpan(retireCount));
RetireWhenMultipleSIMDID();
}
_meaningful = _threads.Count > 1;
_awaitingLeaderHandle = new AwaitingQueue();
_meaningful = _threads.Length > 1;
// _awaitingLeaderHandle = new AwaitingQueue();
IsInitialized = true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void RetireThread(
MatrixParallelThread thread
)
{
thread.Pause();
thread.Dispose();
_retiredThreads.Push(thread);
}
private static void RetireWhenMultipleSIMDID()
{
MatrixParallelThread[] allThreads = new MatrixParallelThread[Environment.ProcessorCount];
int offset = 0;
foreach (MatrixParallelThread[] threadArr in _numaThreads)
{
Array.Copy(threadArr, 0, allThreads, offset, threadArr.Length);
offset += threadArr.Length;
}
Array.Sort(allThreads, static(
left,
right
) => CompareSIMDID(left.Simd, right.Simd));
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, allThreads.Length));
// HashSet<MatrixParallelThread> retiredSet = [];
MatrixParallelThread[] retireArr = new MatrixParallelThread[retireCount];
for (int i = 0; i < retireCount; i++)
{
MatrixParallelThread thread = allThreads[i];
retireArr[i] = thread;
RetireThread(thread);
}
// rebuild numa active arrays
for (int numa = 0; numa < _numaThreads.Length; numa++)
{
MatrixParallelThread[] source = _numaThreads[numa];
int keepCount = 0;
for (int i = 0; i < source.Length; i++)
{
if (!retireArr.Contains(source[i])) {
keepCount++;
}
}
MatrixParallelThread[] active = new MatrixParallelThread[keepCount];
int index = 0;
for (int i = 0; i < source.Length; i++)
{
MatrixParallelThread thread = source[i];
if (!retireArr.Contains(thread)) {
active[index++] = thread;
}
}
_numaThreads[numa] = active;
}
_threads = allThreads[retireCount..];
}
#region C invoke
[LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
@@ -148,15 +206,6 @@ namespace StgSharp.Mathematics.Numeric
ulong id
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void RetireThread(
MatrixParallelThread thread
)
{
thread.Pause();
_retiredThreads.Push(thread);
}
[LibraryImport(Native.LibName, EntryPoint = "sn_get_unite_simd")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
@@ -165,5 +214,6 @@ namespace StgSharp.Mathematics.Numeric
SIMDID right
);
#endregion
}
}