find a way to create Thread binded to LogicalCore properly
This commit is contained in:
@@ -36,12 +36,12 @@ namespace StgSharp.Mathematics.Numeric
|
||||
public static unsafe partial class MatrixParallel
|
||||
{
|
||||
|
||||
private static int[] NumaPayload;
|
||||
private static MatrixParallelThread[][] NumaThreads;
|
||||
private static int[] _numaPayload;
|
||||
private static MatrixParallelThread[][] _numaThreads;
|
||||
|
||||
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
|
||||
// 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Collections;
|
||||
using StgSharp.Common.HighPerformance.ProcessorAbstraction;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
@@ -49,7 +47,9 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
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; }
|
||||
|
||||
@@ -86,17 +86,20 @@ namespace StgSharp.Mathematics.Numeric
|
||||
SIMDID globalSimd = new SIMDID(0);
|
||||
_threads = new(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;
|
||||
ulong mask = simd.Mask;
|
||||
contextMap[mask] = contextMap.TryGetValue(mask, out int contextCount) ? (contextCount + 1) : 1;
|
||||
globalSimd = (globalSimd.Mask != 0) ? UniteSIMDID(globalSimd, simd) : simd;
|
||||
}
|
||||
_numaThreads = Numa.CreateForeachThread<MatrixParallelThread>(
|
||||
core => {
|
||||
MatrixParallelThread thread = new(core);
|
||||
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();
|
||||
LoadIntrinsicFunction(&globalContext, globalSimd.Mask);
|
||||
@@ -111,8 +114,13 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
} else
|
||||
{
|
||||
// TODO 切换来源
|
||||
// TODO _threads退休并移除
|
||||
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));
|
||||
for (int i = 0; i < retireCount; i++) {
|
||||
RetireThread(allThreads[i]);
|
||||
@@ -125,26 +133,37 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")]
|
||||
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
[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")]
|
||||
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
|
||||
private static partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
|
||||
private static partial void LoadIntrinsicFunction(
|
||||
IntrinsicContext* context,
|
||||
ulong id
|
||||
);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void RetireThread(MatrixParallelThread thread)
|
||||
private static void RetireThread(
|
||||
MatrixParallelThread thread
|
||||
)
|
||||
{
|
||||
thread.Pause();
|
||||
_retiredThreads.Push(thread);
|
||||
}
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_get_unite_simd")]
|
||||
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
[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
|
||||
// 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Common.HighPerformance.ProcessorAbstraction;
|
||||
using StgSharp.Internal;
|
||||
using StgSharp.Mathematics.Numeric.Runtime;
|
||||
using StgSharp.Threading;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
@@ -60,15 +59,17 @@ namespace StgSharp.Mathematics.Numeric
|
||||
private volatile MatrixThreadState _target;
|
||||
private Thread _managedThread;
|
||||
|
||||
internal MatrixParallelThread(int poolId)
|
||||
internal MatrixParallelThread(
|
||||
LogicalCore binded
|
||||
)
|
||||
{
|
||||
_resetEvent = new ManualResetEventSlim(false);
|
||||
_managedThread = new Thread(MatrixParallelWorkLoad);
|
||||
_managedThread.Start();
|
||||
PoolId = poolId;
|
||||
BindedLogicalCore = binded;
|
||||
}
|
||||
|
||||
public int PoolId { get; init; }
|
||||
public LogicalCore BindedLogicalCore { get; init; }
|
||||
|
||||
public ThreadPriority AfterWork { get; set; }
|
||||
|
||||
@@ -100,15 +101,9 @@ namespace StgSharp.Mathematics.Numeric
|
||||
_managedThread.Join();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void SetSingleThreadTask(ref MatrixParallelTask task)
|
||||
{
|
||||
if (IsSingle) {
|
||||
*_currentTask = task;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
protected virtual void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
@@ -127,7 +122,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
State = MatrixThreadState.Born;
|
||||
|
||||
// Bind OS-thread affinity once when the worker starts running
|
||||
SetThreadAffinity(PoolId);
|
||||
ThreadHelper.SetThreadAffinity(BindedLogicalCore);
|
||||
_ = Thread.Yield();
|
||||
if (Simd.Mask != 0)
|
||||
{
|
||||
@@ -180,19 +175,17 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
#pragma warning disable CA5393
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_get_simd_level_local")]
|
||||
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
|
||||
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")]
|
||||
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
[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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user