diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs b/StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs index ea463228..0cb4baf3 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs @@ -49,7 +49,12 @@ namespace StgSharp.Mathematics try { Matrix result = GCMatrix.FromDefault(left.ColumnLength, left.RowLength); - MatrixParallel.PublishTask(); + unsafe + { + MatrixParallelTaskPackage* handle = MatrixParallelFactory.CreateBaseTask(); + handle->Left = left.; + handle->Right = right.Kernel; + } MatrixParallel.LaunchParallel(SleepMode.DeepSleep); return result; } diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs b/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs index 1a23bf2b..f9b79612 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs @@ -71,6 +71,12 @@ namespace StgSharp.Mathematics set => throw new NotImplementedException(); } + internal override unsafe MatrixKernel* Buffer + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _buffer; + } + public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row) { AssertIndex(column, row); diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs b/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs index c1053dd3..610476a9 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs @@ -75,6 +75,12 @@ namespace StgSharp.Mathematics set => throw new NotImplementedException(); } + internal override unsafe MatrixKernel* Buffer + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => k_buffer; + } + public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row) { AssertIndex(column, row); diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.cs b/StgSharp.Common/Mathematics/Linear/Matrix.cs index 996b83d7..fd9312ab 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.cs @@ -32,7 +32,6 @@ using StgSharp.HighPerformance; using System; using System.Numerics; using System.Runtime.CompilerServices; -using System.Text; using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator; using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle; @@ -74,6 +73,8 @@ namespace StgSharp.Mathematics public static MatrixAllocation DefaultAllocation { get; set; } = MatrixAllocation.GC; + internal abstract MatrixKernel* Buffer { get; } + protected int KernelColumnLength { get; init; } protected int KernelRowLength { get; init; } @@ -156,7 +157,7 @@ namespace StgSharp.Mathematics public readonly MatrixSegment Current { -[MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] get => new(_current, _secondarySize, _secondaryStride); } @@ -201,9 +202,9 @@ namespace StgSharp.Mathematics public MatrixKernel* Current { -[MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] get; -[MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] private set; } = source - stride; diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs index 0952c837..87698137 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs @@ -62,6 +62,11 @@ namespace StgSharp.Mathematics internal unsafe struct MatrixParallelTaskPackage { + public static unsafe void Copy(MatrixParallelTaskPackage* source, MatrixParallelTaskPackage* target) + { + Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTaskPackage), sizeof(MatrixParallelTaskPackage)); + } + #region source // Group 1: Left/Right matrix kernel base address @@ -126,6 +131,6 @@ namespace StgSharp.Mathematics public long ReservedLong0; // 8 public long ReservedLong1; // 8 - #endregion + #endregion } } diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs index 71d5fb04..94234752 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs @@ -32,6 +32,7 @@ using StgSharp.HighPerformance.Memory; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; @@ -40,21 +41,31 @@ namespace StgSharp.Mathematics internal static unsafe class MatrixParallelFactory { - private static readonly SlabAllocator _scalarAllocator = SlabAllocator.Create(64, SlabBufferLayout.Chunked, true); + private static SlabAllocator _scalarAllocator ; - private static readonly SlabAllocator _wrapAllocator = SlabAllocator.Create(64, SlabBufferLayout.Chunked, true); + private static SlabAllocator _wrapAllocator ; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static MatrixParallelTaskPackage* CreateBaseTask() + { + MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate(); + return p; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ScalarPacket* CreateScalarPacket() { ScalarPacket* p = (ScalarPacket*)_scalarAllocator.Allocate(); return p; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Release(ScalarPacket* source) { _scalarAllocator.Free((nuint)source); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Release(MatrixParallelTaskPackage* source) { if (source->Scalar != null) { @@ -63,5 +74,12 @@ namespace StgSharp.Mathematics _wrapAllocator.Free((nuint)source); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void Init() + { + _scalarAllocator = SlabAllocator.Create(64, SlabBufferLayout.Chunked, true); + _wrapAllocator = SlabAllocator.Create(64, SlabBufferLayout.Chunked, true); + } + } } \ No newline at end of file diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs index 7a9f874b..024053c9 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs @@ -69,10 +69,7 @@ namespace StgSharp.Mathematics private static Thread[] _threads; private static MatrixParallelWrap[] _wraps; - private static readonly (int a, int b)[] LookupTable = - [(1, 1), (0, 2), (3, 0), (2, 1), - (1, 2), (0, 3), (3, 1), (2, 2), (1, 3), - (0, 4), (3, 2), (2, 3), (1, 4), (0, 5),]; + private static readonly (int a, int b)[] LookupTable = [ (1, 1), (0, 2), (3, 0), (2, 1), (1, 2), (0, 3), (3, 1), (2, 2), (1, 3), (0, 4), (3, 2), (2, 3), (1, 4), (0, 5), ]; private static volatile int RemainThreadCount = 0; private static readonly object _lock = new(); @@ -113,16 +110,16 @@ namespace StgSharp.Mathematics if (IsInitialized) { return; } + MatrixParallelFactory.Init(); int count = Environment.ProcessorCount - 4; count = count == 5 ? 4 : count; _threads = new Thread[count]; if (count < 6) { - _wraps = [new MatrixParallelWrap(0, count, true)]; + _wraps = [ new MatrixParallelWrap(0, count, true) ]; } else if (count == 6) { - _wraps = [new MatrixParallelWrap(0, 3, true), - new MatrixParallelWrap(3, 3, false)]; + _wraps = [ new MatrixParallelWrap(0, 3, true), new MatrixParallelWrap(3, 3, false) ]; } else { (int _3wrap, int _4wrap) = FastDecompose(count); @@ -144,6 +141,7 @@ namespace StgSharp.Mathematics { RemainThreadCount = _threads.Length; _AfterWork = (ThreadPriority)afterWork; + SetAllWrap(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -159,14 +157,6 @@ namespace StgSharp.Mathematics _ = Interlocked.Decrement(ref RemainThreadCount); } - public static unsafe void PublishTask(MatrixParallelTaskPackage* package) - { - int primCountPerWrap = package->PrimCount / _threads.Length, extraPrimCount = package->PrimCount % _threads.Length; - int secCountPerWrap = package->SecCount / _threads.Length, extraSecCount = package->SecCount % _threads.Length; - - foreach (MatrixParallelWrap wrap in _wraps) { } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ResetThreadCount() {