This commit is contained in:
Youmu Liu
2026-03-26 18:02:13 +08:00
parent d960d02972
commit 06e3aa119b
20 changed files with 198 additions and 169 deletions
@@ -45,7 +45,7 @@ namespace StgSharp.Mathematics.Numeric
private class AwaitingQueue
{
private Element _quickElements = new();
private Element _quickElements;
private volatile int _count;
private readonly Queue<WaitHandle> _fallback = [];
@@ -51,7 +51,7 @@ namespace StgSharp.Mathematics.Numeric
internal static unsafe MatrixParallelWrap ScheduleTask<TTask>(
MatrixParallelThread[] workers,
MatrixParallelTask* package
) where TTask : IPrefetchPredict
) where TTask : IL4Predict
{
/*
int primCount = package->PrimCount;
@@ -1,17 +0,0 @@
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
}
}
@@ -34,6 +34,16 @@ using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric.Runtime
{
internal interface IMatrixPredictor<TSelf> : IPrefetchPredict
where TSelf : unmanaged, IMatrixPredictor<TSelf> { }
internal interface IMatrixPredictor<TSelf> : IL4Predict
where TSelf : unmanaged, IMatrixPredictor<TSelf>
{
static abstract PredictPolicy Policy { get; }
}
internal enum PredictPolicy
{
Transparent = 0,
Sequential = 1,
}
}
@@ -43,22 +43,23 @@ namespace StgSharp.Mathematics.Numeric
{
private static readonly ConcurrentStack<MatrixParallelQueue> _recycle = [];
private ulong _cursor;
private ulong _maxCount;
private long _cursor;
private long _maxCount;
private MatrixParallelQueue() { }
public MatrixParallelTask* BaseTask { get; private set; }
public static MatrixParallelQueue Rent(
MatrixParallelTask* pack
MatrixParallelTask* pack,
long count
)
{
if (!_recycle.TryPop(out MatrixParallelQueue? queue)) {
queue = new MatrixParallelQueue();
}
queue.BaseTask = pack;
queue._maxCount = ((ulong)pack->CommonX) * ((ulong)pack->CommonY);
queue._maxCount = count;
queue._cursor = 0;
return queue;
}
@@ -49,7 +49,8 @@ namespace StgSharp.Mathematics.Numeric
{
private IntrinsicContext* _context;
private volatile bool _disposed, _isRetireRequested = false;
private MatrixParallelTask* _currentTask;
private volatile bool _disposed, _isRetireRequested ;
private readonly ManualResetEventSlim _resetEvent;
private readonly Thread _managedThread;
@@ -67,8 +68,6 @@ namespace StgSharp.Mathematics.Numeric
public ThreadPriority AfterWork { get; set; }
internal ManualResetEventSlim? LeaderEvent { get; set; }
internal ref readonly IntrinsicContext Intrinsic => ref (*_context);
internal bool IsSingle { get; set; }
@@ -91,6 +90,16 @@ namespace StgSharp.Mathematics.Numeric
_resetEvent.Set();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void SetSingleThreadTask(
ref MatrixParallelTask task
)
{
if (IsSingle) {
*_currentTask = task;
}
}
protected virtual void Dispose(
bool disposing
)
@@ -117,6 +126,7 @@ namespace StgSharp.Mathematics.Numeric
_ = Thread.Yield();
SIMDID feature = new(GetSimdId());
IntrinsicContext* contextPtr = stackalloc IntrinsicContext[1];
MatrixParallelTask* taskPtr = stackalloc MatrixParallelTask[4];
LoadIntrinsicFunction(contextPtr, feature.Mask);
_context = contextPtr;
State = MatrixThreadState.Ready;
@@ -134,11 +144,11 @@ namespace StgSharp.Mathematics.Numeric
_managedThread.Priority = ThreadPriority.Highest;
if (IsSingle)
{
// TODO 将外部线程绑定到当前线程上,并执行任务包
MatrixParallelTask* outer = _currentTask;
IsSingle = false;
} else
{
MatrixParallelQueue taskQueue = BindedWrap.TaskQueue;
MatrixParallelTask* p = taskQueue.BaseTask;
/*
if (0 > (int)p ->ComputeHandle) {
SpecialBufferCompute(p);
@@ -162,19 +172,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
);
[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,
@@ -26,6 +26,7 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Collections;
using StgSharp.HighPerformance.Memory;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
@@ -59,25 +60,16 @@ namespace StgSharp.Mathematics.Numeric
)
{
_threads = threadArray;
IsMultithread = threadArray.Length > 1;
_countdownEvent = new CountdownEvent(threadArray.Length);
}
public ReadOnlySpan<MatrixParallelThread> Threads
{
get
{
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap));
return _threads;
}
}
public ReadOnlySpan<MatrixParallelThread> Threads => _disposed ? [] : _threads;
public ThreadPriority AfterWork { get; private set; }
internal MatrixParallelQueue TaskQueue { get; set; }
internal L4 DataCache { get; set; }
private bool IsMultithread { get; init; }
internal MatrixParallelQueue TaskQueue { get; set; }
public void Dispose()
{
@@ -90,6 +82,7 @@ namespace StgSharp.Mathematics.Numeric
SleepMode afterWork
)
{
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap));
AfterWork = (ThreadPriority)afterWork;
foreach (MatrixParallelThread thread in _threads)
{
@@ -101,7 +94,9 @@ namespace StgSharp.Mathematics.Numeric
public void WaitAllWraps()
{
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap));
if (_disposed) {
return;
}
_countdownEvent.Wait();
}