many versions of simdid
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// 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
|
||||
@@ -57,7 +57,9 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
private static bool IsInitialized { get; set; } = false;
|
||||
|
||||
internal static unsafe IntrinsicContext* GetContext(SIMDID simd)
|
||||
internal static unsafe IntrinsicContext* GetContext(
|
||||
SIMDID simd
|
||||
)
|
||||
{
|
||||
if (!IsParallelSupported) {
|
||||
return null;
|
||||
@@ -92,7 +94,10 @@ namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
MatrixParallelThread thread = new MatrixParallelThread(i);
|
||||
_threads.Push(thread);
|
||||
SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready);
|
||||
}
|
||||
|
||||
// TODO 构建全局的保底上下文
|
||||
if (contextMap.Keys.Count == 1)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
@@ -110,7 +115,17 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
private static unsafe partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
|
||||
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
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// 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
|
||||
@@ -35,9 +35,11 @@ namespace StgSharp.Mathematics.Numeric
|
||||
internal enum MatrixThreadState : int
|
||||
{
|
||||
|
||||
Ready = 0,
|
||||
Running = 1,
|
||||
Idle = 2,
|
||||
Born = 0,
|
||||
Ready = 1,
|
||||
Running = 2,
|
||||
Idle = 4,
|
||||
Retired = 8,
|
||||
|
||||
}
|
||||
|
||||
@@ -45,11 +47,13 @@ namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
|
||||
private IntPtr* _context;
|
||||
private bool disposedValue;
|
||||
private volatile bool _disposed, _isRetireRequested = false;
|
||||
private readonly ManualResetEventSlim _resetEvent;
|
||||
private readonly Thread _managedThread;
|
||||
|
||||
internal MatrixParallelThread(int poolId)
|
||||
internal MatrixParallelThread(
|
||||
int poolId
|
||||
)
|
||||
{
|
||||
_resetEvent = new ManualResetEventSlim(false);
|
||||
_managedThread = new Thread(MatrixParallelWorkLoad);
|
||||
@@ -69,7 +73,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
internal MatrixParallelWrap BindedWrap { get; set; }
|
||||
|
||||
internal MatrixThreadState state { get; set; }
|
||||
internal MatrixThreadState State { get; set; }
|
||||
|
||||
internal SIMDID Simd { get; set; }
|
||||
|
||||
@@ -80,7 +84,9 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void ExecuteTaskPackage(MatrixParallelQueue taskQueue)
|
||||
public static unsafe void ExecuteTaskPackage(
|
||||
MatrixParallelQueue taskQueue
|
||||
)
|
||||
{
|
||||
MatrixParallelTaskPackage* p = taskQueue.Package;
|
||||
switch (p->ComputeMode.OperationStyle)
|
||||
@@ -121,44 +127,54 @@ namespace StgSharp.Mathematics.Numeric
|
||||
_resetEvent.Set();
|
||||
}
|
||||
|
||||
[DllImport(
|
||||
dllName: Native.LibName,
|
||||
EntryPoint = "sn_get_simd_level_local",
|
||||
CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern ulong GetSimdId();
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_get_simd_level_local")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
internal static partial ulong GetSimdId();
|
||||
|
||||
[DllImport(
|
||||
dllName:Native.LibName,
|
||||
EntryPoint = "sn_set_thread_affinity",
|
||||
CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void SetThreadAffinity(int coreId);
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
internal static partial void SetThreadAffinity(
|
||||
int coreId
|
||||
);
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
protected virtual void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
{
|
||||
if (!disposedValue)
|
||||
if (!_disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_resetEvent.Dispose();
|
||||
_isRetireRequested = true;
|
||||
_resetEvent.Set();
|
||||
_managedThread.Join();
|
||||
_resetEvent.Dispose();
|
||||
}
|
||||
disposedValue = true;
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe void MatrixParallelWorkLoad()
|
||||
{
|
||||
State = MatrixThreadState.Born;
|
||||
|
||||
// Bind OS-thread affinity once when the worker starts running
|
||||
SetThreadAffinity(PoolId);
|
||||
Thread.Sleep(1);
|
||||
Thread.Yield();
|
||||
SIMDID feature = new();
|
||||
feature.Mask = GetSimdId();
|
||||
_context = (nint*)MatrixParallel.GetContext(feature);
|
||||
State = MatrixThreadState.Ready;
|
||||
while (true)
|
||||
{
|
||||
// Thread self = Thread.CurrentThread;
|
||||
_resetEvent.Wait();
|
||||
_resetEvent.Reset();
|
||||
if (_isRetireRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
State = MatrixThreadState.Running;
|
||||
AfterWork = BindedWrap.AfterWork;
|
||||
_managedThread.Priority = ThreadPriority.Highest;
|
||||
if (IsSingle)
|
||||
@@ -170,7 +186,9 @@ namespace StgSharp.Mathematics.Numeric
|
||||
BindedWrap.ReportThreadAccomplished();
|
||||
}
|
||||
_managedThread.Priority = AfterWork;
|
||||
State = MatrixThreadState.Idle;
|
||||
}
|
||||
State = MatrixThreadState.Retired;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user