matrix parallel start to use two different policy

This commit is contained in:
Youmu Liu
2026-04-29 20:01:07 +08:00
parent ae664cb541
commit 37a96ba4fc
2 changed files with 87 additions and 25 deletions
@@ -71,7 +71,7 @@ namespace StgSharp.Mathematics.Numeric
op(task); op(task);
return; return;
} }
MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask<BufferSequentialQueue>(task); MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask<BufferQueueLeftRightAns>(task);
taskWrap.LaunchParallel(SleepMode.DeepSleep); taskWrap.LaunchParallel(SleepMode.DeepSleep);
taskWrap.Dispose(); taskWrap.Dispose();
} }
@@ -42,78 +42,139 @@ namespace StgSharp.Mathematics.Numeric
internal unsafe interface IMatrixParallelQueue<TSelf> where TSelf : IMatrixParallelQueue<TSelf> internal unsafe interface IMatrixParallelQueue<TSelf> where TSelf : IMatrixParallelQueue<TSelf>
{ {
static abstract bool NeedNumaBuffer { get; }
static abstract TSelf Create( static abstract TSelf Create(
MatrixParallelTask* baseTask, MatrixParallelTask* baseTask,
L4 buffer L4 buffer,
int numaCount
); );
void Return( void Return(
MatrixParallelTask* task MatrixParallelTask* task,
in L4CacheLine left,
in L4CacheLine right,
in L4CacheLine ans
); );
bool Steal( bool Steal(
MatrixParallelTask* task MatrixParallelTask* task,
out L4CacheLine left,
out L4CacheLine right,
out L4CacheLine ans
); );
} }
internal sealed unsafe class BufferSequentialQueue : IMatrixParallelQueue<BufferSequentialQueue> internal sealed unsafe class BufferQueueLeftRightAns : IMatrixParallelQueue<BufferQueueLeftRightAns>
{ {
private MatrixParallelTask* baseTask; private MatrixParallelTask* _baseTask;
private bool _needNuma;
private L4 _buffer;
private volatile nuint _cursor;
private readonly nuint Count;
private BufferSequentialQueue() { } private BufferQueueLeftRightAns() { }
private BufferSequentialQueue( private BufferQueueLeftRightAns(
MatrixParallelTask* baseTask MatrixParallelTask* baseTask
) )
{ {
this.baseTask = baseTask; _baseTask = baseTask;
Count = ((nuint)_baseTask->CommonX) * ((nuint)_baseTask->CommonY);
} }
public static bool NeedNumaBuffer => throw new NotImplementedException(); public static unsafe BufferQueueLeftRightAns Create(
MatrixParallelTask* baseTask,
public static unsafe BufferSequentialQueue Create( L4 buffer,
MatrixParallelTask* baseTask, int numaCount
L4 buffer
) )
{ {
return new BufferSequentialQueue(baseTask); return new BufferQueueLeftRightAns(baseTask);
} }
public unsafe void Return( public unsafe void Return(
MatrixParallelTask* task MatrixParallelTask* task,
) { } in L4CacheLine left,
in L4CacheLine right,
in L4CacheLine ans
)
{
if (left.Address != 0) {
left.Dispose();
}
if (right.Address != 0) {
right.Dispose();
}
if (ans.Address != 0) {
ans.Dispose();
}
}
public unsafe bool Steal( public unsafe bool Steal(
MatrixParallelTask* task MatrixParallelTask* task,
out L4CacheLine left,
out L4CacheLine right,
out L4CacheLine ans
) )
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
private class Predictor : IL4Predict private class Predictor(
BufferQueueLeftRightAns host
) : IL4Predict
{ {
private readonly BufferQueueLeftRightAns _host;
public PredictResult Predict( public PredictResult Predict(
Span<CacheLineDescription> node, Span<CacheLineDescription> node,
out int count out int count
) )
{ {
throw new NotImplementedException(); nuint begin = _host._cursor;
nuint mat1 = (nuint)_host._baseTask->Mat1;
nuint mat2 = (nuint)_host._baseTask->Mat2;
nuint mat3 = (nuint)_host._baseTask->Mat3;
nuint _cur = 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 = ((nuint)_host._baseTask->Mat1) + (((nuint)_host._baseTask->CommonX) * ((nuint)_host._baseTask->CommonY) * _host._baseTask->ElementType.Size);
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 * _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( public void Prefetch(
nuint origin, nuint origin,
uint policy, uint policy,
Span<byte> cache Span<byte> cache
) )
{ {
throw new NotImplementedException(); 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( public void WriteBack(
nuint origin, nuint origin,
uint policy, uint policy,
@@ -123,8 +184,8 @@ namespace StgSharp.Mathematics.Numeric
MatrixCachePolicy pEnum = (MatrixCachePolicy)policy; MatrixCachePolicy pEnum = (MatrixCachePolicy)policy;
if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0) if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0)
{ {
byte* span = (byte*) origin; byte* bPtr = (byte*)origin;
cache.CopyTo(new Span<byte>(span, cache.Length)); cache.CopyTo(new Span<byte>(bPtr, cache.Length));
} }
} }
@@ -132,3 +193,4 @@ namespace StgSharp.Mathematics.Numeric
} }
} }