find a way to create Thread binded to LogicalCore properly

This commit is contained in:
Youmu Liu
2026-04-14 18:08:22 +08:00
parent b07013a003
commit fdf11c729f
15 changed files with 228 additions and 254 deletions
@@ -67,13 +67,15 @@ namespace StgSharp.Common.HighPerformance.ProcessorAbstraction
return []; return [];
} }
T[][] arr = new T[_numaNodes.Length][]; T[][] arr = new T[_numaNodes.Length][];
foreach (NumaNode node in _numaNodes) for (int i = 0; i < _numaNodes.Length; i++)
{ {
NumaNode node = _numaNodes[i];
T[] values = new T[node.LogicalCoreCount]; T[] values = new T[node.LogicalCoreCount];
ReadOnlySpan<LogicalCore> cores = node.LogicalCores; ReadOnlySpan<LogicalCore> cores = node.LogicalCores;
for (int i = 0; i < cores.Length; i++) { for (int j = 0; j < cores.Length; j++) {
values[i] = factory(cores[i]); values[j] = factory(cores[j]);
} }
arr[i] = values;
} }
return arr; return arr;
} }
-58
View File
@@ -1,58 +0,0 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="EmptyThread"
// Project: StgSharp
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace StgSharp.Threading
{
public static partial class ThreadHelper
{
private static readonly Thread _emptyThreadCache = new Thread(() => { });
static ThreadHelper()
{
_emptyThreadCache.Start();
}
/// <summary>
/// An individual thread doing nothing, and its <see cref="Thread.IsAlive" /> <see
/// langword="property" /> always returns false.
/// </summary>
public static Thread EmptyThread
{
get => _emptyThreadCache;
}
}
}
-47
View File
@@ -1,47 +0,0 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="NumaBuffer"
// Project: StgSharp
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Common.Threading
{
public unsafe struct NumaBuffer<T> where T : unmanaged
{
public readonly T* Buffers;
public readonly int BufferColumnCapacity;
public readonly int BufferRowCapacity;
public int CurrentColumnCount;
public int CurrentRowCount;
}
}
@@ -1,6 +1,6 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MainThreadAssert" // file="ThreadHelper"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload. All rights reserved.
@@ -25,8 +25,10 @@
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.Common.HighPerformance.ProcessorAbstraction;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
namespace StgSharp.Threading namespace StgSharp.Threading
@@ -35,9 +37,29 @@ namespace StgSharp.Threading
{ {
private static readonly int mainThreadId = Environment.CurrentManagedThreadId; private static readonly int mainThreadId = Environment.CurrentManagedThreadId;
private static readonly ThreadLocal<bool> isNotMainThread = new( private static readonly ThreadLocal<bool> isNotMainThread = new(
() => Environment.CurrentManagedThreadId != mainThreadId); () => Environment.CurrentManagedThreadId != mainThreadId);
static ThreadHelper()
{
EmptyThread.Start();
}
/// <summary>
/// An individual thread doing nothing, and its <see cref="Thread.IsAlive" /> <see
/// langword="property" /> always returns false.
/// </summary>
public static Thread EmptyThread { get; } = new(() => { });
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BindToLogicalCore(
LogicalCore core
)
{
SetThreadAffinity(core.GlobalCpuId, core.NumaNodeId);
}
[Conditional("DEBUG")] [Conditional("DEBUG")]
internal static void MainThreadAssert() internal static void MainThreadAssert()
{ {
@@ -47,5 +69,16 @@ namespace StgSharp.Threading
} }
} }
#pragma warning disable CA5393
[LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
internal static partial void SetThreadAffinity(
uint globalCoreId,
uint numaNodeId
);
#pragma warning restore CA5393
} }
} }
@@ -1,6 +1,33 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="VariableCapacitySemaphore" // file="VariableCapacitySemaphore"
// Project: StgSharp
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="VariableCapacitySemaphore"
// Project: StgSharp.Common // Project: StgSharp.Common
// AuthorGroup: Nitload // AuthorGroup: Nitload
// License: MIT // License: MIT
@@ -40,7 +67,11 @@ namespace StgSharp.Threading
/// <param name="stripes"> /// <param name="stripes">
/// Optional stripe count for MRES (<= 0 to auto choose by CPU). /// Optional stripe count for MRES (<= 0 to auto choose by CPU).
/// </param> /// </param>
public VariableCapacitySemaphore(int initialCount = 0, int maxPermits = 1, int stripes = 0) public VariableCapacitySemaphore(
int initialCount = 0,
int maxPermits = 1,
int stripes = 0
)
{ {
ArgumentOutOfRangeException.ThrowIfLessThan(maxPermits, 1); ArgumentOutOfRangeException.ThrowIfLessThan(maxPermits, 1);
ArgumentOutOfRangeException.ThrowIfNegative(initialCount); ArgumentOutOfRangeException.ThrowIfNegative(initialCount);
@@ -48,14 +79,16 @@ namespace StgSharp.Threading
_max = maxPermits; _max = maxPermits;
_current = Math.Min(initialCount, maxPermits); _current = Math.Min(initialCount, maxPermits);
int stripeCount = stripes > 0 ? stripes : ClampToPow2(Math.Min(Environment.ProcessorCount, 8)); int stripeCount = (stripes > 0) ?
stripes :
ClampToPow2(Math.Min(Environment.ProcessorCount, 8));
_events = new ManualResetEventSlim[stripeCount]; _events = new ManualResetEventSlim[stripeCount];
bool set = _current > 0; bool set = _current > 0;
for (int i = 0; i < stripeCount; i++) { for (int i = 0; i < stripeCount; i++) {
_events[i] = new ManualResetEventSlim(set, 0); _events[i] = new ManualResetEventSlim(set, 0);
} }
_stripeMask = IsPow2(stripeCount) ? (stripeCount - 1) : -1; _stripeMask = IsPow2(stripeCount) ? (stripeCount - 1) : (-1);
} }
/// <summary> /// <summary>
@@ -139,7 +172,9 @@ namespace StgSharp.Threading
/// count. /// count.
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Release(int permits) public int Release(
int permits
)
{ {
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(permits); ArgumentOutOfRangeException.ThrowIfNegativeOrZero(permits);
while (true) while (true)
@@ -150,7 +185,7 @@ namespace StgSharp.Threading
return cur; return cur;
} }
long target = (long)cur + permits; long target = ((long)cur) + permits;
if (target > max) { if (target > max) {
target = max; target = max;
} }
@@ -201,7 +236,7 @@ namespace StgSharp.Threading
while (true) while (true)
{ {
int cur = Volatile.Read(ref _current); int cur = Volatile.Read(ref _current);
if (cur > 0 && Interlocked.CompareExchange(ref _current, cur - 1, cur) == cur) if ((cur > 0) && (Interlocked.CompareExchange(ref _current, cur - 1, cur) == cur))
{ {
if (cur - 1 == 0) { if (cur - 1 == 0) {
ResetAll(); ResetAll();
@@ -222,7 +257,10 @@ namespace StgSharp.Threading
/// <summary> /// <summary>
/// Block until a permit is available or canceled, then consume one. /// Block until a permit is available or canceled, then consume one.
/// </summary> /// </summary>
public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken = default) public bool Wait(
int millisecondsTimeout,
CancellationToken cancellationToken = default
)
{ {
ManualResetEventSlim gate = GetStripeEvent(); ManualResetEventSlim gate = GetStripeEvent();
SpinWait spinner = new SpinWait(); SpinWait spinner = new SpinWait();
@@ -232,7 +270,7 @@ namespace StgSharp.Threading
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
int cur = Volatile.Read(ref _current); int cur = Volatile.Read(ref _current);
if (cur > 0 && Interlocked.CompareExchange(ref _current, cur - 1, cur) == cur) if ((cur > 0) && (Interlocked.CompareExchange(ref _current, cur - 1, cur) == cur))
{ {
if (cur - 1 == 0) { if (cur - 1 == 0) {
ResetAll(); ResetAll();
@@ -263,7 +301,9 @@ namespace StgSharp.Threading
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int ClampToPow2(int x) private static int ClampToPow2(
int x
)
{ {
if (x < 1) { if (x < 1) {
x = 1; x = 1;
@@ -274,7 +314,7 @@ namespace StgSharp.Threading
} }
int p = 1; int p = 1;
while ((p << 1) <= x) { while (p << 1 <= x) {
p <<= 1; p <<= 1;
} }
@@ -294,7 +334,12 @@ namespace StgSharp.Threading
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsPow2(int x) => (x & (x - 1)) == 0; private static bool IsPow2(
int x
)
{
return (x & (x - 1)) == 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ResetAll() private void ResetAll()
@@ -36,12 +36,12 @@ namespace StgSharp.Mathematics.Numeric
public static unsafe partial class MatrixParallel public static unsafe partial class MatrixParallel
{ {
private static int[] NumaPayload; private static int[] _numaPayload;
private static MatrixParallelThread[][] NumaThreads; private static MatrixParallelThread[][] _numaThreads;
private static void UpdateThreadPayload() private static void UpdateThreadPayload()
{ {
for (int i = 0; i < NumaPayload.Length; i++) { } for (int i = 0; i < _numaPayload.Length; i++) { }
} }
} }
@@ -1,34 +1,32 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallel.cs" // file="MatrixParallel"
// 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 this permission notice shall be included in all
// The above copyright notice and // copies or substantial portions of the Software.
// 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,
// THE SOFTWARE IS PROVIDED “AS IS”, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // SOFTWARE.
// 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.
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.Collections; using StgSharp.Collections;
using StgSharp.Common.HighPerformance.ProcessorAbstraction;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@@ -49,7 +47,9 @@ namespace StgSharp.Mathematics.Numeric
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6; public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6;
public static bool IsParallelMeaningful => IsParallelSupported && IsInitialized && _meaningful; public static bool IsParallelMeaningful => IsParallelSupported &&
IsInitialized &&
_meaningful;
public static int ContextCount { get; private set; } public static int ContextCount { get; private set; }
@@ -86,17 +86,20 @@ namespace StgSharp.Mathematics.Numeric
SIMDID globalSimd = new SIMDID(0); SIMDID globalSimd = new SIMDID(0);
_threads = new(count); _threads = new(count);
_retiredThreads = new(RESERVED_THREAD_COUNT); _retiredThreads = new(RESERVED_THREAD_COUNT);
for (int i = 0; i < count; i++)
{
MatrixParallelThread thread = new(i);
_threads.Push(thread);
SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready);
SIMDID simd = thread.Simd; _numaThreads = Numa.CreateForeachThread<MatrixParallelThread>(
ulong mask = simd.Mask; core => {
contextMap[mask] = contextMap.TryGetValue(mask, out int contextCount) ? (contextCount + 1) : 1; MatrixParallelThread thread = new(core);
globalSimd = (globalSimd.Mask != 0) ? UniteSIMDID(globalSimd, simd) : simd; SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready);
}
SIMDID simd = thread.Simd;
ulong mask = simd.Mask;
contextMap[mask] = contextMap.TryGetValue(mask, out int contextCount) ?
(contextCount + 1) :
1;
globalSimd = (globalSimd.Mask != 0) ? UniteSIMDID(globalSimd, simd) : simd;
return thread;
});
IntrinsicContext globalContext = new(); IntrinsicContext globalContext = new();
LoadIntrinsicFunction(&globalContext, globalSimd.Mask); LoadIntrinsicFunction(&globalContext, globalSimd.Mask);
@@ -111,8 +114,13 @@ namespace StgSharp.Mathematics.Numeric
} }
} else } else
{ {
// TODO 切换来源
// TODO _threads退休并移除
MatrixParallelThread[] allThreads = _threads.PopRange(_threads.Count); MatrixParallelThread[] allThreads = _threads.PopRange(_threads.Count);
Array.Sort(allThreads, static(left, right) => CompareSIMDID(left.Simd, right.Simd)); Array.Sort(allThreads, static(
left,
right
) => 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 - 1));
for (int i = 0; i < retireCount; i++) { for (int i = 0; i < retireCount; i++) {
RetireThread(allThreads[i]); RetireThread(allThreads[i]);
@@ -125,26 +133,37 @@ namespace StgSharp.Mathematics.Numeric
} }
[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)]
private static partial int CompareSIMDID(SIMDID left, SIMDID right); private static partial int CompareSIMDID(
SIMDID left,
SIMDID right
);
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")] [LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)] [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
private static partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id); private static partial void LoadIntrinsicFunction(
IntrinsicContext* context,
ulong id
);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void RetireThread(MatrixParallelThread thread) private static void RetireThread(
MatrixParallelThread thread
)
{ {
thread.Pause(); thread.Pause();
_retiredThreads.Push(thread); _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)]
private static partial SIMDID UniteSIMDID(SIMDID left, SIMDID right); private static partial SIMDID UniteSIMDID(
SIMDID left,
SIMDID right
);
} }
} }
@@ -1,35 +1,34 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallelThread.cs" // file="MatrixParallelThread"
// 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 this permission notice shall be included in all
// The above copyright notice and // copies or substantial portions of the Software.
// 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,
// THE SOFTWARE IS PROVIDED “AS IS”, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // SOFTWARE.
// 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.
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.Common.HighPerformance.ProcessorAbstraction;
using StgSharp.Internal; using StgSharp.Internal;
using StgSharp.Mathematics.Numeric.Runtime; using StgSharp.Mathematics.Numeric.Runtime;
using StgSharp.Threading;
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@@ -60,15 +59,17 @@ namespace StgSharp.Mathematics.Numeric
private volatile MatrixThreadState _target; private volatile MatrixThreadState _target;
private Thread _managedThread; private Thread _managedThread;
internal MatrixParallelThread(int poolId) internal MatrixParallelThread(
LogicalCore binded
)
{ {
_resetEvent = new ManualResetEventSlim(false); _resetEvent = new ManualResetEventSlim(false);
_managedThread = new Thread(MatrixParallelWorkLoad); _managedThread = new Thread(MatrixParallelWorkLoad);
_managedThread.Start(); _managedThread.Start();
PoolId = poolId; BindedLogicalCore = binded;
} }
public int PoolId { get; init; } public LogicalCore BindedLogicalCore { get; init; }
public ThreadPriority AfterWork { get; set; } public ThreadPriority AfterWork { get; set; }
@@ -100,15 +101,9 @@ namespace StgSharp.Mathematics.Numeric
_managedThread.Join(); _managedThread.Join();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] protected virtual void Dispose(
internal void SetSingleThreadTask(ref MatrixParallelTask task) bool disposing
{ )
if (IsSingle) {
*_currentTask = task;
}
}
protected virtual void Dispose(bool disposing)
{ {
if (!_disposed) if (!_disposed)
{ {
@@ -127,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
SetThreadAffinity(PoolId); ThreadHelper.SetThreadAffinity(BindedLogicalCore);
_ = Thread.Yield(); _ = Thread.Yield();
if (Simd.Mask != 0) if (Simd.Mask != 0)
{ {
@@ -180,19 +175,17 @@ namespace StgSharp.Mathematics.Numeric
#pragma warning disable CA5393 #pragma warning disable CA5393
[LibraryImport(Native.LibName, EntryPoint = "sn_get_simd_level_local")] [LibraryImport(Native.LibName, EntryPoint = "sn_get_simd_level_local")]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)] [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
internal static partial ulong GetSimdId(); internal static partial ulong GetSimdId();
[LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
internal static partial void SetThreadAffinity(int coreId);
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")] [LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)] [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
private static unsafe partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id); private static unsafe partial void LoadIntrinsicFunction(
IntrinsicContext* context,
ulong id
);
#pragma warning restore CA5393 #pragma warning restore CA5393
} }
} }
+3 -2
View File
@@ -110,7 +110,7 @@
<LanguageStandard_C>stdc11</LanguageStandard_C> <LanguageStandard_C>stdc11</LanguageStandard_C>
<MultiProcessorCompilation>true</MultiProcessorCompilation> <MultiProcessorCompilation>true</MultiProcessorCompilation>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed> <FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<AdditionalIncludeDirectories>$(ProjectDir)lib;$(ProjectDir)sn-head;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(ProjectDir)lib;$(ProjectDir)sn-head;$(ProjectDir)sn-template;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
<Link> <Link>
<!-- DLL 应该用 Windows,不是 Console --> <!-- DLL 应该用 Windows,不是 Console -->
@@ -172,10 +172,10 @@
<!-- source files --> <!-- source files -->
<!-- ════════════════════════════════════════════════════════ --> <!-- ════════════════════════════════════════════════════════ -->
<ItemGroup> <ItemGroup>
<ClCompile Include="sn-src\m.basic.x86.c" />
<ClCompile Include="sn-src\dllmain.c" /> <ClCompile Include="sn-src\dllmain.c" />
<ClCompile Include="sn-src\m.bufop.x86.c" /> <ClCompile Include="sn-src\m.bufop.x86.c" />
<ClCompile Include="sn-src\m.solve.x86.c" /> <ClCompile Include="sn-src\m.solve.x86.c" />
<ClCompile Include="sn-src\m.transpose.x86.c" />
<ClCompile Include="sn-src\mk.fma.x86.c" /> <ClCompile Include="sn-src\mk.fma.x86.c" />
<ClCompile Include="sn-src\s.factorial.x86.c" /> <ClCompile Include="sn-src\s.factorial.x86.c" />
<ClCompile Include="sn-src\sk.fma.x86.c" /> <ClCompile Include="sn-src\sk.fma.x86.c" />
@@ -208,6 +208,7 @@
<ClInclude Include="sn-head\sn_target.h" /> <ClInclude Include="sn-head\sn_target.h" />
<ClInclude Include="sn-head\StgSharpNative.h" /> <ClInclude Include="sn-head\StgSharpNative.h" />
<ClInclude Include="sn-head\wgl.h" /> <ClInclude Include="sn-head\wgl.h" />
<ClInclude Include="sn-template\sn_template.fma.x86.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="naming.md" /> <None Include="naming.md" />
@@ -16,6 +16,9 @@
<Filter Include="Document"> <Filter Include="Document">
<UniqueIdentifier>{fbd74b53-5314-4d9d-a788-07bd1b413f74}</UniqueIdentifier> <UniqueIdentifier>{fbd74b53-5314-4d9d-a788-07bd1b413f74}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="模板文件">
<UniqueIdentifier>{c7081772-a85a-4372-b55c-a6dac43c6913}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="sn-src\dllmain.c"> <ClCompile Include="sn-src\dllmain.c">
@@ -39,9 +42,6 @@
<ClCompile Include="sn-src\load_glfw.c"> <ClCompile Include="sn-src\load_glfw.c">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="sn-src\m.basic.x86.c">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="sn-src\sk.fma.x86.c"> <ClCompile Include="sn-src\sk.fma.x86.c">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
@@ -69,6 +69,9 @@
<ClCompile Include="sn-src\thread.windows.c"> <ClCompile Include="sn-src\thread.windows.c">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="sn-src\m.transpose.x86.c">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="sn-head\gl.h"> <ClInclude Include="sn-head\gl.h">
@@ -113,6 +116,9 @@
<ClInclude Include="sn-head\sn_intrinsic.std.h"> <ClInclude Include="sn-head\sn_intrinsic.std.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="sn-template\sn_template.fma.x86.h">
<Filter>模板文件</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Library Include="lib\glfw\glfw3.lib"> <Library Include="lib\glfw\glfw3.lib">
+1 -1
View File
@@ -54,7 +54,7 @@ SN_API SIMDID SN_DECL sn_get_simd_level_global();
SN_API SIMDID SN_DECL sn_get_simd_level_local(); SN_API SIMDID SN_DECL sn_get_simd_level_local();
SN_API SIMDID SN_DECL sn_get_unite_simd(SIMDID left, SIMDID right); SN_API SIMDID SN_DECL sn_get_unite_simd(SIMDID left, SIMDID right);
SN_API int SN_DECL sn_compare_simd(SIMDID left, SIMDID right); SN_API int SN_DECL sn_compare_simd(SIMDID left, SIMDID right);
SN_API void SN_DECL sn_set_thread_affinity(int core_id); SN_API void SN_DECL sn_set_thread_affinity(int core_id, int numa_id);
SN_API void* SN_DECL sn_numa_alloc(size_t size, int numa_id); SN_API void* SN_DECL sn_numa_alloc(size_t size, int numa_id);
SN_API void SN_DECL sn_numa_free(void *ptr, size_t size, int numa_id); SN_API void SN_DECL sn_numa_free(void *ptr, size_t size, int numa_id);
SN_API int SN_DECL sn_get_numa_each(int numa_id, uint64_t *internal, uint32_t *global); SN_API int SN_DECL sn_get_numa_each(int numa_id, uint64_t *internal, uint32_t *global);
+1 -1
View File
@@ -148,7 +148,7 @@ SN_MK_PROC_DECL_STD(float, 512, , fma)
register __m512 c_3 = c_0; register __m512 c_3 = c_0;
for (size_t z = 0; z < common_z; z+=4) { for (size_t z = 0; z < common_z; z+=4) {
SN_MAT_KERNEL_LOCAL const *right = right + z; SN_MAT_KERNEL_LOCAL const *right_ = right + z;
SN_MAT_KERNEL_LOCAL const *left_0 = left + z + 0; SN_MAT_KERNEL_LOCAL const *left_0 = left + z + 0;
SN_MAT_KERNEL_LOCAL const *left_1 = left + z + 1; SN_MAT_KERNEL_LOCAL const *left_1 = left + z + 1;
SN_MAT_KERNEL_LOCAL const *left_2 = left + z + 2; SN_MAT_KERNEL_LOCAL const *left_2 = left + z + 2;
+4 -20
View File
@@ -1,10 +1,11 @@
#include "sn_target.h" #include "sn_target.h"
#include "sn_intrinsic.h"
#include "StgSharpNative.h"
static SIMDID global_simdid = { 0 }; /* clang-format off */
#if SN_IS_ARCH(SN_ARCH_X86_64) #if SN_IS_ARCH(SN_ARCH_X86_64)
#include "sn_intrinsic.h"
#include "StgSharpNative.h"
static SIMDID global_simdid = { 0 };
#include "sn_intrinsic.context.x86.h" #include "sn_intrinsic.context.x86.h"
@@ -350,23 +351,6 @@ SN_API SIMDID SN_DECL sn_get_simd_level_global()
return global_simdid; return global_simdid;
} }
#else
SN_API SIMDID SN_DECL sn_get_simd_level_local()
{
SIMDID simdid;
simdid.mask = 0;
return simdid;
}
SN_API SIMDID SN_DECL sn_get_simd_level_global()
{
if (global_simdid.mask) {
return global_simdid;
}
global_simdid = sn_get_simd_level_local();
return global_simdid;
}
#endif #endif
+9 -12
View File
@@ -137,7 +137,7 @@ static int sn_linux_count_cpu_list(char const *path)
if (!sn_linux_read_first_line(path, buffer, sizeof(buffer))) if (!sn_linux_read_first_line(path, buffer, sizeof(buffer)))
return 0; return 0;
sn_linux_counter_context counter = {0}; sn_linux_counter_context counter = { 0 };
if (!sn_linux_visit_id_list(buffer, sn_linux_count_id, &counter)) if (!sn_linux_visit_id_list(buffer, sn_linux_count_id, &counter))
return 0; return 0;
@@ -173,8 +173,7 @@ static size_t sn_linux_round_up_to_page_size(size_t size)
return rounded - (rounded % effective_page_size); return rounded - (rounded % effective_page_size);
} }
static int sn_linux_bind_memory_to_node(void *address, size_t size, int numa_id, static int sn_linux_bind_memory_to_node(void *address, size_t size, int numa_id, uint32_t flags)
uint32_t flags)
{ {
#if defined(SYS_mbind) #if defined(SYS_mbind)
size_t bits_per_word = sizeof(unsigned long) * 8u; size_t bits_per_word = sizeof(unsigned long) * 8u;
@@ -185,9 +184,8 @@ static int sn_linux_bind_memory_to_node(void *address, size_t size, int numa_id,
node_mask[(size_t)numa_id / bits_per_word] = 1ul << ((size_t)numa_id % bits_per_word); node_mask[(size_t)numa_id / bits_per_word] = 1ul << ((size_t)numa_id % bits_per_word);
int syscall_result = (int)syscall(SYS_mbind, address, size, int syscall_result = (int)syscall(SYS_mbind, address, size, MPOL_BIND | MPOL_F_STATIC_NODES,
MPOL_BIND | MPOL_F_STATIC_NODES, node_mask, node_mask, (unsigned long)numa_id + 1ul, flags);
(unsigned long)numa_id + 1ul, flags);
free(node_mask); free(node_mask);
return syscall_result == 0; return syscall_result == 0;
#else #else
@@ -216,7 +214,7 @@ static int sn_linux_populate_writable_mapping(void *address, size_t size)
/* Pin the calling thread to a single logical processor. /* Pin the calling thread to a single logical processor.
* core_id >= 0 : bind to that logical processor (global index, across groups). * core_id >= 0 : bind to that logical processor (global index, across groups).
* core_id < 0 : clear affinity constraint within the current group. */ * core_id < 0 : clear affinity constraint within the current group. */
SN_API void SN_DECL sn_set_thread_affinity(int core_id) SN_API void SN_DECL sn_set_thread_affinity(int core_id, int numa_id)
{ {
int cpu_count = (int)sysconf(_SC_NPROCESSORS_ONLN); int cpu_count = (int)sysconf(_SC_NPROCESSORS_ONLN);
if (cpu_count <= 0) if (cpu_count <= 0)
@@ -246,7 +244,7 @@ SN_API int SN_DECL sn_get_numa_overall(int *out_numa_count, int *count_of_each_n
if (!sn_linux_read_first_line("/sys/devices/system/node/online", buffer, sizeof(buffer))) if (!sn_linux_read_first_line("/sys/devices/system/node/online", buffer, sizeof(buffer)))
return 0; return 0;
sn_linux_max_context max_context = {-1}; sn_linux_max_context max_context = { -1 };
if (!sn_linux_visit_id_list(buffer, sn_linux_track_max_id, &max_context) || if (!sn_linux_visit_id_list(buffer, sn_linux_track_max_id, &max_context) ||
max_context.max_value < 0) max_context.max_value < 0)
return 0; return 0;
@@ -258,7 +256,7 @@ SN_API int SN_DECL sn_get_numa_overall(int *out_numa_count, int *count_of_each_n
for (int index = 0; index <= max_context.max_value; index++) for (int index = 0; index <= max_context.max_value; index++)
count_of_each_numa[index] = 0; count_of_each_numa[index] = 0;
sn_linux_fill_node_count_context fill_context = {count_of_each_numa}; sn_linux_fill_node_count_context fill_context = { count_of_each_numa };
if (!sn_linux_visit_id_list(buffer, sn_linux_fill_node_cpu_count, &fill_context)) if (!sn_linux_visit_id_list(buffer, sn_linux_fill_node_cpu_count, &fill_context))
return 0; return 0;
} }
@@ -277,7 +275,7 @@ SN_API int SN_DECL sn_get_numa_each(int numa_id, uint64_t *internal, uint32_t *g
if (!sn_linux_read_first_line(path, buffer, sizeof(buffer))) if (!sn_linux_read_first_line(path, buffer, sizeof(buffer)))
return 0; return 0;
sn_linux_fill_cpu_context fill_context = {internal, global, 0}; sn_linux_fill_cpu_context fill_context = { internal, global, 0 };
return sn_linux_visit_id_list(buffer, sn_linux_fill_cpu_id, &fill_context); return sn_linux_visit_id_list(buffer, sn_linux_fill_cpu_id, &fill_context);
} }
@@ -293,8 +291,7 @@ SN_API void *SN_DECL sn_numa_alloc(size_t size, int numa_id)
mmap_flags |= MAP_POPULATE; mmap_flags |= MAP_POPULATE;
#endif #endif
void *memory = mmap(NULL, allocation_size, PROT_READ | PROT_WRITE, void *memory = mmap(NULL, allocation_size, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
mmap_flags, -1, 0);
if (memory == MAP_FAILED) if (memory == MAP_FAILED)
return NULL; return NULL;
+4 -5
View File
@@ -6,9 +6,9 @@
/* Pin the calling thread to a single logical processor. /* Pin the calling thread to a single logical processor.
* core_id >= 0 : bind to that logical processor (global index, across groups). * core_id >= 0 : bind to that logical processor (global index, across groups).
* core_id < 0 : clear affinity constraint within the current group. */ * core_id < 0 : clear affinity constraint within the current group. */
SN_API void SN_DECL sn_set_thread_affinity(int core_id) SN_API void SN_DECL sn_set_thread_affinity(int core_id, int numa_id)
{ {
if (core_id < 0) { if (core_id < 0 || numa_id < 0) {
/* Query which group the thread currently belongs to */ /* Query which group the thread currently belongs to */
GROUP_AFFINITY current; GROUP_AFFINITY current;
GetThreadGroupAffinity(GetCurrentThread(), &current); GetThreadGroupAffinity(GetCurrentThread(), &current);
@@ -21,14 +21,13 @@ SN_API void SN_DECL sn_set_thread_affinity(int core_id)
SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL); SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL);
return; return;
} }
WORD group = (WORD)(core_id / 64);
int index = core_id % 64; int index = core_id % 64;
DWORD count = GetActiveProcessorCount(group); DWORD count = GetActiveProcessorCount(numa_id);
if ((DWORD)index >= count) if ((DWORD)index >= count)
return; return;
GROUP_AFFINITY ga; GROUP_AFFINITY ga;
memset(&ga, 0, sizeof(ga)); memset(&ga, 0, sizeof(ga));
ga.Group = group; ga.Group = (WORD)numa_id;
ga.Mask = (KAFFINITY)1 << index; ga.Mask = (KAFFINITY)1 << index;
SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL); SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL);
} }