From 556008ad8cb9b9c2910c1f93146e66dd5e3d1229 Mon Sep 17 00:00:00 2001 From: Nitload Date: Sun, 12 Apr 2026 23:55:18 +0800 Subject: [PATCH] L4 is now very unsafe --- .../HighPerformance/Memory/L4.Prefetch.cs | 1 + .../Numeric/Runtime/MatrixEnumerator.cs | 80 +++++------- .../Numeric/Runtime/MatrixOperatorPolicy.cs | 56 +++++++- .../Numeric/Runtime/MatrixParallel.cs | 85 +++++-------- .../Numeric/Runtime/MatrixParallelThread.cs | 120 ++++++++---------- 5 files changed, 177 insertions(+), 165 deletions(-) diff --git a/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs b/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs index 0e179118..b9dfe4bc 100644 --- a/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs +++ b/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs @@ -121,6 +121,7 @@ namespace StgSharp.HighPerformance.Memory head->_origin = address; head->_profile = pHandle.MapPolicy; head->_refCount = 0; + head->_predictor = predictIndex; head->_address = (nuint)(_data + index); // pre fetch them here diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixEnumerator.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixEnumerator.cs index c1a277fd..b03920f3 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixEnumerator.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixEnumerator.cs @@ -1,27 +1,30 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="MatrixEnumerator" +// file="MatrixEnumerator.cs" // Project: StgSharp -// AuthorGroup: Nitload -// Copyright (c) Nitload. All rights reserved. +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. 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. +// 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. // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- @@ -44,51 +47,38 @@ namespace StgSharp.Mathematics.Numeric.Runtime } - public unsafe interface IMatrixCachePredictor : IL4Predict - where T : unmanaged, INumber - where TSelf : class, IMatrixCachePredictor + public unsafe interface IMatrixCachePredictor : IL4Predict where T: unmanaged, INumber + where TSelf: class, IMatrixCachePredictor { - public bool NeedWriteBack { get; } - public static abstract PredictPolicy Policy { get; set; } } public sealed unsafe class BufferSequentialPredictor : IMatrixCachePredictor, T> - where T : unmanaged, INumber + where T: unmanaged, INumber { - private MatrixKernelEnumerator enumerator; - - public bool NeedWriteBack => throw new NotImplementedException(); + private MatrixParallelTask* origin; public static PredictPolicy Policy { get; set; } = PredictPolicy.SquareRowMajor; - private MatrixKernel* Buffer { get; set; } - - public PredictResult Predict( - Span node, - out int count - ) + public bool IsSamePolicy(uint policy, IL4Predict predict) { throw new NotImplementedException(); } - public void Prefetch( - nuint origin, - ulong policy, - Span cache - ) + public PredictResult Predict(Span node, out int count) { throw new NotImplementedException(); } - public void WriteBack( - nuint origin, - ulong policy, - ReadOnlySpan cache - ) + public void Prefetch(nuint origin, uint policy, Span cache) + { + throw new NotImplementedException(); + } + + public void WriteBack(nuint origin, uint policy, ReadOnlySpan cache) { throw new NotImplementedException(); } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixOperatorPolicy.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixOperatorPolicy.cs index 2c04c556..b00db5ce 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixOperatorPolicy.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixOperatorPolicy.cs @@ -1,3 +1,33 @@ +//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// file="MatrixOperatorPolicy.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. 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; @@ -6,10 +36,32 @@ using System.Threading.Tasks; namespace StgSharp.Mathematics.Numeric.Runtime { - internal enum MatrixOperatorPolicy + internal enum MatrixCachePolicy { - Sequential = 1, + None = 0, + #region usage + + Left = 1, + Right = 2, + Ans = 3, + + #endregion + + #region index direction + + RowMajor = 4, + ColMajor = 5, + + #endregion + + #region shape + + Square = 8, + UpperTriangle = 9, + LowerTriangle = 10, + + #endregion } } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.cs index 4d8898f8..c829c0c7 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.cs @@ -1,27 +1,30 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="MatrixParallel" +// file="MatrixParallel.cs" // Project: StgSharp -// AuthorGroup: Nitload -// Copyright (c) Nitload. All rights reserved. +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. 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. +// 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. // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- @@ -46,9 +49,7 @@ namespace StgSharp.Mathematics.Numeric public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6; - public static bool IsParallelMeaningful => IsParallelSupported && - IsInitialized && - _meaningful; + public static bool IsParallelMeaningful => IsParallelSupported && IsInitialized && _meaningful; public static int ContextCount { get; private set; } @@ -58,7 +59,7 @@ namespace StgSharp.Mathematics.Numeric { foreach (MatrixParallelThread t in _threads) { - t.Retire(); + t.Pause(); t.Dispose(); } foreach (MatrixParallelThread t in _retiredThreads) { @@ -93,9 +94,7 @@ namespace StgSharp.Mathematics.Numeric SIMDID simd = thread.Simd; ulong mask = simd.Mask; - contextMap[mask] = contextMap.TryGetValue(mask, out int contextCount) ? - (contextCount + 1) : - 1; + contextMap[mask] = contextMap.TryGetValue(mask, out int contextCount) ? (contextCount + 1) : 1; globalSimd = (globalSimd.Mask != 0) ? UniteSIMDID(globalSimd, simd) : simd; } @@ -113,10 +112,7 @@ namespace StgSharp.Mathematics.Numeric } else { MatrixParallelThread[] allThreads = _threads.PopRange(_threads.Count); - Array.Sort(allThreads, static( - left, - right - ) => CompareSIMDID(left.Simd, right.Simd)); + Array.Sort(allThreads, static(left, right) => CompareSIMDID(left.Simd, right.Simd)); int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, allThreads.Length - 1)); for (int i = 0; i < retireCount; i++) { RetireThread(allThreads[i]); @@ -129,37 +125,26 @@ namespace StgSharp.Mathematics.Numeric } [LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + [UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])] [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)] - private static partial int CompareSIMDID( - SIMDID left, - SIMDID right - ); + private static partial int CompareSIMDID(SIMDID left, SIMDID right); [LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + [UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])] [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)] - private static partial void LoadIntrinsicFunction( - IntrinsicContext* context, - ulong id - ); + private static partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id); [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void RetireThread( - MatrixParallelThread thread - ) + private static void RetireThread(MatrixParallelThread thread) { - thread.Retire(); + thread.Pause(); _retiredThreads.Push(thread); } [LibraryImport(Native.LibName, EntryPoint = "sn_get_unite_simd")] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + [UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])] [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)] - private static partial SIMDID UniteSIMDID( - SIMDID left, - SIMDID right - ); + private static partial SIMDID UniteSIMDID(SIMDID left, SIMDID right); } } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelThread.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelThread.cs index 08834cb1..3bb1fb3a 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelThread.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelThread.cs @@ -1,27 +1,30 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="MatrixParallelThread" +// file="MatrixParallelThread.cs" // Project: StgSharp -// AuthorGroup: Nitload -// Copyright (c) Nitload. All rights reserved. +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. 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. +// 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. // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- @@ -50,14 +53,14 @@ namespace StgSharp.Mathematics.Numeric { private IntrinsicContext* _context; - private MatrixParallelTask* _currentTask; - private volatile bool _disposed, _isRetireRequested; + private readonly MatrixParallelTask* _currentTask; + private volatile bool _disposed; private readonly ManualResetEventSlim _resetEvent; + + private volatile MatrixThreadState _target; private Thread _managedThread; - internal MatrixParallelThread( - int poolId - ) + internal MatrixParallelThread(int poolId) { _resetEvent = new ManualResetEventSlim(false); _managedThread = new Thread(MatrixParallelWorkLoad); @@ -69,8 +72,6 @@ namespace StgSharp.Mathematics.Numeric public ThreadPriority AfterWork { get; set; } - internal bool IsSingle { get; set; } - internal MatrixThreadState State { get; private set; } internal SIMDID Simd { get; private set; } @@ -88,31 +89,26 @@ namespace StgSharp.Mathematics.Numeric } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal void Retire() + internal void Pause() { ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelThread)); if (State == MatrixThreadState.Retired) { return; } - _isRetireRequested = true; _resetEvent.Set(); SpinWait.SpinUntil(() => State == MatrixThreadState.Retired); _managedThread.Join(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal void SetSingleThreadTask( - ref MatrixParallelTask task - ) + internal void SetSingleThreadTask(ref MatrixParallelTask task) { if (IsSingle) { *_currentTask = task; } } - protected virtual void Dispose( - bool disposing - ) + protected virtual void Dispose(bool disposing) { if (!_disposed) { @@ -152,37 +148,30 @@ namespace StgSharp.Mathematics.Numeric { break; } - if (_isRetireRequested) + if (_target == MatrixThreadState.Idle) { - State = MatrixThreadState.Retired; + State = MatrixThreadState.Idle; continue; } State = MatrixThreadState.Idle; State = MatrixThreadState.Running; _managedThread.Priority = ThreadPriority.Highest; - if (IsSingle) - { - MatrixParallelTask* outer = _currentTask; - // compute here - IsSingle = false; - } else - { - // MatrixParallelQueue taskQueue = BindedWrap.TaskQueue; - /* - if (0 > (int)p ->ComputeHandle) { - SpecialBufferCompute(p); - } - switch (p->ComputeMode) - { - case MatrixIndexStyle.BUFFER_INDEX: - BufferCompute(p); - break; - default: - break; - } - /**/ + // MatrixParallelQueue taskQueue = BindedWrap.TaskQueue; + /* + if (0 > (int)p ->ComputeHandle) { + SpecialBufferCompute(p); } + switch (p->ComputeMode) + { + case MatrixIndexStyle.BUFFER_INDEX: + BufferCompute(p); + break; + default: + break; + } + /**/ + _managedThread.Priority = AfterWork; State = MatrixThreadState.Idle; } @@ -191,24 +180,19 @@ namespace StgSharp.Mathematics.Numeric #pragma warning disable CA5393 [LibraryImport(Native.LibName, EntryPoint = "sn_get_simd_level_local")] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + [UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])] [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)] internal static partial ulong GetSimdId(); [LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + [UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])] [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)] - internal static partial void SetThreadAffinity( - int coreId - ); + internal static partial void SetThreadAffinity(int coreId); [LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")] - [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + [UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])] [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)] - private static unsafe partial void LoadIntrinsicFunction( - IntrinsicContext* context, - ulong id - ); + private static unsafe partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id); #pragma warning restore CA5393 } }