find a way to create Thread binded to LogicalCore properly
This commit is contained in:
@@ -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
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user