daily commit on 0916

This commit is contained in:
Youmu Liu
2025-09-16 21:31:02 +08:00
parent fa54037a44
commit 9312407b0f
7 changed files with 54 additions and 23 deletions
@@ -49,7 +49,12 @@ namespace StgSharp.Mathematics
try try
{ {
Matrix<float> result = GCMatrix<float>.FromDefault(left.ColumnLength, left.RowLength); Matrix<float> result = GCMatrix<float>.FromDefault(left.ColumnLength, left.RowLength);
MatrixParallel.PublishTask(); unsafe
{
MatrixParallelTaskPackage* handle = MatrixParallelFactory.CreateBaseTask();
handle->Left = left.;
handle->Right = right.Kernel;
}
MatrixParallel.LaunchParallel(SleepMode.DeepSleep); MatrixParallel.LaunchParallel(SleepMode.DeepSleep);
return result; return result;
} }
@@ -71,6 +71,12 @@ namespace StgSharp.Mathematics
set => throw new NotImplementedException(); set => throw new NotImplementedException();
} }
internal override unsafe MatrixKernel* Buffer
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _buffer;
}
public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row) public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row)
{ {
AssertIndex(column, row); AssertIndex(column, row);
@@ -75,6 +75,12 @@ namespace StgSharp.Mathematics
set => throw new NotImplementedException(); 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) public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row)
{ {
AssertIndex(column, row); AssertIndex(column, row);
+5 -4
View File
@@ -32,7 +32,6 @@ using StgSharp.HighPerformance;
using System; using System;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text;
using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator; using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle; using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
@@ -74,6 +73,8 @@ namespace StgSharp.Mathematics
public static MatrixAllocation DefaultAllocation { get; set; } = MatrixAllocation.GC; public static MatrixAllocation DefaultAllocation { get; set; } = MatrixAllocation.GC;
internal abstract MatrixKernel* Buffer { get; }
protected int KernelColumnLength { get; init; } protected int KernelColumnLength { get; init; }
protected int KernelRowLength { get; init; } protected int KernelRowLength { get; init; }
@@ -156,7 +157,7 @@ namespace StgSharp.Mathematics
public readonly MatrixSegment Current public readonly MatrixSegment Current
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_current, _secondarySize, _secondaryStride); get => new(_current, _secondarySize, _secondaryStride);
} }
@@ -201,9 +202,9 @@ namespace StgSharp.Mathematics
public MatrixKernel* Current public MatrixKernel* Current
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get; get;
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private set; private set;
} = source - stride; } = source - stride;
@@ -62,6 +62,11 @@ namespace StgSharp.Mathematics
internal unsafe struct MatrixParallelTaskPackage internal unsafe struct MatrixParallelTaskPackage
{ {
public static unsafe void Copy(MatrixParallelTaskPackage* source, MatrixParallelTaskPackage* target)
{
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTaskPackage), sizeof(MatrixParallelTaskPackage));
}
#region source #region source
// Group 1: Left/Right matrix kernel base address // Group 1: Left/Right matrix kernel base address
@@ -32,6 +32,7 @@ using StgSharp.HighPerformance.Memory;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -40,21 +41,31 @@ namespace StgSharp.Mathematics
internal static unsafe class MatrixParallelFactory internal static unsafe class MatrixParallelFactory
{ {
private static readonly SlabAllocator<ScalarPacket> _scalarAllocator = SlabAllocator<ScalarPacket>.Create(64, SlabBufferLayout.Chunked, true); private static SlabAllocator<ScalarPacket> _scalarAllocator ;
private static readonly SlabAllocator<MatrixParallelTaskPackage> _wrapAllocator = SlabAllocator<MatrixParallelTaskPackage>.Create(64, SlabBufferLayout.Chunked, true); private static SlabAllocator<MatrixParallelTaskPackage> _wrapAllocator ;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MatrixParallelTaskPackage* CreateBaseTask()
{
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
return p;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ScalarPacket* CreateScalarPacket() public static ScalarPacket* CreateScalarPacket()
{ {
ScalarPacket* p = (ScalarPacket*)_scalarAllocator.Allocate(); ScalarPacket* p = (ScalarPacket*)_scalarAllocator.Allocate();
return p; return p;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Release(ScalarPacket* source) public static void Release(ScalarPacket* source)
{ {
_scalarAllocator.Free((nuint)source); _scalarAllocator.Free((nuint)source);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Release(MatrixParallelTaskPackage* source) public static void Release(MatrixParallelTaskPackage* source)
{ {
if (source->Scalar != null) { if (source->Scalar != null) {
@@ -63,5 +74,12 @@ namespace StgSharp.Mathematics
_wrapAllocator.Free((nuint)source); _wrapAllocator.Free((nuint)source);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Init()
{
_scalarAllocator = SlabAllocator<ScalarPacket>.Create(64, SlabBufferLayout.Chunked, true);
_wrapAllocator = SlabAllocator<MatrixParallelTaskPackage>.Create(64, SlabBufferLayout.Chunked, true);
}
} }
} }
@@ -69,10 +69,7 @@ namespace StgSharp.Mathematics
private static Thread[] _threads; private static Thread[] _threads;
private static MatrixParallelWrap[] _wraps; private static MatrixParallelWrap[] _wraps;
private static readonly (int a, int b)[] LookupTable = 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), ];
[(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 volatile int RemainThreadCount = 0;
private static readonly object _lock = new(); private static readonly object _lock = new();
@@ -113,16 +110,16 @@ namespace StgSharp.Mathematics
if (IsInitialized) { if (IsInitialized) {
return; return;
} }
MatrixParallelFactory.Init();
int count = Environment.ProcessorCount - 4; int count = Environment.ProcessorCount - 4;
count = count == 5 ? 4 : count; count = count == 5 ? 4 : count;
_threads = new Thread[count]; _threads = new Thread[count];
if (count < 6) if (count < 6)
{ {
_wraps = [new MatrixParallelWrap(0, count, true)]; _wraps = [ new MatrixParallelWrap(0, count, true) ];
} else if (count == 6) } else if (count == 6)
{ {
_wraps = [new MatrixParallelWrap(0, 3, true), _wraps = [ new MatrixParallelWrap(0, 3, true), new MatrixParallelWrap(3, 3, false) ];
new MatrixParallelWrap(3, 3, false)];
} else } else
{ {
(int _3wrap, int _4wrap) = FastDecompose(count); (int _3wrap, int _4wrap) = FastDecompose(count);
@@ -144,6 +141,7 @@ namespace StgSharp.Mathematics
{ {
RemainThreadCount = _threads.Length; RemainThreadCount = _threads.Length;
_AfterWork = (ThreadPriority)afterWork; _AfterWork = (ThreadPriority)afterWork;
SetAllWrap();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -159,14 +157,6 @@ namespace StgSharp.Mathematics
_ = Interlocked.Decrement(ref RemainThreadCount); _ = 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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ResetThreadCount() public static void ResetThreadCount()
{ {