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
+16
View File
@@ -54,5 +54,21 @@ namespace StgSharp.Mathematics.Internal
Unsafe.SkipInit(out this); 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.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.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric namespace StgSharp.Mathematics.Numeric
{ {
public static partial class MatrixParallel public static unsafe partial class MatrixParallel
{ {
private static bool _meaningful; private static bool _meaningful;
@@ -51,7 +41,10 @@ namespace StgSharp.Mathematics.Numeric
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6; 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; } private static bool IsInitialized { get; set; }
@@ -75,7 +68,6 @@ namespace StgSharp.Mathematics.Numeric
MatrixParallelThread thread = new(i); MatrixParallelThread thread = new(i);
_threads.Push(thread); _threads.Push(thread);
SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready); SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready);
var index = (IsParallelSupported && contextMap.TryGetValue(simd, out int index)) ? index : (-1);
} }
// TODO 构建全局的保底上下文 // TODO 构建全局的保底上下文
@@ -102,12 +94,5 @@ namespace StgSharp.Mathematics.Numeric
ulong id 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 public unsafe partial class MatrixParallelThread : IDisposable
{ {
private IntPtr* _context; private IntrinsicContext* _context;
private volatile bool _disposed, _isRetireRequested = false; private volatile bool _disposed, _isRetireRequested = false;
private readonly ManualResetEventSlim _resetEvent; private readonly ManualResetEventSlim _resetEvent;
private readonly Thread _managedThread; private readonly Thread _managedThread;
@@ -69,15 +69,15 @@ namespace StgSharp.Mathematics.Numeric
internal ManualResetEventSlim? LeaderEvent { get; set; } 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 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() public void Dispose()
{ {
@@ -116,9 +116,9 @@ namespace StgSharp.Mathematics.Numeric
SetThreadAffinity(PoolId); SetThreadAffinity(PoolId);
_ = Thread.Yield(); _ = Thread.Yield();
SIMDID feature = new(GetSimdId()); SIMDID feature = new(GetSimdId());
int index = MatrixParallel.GetContext(feature); IntrinsicContext* contextPtr = stackalloc IntrinsicContext[1];
Intrinsic = new IntrinsicContext(); LoadIntrinsicFunction(contextPtr, feature.Mask);
if (index > 0) { } _context = contextPtr;
State = MatrixThreadState.Ready; State = MatrixThreadState.Ready;
while (true) while (true)
{ {
@@ -173,6 +173,13 @@ namespace StgSharp.Mathematics.Numeric
int coreId 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 #pragma warning restore CA5393
} }
} }