matrix thread pool use ParaWaveFront and heavy sync

This commit is contained in:
Youmu Liu
2026-04-26 17:38:10 +08:00
parent 251762791e
commit ecb1f04770
7 changed files with 152 additions and 131 deletions
@@ -84,7 +84,7 @@ namespace StgSharp.Mathematics.Numeric
_retiredThreads = new(RESERVED_THREAD_COUNT);
// init thread on all logical cores
_numaThreads = Numa.CreateForeachThread<MatrixParallelThread>(
MatrixParallelThread[][] numaWaveOrigin = Numa.CreateForeachThread<MatrixParallelThread>(
core => {
MatrixParallelThread thread = new(core);
SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready);
@@ -108,15 +108,15 @@ namespace StgSharp.Mathematics.Numeric
// retire threads
if (ContextCount == 1)
{
MatrixParallelThread[] threadArr = _numaThreads[0];
MatrixParallelThread[] threadArr = numaWaveOrigin[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];
_numaWaveFront[0] = [.. threadArr[0..^retireCount]];
} else
{
RetireWhenMultipleSIMDID();
RetireWhenMultipleSIMDID(numaWaveOrigin);
}
_meaningful = _threads.Length > 1;
@@ -134,11 +134,13 @@ namespace StgSharp.Mathematics.Numeric
_retiredThreads.Push(thread);
}
private static void RetireWhenMultipleSIMDID()
private static void RetireWhenMultipleSIMDID(
MatrixParallelThread[][] threads
)
{
MatrixParallelThread[] allThreads = new MatrixParallelThread[Environment.ProcessorCount];
int offset = 0;
foreach (MatrixParallelThread[] threadArr in _numaThreads)
foreach (MatrixParallelThread[] threadArr in threads)
{
Array.Copy(threadArr, 0, allThreads, offset, threadArr.Length);
offset += threadArr.Length;
@@ -159,9 +161,9 @@ namespace StgSharp.Mathematics.Numeric
}
// rebuild numa active arrays
for (int numa = 0; numa < _numaThreads.Length; numa++)
for (int numa = 0; numa < _numaWaveFront.Length; numa++)
{
MatrixParallelThread[] source = _numaThreads[numa];
MatrixParallelThread[] source = threads[numa];
int keepCount = 0;
for (int i = 0; i < source.Length; i++)
@@ -181,7 +183,7 @@ namespace StgSharp.Mathematics.Numeric
}
}
_numaThreads[numa] = active;
_numaWaveFront[numa] = [.. active];
}
_threads = allThreads[retireCount..];