matrix parallel start to use two different policy

This commit is contained in:
Youmu Liu
2026-04-27 20:08:10 +08:00
parent ecb1f04770
commit ae664cb541
16 changed files with 170 additions and 239 deletions
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixEnumerator"
// file="MatrixCompute.Sequential"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
@@ -26,73 +26,21 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance.Memory;
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;
namespace StgSharp.Mathematics.Numeric.Runtime
using unsafe MkProc = delegate* unmanaged[Cdecl]<StgSharp.Mathematics.Numeric.Runtime.MatrixParallelTask*, void>;
namespace StgSharp.Mathematics.Numeric
{
public enum PredictPolicy
public static unsafe partial class MatrixCompute
{
Transparent = 0,
BufferSequential = 1,
SquareRowMajor = 2,
SquareColMajor = 3,
}
public unsafe interface IMatrixCachePredictor<TSelf> : IL4Predict
where TSelf : class, IMatrixCachePredictor<TSelf>
{
public static abstract PredictPolicy Policy { get; set; }
}
public sealed unsafe class BufferSequentialPredictor : IMatrixCachePredictor<BufferSequentialPredictor>
{
private readonly MatrixParallelTask* origin;
private readonly int _kernelSize;
public static PredictPolicy Policy { get; set; } = PredictPolicy.SquareRowMajor;
public bool IsSamePolicy(
uint policy,
IL4Predict predict
)
{
throw new NotImplementedException();
}
public PredictResult Predict(
Span<CacheLineDescription> node,
out int count
)
{
throw new NotImplementedException();
}
public void Prefetch(
nuint origin,
uint policy,
Span<byte> cache
)
{
throw new NotImplementedException();
}
public void WriteBack(
nuint origin,
uint policy,
ReadOnlySpan<byte> cache
)
{
throw new NotImplementedException();
}
}
}
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixCompute.Sequential"
// file="MatrixCompute.Single"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
@@ -25,18 +25,16 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance.Memory;
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;
using unsafe MkProc = delegate* unmanaged[Cdecl]<StgSharp.Mathematics.Numeric.Runtime.MatrixParallelTask*, void>;
namespace StgSharp.Mathematics.Numeric.Linear
namespace StgSharp.Mathematics.Numeric
{
public static unsafe partial class MatrixCompute
{
@@ -78,33 +76,5 @@ namespace StgSharp.Mathematics.Numeric.Linear
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) || !MatrixParallel.IsParallelMeaningful)
{
op(task);
return;
} else if (count <= 1024)
{
pool = MatrixParallel.LeadParallel(1);
op(task);
MatrixParallel.ReturnParallelResources(ref pool);
return;
}
pool = MatrixParallel.LeadParallel(count / 1024);
MatrixParallelWrap taskGroup = MatrixParallel.ScheduleTask<BufferSequentialPredictor>(pool, task);
taskGroup.LaunchParallel(SleepMode.DeepSleep);
taskGroup.Dispose();
}
}
}
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixCompute"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -28,6 +28,7 @@
using StgSharp.HighPerformance.Memory;
using StgSharp.Internal;
using StgSharp.Mathematics;
using StgSharp.Mathematics.Numeric.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -36,9 +37,11 @@ using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using unsafe MkProc = delegate* unmanaged[Cdecl]<StgSharp.Mathematics.Numeric.Runtime.MatrixParallelTask*, void>;
namespace StgSharp.Mathematics.Numeric
{
public static partial class MatrixCompute
public static unsafe partial class MatrixCompute
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -49,7 +52,28 @@ namespace StgSharp.Mathematics.Numeric
int y
) where T : unmanaged, INumber<T>
{
return (MatrixKernel*)(buffer + (colLength * x + y));
return (MatrixKernel*)(buffer + ((colLength * x) + y));
}
[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) || !MatrixParallel.IsParallelMeaningful)
{
op(task);
return;
}
MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask<BufferSequentialQueue>(task);
taskWrap.LaunchParallel(SleepMode.DeepSleep);
taskWrap.Dispose();
}
}
@@ -50,6 +50,7 @@ namespace StgSharp.Mathematics.Numeric.Runtime
RowMajor = 4,
ColMajor = 5,
Sequential = 6,
#endregion
@@ -42,8 +42,8 @@ namespace StgSharp.Mathematics.Numeric
private static NumaMeasurement[] _numaMeasurements;
private static int[] _numaPayload;
private static LinearBag<MatrixParallelThread>[] _numaSyncHeavy;
private static LinearBag<MatrixParallelThread>[] _numaWaveFront;
private static MatrixParallelThread[][] _numaSyncHeavy;
private static MatrixParallelThread[][] _numaWaveFront;
private static MatrixParallelThread[] _threads;
private static void UpdateThreadPayload()
@@ -49,9 +49,9 @@ namespace StgSharp.Mathematics.Numeric
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe MatrixParallelWrap ScheduleTask<TTask>(
internal static unsafe MatrixParallelWrap ScheduleTask<TTaskQueue>(
MatrixParallelTask* package
) where TTask : IL4Predict
) where TTaskQueue : IMatrixParallelQueue<TTaskQueue>
{
/*
int primCount = package->PrimCount;
@@ -73,9 +73,10 @@ namespace StgSharp.Mathematics.Numeric
ulong total = (ulong)_numaWaveFront[i].Length;
NumaMeasurement measure = _numaMeasurements[i];
ulong numaPayload = measure.ActiveTasks;
uint numaActive = measure.ActiveThreadCount;
// uint numaActive = measure.ActiveThreadCount;
long curPayload = ((long)total) - (((long)(numaPayload + additionalPayload + 63)) / 64);
if (curPayload > payload)
if (curPayload < payload)
{
payload = curPayload;
payloadIndex = i;
@@ -84,7 +84,7 @@ namespace StgSharp.Mathematics.Numeric
_retiredThreads = new(RESERVED_THREAD_COUNT);
// init thread on all logical cores
MatrixParallelThread[][] numaWaveOrigin = Numa.CreateForeachThread<MatrixParallelThread>(
_numaWaveFront = Numa.CreateForeachThread<MatrixParallelThread>(
core => {
MatrixParallelThread thread = new(core);
SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready);
@@ -108,15 +108,15 @@ namespace StgSharp.Mathematics.Numeric
// retire threads
if (ContextCount == 1)
{
MatrixParallelThread[] threadArr = numaWaveOrigin[0];
MatrixParallelThread[] threadArr = _numaWaveFront[0];
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, threadArr.Length));
for (int i = 1; i <= retireCount; i++) {
RetireThread(threadArr[^i]);
}
_numaWaveFront[0] = [.. threadArr[0..^retireCount]];
_numaWaveFront[0] = threadArr[0..^retireCount];
} else
{
RetireWhenMultipleSIMDID(numaWaveOrigin);
RetireWhenMultipleSIMDID(_numaWaveFront);
}
_meaningful = _threads.Length > 1;
@@ -151,7 +151,6 @@ namespace StgSharp.Mathematics.Numeric
) => CompareSIMDID(left.Simd, right.Simd));
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, allThreads.Length));
// HashSet<MatrixParallelThread> retiredSet = [];
MatrixParallelThread[] retireArr = new MatrixParallelThread[retireCount];
for (int i = 0; i < retireCount; i++)
{
@@ -183,7 +182,7 @@ namespace StgSharp.Mathematics.Numeric
}
}
_numaWaveFront[numa] = [.. active];
_numaWaveFront[numa] = active;
}
_threads = allThreads[retireCount..];
@@ -39,18 +39,96 @@ using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
internal unsafe class MatrixParallelQueue<TPredictor>
where TPredictor : class, IMatrixCachePredictor<TPredictor>
internal unsafe interface IMatrixParallelQueue<TSelf> where TSelf : IMatrixParallelQueue<TSelf>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Steal(
MatrixParallelTask* task
static abstract bool NeedNumaBuffer { get; }
static abstract TSelf Create(
MatrixParallelTask* baseTask,
L4 buffer
);
void Return(
MatrixParallelTask* task
);
bool Steal(
MatrixParallelTask* task
);
}
internal sealed unsafe class BufferSequentialQueue : IMatrixParallelQueue<BufferSequentialQueue>
{
private MatrixParallelTask* baseTask;
private BufferSequentialQueue() { }
private BufferSequentialQueue(
MatrixParallelTask* baseTask
)
{
this.baseTask = baseTask;
}
public static bool NeedNumaBuffer => throw new NotImplementedException();
public static unsafe BufferSequentialQueue Create(
MatrixParallelTask* baseTask,
L4 buffer
)
{
return new BufferSequentialQueue(baseTask);
}
public unsafe void Return(
MatrixParallelTask* task
) { }
public unsafe bool Steal(
MatrixParallelTask* task
)
{
// TODO 使用提取的内容填充task
throw new NotImplementedException();
}
private class Predictor : IL4Predict
{
public PredictResult Predict(
Span<CacheLineDescription> node,
out int count
)
{
throw new NotImplementedException();
}
public void Prefetch(
nuint origin,
uint policy,
Span<byte> cache
)
{
throw new NotImplementedException();
}
public void WriteBack(
nuint origin,
uint policy,
ReadOnlySpan<byte> cache
)
{
MatrixCachePolicy pEnum = (MatrixCachePolicy)policy;
if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0)
{
byte* span = (byte*) origin;
cache.CopyTo(new Span<byte>(span, cache.Length));
}
}
}
}
}