back to matrix, simplified thread init

This commit is contained in:
Youmu Liu
2026-03-24 13:45:14 +08:00
parent eee88e1098
commit d960d02972
3 changed files with 41 additions and 33 deletions
+18 -2
View File
@@ -39,8 +39,8 @@ namespace StgSharp.Mathematics.Internal
internal unsafe struct SIMDID
{
[FieldOffset(0)]public fixed byte MaskByte[16];
[FieldOffset(0)]public ulong Mask;
[FieldOffset(0)] public fixed byte MaskByte[16];
[FieldOffset(0)] public ulong Mask;
internal SIMDID(
ulong data
@@ -54,5 +54,21 @@ namespace StgSharp.Mathematics.Internal
Unsafe.SkipInit(out this);
}
public static bool operator !=(
SIMDID left,
SIMDID right
)
{
return left.Mask != right.Mask;
}
public static bool operator ==(
SIMDID left,
SIMDID right
)
{
return left.Mask == right.Mask;
}
}
}
@@ -26,23 +26,13 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Collections;
using StgSharp.Internal;
using StgSharp.Mathematics.Internal;
using StgSharp.Mathematics.Internal;
using StgSharp.Mathematics.Memory;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
public static partial class MatrixParallel
public static unsafe partial class MatrixParallel
{
private static bool _meaningful;
@@ -51,7 +41,10 @@ namespace StgSharp.Mathematics.Numeric
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6;
public static bool IsParallelMeaningful => (Environment.ProcessorCount > 6) && IsInitialized;
public static bool IsParallelMeaningful { get; } = (Environment.ProcessorCount > 6) &&
IsInitialized;
public static int ContextCount { get; private set; }
private static bool IsInitialized { get; set; }
@@ -75,7 +68,6 @@ namespace StgSharp.Mathematics.Numeric
MatrixParallelThread thread = new(i);
_threads.Push(thread);
SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready);
var index = (IsParallelSupported && contextMap.TryGetValue(simd, out int index)) ? index : (-1);
}
// TODO 构建全局的保底上下文
@@ -96,18 +88,11 @@ namespace StgSharp.Mathematics.Numeric
}
[LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
private static unsafe partial ulong CompareSIMDID(
IntrinsicContext* context,
ulong id
);
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
private static unsafe partial void LoadIntrinsicFunction(
IntrinsicContext* context,
ulong id
);
}
}
@@ -48,7 +48,7 @@ namespace StgSharp.Mathematics.Numeric
public unsafe partial class MatrixParallelThread : IDisposable
{
private IntPtr* _context;
private IntrinsicContext* _context;
private volatile bool _disposed, _isRetireRequested = false;
private readonly ManualResetEventSlim _resetEvent;
private readonly Thread _managedThread;
@@ -69,15 +69,15 @@ namespace StgSharp.Mathematics.Numeric
internal ManualResetEventSlim? LeaderEvent { get; set; }
internal bool IsSingle { get; set; }
internal ref readonly IntrinsicContext Intrinsic => ref (*_context);
internal IntrinsicContext Intrinsic { get; private set; }
internal bool IsSingle { get; set; }
internal MatrixParallelWrap BindedWrap { get; set; }
internal MatrixThreadState State { get; set; }
internal MatrixThreadState State { get; private set; }
internal SIMDID Simd { get; set; }
internal SIMDID Simd { get; private set; }
public void Dispose()
{
@@ -116,9 +116,9 @@ namespace StgSharp.Mathematics.Numeric
SetThreadAffinity(PoolId);
_ = Thread.Yield();
SIMDID feature = new(GetSimdId());
int index = MatrixParallel.GetContext(feature);
Intrinsic = new IntrinsicContext();
if (index > 0) { }
IntrinsicContext* contextPtr = stackalloc IntrinsicContext[1];
LoadIntrinsicFunction(contextPtr, feature.Mask);
_context = contextPtr;
State = MatrixThreadState.Ready;
while (true)
{
@@ -162,17 +162,24 @@ 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) ])]
[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)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
private static unsafe partial void LoadIntrinsicFunction(
IntrinsicContext* context,
ulong id
);
#pragma warning restore CA5393
}
}