can create and retire matrix thread by numa infomation

This commit is contained in:
Youmu Liu
2026-04-15 18:08:08 +08:00
parent fdf11c729f
commit 2203a46eb7
11 changed files with 160 additions and 123 deletions
+3 -3
View File
@@ -74,9 +74,9 @@ namespace StgSharp.Threading
[LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
internal static partial void SetThreadAffinity(
uint globalCoreId,
uint numaNodeId
private static partial void SetThreadAffinity(
uint globalCoreId,
uint numaNodeId
);
#pragma warning restore CA5393
@@ -89,7 +89,7 @@ namespace StgSharp.Mathematics.Numeric.Linear
MkProc op = (MkProc)NumericalModule.GlobalContext.
mat_mk[elementType.IntrinsicNode].
Get(task->ComputeHandle);
if (count <= 256)
if ((count <= 256) || !MatrixParallel.IsParallelMeaningful)
{
op(task);
return;
@@ -101,43 +101,10 @@ namespace StgSharp.Mathematics.Numeric.Linear
return;
}
pool = MatrixParallel.LeadParallel(count / 1024);
MatrixParallelWrap taskGroup = MatrixParallel.ScheduleTask<SequentialPredictor>(pool, task);
MatrixParallelWrap taskGroup = MatrixParallel.ScheduleTask<BufferSequentialPredictor>(pool, task);
taskGroup.LaunchParallel(SleepMode.DeepSleep);
taskGroup.Dispose();
}
private struct SequentialPredictor : IMatrixPredictor<SequentialPredictor>
{
public static PredictPolicy Policy => PredictPolicy.Sequential;
public PredictResult Predict(
Span<CacheLinePrediction> node,
out int count
)
{
throw new NotImplementedException();
}
public void Prefetch(
nuint origin,
ulong policy,
Span<byte> cache
)
{
throw new NotImplementedException();
}
public void WriteBack(
nuint origin,
ulong policy,
ReadOnlySpan<byte> cache
)
{
throw new NotImplementedException();
}
}
}
}
@@ -76,7 +76,14 @@ namespace StgSharp.Mathematics.Numeric
IModuleInitializeProfile profile
)
{
MatrixParallel.Init();
try
{
MatrixParallel.Init();
}
finally
{
GC.Collect();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixEnumerator.cs"
// file="MatrixEnumerator"
// 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 in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -47,38 +44,52 @@ namespace StgSharp.Mathematics.Numeric.Runtime
}
public unsafe interface IMatrixCachePredictor<TSelf, T> : IL4Predict where T: unmanaged, INumber<T>
where TSelf: class, IMatrixCachePredictor<TSelf, T>
public unsafe interface IMatrixCachePredictor<TSelf> : IL4Predict
where TSelf : class, IMatrixCachePredictor<TSelf>
{
public static abstract PredictPolicy Policy { get; set; }
}
public sealed unsafe class BufferSequentialPredictor<T> : IMatrixCachePredictor<BufferSequentialPredictor<T>, T>
where T: unmanaged, INumber<T>
public sealed unsafe class BufferSequentialPredictor : IMatrixCachePredictor<BufferSequentialPredictor>
{
private MatrixParallelTask* origin;
private readonly MatrixParallelTask* origin;
private readonly int _kernelSize;
public static PredictPolicy Policy { get; set; } = PredictPolicy.SquareRowMajor;
public bool IsSamePolicy(uint policy, IL4Predict predict)
public bool IsSamePolicy(
uint policy,
IL4Predict predict
)
{
throw new NotImplementedException();
}
public PredictResult Predict(Span<CacheLinePrediction> node, out int count)
public PredictResult Predict(
Span<CacheLinePrediction> node,
out int count
)
{
throw new NotImplementedException();
}
public void Prefetch(nuint origin, uint policy, Span<byte> cache)
public void Prefetch(
nuint origin,
uint policy,
Span<byte> cache
)
{
throw new NotImplementedException();
}
public void WriteBack(nuint origin, uint policy, ReadOnlySpan<byte> cache)
public void WriteBack(
nuint origin,
uint policy,
ReadOnlySpan<byte> cache
)
{
throw new NotImplementedException();
}
@@ -25,6 +25,7 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -36,13 +37,26 @@ namespace StgSharp.Mathematics.Numeric
public static unsafe partial class MatrixParallel
{
private static L4[] _numaBuffer;
private static NumaMeasurement[] _numaMeasurements;
private static int[] _numaPayload;
private static MatrixParallelThread[][] _numaThreads;
private static MatrixParallelThread[] _threads;
private static void UpdateThreadPayload()
{
for (int i = 0; i < _numaPayload.Length; i++) { }
}
private struct NumaMeasurement
{
public uint ActivePredictors;
public uint ActiveThreadCount;
public ulong ActiveTasks;
}
}
}
@@ -43,8 +43,6 @@ namespace StgSharp.Mathematics.Numeric
private static CapacityFixedStack<MatrixParallelThread> _retiredThreads;
private static CapacityFixedStack<MatrixParallelThread> _threads;
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6;
public static bool IsParallelMeaningful => IsParallelSupported &&
@@ -84,9 +82,9 @@ namespace StgSharp.Mathematics.Numeric
contextMap = [];
int count = Environment.ProcessorCount;
SIMDID globalSimd = new SIMDID(0);
_threads = new(count);
_retiredThreads = new(RESERVED_THREAD_COUNT);
// init thread on all logical cores
_numaThreads = Numa.CreateForeachThread<MatrixParallelThread>(
core => {
MatrixParallelThread thread = new(core);
@@ -101,37 +99,97 @@ namespace StgSharp.Mathematics.Numeric
return thread;
});
// make global simd context
IntrinsicContext globalContext = new();
LoadIntrinsicFunction(&globalContext, globalSimd.Mask);
NumericalModule.GlobalContext = globalContext;
NumericalModule.GlobalIntrinsicMask = globalSimd;
ContextCount = contextMap.Count;
// retire threads
if (ContextCount == 1)
{
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, _threads.Count));
for (int i = 0; i < retireCount; i++) {
RetireThread(_threads.Pop());
MatrixParallelThread[] threadArr = _numaThreads[0];
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, threadArr.Length));
for (int i = 1; i <= retireCount; i++) {
RetireThread(threadArr[^i]);
}
_numaThreads[0] = threadArr[0..^retireCount];
} else
{
// TODO 切换来源
// TODO _threads退休并移除
MatrixParallelThread[] allThreads = _threads.PopRange(_threads.Count);
Array.Sort(allThreads, static(
left,
right
) => CompareSIMDID(left.Simd, right.Simd));
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, allThreads.Length - 1));
for (int i = 0; i < retireCount; i++) {
RetireThread(allThreads[i]);
}
_threads.PushRange(allThreads.AsSpan(retireCount));
RetireWhenMultipleSIMDID();
}
_meaningful = _threads.Count > 1;
_awaitingLeaderHandle = new AwaitingQueue();
_meaningful = _threads.Length > 1;
// _awaitingLeaderHandle = new AwaitingQueue();
IsInitialized = true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void RetireThread(
MatrixParallelThread thread
)
{
thread.Pause();
thread.Dispose();
_retiredThreads.Push(thread);
}
private static void RetireWhenMultipleSIMDID()
{
MatrixParallelThread[] allThreads = new MatrixParallelThread[Environment.ProcessorCount];
int offset = 0;
foreach (MatrixParallelThread[] threadArr in _numaThreads)
{
Array.Copy(threadArr, 0, allThreads, offset, threadArr.Length);
offset += threadArr.Length;
}
Array.Sort(allThreads, static(
left,
right
) => 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++)
{
MatrixParallelThread thread = allThreads[i];
retireArr[i] = thread;
RetireThread(thread);
}
// rebuild numa active arrays
for (int numa = 0; numa < _numaThreads.Length; numa++)
{
MatrixParallelThread[] source = _numaThreads[numa];
int keepCount = 0;
for (int i = 0; i < source.Length; i++)
{
if (!retireArr.Contains(source[i])) {
keepCount++;
}
}
MatrixParallelThread[] active = new MatrixParallelThread[keepCount];
int index = 0;
for (int i = 0; i < source.Length; i++)
{
MatrixParallelThread thread = source[i];
if (!retireArr.Contains(thread)) {
active[index++] = thread;
}
}
_numaThreads[numa] = active;
}
_threads = allThreads[retireCount..];
}
#region C invoke
[LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
@@ -148,15 +206,6 @@ namespace StgSharp.Mathematics.Numeric
ulong id
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void RetireThread(
MatrixParallelThread thread
)
{
thread.Pause();
_retiredThreads.Push(thread);
}
[LibraryImport(Native.LibName, EntryPoint = "sn_get_unite_simd")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
@@ -165,5 +214,6 @@ namespace StgSharp.Mathematics.Numeric
SIMDID right
);
#endregion
}
}
@@ -39,8 +39,8 @@ using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
internal unsafe class MatrixParallelQueue<TPredictor, T> where T : unmanaged, INumber<T>
where TPredictor : class, IMatrixCachePredictor<TPredictor, T>
internal unsafe class MatrixParallelQueue<TPredictor>
where TPredictor : class, IMatrixCachePredictor<TPredictor>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -48,7 +48,7 @@ namespace StgSharp.Mathematics.Numeric
}
public unsafe partial class MatrixParallelThread : IDisposable
public unsafe partial class MatrixParallelThread : IEquatable<MatrixParallelThread>, IDisposable
{
private IntrinsicContext* _context;
@@ -122,7 +122,7 @@ namespace StgSharp.Mathematics.Numeric
State = MatrixThreadState.Born;
// Bind OS-thread affinity once when the worker starts running
ThreadHelper.SetThreadAffinity(BindedLogicalCore);
ThreadHelper.BindToLogicalCore(BindedLogicalCore);
_ = Thread.Yield();
if (Simd.Mask != 0)
{
@@ -186,6 +186,13 @@ namespace StgSharp.Mathematics.Numeric
IntrinsicContext* context,
ulong id
);
public bool Equals(
MatrixParallelThread? other
)
{
return (other is not null) && ReferenceEquals(this, other);
}
#pragma warning restore CA5393
}
}
@@ -5,11 +5,6 @@
<None Remove="Numeric\MatrixParallelWrap.cs.old" />
</ItemGroup>
<ItemGroup>
<Compile Include="Numeric\Runtime\MatrixParallel.Await.old" />
<Compile Include="Numeric\Runtime\MatrixParallel.AwaitingQueue.old" />
</ItemGroup>
<ItemGroup>
<None Include="Numeric\Runtime\MatrixParallel.Publish.cs" />
</ItemGroup>
-1
View File
@@ -41,7 +41,6 @@ extern SN_API char infolog[512];
SN_API int SN_DECL glCheckShaderStat(GladGLContext *context, uint64_t shaderHandle, int key,
char **logRef);
SN_API void SN_DECL initGL(int majorVersion, int minorVersion);
SN_API unsigned int SN_DECL linkShaderProgram(GladGLContext *context, GLuint shadertype);
SN_API void SN_DECL loadImageData(char *location, Image *out, imageLoader loader);
SN_API GLFWglproc SN_DECL loadGlfuncDefault(char *procName);
SN_API void SN_DECL unloadImageData(Image *out);
-13
View File
@@ -44,19 +44,6 @@ SN_API GLFWglproc SN_DECL loadGlfuncDefault(char *procName)
return (GLFWglproc)ret;
}
SN_API unsigned int SN_DECL linkShaderProgram(GladGLContext *context, GLuint shaderProgram)
{
context->LinkProgram(shaderProgram);
int success;
context->GetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
context->GetProgramInfoLog(shaderProgram, 512, NULL, infolog);
return 0;
} else {
return shaderProgram;
}
}
SN_API void SN_DECL loadImageData(char *location, Image *out, imageLoader loader)
{