mat
This commit is contained in:
@@ -64,7 +64,10 @@ namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
get
|
||||
{
|
||||
if (y < 0 || y > _size.ElementColumnLength || x < 0 || x > _size.ElementRowLength) {
|
||||
if ((y < 0) ||
|
||||
(y > _size.ElementColumnLength) ||
|
||||
(x < 0) ||
|
||||
(x > _size.ElementRowLength)) {
|
||||
throw new ArgumentOutOfRangeException($"Index [{x},{y}] is out of matrix index range.");
|
||||
}
|
||||
long offset = GetElementOffset(x, y);
|
||||
@@ -75,7 +78,10 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
set
|
||||
{
|
||||
if (y < 0 || y > _size.ElementColumnLength || x < 0 || x > _size.ElementRowLength) {
|
||||
if ((y < 0) ||
|
||||
(y > _size.ElementColumnLength) ||
|
||||
(x < 0) ||
|
||||
(x > _size.ElementRowLength)) {
|
||||
throw new ArgumentOutOfRangeException($"Index [{x},{y}] is out of matrix index range.");
|
||||
}
|
||||
long offset = GetElementOffset(x, y);
|
||||
@@ -106,8 +112,8 @@ namespace StgSharp.Mathematics.Numeric
|
||||
ReadOnlySpan<MatrixIndex> row
|
||||
]
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
set { throw new NotImplementedException(); }
|
||||
get => throw new NotImplementedException();
|
||||
set => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -122,11 +128,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
public MatrixLayout Layout { get; init; }
|
||||
|
||||
internal MatrixKernel<T>* Buffer
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => (MatrixKernel<T>*)_buffer.BufferPointer;
|
||||
}
|
||||
internal MatrixKernel<T>* Buffer => (MatrixKernel<T>*)_buffer.BufferPointer;
|
||||
|
||||
protected internal int KernelColumnLength => _size.KernelColumnLength;
|
||||
|
||||
@@ -138,9 +140,9 @@ namespace StgSharp.Mathematics.Numeric
|
||||
int y
|
||||
)
|
||||
{
|
||||
return ref _boarder.IsInBuffer(x, y, in _size) ?
|
||||
ref _buffer[_boarder.GetIndexOffset(x, y, in _size)] :
|
||||
ref Unsafe.NullRef<MatrixKernel<T>>();
|
||||
return ref (_boarder.IsInBuffer(x, y, in _size) ?
|
||||
ref _buffer[_boarder.GetIndexOffset(x, y, in _size)] :
|
||||
ref Unsafe.NullRef<MatrixKernel<T>>());
|
||||
}
|
||||
|
||||
internal long GetElementOffset(
|
||||
@@ -158,7 +160,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
long kernelOffset = _boarder.GetIndexOffset(k_r, k_c, in _size);
|
||||
|
||||
return kernelOffset * 16 + 4 * r + c;
|
||||
return (kernelOffset * 16) + (4 * r) + c;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -195,8 +197,6 @@ namespace StgSharp.Mathematics.Numeric
|
||||
// Caller guarantees: indices in range, layout is dense square.
|
||||
int k_c = y / 4;
|
||||
int k_r = x / 4;
|
||||
int c = y % 4;
|
||||
int r = x % 4;
|
||||
return ref _buffer[_boarder.GetIndexOffset(k_c, k_r, in _size)];
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ using StgSharp.Mathematics.Numeric.Runtime;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -71,34 +72,45 @@ namespace StgSharp.Mathematics.Numeric.Linear
|
||||
t->Mat1 = (MatrixKernel*)leftPtr;
|
||||
t->Mat2 = (MatrixKernel*)rightPtr;
|
||||
t->Mat3 = (MatrixKernel*)ansPtr;
|
||||
t->ElementType = MatrixElementType.F64;
|
||||
t->ElementType = f32;
|
||||
t->ComputeHandle = MatrixIntrinsicHandle.Add;
|
||||
t->Scalar.Data<long>(0) = count;
|
||||
SequentialOperation(t, count);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void SequentialOperation(
|
||||
MatrixParallelTask* task,
|
||||
long count
|
||||
)
|
||||
{
|
||||
MatrixElementType elementType = task->ElementType;
|
||||
MatrixParallelThread[] pool;
|
||||
MkProc op = (MkProc)NumericalModule.GlobalContext.
|
||||
mat_mk[elementType.IntrinsicNode].
|
||||
Get(task->ComputeHandle);
|
||||
if (count <= 256)
|
||||
{
|
||||
add(t);
|
||||
|
||||
// .buffer_add(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
op(task);
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
pool = MatrixParallel.LeadParallel(1);
|
||||
add(t);
|
||||
op(task);
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
pool = MatrixParallel.LeadParallel((int)(count / 1024));
|
||||
MatrixParallelWrap taskGroup = MatrixParallel.ScheduleTask<SequentialPredictor>(pool, t);
|
||||
|
||||
pool = MatrixParallel.LeadParallel(count / 1024);
|
||||
MatrixParallelWrap taskGroup = MatrixParallel.ScheduleTask<SequentialPredictor>(pool, task);
|
||||
taskGroup.LaunchParallel(SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
private struct SequentialPredictor : IMatrixPredictor<SequentialPredictor>
|
||||
{
|
||||
|
||||
public static PredictPolicy Policy => PredictPolicy.Sequential;
|
||||
|
||||
public bool Predict(
|
||||
Span<CacheLinePrediction> node,
|
||||
out int count
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user