matrix pool init ok, working on task publish
This commit is contained in:
@@ -33,32 +33,22 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
[Flags]
|
||||
public enum PredictResult
|
||||
{
|
||||
|
||||
Success = 1,
|
||||
Exhausted = 2,
|
||||
|
||||
}
|
||||
|
||||
public interface IL4Predict
|
||||
{
|
||||
|
||||
bool IsExhausted { get; }
|
||||
PredictResult Predict(Span<CacheLinePrediction> node, out int count);
|
||||
|
||||
/// <summary>
|
||||
/// Writes predicted cache line descriptors into <paramref name="node" />.
|
||||
/// </summary>
|
||||
/// <param name="node">
|
||||
/// Prediction output buffer.
|
||||
/// </param>
|
||||
/// <param name="count">
|
||||
/// When this method returns, contains the number of valid entries written to <paramref ///
|
||||
/// name="node" />. The value must not exceed <paramref name="node" />.Length.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if any prediction data was produced; otherwise, <see langword="false"
|
||||
/// />.
|
||||
/// </returns>
|
||||
bool Predict(
|
||||
Span<CacheLinePrediction> node,
|
||||
out int count
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Materializes the cache line identified by <paramref name="origin" /> into <paramref ///
|
||||
/// Materializes the cache line identified by <paramref name="origin" /> into <paramref /// ///
|
||||
/// name="cache" />.
|
||||
/// </summary>
|
||||
/// <param name="origin">
|
||||
@@ -70,11 +60,7 @@ namespace StgSharp.HighPerformance.Memory
|
||||
/// <param name="cache">
|
||||
/// Destination cache line buffer.
|
||||
/// </param>
|
||||
void Prefetch(
|
||||
nuint origin,
|
||||
ulong policy,
|
||||
Span<byte> cache
|
||||
);
|
||||
void Prefetch(nuint origin, ulong policy, Span<byte> cache);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the cache line in <paramref name="cache" /> back to <paramref name="origin" />.
|
||||
@@ -88,32 +74,24 @@ namespace StgSharp.HighPerformance.Memory
|
||||
/// <param name="cache">
|
||||
/// Source cache line buffer.
|
||||
/// </param>
|
||||
void WriteBack(
|
||||
nuint origin,
|
||||
ulong policy,
|
||||
ReadOnlySpan<byte> cache
|
||||
);
|
||||
void WriteBack(nuint origin, ulong policy, ReadOnlySpan<byte> cache);
|
||||
|
||||
}
|
||||
|
||||
public struct CacheLinePrediction : IEquatable<CacheLinePrediction>
|
||||
{
|
||||
|
||||
public override readonly bool Equals(
|
||||
object? obj
|
||||
)
|
||||
public override readonly bool Equals(object? obj)
|
||||
{
|
||||
return obj is CacheLinePrediction prediction &&
|
||||
return (obj is CacheLinePrediction prediction) &&
|
||||
Address.Equals(prediction.Address) &&
|
||||
MapPolicy == prediction.MapPolicy;
|
||||
(MapPolicy == prediction.MapPolicy);
|
||||
}
|
||||
|
||||
public readonly bool Equals(
|
||||
CacheLinePrediction other
|
||||
)
|
||||
public readonly bool Equals(CacheLinePrediction other)
|
||||
{
|
||||
return
|
||||
Address == other.Address && MapPolicy == other.MapPolicy;
|
||||
(Address == other.Address) && (MapPolicy == other.MapPolicy);
|
||||
}
|
||||
|
||||
public override readonly int GetHashCode()
|
||||
@@ -121,17 +99,11 @@ namespace StgSharp.HighPerformance.Memory
|
||||
return HashCode.Combine(Address, MapPolicy);
|
||||
}
|
||||
|
||||
public static bool operator !=(
|
||||
CacheLinePrediction left,
|
||||
CacheLinePrediction right
|
||||
)
|
||||
public static bool operator !=(CacheLinePrediction left, CacheLinePrediction right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
public static bool operator ==(
|
||||
CacheLinePrediction left,
|
||||
CacheLinePrediction right
|
||||
)
|
||||
public static bool operator ==(CacheLinePrediction left, CacheLinePrediction right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ namespace StgSharp.Mathematics.Numeric.Linear
|
||||
|
||||
public static PredictPolicy Policy => PredictPolicy.Sequential;
|
||||
|
||||
public bool Predict(
|
||||
public PredictResult Predict(
|
||||
Span<CacheLinePrediction> node,
|
||||
out int count
|
||||
)
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="NumericalModule"
|
||||
// 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
|
||||
@@ -61,26 +61,23 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
public string ModuleName => "Math.Numerical";
|
||||
|
||||
internal static IntrinsicContext GlobalContext
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get;
|
||||
} = new();
|
||||
internal static IntrinsicContext GlobalContext { get; set; } = new();
|
||||
|
||||
internal static SIMDID GlobalIntrinsicMask { get; set; }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void InitializeModule()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void InitializeModule(
|
||||
IModuleInitializeProfile profile
|
||||
)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void InitializeModule(IModuleInitializeProfile profile)
|
||||
{
|
||||
MatrixParallel.Init();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void UninitializeModule()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.Publish"
|
||||
// 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,8 +50,8 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
internal static unsafe MatrixParallelWrap ScheduleTask<TTask>(
|
||||
MatrixParallelThread[] workers,
|
||||
MatrixParallelTask* package
|
||||
) where TTask : IL4Predict
|
||||
MatrixParallelTask* package)
|
||||
where TTask: IL4Predict
|
||||
{
|
||||
/*
|
||||
int primCount = package->PrimCount;
|
||||
@@ -61,10 +61,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
// Console.WriteLine($"amount of kernels is {count}");
|
||||
|
||||
return new(workers)
|
||||
{
|
||||
TaskQueue = MatrixParallelQueue.Rent(package)
|
||||
};
|
||||
return new(workers);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel"
|
||||
// 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
|
||||
@@ -35,14 +35,17 @@ namespace StgSharp.Mathematics.Numeric
|
||||
public static unsafe partial class MatrixParallel
|
||||
{
|
||||
|
||||
private const int RESERVED_THREAD_COUNT = 4;
|
||||
|
||||
private static bool _meaningful;
|
||||
|
||||
private static CapacityFixedStack<MatrixParallelThread> _retiredThreads;
|
||||
|
||||
private static CapacityFixedStack<MatrixParallelThread> _threads;
|
||||
|
||||
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6;
|
||||
|
||||
public static bool IsParallelMeaningful { get; } = (Environment.ProcessorCount > 6) &&
|
||||
IsInitialized;
|
||||
public static bool IsParallelMeaningful => IsParallelSupported && IsInitialized && _meaningful;
|
||||
|
||||
public static int ContextCount { get; private set; }
|
||||
|
||||
@@ -50,7 +53,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
internal static void Init()
|
||||
{
|
||||
Dictionary<SIMDID, int> contextMap = [];
|
||||
Dictionary<ulong, int> contextMap = [];
|
||||
|
||||
if (IsInitialized) {
|
||||
return;
|
||||
@@ -62,37 +65,70 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
contextMap = [];
|
||||
int count = Environment.ProcessorCount;
|
||||
bool hasGlobalSimd = false;
|
||||
SIMDID globalSimd = default;
|
||||
_threads = new(count);
|
||||
_retiredThreads = new(RESERVED_THREAD_COUNT);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
MatrixParallelThread thread = new(i);
|
||||
_threads.Push(thread);
|
||||
SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready);
|
||||
|
||||
SIMDID simd = thread.Simd;
|
||||
ulong mask = simd.Mask;
|
||||
contextMap[mask] = contextMap.TryGetValue(mask, out int contextCount) ? (contextCount + 1) : 1;
|
||||
globalSimd = hasGlobalSimd ? UniteSIMDID(globalSimd, simd) : simd;
|
||||
hasGlobalSimd = true;
|
||||
}
|
||||
|
||||
// TODO 构建全局的保底上下文
|
||||
foreach (MatrixParallelThread thread in _threads) { }
|
||||
if (contextMap.Keys.Count == 1)
|
||||
IntrinsicContext globalContext = new();
|
||||
LoadIntrinsicFunction(&globalContext, globalSimd.Mask);
|
||||
NumericalModule.GlobalContext = globalContext;
|
||||
NumericalModule.GlobalIntrinsicMask = globalSimd;
|
||||
ContextCount = contextMap.Count;
|
||||
if (ContextCount == 1)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
MatrixParallelThread thread = _threads.Pop();
|
||||
thread.Dispose();
|
||||
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, _threads.Count));
|
||||
for (int i = 0; i < retireCount; i++) {
|
||||
RetireThread(_threads.Pop());
|
||||
}
|
||||
} else
|
||||
{
|
||||
// TODO 排序并丢弃四个能力最弱线程
|
||||
MatrixParallelThread[] allThreads = _threads.PopRange(_threads.Count);
|
||||
Array.Sort(allThreads, static(left, right) => CompareSIMDID(left.Simd, right.Simd));
|
||||
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, allThreads.Length - 1));
|
||||
for (int i = 0; i < retireCount; i++) {
|
||||
RetireThread(allThreads[i]);
|
||||
}
|
||||
_threads.PushRange(allThreads.AsSpan(retireCount));
|
||||
}
|
||||
_meaningful = _threads.Count > 1;
|
||||
_awaitingLeaderHandle = new AwaitingQueue();
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
private static unsafe partial ulong CompareSIMDID(
|
||||
IntrinsicContext* context,
|
||||
ulong id
|
||||
);
|
||||
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
|
||||
private static partial int CompareSIMDID(SIMDID left, SIMDID right);
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
|
||||
private static partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void RetireThread(MatrixParallelThread thread)
|
||||
{
|
||||
thread.Retire();
|
||||
_retiredThreads.Push(thread);
|
||||
}
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_get_unite_simd")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
|
||||
private static partial SIMDID UniteSIMDID(SIMDID left, SIMDID right);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,30 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelQueue"
|
||||
// file="MatrixParallelQueue.cs"
|
||||
// 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
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
@@ -39,42 +42,11 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
internal unsafe class MatrixParallelQueue
|
||||
internal unsafe ref struct MatrixParallelQueue
|
||||
{
|
||||
|
||||
private static readonly ConcurrentStack<MatrixParallelQueue> _recycle = [];
|
||||
private long _cursor;
|
||||
private long _maxCount;
|
||||
|
||||
private MatrixParallelQueue() { }
|
||||
|
||||
public MatrixParallelTask* BaseTask { get; private set; }
|
||||
|
||||
public static MatrixParallelQueue Rent(
|
||||
MatrixParallelTask* pack,
|
||||
long count
|
||||
)
|
||||
{
|
||||
if (!_recycle.TryPop(out MatrixParallelQueue? queue)) {
|
||||
queue = new MatrixParallelQueue();
|
||||
}
|
||||
queue.BaseTask = pack;
|
||||
queue._maxCount = count;
|
||||
queue._cursor = 0;
|
||||
return queue;
|
||||
}
|
||||
|
||||
public static void Return(
|
||||
MatrixParallelQueue queue
|
||||
)
|
||||
{
|
||||
_recycle.Push(queue);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool Steal(
|
||||
MatrixParallelTask* task
|
||||
)
|
||||
public bool Steal(MatrixParallelTask* task)
|
||||
{
|
||||
// TODO 使用提取的内容填充task
|
||||
throw new NotImplementedException();
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelTask"
|
||||
// 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
|
||||
@@ -32,6 +32,16 @@ using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric.Runtime
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct MatrixParallelSource
|
||||
{
|
||||
|
||||
public MatrixParallelTask Task;
|
||||
public ulong Count;
|
||||
public ulong Cursor;
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
internal unsafe struct MatrixParallelTask
|
||||
{
|
||||
@@ -56,13 +66,9 @@ namespace StgSharp.Mathematics.Numeric.Runtime
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Copy(
|
||||
MatrixParallelTask* source,
|
||||
MatrixParallelTask* target
|
||||
)
|
||||
public static void Copy(MatrixParallelTask* source, MatrixParallelTask* target)
|
||||
{
|
||||
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTask),
|
||||
sizeof(MatrixParallelTask));
|
||||
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTask), sizeof(MatrixParallelTask));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
if (Simd.Mask != 0)
|
||||
{
|
||||
SIMDID feature = new(GetSimdId());
|
||||
IntrinsicContext* contextPtr = stackalloc IntrinsicContext[1];
|
||||
MatrixParallelTask* taskPtr = stackalloc MatrixParallelTask[4];
|
||||
LoadIntrinsicFunction(contextPtr, feature.Mask);
|
||||
Simd = feature;
|
||||
_context = contextPtr;
|
||||
}
|
||||
MatrixParallelTask* taskPtr = stackalloc MatrixParallelTask[4];
|
||||
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);
|
||||
@@ -179,17 +217,12 @@ namespace StgSharp.Mathematics.Numeric
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")]
|
||||
[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)])]
|
||||
[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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,36 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelWrap"
|
||||
// file="MatrixParallelWrap.cs"
|
||||
// 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
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Collections;
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using StgSharp.Mathematics.Numeric.Runtime;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
@@ -48,16 +52,14 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
}
|
||||
|
||||
public sealed class MatrixParallelWrap : IDisposable
|
||||
public class MatrixParallelWrap : IDisposable
|
||||
{
|
||||
|
||||
private MatrixParallelThread[] _threads;
|
||||
private bool _disposed;
|
||||
private readonly CountdownEvent _countdownEvent;
|
||||
|
||||
internal MatrixParallelWrap(
|
||||
MatrixParallelThread[] threadArray
|
||||
)
|
||||
internal MatrixParallelWrap(MatrixParallelThread[] threadArray)
|
||||
{
|
||||
_threads = threadArray;
|
||||
_countdownEvent = new CountdownEvent(threadArray.Length);
|
||||
@@ -69,18 +71,17 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
internal L4 DataCache { get; set; }
|
||||
|
||||
internal MatrixParallelQueue TaskQueue { get; set; }
|
||||
internal PredictPolicy Policy { get; set; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void LaunchParallel(
|
||||
SleepMode afterWork
|
||||
)
|
||||
public void LaunchParallel(SleepMode afterWork)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap));
|
||||
AfterWork = (ThreadPriority)afterWork;
|
||||
@@ -107,9 +108,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
_ = _countdownEvent.Signal();
|
||||
}
|
||||
|
||||
private void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user