matrix parallel start to use two different policy

This commit is contained in:
Youmu Liu
2026-04-27 20:08:10 +08:00
parent ecb1f04770
commit ae664cb541
16 changed files with 170 additions and 239 deletions
@@ -1,98 +0,0 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixEnumerator"
// Project: StgSharp
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric.Runtime
{
public enum PredictPolicy
{
Transparent = 0,
BufferSequential = 1,
SquareRowMajor = 2,
SquareColMajor = 3,
}
public unsafe interface IMatrixCachePredictor<TSelf> : IL4Predict
where TSelf : class, IMatrixCachePredictor<TSelf>
{
public static abstract PredictPolicy Policy { get; set; }
}
public sealed unsafe class BufferSequentialPredictor : IMatrixCachePredictor<BufferSequentialPredictor>
{
private readonly MatrixParallelTask* origin;
private readonly int _kernelSize;
public static PredictPolicy Policy { get; set; } = PredictPolicy.SquareRowMajor;
public bool IsSamePolicy(
uint policy,
IL4Predict predict
)
{
throw new NotImplementedException();
}
public PredictResult Predict(
Span<CacheLineDescription> node,
out int count
)
{
throw new NotImplementedException();
}
public void Prefetch(
nuint origin,
uint policy,
Span<byte> cache
)
{
throw new NotImplementedException();
}
public void WriteBack(
nuint origin,
uint policy,
ReadOnlySpan<byte> cache
)
{
throw new NotImplementedException();
}
}
}
@@ -50,6 +50,7 @@ namespace StgSharp.Mathematics.Numeric.Runtime
RowMajor = 4,
ColMajor = 5,
Sequential = 6,
#endregion
@@ -42,8 +42,8 @@ namespace StgSharp.Mathematics.Numeric
private static NumaMeasurement[] _numaMeasurements;
private static int[] _numaPayload;
private static LinearBag<MatrixParallelThread>[] _numaSyncHeavy;
private static LinearBag<MatrixParallelThread>[] _numaWaveFront;
private static MatrixParallelThread[][] _numaSyncHeavy;
private static MatrixParallelThread[][] _numaWaveFront;
private static MatrixParallelThread[] _threads;
private static void UpdateThreadPayload()
@@ -49,9 +49,9 @@ namespace StgSharp.Mathematics.Numeric
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe MatrixParallelWrap ScheduleTask<TTask>(
internal static unsafe MatrixParallelWrap ScheduleTask<TTaskQueue>(
MatrixParallelTask* package
) where TTask : IL4Predict
) where TTaskQueue : IMatrixParallelQueue<TTaskQueue>
{
/*
int primCount = package->PrimCount;
@@ -73,9 +73,10 @@ namespace StgSharp.Mathematics.Numeric
ulong total = (ulong)_numaWaveFront[i].Length;
NumaMeasurement measure = _numaMeasurements[i];
ulong numaPayload = measure.ActiveTasks;
uint numaActive = measure.ActiveThreadCount;
// uint numaActive = measure.ActiveThreadCount;
long curPayload = ((long)total) - (((long)(numaPayload + additionalPayload + 63)) / 64);
if (curPayload > payload)
if (curPayload < payload)
{
payload = curPayload;
payloadIndex = i;
@@ -84,7 +84,7 @@ namespace StgSharp.Mathematics.Numeric
_retiredThreads = new(RESERVED_THREAD_COUNT);
// init thread on all logical cores
MatrixParallelThread[][] numaWaveOrigin = Numa.CreateForeachThread<MatrixParallelThread>(
_numaWaveFront = 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 = numaWaveOrigin[0];
MatrixParallelThread[] threadArr = _numaWaveFront[0];
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, threadArr.Length));
for (int i = 1; i <= retireCount; i++) {
RetireThread(threadArr[^i]);
}
_numaWaveFront[0] = [.. threadArr[0..^retireCount]];
_numaWaveFront[0] = threadArr[0..^retireCount];
} else
{
RetireWhenMultipleSIMDID(numaWaveOrigin);
RetireWhenMultipleSIMDID(_numaWaveFront);
}
_meaningful = _threads.Length > 1;
@@ -151,7 +151,6 @@ namespace StgSharp.Mathematics.Numeric
) => 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++)
{
@@ -183,7 +182,7 @@ namespace StgSharp.Mathematics.Numeric
}
}
_numaWaveFront[numa] = [.. active];
_numaWaveFront[numa] = active;
}
_threads = allThreads[retireCount..];
@@ -39,18 +39,96 @@ using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
internal unsafe class MatrixParallelQueue<TPredictor>
where TPredictor : class, IMatrixCachePredictor<TPredictor>
internal unsafe interface IMatrixParallelQueue<TSelf> where TSelf : IMatrixParallelQueue<TSelf>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Steal(
MatrixParallelTask* task
static abstract bool NeedNumaBuffer { get; }
static abstract TSelf Create(
MatrixParallelTask* baseTask,
L4 buffer
);
void Return(
MatrixParallelTask* task
);
bool Steal(
MatrixParallelTask* task
);
}
internal sealed unsafe class BufferSequentialQueue : IMatrixParallelQueue<BufferSequentialQueue>
{
private MatrixParallelTask* baseTask;
private BufferSequentialQueue() { }
private BufferSequentialQueue(
MatrixParallelTask* baseTask
)
{
this.baseTask = baseTask;
}
public static bool NeedNumaBuffer => throw new NotImplementedException();
public static unsafe BufferSequentialQueue Create(
MatrixParallelTask* baseTask,
L4 buffer
)
{
return new BufferSequentialQueue(baseTask);
}
public unsafe void Return(
MatrixParallelTask* task
) { }
public unsafe bool Steal(
MatrixParallelTask* task
)
{
// TODO 使用提取的内容填充task
throw new NotImplementedException();
}
private class Predictor : IL4Predict
{
public PredictResult Predict(
Span<CacheLineDescription> node,
out int count
)
{
throw new NotImplementedException();
}
public void Prefetch(
nuint origin,
uint policy,
Span<byte> cache
)
{
throw new NotImplementedException();
}
public void WriteBack(
nuint origin,
uint policy,
ReadOnlySpan<byte> cache
)
{
MatrixCachePolicy pEnum = (MatrixCachePolicy)policy;
if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0)
{
byte* span = (byte*) origin;
cache.CopyTo(new Span<byte>(span, cache.Length));
}
}
}
}
}