before rewrite of HLSF

This commit is contained in:
Youmu Liu
2026-05-07 11:16:41 +08:00
parent e636410ba6
commit c343e763ed
4 changed files with 34 additions and 99 deletions
@@ -39,7 +39,7 @@ namespace StgSharp.HighPerformance.Memory
nuint size nuint size
) )
{ {
if (size > _maxSize) { if (size > _maxAllocSize) {
throw new InvalidOperationException("Requested size exceeds maximum allocatable size."); throw new InvalidOperationException("Requested size exceeds maximum allocatable size.");
} }
@@ -89,7 +89,7 @@ namespace StgSharp.HighPerformance.Memory
out HlsfHandle handle out HlsfHandle handle
) )
{ {
if (size > _maxSize) if (size > _maxAllocSize)
{ {
handle = default; handle = default;
return false; return false;
@@ -47,7 +47,7 @@ namespace StgSharp.HighPerformance.Memory
private readonly Action<nuint> _freeHandle; private readonly Action<nuint> _freeHandle;
private bool disposedValue; private bool disposedValue;
private readonly int _align; private readonly int _align;
private readonly nuint _maxSize; private readonly nuint _maxAllocSize;
private readonly nuint _size; private readonly nuint _size;
private readonly SlabAllocator<Entry> _entries; private readonly SlabAllocator<Entry> _entries;
@@ -98,7 +98,7 @@ namespace StgSharp.HighPerformance.Memory
size *= 4; size *= 4;
_levelSizeArray[i] = size; _levelSizeArray[i] = size;
} }
_maxSize = maxBlockSize; _maxAllocSize = maxBlockSize;
_freeHandle = freeHandle ?? (static _ => { }); _freeHandle = freeHandle ?? (static _ => { });
} }
@@ -71,7 +71,7 @@ namespace StgSharp.Mathematics.Numeric
op(task); op(task);
return; return;
} }
MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask<BufferQueueLeftRightAns_FullNuma>(task); MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask<BufferQueueLeftRightAns_NoNuma>(task);
taskWrap.LaunchParallel(SleepMode.DeepSleep); taskWrap.LaunchParallel(SleepMode.DeepSleep);
taskWrap.Dispose(); taskWrap.Dispose();
} }
@@ -43,29 +43,32 @@ namespace StgSharp.Mathematics.Numeric
internal unsafe interface IMatrixParallelQueue<TSelf> where TSelf : IMatrixParallelQueue<TSelf> internal unsafe interface IMatrixParallelQueue<TSelf> where TSelf : IMatrixParallelQueue<TSelf>
{ {
void Return( bool InitNumaPredictor(
MatrixParallelTask* task, out IL4Predict predictor
in L4CacheLine left,
in L4CacheLine right,
in L4CacheLine ans
); );
bool Steal( bool RequestTaskResource(
MatrixParallelTask* task, MatrixParallelTask* task,
out L4CacheLine left, out L4CacheLine left,
out L4CacheLine right, out L4CacheLine right,
out L4CacheLine ans out L4CacheLine ans
); );
void ReturnTaskResource(
MatrixParallelTask* task,
in L4CacheLine left,
in L4CacheLine right,
in L4CacheLine ans
);
} }
internal interface IBufferQueueLeftRightAns : IMatrixParallelQueue<IBufferQueueLeftRightAns> { } internal interface IBufferQueueLeftRightAns : IMatrixParallelQueue<IBufferQueueLeftRightAns> { }
internal sealed unsafe class BufferQueueLeftRightAns_FullNuma : IMatrixParallelQueue<IBufferQueueLeftRightAns> internal sealed unsafe class BufferQueueLeftRightAns_NoNuma : IMatrixParallelQueue<IBufferQueueLeftRightAns>
{ {
private MatrixParallelTask* _baseTask; private MatrixParallelTask* _baseTask;
private L4 _buffer;
private readonly nuint _byteSize; private readonly nuint _byteSize;
private readonly nuint _count; private readonly nuint _count;
private volatile nuint _cursor; private volatile nuint _cursor;
@@ -74,9 +77,9 @@ namespace StgSharp.Mathematics.Numeric
private volatile nuint _mat2; private volatile nuint _mat2;
private volatile nuint _mat3; private volatile nuint _mat3;
private BufferQueueLeftRightAns_FullNuma() { } private BufferQueueLeftRightAns_NoNuma() { }
internal BufferQueueLeftRightAns_FullNuma( internal BufferQueueLeftRightAns_NoNuma(
MatrixParallelTask* baseTask MatrixParallelTask* baseTask
) )
{ {
@@ -90,103 +93,35 @@ namespace StgSharp.Mathematics.Numeric
_mat3 = (nuint)_baseTask->Mat3; _mat3 = (nuint)_baseTask->Mat3;
} }
public unsafe void Return( public bool InitNumaPredictor(
MatrixParallelTask* task, out IL4Predict predictor
in L4CacheLine left,
in L4CacheLine right,
in L4CacheLine ans
) )
{ {
if (left.Address != 0) { predictor = null;
left.Dispose(); return false;
}
if (right.Address != 0) {
right.Dispose();
}
if (ans.Address != 0) {
ans.Dispose();
}
} }
public unsafe bool Steal( public unsafe bool RequestTaskResource(
MatrixParallelTask* task, MatrixParallelTask* task,
out L4CacheLine left, out L4CacheLine left,
out L4CacheLine right, out L4CacheLine right,
out L4CacheLine ans out L4CacheLine ans
) )
{ {
throw new NotImplementedException(); left = new L4CacheLine(0, ref Unsafe.AsRef<int>(null), ref Unsafe.AsRef<int>(null));
right = new L4CacheLine(0, ref Unsafe.AsRef<int>(null), ref Unsafe.AsRef<int>(null));
ans = new L4CacheLine(0, ref Unsafe.AsRef<int>(null), ref Unsafe.AsRef<int>(null));
return true;
} }
private class Predictor( public unsafe void ReturnTaskResource(
BufferQueueLeftRightAns_FullNuma host MatrixParallelTask* task,
) : IL4Predict in L4CacheLine left,
in L4CacheLine right,
in L4CacheLine ans
)
{ {
return;
private readonly BufferQueueLeftRightAns_FullNuma _host;
public PredictResult Predict(
Span<CacheLineDescription> node,
out int count
)
{
nuint begin = _host._cursor;
nuint mat1 = (nuint)_host._baseTask->Mat1;
nuint mat2 = (nuint)_host._baseTask->Mat2;
nuint mat3 = (nuint)_host._baseTask->Mat3;
nuint _cur = 16 * begin * _host._baseTask->ElementType.Size;
nuint mat1_cur = ((nuint)_host._baseTask->Mat1) + _cur;
nuint mat2_cur = ((nuint)_host._baseTask->Mat2) + _cur;
nuint mat3_cur = ((nuint)_host._baseTask->Mat3) + _cur;
nuint end = _host._byteSize;
nuint mat1_end = ((nuint)_host._baseTask->Mat1) + end;
nuint mat2_end = ((nuint)_host._baseTask->Mat2) + end;
nuint mat3_end = ((nuint)_host._baseTask->Mat3) + end;
nuint size = 1024 * 16 * ((nuint)_host._baseTask->ElementType.Size);
for (int i = 0; i < node.Length; i += 3)
{
int cycle = i / 3;
ref CacheLineDescription prediction = ref node[i];
prediction.Address = mat1_cur + size;
}
count = 1;
return PredictResult.Success;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Prefetch(
nuint origin,
uint policy,
Span<byte> cache
)
{
MatrixCachePolicy pEnum = (MatrixCachePolicy)policy;
if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0)
{
if ((pEnum & (MatrixCachePolicy.Left | MatrixCachePolicy.Right)) != 0)
{
byte* bPtr = (byte*)origin;
new Span<byte>(bPtr, cache.Length).CopyTo(cache);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteBack(
nuint origin,
uint policy,
ReadOnlySpan<byte> cache
)
{
MatrixCachePolicy pEnum = (MatrixCachePolicy)policy;
if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0)
{
byte* bPtr = (byte*)origin;
cache.CopyTo(new Span<byte>(bPtr, cache.Length));
}
}
} }
} }