From c343e763edb2d22b6c6c3a4b2d7c03519be8eed7 Mon Sep 17 00:00:00 2001 From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com> Date: Thu, 7 May 2026 11:16:41 +0800 Subject: [PATCH] before rewrite of HLSF --- .../Memory/HLSFAllocator.Alloc.cs | 4 +- .../HighPerformance/Memory/HLSFAllocator.cs | 4 +- .../Numeric/Linear/MatrixCompute.cs | 2 +- .../Numeric/Runtime/MatrixParallelQueue.cs | 123 +++++------------- 4 files changed, 34 insertions(+), 99 deletions(-) diff --git a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs index b4a06aa5..149f7bbd 100644 --- a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs +++ b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs @@ -39,7 +39,7 @@ namespace StgSharp.HighPerformance.Memory nuint size ) { - if (size > _maxSize) { + if (size > _maxAllocSize) { throw new InvalidOperationException("Requested size exceeds maximum allocatable size."); } @@ -89,7 +89,7 @@ namespace StgSharp.HighPerformance.Memory out HlsfHandle handle ) { - if (size > _maxSize) + if (size > _maxAllocSize) { handle = default; return false; diff --git a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs index 358483e2..53d58540 100644 --- a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs +++ b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs @@ -47,7 +47,7 @@ namespace StgSharp.HighPerformance.Memory private readonly Action _freeHandle; private bool disposedValue; private readonly int _align; - private readonly nuint _maxSize; + private readonly nuint _maxAllocSize; private readonly nuint _size; private readonly SlabAllocator _entries; @@ -98,7 +98,7 @@ namespace StgSharp.HighPerformance.Memory size *= 4; _levelSizeArray[i] = size; } - _maxSize = maxBlockSize; + _maxAllocSize = maxBlockSize; _freeHandle = freeHandle ?? (static _ => { }); } diff --git a/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs b/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs index 33d6c90a..aff0c716 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 c2cc5a93..05e7c711 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs @@ -43,29 +43,32 @@ namespace StgSharp.Mathematics.Numeric internal unsafe interface IMatrixParallelQueue where TSelf : IMatrixParallelQueue { - void Return( - MatrixParallelTask* task, - in L4CacheLine left, - in L4CacheLine right, - in L4CacheLine ans + bool InitNumaPredictor( + out IL4Predict predictor ); - bool Steal( + bool RequestTaskResource( MatrixParallelTask* task, out L4CacheLine left, out L4CacheLine right, out L4CacheLine ans ); + void ReturnTaskResource( + MatrixParallelTask* task, + in L4CacheLine left, + in L4CacheLine right, + in L4CacheLine ans + ); + } internal interface IBufferQueueLeftRightAns : IMatrixParallelQueue { } - internal sealed unsafe class BufferQueueLeftRightAns_FullNuma : IMatrixParallelQueue + internal sealed unsafe class BufferQueueLeftRightAns_NoNuma : IMatrixParallelQueue { private MatrixParallelTask* _baseTask; - private L4 _buffer; private readonly nuint _byteSize; private readonly nuint _count; private volatile nuint _cursor; @@ -74,9 +77,9 @@ namespace StgSharp.Mathematics.Numeric private volatile nuint _mat2; private volatile nuint _mat3; - private BufferQueueLeftRightAns_FullNuma() { } + private BufferQueueLeftRightAns_NoNuma() { } - internal BufferQueueLeftRightAns_FullNuma( + internal BufferQueueLeftRightAns_NoNuma( MatrixParallelTask* baseTask ) { @@ -90,103 +93,35 @@ namespace StgSharp.Mathematics.Numeric _mat3 = (nuint)_baseTask->Mat3; } - public unsafe void Return( - MatrixParallelTask* task, - in L4CacheLine left, - in L4CacheLine right, - in L4CacheLine ans + public bool InitNumaPredictor( + out IL4Predict predictor ) { - if (left.Address != 0) { - left.Dispose(); - } - if (right.Address != 0) { - right.Dispose(); - } - if (ans.Address != 0) { - ans.Dispose(); - } + predictor = null; + return false; } - public unsafe bool Steal( + public unsafe bool RequestTaskResource( MatrixParallelTask* task, out L4CacheLine left, out L4CacheLine right, out L4CacheLine ans ) { - throw new NotImplementedException(); + left = new L4CacheLine(0, ref Unsafe.AsRef(null), ref Unsafe.AsRef(null)); + right = new L4CacheLine(0, ref Unsafe.AsRef(null), ref Unsafe.AsRef(null)); + ans = new L4CacheLine(0, ref Unsafe.AsRef(null), ref Unsafe.AsRef(null)); + return true; } - private class Predictor( - BufferQueueLeftRightAns_FullNuma host - ) : IL4Predict + public unsafe void ReturnTaskResource( + MatrixParallelTask* task, + in L4CacheLine left, + in L4CacheLine right, + in L4CacheLine ans + ) { - - private readonly BufferQueueLeftRightAns_FullNuma _host; - - public PredictResult Predict( - Span node, - out int count - ) - { - nuint begin = _host._cursor; - nuint mat1 = (nuint)_host._baseTask->Mat1; - nuint mat2 = (nuint)_host._baseTask->Mat2; - nuint mat3 = (nuint)_host._baseTask->Mat3; - nuint _cur = 16 * 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 = _host._byteSize; - 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 * ((nuint)_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 - ) - { - 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, - ReadOnlySpan cache - ) - { - MatrixCachePolicy pEnum = (MatrixCachePolicy)policy; - if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0) - { - byte* bPtr = (byte*)origin; - cache.CopyTo(new Span(bPtr, cache.Length)); - } - } - + return; } }