From e521bed222cbb8d12c9191f3b083521ce36d12ef Mon Sep 17 00:00:00 2001 From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com> Date: Thu, 19 Mar 2026 19:53:22 +0800 Subject: [PATCH] back to matrix, but trouble in combining L4 predict with matrixparallelqueue --- .cr/team/Editor/Code Cleanup.xml | 57 ++++++++ .../Internal/IntrinsicContext.cs | 10 +- .../Internal/MatrixParallelTask.cs | 6 +- .../Linear/MatrixCompute.Sequential.cs | 129 ++++++++++++++++++ .../Numeric/Linear/MatrixCompute.cs | 7 +- .../Numeric/Runtime/MatrixParallel.Publish.cs | 8 +- .../Numeric/Runtime/MatrixParallelQueue.cs | 2 +- .../Numeric/Runtime/MatrixParallelWrap.cs | 38 ++++-- .../Numeric/Runtime/MatrixPredictor.cs | 59 ++++++++ .../Numeric/Runtime/MatrixTaskFactory.cs | 117 ---------------- 10 files changed, 291 insertions(+), 142 deletions(-) create mode 100644 .cr/team/Editor/Code Cleanup.xml create mode 100644 StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Sequential.cs create mode 100644 StgSharp.Mathematics/Numeric/Runtime/MatrixPredictor.cs delete mode 100644 StgSharp.Mathematics/Numeric/Runtime/MatrixTaskFactory.cs diff --git a/.cr/team/Editor/Code Cleanup.xml b/.cr/team/Editor/Code Cleanup.xml new file mode 100644 index 00000000..f4cdadea --- /dev/null +++ b/.cr/team/Editor/Code Cleanup.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + False + False + + + + + + + + + + + + + + + + + + + + + + True + True + + + + + + + + True + True + + + + + True + True + + + + + \ No newline at end of file diff --git a/StgSharp.Mathematics/Internal/IntrinsicContext.cs b/StgSharp.Mathematics/Internal/IntrinsicContext.cs index c49c68a4..3da2ede2 100644 --- a/StgSharp.Mathematics/Internal/IntrinsicContext.cs +++ b/StgSharp.Mathematics/Internal/IntrinsicContext.cs @@ -159,6 +159,14 @@ namespace StgSharp.Mathematics.Internal scalar); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly nint Get( + MatrixIntrinsicHandle index + ) + { + return this[(int)index]; + } + } [InlineArray(MatrixElementType.IntrinsicCount)] @@ -171,4 +179,4 @@ namespace StgSharp.Mathematics.Internal #endregion -} \ No newline at end of file +} diff --git a/StgSharp.Mathematics/Internal/MatrixParallelTask.cs b/StgSharp.Mathematics/Internal/MatrixParallelTask.cs index 32e2a01a..abe9bcdd 100644 --- a/StgSharp.Mathematics/Internal/MatrixParallelTask.cs +++ b/StgSharp.Mathematics/Internal/MatrixParallelTask.cs @@ -36,10 +36,10 @@ namespace StgSharp.Mathematics.Internal internal unsafe struct MatrixParallelTask { - [FieldOffset(0)]public MatrixKernel* Mat1; // 8 + [FieldOffset(0)] public MatrixKernel* Mat1; // 8 [FieldOffset(8)] public MatrixKernel* Mat2; // 8 [FieldOffset(16)] public MatrixKernel* Mat3; // 16 - [FieldOffset(40)] public int Common_k; + [FieldOffset(40)] public int CommonK; [FieldOffset(32)] public int CommonX; [FieldOffset(36)] public int CommonY; [FieldOffset(44)] public int i; @@ -65,4 +65,4 @@ namespace StgSharp.Mathematics.Internal } } -} \ No newline at end of file +} diff --git a/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Sequential.cs b/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Sequential.cs new file mode 100644 index 00000000..614b61cf --- /dev/null +++ b/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Sequential.cs @@ -0,0 +1,129 @@ +//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// file="MatrixCompute.Sequential" +// 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 StgSharp.Mathematics.Numeric.Runtime; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using unsafe MkProc = delegate* unmanaged[Cdecl]; + +namespace StgSharp.Mathematics.Numeric.Linear +{ + public static unsafe partial class MatrixCompute + { + + public static void Add( + Matrix left, + Matrix right, + Matrix ans + ) + { + ArgumentNullException.ThrowIfNull(left, nameof(left)); + ArgumentNullException.ThrowIfNull(right, nameof(right)); + ArgumentNullException.ThrowIfNull(ans, nameof(ans)); + if ((left.ColumnLength != right.ColumnLength) || + (left.RowLength != right.RowLength) || + (left.ColumnLength != ans.ColumnLength) || + (left.RowLength != ans.RowLength)) { + throw new MatrixDimensionMismatchException( + left, right, ans + ); + } + MatrixElementType f32 = MatrixElementType.F32; + long count = ((long)left.KernelColumnLength) * left.KernelRowLength; + + MatrixKernel* leftPtr = left.Buffer; + MatrixKernel* rightPtr = right.Buffer; + MatrixKernel* ansPtr = ans.Buffer; + MkProc add = (MkProc)NumericalModule.GlobalContext. + mat_mk[f32.IntrinsicNode]. + Get(MatrixIntrinsicHandle.Add); + MatrixParallelTask* t = stackalloc MatrixParallelTask[1]; + + t->Mat1 = (MatrixKernel*)leftPtr; + t->Mat2 = (MatrixKernel*)rightPtr; + t->Mat3 = (MatrixKernel*)ansPtr; + t->ElementType = MatrixElementType.F64; + t->Scalar.Data(0) = count; + if (count <= 256) + { + add(t); + + // .buffer_add(leftPtr, rightPtr, ansPtr, count); + + return; + } else if (count <= 1024) + { + MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1); + add(t); + MatrixParallel.ReturnParallelResources(ref pool); + return; + } + MatrixParallelWrap taskGroup = MatrixParallel.ScheduleTask((nuint)add, t); + + taskGroup.LaunchParallel(SleepMode.DeepSleep); + taskGroup.Dispose(); + return; + } + + private sealed class SequentialPredictor : MatrixPredictor + { + + public override bool Predict( + Span node, + out int count + ) + { + throw new NotImplementedException(); + } + + public override void Prefetch( + nuint origin, + ulong policy, + Span cache + ) + { + throw new NotImplementedException(); + } + + public override void WriteBack( + nuint origin, + ulong policy, + ReadOnlySpan cache + ) + { + throw new NotImplementedException(); + } + + } + + } +} diff --git a/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs b/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs index ae6cbc33..371c325c 100644 --- a/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs +++ b/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs @@ -25,6 +25,7 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- +using StgSharp.HighPerformance.Memory; using StgSharp.Internal; using StgSharp.Mathematics; using System; @@ -37,7 +38,7 @@ using System.Threading.Tasks; namespace StgSharp.Mathematics.Numeric { - public partial class MatrixCompute + public static partial class MatrixCompute { [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -45,8 +46,8 @@ namespace StgSharp.Mathematics.Numeric MatrixKernel* buffer, int colLength, int x, - int y) - where T: unmanaged, INumber + int y + ) where T : unmanaged, INumber { return (MatrixKernel*)(buffer + (colLength * x + y)); } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs index 92d512c0..44937827 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs @@ -31,6 +31,7 @@ using StgSharp.Mathematics; using StgSharp.Mathematics.Internal; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Numerics; using System.Runtime.CompilerServices; @@ -48,6 +49,7 @@ namespace StgSharp.Mathematics.Numeric [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static unsafe MatrixParallelWrap ScheduleTask( + nuint computeHandle, MatrixParallelTask* package ) where TTask : IPrefetchPredict { @@ -61,8 +63,10 @@ namespace StgSharp.Mathematics.Numeric long recommendCount = /*count / /**/ 1024; MatrixParallelThread[] pool = LeadParallel((int)recommendCount); - MatrixParallelWrap wrap = new MatrixParallelWrap(pool); - wrap.TaskQueue = MatrixParallelQueue.Rent(package); + MatrixParallelWrap wrap = new(pool) + { + TaskQueue = MatrixParallelQueue.Rent(package) + }; return wrap; } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs index 3e71981e..c2838d83 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs @@ -61,7 +61,7 @@ namespace StgSharp.Mathematics.Numeric queue = new MatrixParallelQueue(); } queue._package = pack; - queue._maxCount = (ulong)pack->CommonX * (ulong)pack->CommonY; + queue._maxCount = ((ulong)pack->CommonX) * ((ulong)pack->CommonY); queue._cursor = 0; return queue; } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelWrap.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelWrap.cs index ef5c81a6..c6f8f51a 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelWrap.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelWrap.cs @@ -47,12 +47,12 @@ namespace StgSharp.Mathematics.Numeric } - public class MatrixParallelWrap : IDisposable + public sealed class MatrixParallelWrap : IDisposable { private MatrixParallelThread[] _threads; - private bool _disposed = false; - private int _count; + private bool _disposed; + private readonly CountdownEvent _countdownEvent; internal MatrixParallelWrap( MatrixParallelThread[] threadArray @@ -60,7 +60,7 @@ namespace StgSharp.Mathematics.Numeric { _threads = threadArray; IsMultithread = threadArray.Length > 1; - _count = threadArray.Length; + _countdownEvent = new CountdownEvent(threadArray.Length); } public ReadOnlySpan Threads @@ -81,11 +81,8 @@ namespace StgSharp.Mathematics.Numeric public void Dispose() { - if (_disposed) { - return; - } - MatrixParallel.ReturnParallelResources(ref _threads); - _disposed = true; + Dispose(true); + GC.SuppressFinalize(this); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -104,19 +101,30 @@ namespace StgSharp.Mathematics.Numeric public void WaitAllWraps() { - if (_disposed) { - throw new ObjectDisposedException(nameof(MatrixParallelWrap)); - } - SpinWait.SpinUntil(() => Volatile.Read(ref _count) == 0); + ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap)); + _countdownEvent.Wait(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void ReportThreadAccomplished() + { + ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap)); + _ = _countdownEvent.Signal(); + } + + private void Dispose( + bool disposing + ) { if (_disposed) { - throw new ObjectDisposedException(nameof(MatrixParallelWrap)); + return; } - Interlocked.Decrement(ref _count); + if (disposing) + { + MatrixParallel.ReturnParallelResources(ref _threads); + _countdownEvent.Dispose(); + } + _disposed = true; } } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixPredictor.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixPredictor.cs new file mode 100644 index 00000000..743de23b --- /dev/null +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixPredictor.cs @@ -0,0 +1,59 @@ +//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// file="MatrixPredictor" +// 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 abstract class MatrixPredictor : IPrefetchPredict + where TSelf : MatrixPredictor + { + + public abstract bool Predict( + Span node, + out int count + ); + + public abstract void Prefetch( + nuint origin, + ulong policy, + Span cache + ); + + public abstract void WriteBack( + nuint origin, + ulong policy, + ReadOnlySpan cache + ); + + } +} diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixTaskFactory.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixTaskFactory.cs deleted file mode 100644 index 6b0c5fce..00000000 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixTaskFactory.cs +++ /dev/null @@ -1,117 +0,0 @@ -//----------------------------------------------------------------------- -// ----------------------------------------------------------------------- -// file="MatrixTaskFactory" -// 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.Mathematics; -using StgSharp.Mathematics.Internal; -using StgSharp.Mathematics.Memory; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Numerics; -using System.Runtime.CompilerServices; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - -using hlsfAllocator = StgSharp.Mathematics.Memory.HybridLayerSegregatedFitAllocator; - -namespace StgSharp.Mathematics.Numeric -{ - internal static unsafe class MatrixParallelFactory - { - - private static hlsfAllocator _intermediateResult; - - private static -#if NET9_0_OR_GREATER - Lock -#else - object -#endif - _hlsfLock = new(); - - private static SlabAllocator _scalarAllocator ; - - private static SlabAllocator _wrapAllocator; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static MatrixParallelTask* CreateBaseTask() - { - MatrixParallelTask* p = (MatrixParallelTask*)_wrapAllocator.Allocate(); - Span s = new Span((byte*)p, sizeof(MatrixParallelTask)); - s.Fill(0); - return p; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ScalarPacket* CreateScalarPacket() - { - ScalarPacket* p = (ScalarPacket*)_scalarAllocator.Allocate(); - return p; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe MatrixParallelTask* FromExistPackage( - MatrixParallelTask* exist - ) - { - MatrixParallelTask* ptr = (MatrixParallelTask*)_wrapAllocator.Allocate(); - MatrixParallelTask.Copy(exist, ptr); - return ptr; - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - - public static void Release( - ScalarPacket* source - ) - { - _scalarAllocator.Free((nuint)source); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Release( - MatrixParallelTask* source - ) - { - _wrapAllocator.Free((nuint)source); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static void Init() - { - if (_intermediateResult is null) - { - _intermediateResult = new hlsfAllocator(64 * 16 * 16); - _scalarAllocator = SlabAllocator.Create(64 * 1024, SlabBufferLayout.Chunked); - _wrapAllocator = SlabAllocator. - Create(128 * 1024, SlabBufferLayout.Chunked); - } - } - - } -}