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