working on per-PoolThread context loading and global context loading
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Internal;
|
||||
using StgSharp.Internal.Intrinsic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
@@ -46,34 +47,30 @@ namespace StgSharp.Mathematics.Numeric
|
||||
private readonly ManualResetEventSlim _resetEvent;
|
||||
private readonly Thread _managedThread;
|
||||
|
||||
public MatrixParallelThread()
|
||||
internal MatrixParallelThread(
|
||||
int poolId
|
||||
)
|
||||
{
|
||||
_resetEvent = new ManualResetEventSlim(false);
|
||||
_managedThread = new Thread(MatrixParallelWorker);
|
||||
_managedThread = new Thread(MatrixParallelWorkLoad);
|
||||
_managedThread.Start();
|
||||
PoolId = poolId;
|
||||
}
|
||||
|
||||
// ~MatrixComputeThread()
|
||||
// {
|
||||
// Dispose(disposing: false);
|
||||
// }
|
||||
public int PoolId { get; init; }
|
||||
|
||||
public ThreadPriority AfterWork { get; set; }
|
||||
|
||||
internal ManualResetEventSlim? LeaderEvent { get; set; }
|
||||
|
||||
[DllImport(
|
||||
dllName:Native.LibName,
|
||||
EntryPoint = "sn_set_thread_affinity",
|
||||
CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void SetThreadAffinity(
|
||||
ulong affinityMask
|
||||
);
|
||||
|
||||
#region wrap belong
|
||||
internal IntrinsicContext* Intrinsic { get; set; }
|
||||
|
||||
internal MatrixParallelWrap BindedWrap { get; set; }
|
||||
|
||||
internal RoleType Role { get; set; }
|
||||
|
||||
internal SIMDID Simd { get; set; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing:true);
|
||||
@@ -146,6 +143,14 @@ namespace StgSharp.Mathematics.Numeric
|
||||
current.Priority = BindedWrap.AfterWork;
|
||||
}
|
||||
|
||||
[DllImport(
|
||||
dllName:Native.LibName,
|
||||
EntryPoint = "sn_set_thread_affinity",
|
||||
CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void SetThreadAffinity(
|
||||
int coreId
|
||||
);
|
||||
|
||||
protected virtual void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
@@ -162,17 +167,19 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe void MatrixParallelWorker()
|
||||
private unsafe void MatrixParallelWorkLoad()
|
||||
{
|
||||
// Bind OS-thread affinity once when the worker starts running
|
||||
SetThreadAffinity(PoolId);
|
||||
Thread.Sleep(0);
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Thread self = Thread.CurrentThread;
|
||||
AfterWork = BindedWrap.AfterWork;
|
||||
_managedThread.Priority = ThreadPriority.Highest;
|
||||
_resetEvent.Wait();
|
||||
_resetEvent.Reset();
|
||||
AfterWork = BindedWrap.AfterWork;
|
||||
_managedThread.Priority = ThreadPriority.Highest;
|
||||
switch (Role)
|
||||
{
|
||||
case RoleType.Worker://worker
|
||||
@@ -183,7 +190,9 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
// Console.WriteLine($"worker {Environment.CurrentManagedThreadId} end");
|
||||
break;
|
||||
case RoleType.Leader://leader, error because leader should be at outer thread but not here
|
||||
case RoleType.SingleExternal:
|
||||
|
||||
// TODO 将外部线程绑定到当前线程上,并执行任务包
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -192,16 +201,14 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
}
|
||||
|
||||
public RoleType Role { get; internal set; }
|
||||
|
||||
public enum RoleType : int
|
||||
internal enum RoleType : int
|
||||
{
|
||||
|
||||
Worker = 0,
|
||||
Leader = 2,
|
||||
Leader = 1,
|
||||
SingleExternal = 2,
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user