From 0ac7b2b71f2ac79698ec8c8e554503d385b03dcd Mon Sep 17 00:00:00 2001 From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com> Date: Fri, 20 Mar 2026 20:04:45 +0800 Subject: [PATCH] back to matrix, but trouble in combining L4 predict with matrixparallelqueue --- .../Numeric/Runtime/MatrixParallel.Source.cs | 17 +++++ .../Runtime/MatrixParallelQueue.Concurrent.cs | 71 +++++++++++++++++++ ...or.cs => MatrixParallelQueue.Predictor.cs} | 0 .../Numeric/Runtime/MatrixParallelQueue.cs | 22 +++--- .../Numeric/Runtime/MatrixParallelThread.cs | 18 +++-- 5 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Source.cs create mode 100644 StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.Concurrent.cs rename StgSharp.Mathematics/Numeric/Runtime/{MatrixPredictor.cs => MatrixParallelQueue.Predictor.cs} (100%) diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Source.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Source.cs new file mode 100644 index 00000000..7939a38b --- /dev/null +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Source.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StgSharp.Mathematics.Numeric.Runtime +{ + internal class MatrixParallel + { + + + public MatrixParallelTask BaseTask { get; set; } + public + + } +} diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.Concurrent.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.Concurrent.cs new file mode 100644 index 00000000..98c05ebb --- /dev/null +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.Concurrent.cs @@ -0,0 +1,71 @@ +//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// file="MatrixParallelQueue.Concurrent" +// 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 System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StgSharp.Mathematics.Numeric.Runtime +{ + internal unsafe class ConcurrentPointerQueue + { + + private nuint* _current; + private int[] _length; + private nuint[] _queue; + private int _bucketIndex; + private int _head; + private int _tail; + + public ConcurrentPointerQueue() + { + _queue = new nuint[8]; + } + + public void Enqueue( + nuint* pointer, + int count + ) + { + _head++; + _head %= _queue.Length; + _bucketIndex = 0; + _queue[_head] = (nuint)pointer; + _length[_head] = count; + } + + public int TryDequeue( + Span span + ) + { + throw new NotImplementedException(); + } + + } +} diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixPredictor.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.Predictor.cs similarity index 100% rename from StgSharp.Mathematics/Numeric/Runtime/MatrixPredictor.cs rename to StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.Predictor.cs diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs index 73ffabe4..3be87f00 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="MatrixParallelQueue" // 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 @@ -42,14 +42,13 @@ namespace StgSharp.Mathematics.Numeric internal unsafe class MatrixParallelQueue { - private static ConcurrentStack _recycle = []; - private MatrixParallelTask* _package; + private static readonly ConcurrentStack _recycle = []; private ulong _cursor; private ulong _maxCount; private MatrixParallelQueue() { } - public MatrixParallelTask* Package => _package; + public MatrixParallelTask* BaseTask { get; private set; } public MatrixElementType ElementType { get; private set; } @@ -58,12 +57,13 @@ namespace StgSharp.Mathematics.Numeric public static MatrixParallelQueue Rent( MatrixElementType elementType, MatrixIntrinsicHandle handle, - MatrixParallelTask* pack) + MatrixParallelTask* pack + ) { if (!_recycle.TryPop(out MatrixParallelQueue? queue)) { queue = new MatrixParallelQueue(); } - queue._package = pack; + queue.BaseTask = pack; queue._maxCount = ((ulong)pack->CommonX) * ((ulong)pack->CommonY); queue._cursor = 0; queue.ElementType = elementType; @@ -71,13 +71,17 @@ namespace StgSharp.Mathematics.Numeric return queue; } - public static void Return(MatrixParallelQueue queue) + public static void Return( + MatrixParallelQueue queue + ) { _recycle.Push(queue); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Steal(MatrixParallelTask* task) + public bool Steal( + MatrixParallelTask* task + ) { // TODO 使用提取的内容填充task throw new NotImplementedException(); diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelThread.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelThread.cs index a8c0b2dc..7fbd968d 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelThread.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelThread.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="MatrixParallelThread" // 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 @@ -53,7 +53,9 @@ namespace StgSharp.Mathematics.Numeric private readonly ManualResetEventSlim _resetEvent; private readonly Thread _managedThread; - internal MatrixParallelThread(int poolId) + internal MatrixParallelThread( + int poolId + ) { _resetEvent = new ManualResetEventSlim(false); _managedThread = new Thread(MatrixParallelWorkLoad); @@ -95,9 +97,13 @@ namespace StgSharp.Mathematics.Numeric [LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static partial void SetThreadAffinity(int coreId); + internal static partial void SetThreadAffinity( + int coreId + ); - protected virtual void Dispose(bool disposing) + protected virtual void Dispose( + bool disposing + ) { if (!_disposed) { @@ -143,7 +149,7 @@ namespace StgSharp.Mathematics.Numeric } else { MatrixParallelQueue taskQueue = BindedWrap.TaskQueue; - MatrixParallelTask* p = taskQueue.Package; + MatrixParallelTask* p = taskQueue.BaseTask; /* if (0 > (int)p ->ComputeHandle) { SpecialBufferCompute(p);