From 37a96ba4fcabc2e34fbe6d5776191b88334fb4d1 Mon Sep 17 00:00:00 2001 From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com> Date: Wed, 29 Apr 2026 20:01:07 +0800 Subject: [PATCH] matrix parallel start to use two different policy --- .../Numeric/Linear/MatrixCompute.cs | 2 +- .../Numeric/Runtime/MatrixParallelQueue.cs | 110 ++++++++++++++---- 2 files changed, 87 insertions(+), 25 deletions(-) diff --git a/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs b/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs index 75a37271..c13a2d3e 100644 --- a/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs +++ b/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs @@ -71,7 +71,7 @@ namespace StgSharp.Mathematics.Numeric op(task); return; } - MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask(task); + MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask(task); taskWrap.LaunchParallel(SleepMode.DeepSleep); taskWrap.Dispose(); } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs index 94ddcbfb..33f1a639 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs @@ -42,78 +42,139 @@ namespace StgSharp.Mathematics.Numeric internal unsafe interface IMatrixParallelQueue where TSelf : IMatrixParallelQueue { - static abstract bool NeedNumaBuffer { get; } - static abstract TSelf Create( MatrixParallelTask* baseTask, - L4 buffer + L4 buffer, + int numaCount ); void Return( - MatrixParallelTask* task + MatrixParallelTask* task, + in L4CacheLine left, + in L4CacheLine right, + in L4CacheLine ans ); bool Steal( - MatrixParallelTask* task + MatrixParallelTask* task, + out L4CacheLine left, + out L4CacheLine right, + out L4CacheLine ans ); } - internal sealed unsafe class BufferSequentialQueue : IMatrixParallelQueue + internal sealed unsafe class BufferQueueLeftRightAns : IMatrixParallelQueue { - private MatrixParallelTask* baseTask; + private MatrixParallelTask* _baseTask; + private bool _needNuma; + private L4 _buffer; + private volatile nuint _cursor; + private readonly nuint Count; - private BufferSequentialQueue() { } + private BufferQueueLeftRightAns() { } - private BufferSequentialQueue( + private BufferQueueLeftRightAns( MatrixParallelTask* baseTask ) { - this.baseTask = baseTask; + _baseTask = baseTask; + Count = ((nuint)_baseTask->CommonX) * ((nuint)_baseTask->CommonY); } - public static bool NeedNumaBuffer => throw new NotImplementedException(); - - public static unsafe BufferSequentialQueue Create( - MatrixParallelTask* baseTask, - L4 buffer + public static unsafe BufferQueueLeftRightAns Create( + MatrixParallelTask* baseTask, + L4 buffer, + int numaCount ) { - return new BufferSequentialQueue(baseTask); + return new BufferQueueLeftRightAns(baseTask); } public unsafe void Return( - MatrixParallelTask* task - ) { } + MatrixParallelTask* task, + in L4CacheLine left, + in L4CacheLine right, + in L4CacheLine ans + ) + { + if (left.Address != 0) { + left.Dispose(); + } + if (right.Address != 0) { + right.Dispose(); + } + if (ans.Address != 0) { + ans.Dispose(); + } + } public unsafe bool Steal( - MatrixParallelTask* task + MatrixParallelTask* task, + out L4CacheLine left, + out L4CacheLine right, + out L4CacheLine ans ) { throw new NotImplementedException(); } - private class Predictor : IL4Predict + private class Predictor( + BufferQueueLeftRightAns host + ) : IL4Predict { + private readonly BufferQueueLeftRightAns _host; + public PredictResult Predict( Span node, out int count ) { - throw new NotImplementedException(); + nuint begin = _host._cursor; + nuint mat1 = (nuint)_host._baseTask->Mat1; + nuint mat2 = (nuint)_host._baseTask->Mat2; + nuint mat3 = (nuint)_host._baseTask->Mat3; + nuint _cur = begin * _host._baseTask->ElementType.Size; + nuint mat1_cur = ((nuint)_host._baseTask->Mat1) + _cur; + nuint mat2_cur = ((nuint)_host._baseTask->Mat2) + _cur; + nuint mat3_cur = ((nuint)_host._baseTask->Mat3) + _cur; + nuint end = ((nuint)_host._baseTask->Mat1) + (((nuint)_host._baseTask->CommonX) * ((nuint)_host._baseTask->CommonY) * _host._baseTask->ElementType.Size); + nuint mat1_end = ((nuint)_host._baseTask->Mat1) + end; + nuint mat2_end = ((nuint)_host._baseTask->Mat2) + end; + nuint mat3_end = ((nuint)_host._baseTask->Mat3) + end; + + nuint size = 1024 * 16 * _host._baseTask->ElementType.Size; + for (int i = 0; i < node.Length; i += 3) + { + int cycle = i / 3; + ref CacheLineDescription prediction = ref node[i]; + prediction.Address = mat1_cur + size; + } + count = 1; + return PredictResult.Success; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Prefetch( nuint origin, uint policy, Span cache ) { - throw new NotImplementedException(); + MatrixCachePolicy pEnum = (MatrixCachePolicy)policy; + if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0) + { + if ((pEnum & (MatrixCachePolicy.Left | MatrixCachePolicy.Right)) != 0) + { + byte* bPtr = (byte*)origin; + new Span(bPtr, cache.Length).CopyTo(cache); + } + } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteBack( nuint origin, uint policy, @@ -123,8 +184,8 @@ namespace StgSharp.Mathematics.Numeric MatrixCachePolicy pEnum = (MatrixCachePolicy)policy; if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0) { - byte* span = (byte*) origin; - cache.CopyTo(new Span(span, cache.Length)); + byte* bPtr = (byte*)origin; + cache.CopyTo(new Span(bPtr, cache.Length)); } } @@ -132,3 +193,4 @@ namespace StgSharp.Mathematics.Numeric } } +