matrix pool init ok, working on task publish
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelThread"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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
|
||||
@@ -50,13 +50,11 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
private IntrinsicContext* _context;
|
||||
private MatrixParallelTask* _currentTask;
|
||||
private volatile bool _disposed, _isRetireRequested ;
|
||||
private volatile bool _disposed, _isRetireRequested, _isReinitRequested;
|
||||
private readonly ManualResetEventSlim _resetEvent;
|
||||
private readonly Thread _managedThread;
|
||||
private Thread _managedThread;
|
||||
|
||||
internal MatrixParallelThread(
|
||||
int poolId
|
||||
)
|
||||
internal MatrixParallelThread(int poolId)
|
||||
{
|
||||
_resetEvent = new ManualResetEventSlim(false);
|
||||
_managedThread = new Thread(MatrixParallelWorkLoad);
|
||||
@@ -91,29 +89,54 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void SetSingleThreadTask(
|
||||
ref MatrixParallelTask task
|
||||
)
|
||||
internal void ReInit()
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelThread));
|
||||
if (State != MatrixThreadState.Retired) {
|
||||
return;
|
||||
}
|
||||
BindedWrap = null!;
|
||||
_currentTask = null;
|
||||
IsSingle = false;
|
||||
_isRetireRequested = false;
|
||||
_isReinitRequested = true;
|
||||
_resetEvent.Set();
|
||||
_managedThread = new Thread(MatrixParallelWorkLoad);
|
||||
SpinWait.SpinUntil(() => State != MatrixThreadState.Retired);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void Retire()
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelThread));
|
||||
if (State == MatrixThreadState.Retired) {
|
||||
return;
|
||||
}
|
||||
_isRetireRequested = true;
|
||||
_resetEvent.Set();
|
||||
SpinWait.SpinUntil(() => State == MatrixThreadState.Retired);
|
||||
_managedThread.Join();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void SetSingleThreadTask(ref MatrixParallelTask task)
|
||||
{
|
||||
if (IsSingle) {
|
||||
*_currentTask = task;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_isRetireRequested = true;
|
||||
_disposed = true;
|
||||
_resetEvent.Set();
|
||||
_managedThread.Join();
|
||||
_resetEvent.Dispose();
|
||||
}
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,21 +147,36 @@ namespace StgSharp.Mathematics.Numeric
|
||||
// Bind OS-thread affinity once when the worker starts running
|
||||
SetThreadAffinity(PoolId);
|
||||
_ = Thread.Yield();
|
||||
SIMDID feature = new(GetSimdId());
|
||||
IntrinsicContext* contextPtr = stackalloc IntrinsicContext[1];
|
||||
if (Simd.Mask != 0)
|
||||
{
|
||||
SIMDID feature = new(GetSimdId());
|
||||
IntrinsicContext* contextPtr = stackalloc IntrinsicContext[1];
|
||||
LoadIntrinsicFunction(contextPtr, feature.Mask);
|
||||
Simd = feature;
|
||||
_context = contextPtr;
|
||||
}
|
||||
MatrixParallelTask* taskPtr = stackalloc MatrixParallelTask[4];
|
||||
LoadIntrinsicFunction(contextPtr, feature.Mask);
|
||||
_context = contextPtr;
|
||||
State = MatrixThreadState.Ready;
|
||||
while (true)
|
||||
{
|
||||
// Thread self = Thread.CurrentThread;
|
||||
_resetEvent.Wait();
|
||||
_resetEvent.Reset();
|
||||
if (_isRetireRequested)
|
||||
if (_disposed)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (_isRetireRequested)
|
||||
{
|
||||
State = MatrixThreadState.Retired;
|
||||
continue;
|
||||
}
|
||||
if (_isReinitRequested)
|
||||
{
|
||||
_isReinitRequested = false;
|
||||
State = MatrixThreadState.Idle;
|
||||
continue;
|
||||
}
|
||||
State = MatrixThreadState.Running;
|
||||
AfterWork = BindedWrap.AfterWork;
|
||||
_managedThread.Priority = ThreadPriority.Highest;
|
||||
@@ -148,7 +186,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
IsSingle = false;
|
||||
} else
|
||||
{
|
||||
MatrixParallelQueue taskQueue = BindedWrap.TaskQueue;
|
||||
// MatrixParallelQueue taskQueue = BindedWrap.TaskQueue;
|
||||
/*
|
||||
if (0 > (int)p ->ComputeHandle) {
|
||||
SpecialBufferCompute(p);
|
||||
@@ -172,24 +210,19 @@ 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
|
||||
);
|
||||
internal static partial void SetThreadAffinity(int coreId);
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
|
||||
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
|
||||
private static unsafe partial void LoadIntrinsicFunction(
|
||||
IntrinsicContext* context,
|
||||
ulong id
|
||||
);
|
||||
private static unsafe partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
|
||||
#pragma warning restore CA5393
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user