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
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixOperatorPolicy.cs"
// file="MatrixOperatorPolicy"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -36,7 +33,7 @@ using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric.Runtime
{
internal enum MatrixCachePolicy
internal enum MatrixCachePolicy : short
{
None = 0,
@@ -25,6 +25,7 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Collections;
using StgSharp.HighPerformance.Memory;
using System;
using System.Collections.Generic;
@@ -41,7 +42,8 @@ namespace StgSharp.Mathematics.Numeric
private static NumaMeasurement[] _numaMeasurements;
private static int[] _numaPayload;
private static MatrixParallelThread[][] _numaThreads;
private static LinearBag<MatrixParallelThread>[] _numaSyncHeavy;
private static LinearBag<MatrixParallelThread>[] _numaWaveFront;
private static MatrixParallelThread[] _threads;
private static void UpdateThreadPayload()
@@ -65,12 +65,12 @@ namespace StgSharp.Mathematics.Numeric
ulong additionalPayload = 0;
for (int i = 0; i < _numaMeasurements.Length; i++)
{
MatrixParallelThread[] numa = _numaThreads[i];
MatrixParallelThread[] numa = _numaWaveFront[i];
if ((numa is null) || (numa.Length == 0))
{
continue;
}
ulong total = (ulong)_numaThreads[i].Length;
ulong total = (ulong)_numaWaveFront[i].Length;
NumaMeasurement measure = _numaMeasurements[i];
ulong numaPayload = measure.ActiveTasks;
uint numaActive = measure.ActiveThreadCount;
@@ -82,7 +82,7 @@ namespace StgSharp.Mathematics.Numeric
}
}
L4 cache = _numaBuffer[payloadIndex];
MatrixParallelThread[] selectedNuma = _numaThreads[payloadIndex];
MatrixParallelThread[] selectedNuma = _numaWaveFront[payloadIndex];
throw new NotImplementedException();
}
@@ -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..];