[rebuid]use dynamic wrapping instead of static wrapping
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Collections;
|
||||
using StgSharp.HighPerformance;
|
||||
using StgSharp.Internal;
|
||||
using System;
|
||||
@@ -48,8 +49,8 @@ namespace StgSharp.Mathematics.Numeric
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
if (count <= 1024)
|
||||
{
|
||||
MatrixParallelPool pool = MatrixParallel.LeadParallel();
|
||||
if (!pool.TryRentIndividualHandle(out MatrixParallelWrap.Handle? handle))
|
||||
CapacityFixedStack<MatrixParallelThread> pool = MatrixParallel.LeadParallel();
|
||||
if (!pool.TryPop(out MatrixParallelThread thread))
|
||||
{
|
||||
/*
|
||||
* wait
|
||||
@@ -82,8 +83,9 @@ namespace StgSharp.Mathematics.Numeric
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
if (count <= 1024)
|
||||
{
|
||||
MatrixParallelPool pool = MatrixParallel.LeadParallel();
|
||||
if (!pool.TryRentIndividualHandle(out MatrixParallelWrap.Handle? handle))
|
||||
// TODO: this is very awkward, blocking thread renting, need a better way to handle small tasks
|
||||
CapacityFixedStack<MatrixParallelThread> pool = MatrixParallel.LeadParallel();
|
||||
if (!pool.TryPop(out MatrixParallelThread thread))
|
||||
{
|
||||
/*
|
||||
* wait
|
||||
@@ -95,6 +97,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
for (int i = 0; i < count; i++) {
|
||||
NativeIntrinsic.Intrinsic.f32_sub(leftPtr + i, rightPtr + i, ansPtr + i);
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
Stack<MatrixParallelWrap> taskGroup = MatrixParallel.PublicTask(left.GetColumnEnumeration(), right.GetColumnEnumeration(), ans.GetColumnEnumeration(),
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixMarshal.cs"
|
||||
// Project: StgSharp
|
||||
// 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:
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics
|
||||
{
|
||||
public readonly ref struct MatrixMarshal { }
|
||||
}
|
||||
@@ -837,6 +837,8 @@ namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
#if DEBUG
|
||||
throw new InvalidOperationException("Someone steal parallel wraps from the pool.");
|
||||
#else
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -854,6 +856,8 @@ namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
#if DEBUG
|
||||
throw new InvalidOperationException("Zero tasks published.");
|
||||
#else
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,6 @@ namespace StgSharp.Mathematics.Numeric
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private static Thread[] _threads;
|
||||
private static readonly (int a, int b)[] LookupTable = [
|
||||
(1, 1), // 7
|
||||
(0, 2), // 8 (no non-zero solution)
|
||||
@@ -73,6 +72,8 @@ namespace StgSharp.Mathematics.Numeric
|
||||
];
|
||||
private static bool _meaningful;
|
||||
|
||||
private static CapacityFixedStack<MatrixParallelThread> _threads;
|
||||
|
||||
private static volatile int RemainThreadCount = 0;
|
||||
private static readonly object _lock = new();
|
||||
private static ThreadPriority _AfterWork = ThreadPriority.Lowest;
|
||||
@@ -118,46 +119,16 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return;
|
||||
}
|
||||
int count = Environment.ProcessorCount - 4;
|
||||
count = count == 5 ? 4 : count;
|
||||
_threads = new Thread[count];
|
||||
if (count < 6)
|
||||
{
|
||||
if (count == 3)
|
||||
{
|
||||
Pool = new(3, [ new MatrixParallelWrap(0, count) ]);
|
||||
(LargeWrapCount, SmallWrapCount) = (0, 1);
|
||||
} else
|
||||
{
|
||||
count = 4;
|
||||
Pool = new(4, [ new MatrixParallelWrap(0, count) ]);
|
||||
(LargeWrapCount, SmallWrapCount) = (1, 0);
|
||||
}
|
||||
} else if (count == 6)
|
||||
{
|
||||
Pool = new(6, [
|
||||
new MatrixParallelWrap(0, 3),
|
||||
new MatrixParallelWrap(3, 3)
|
||||
]);
|
||||
(LargeWrapCount, SmallWrapCount) = (0, 2);
|
||||
} else
|
||||
{
|
||||
(int _3wrap, int _4wrap) = FastDecompose(count);
|
||||
CapacityFixedStack<MatrixParallelWrap> stack = new(_4wrap + _3wrap);
|
||||
int id = 0, idx = 0;
|
||||
for (; idx < _3wrap; idx++, id += 3) {
|
||||
stack.Push(new MatrixParallelWrap(idx, 3));
|
||||
}
|
||||
for (; idx < _3wrap + _4wrap; idx++, id += 4) {
|
||||
stack.Push(new MatrixParallelWrap(id, 4));
|
||||
}
|
||||
Pool = new MatrixParallelPool(count, stack);
|
||||
_threads = new(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
_threads.Push(new());
|
||||
}
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
public static void LaunchParallel(IEnumerable<MatrixParallelWrap> wraps, SleepMode afterWork)
|
||||
{
|
||||
RemainThreadCount = _threads.Length;
|
||||
RemainThreadCount = _threads.Count;
|
||||
_AfterWork = (ThreadPriority)afterWork;
|
||||
ResetThreadCount();
|
||||
foreach (MatrixParallelWrap w in wraps) {
|
||||
@@ -165,36 +136,25 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void MatrixParallelLeaderWork()
|
||||
{
|
||||
MatrixParallelWrap.ExecuteTaskPackage(LeaderTask);
|
||||
_ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ResetThreadCount()
|
||||
{
|
||||
RemainThreadCount = _threads.Length;
|
||||
RemainThreadCount = _threads.Count;
|
||||
}
|
||||
|
||||
#region wrap field
|
||||
|
||||
private static MatrixParallelPool Pool;
|
||||
private static int LargeWrapCount;
|
||||
private static int SmallWrapCount;
|
||||
|
||||
public static MatrixParallelPool LeadParallel()
|
||||
public static CapacityFixedStack<MatrixParallelThread> LeadParallel()
|
||||
{
|
||||
#if NET9_0_OR_GREATER
|
||||
#else
|
||||
Monitor.Enter(_lock);
|
||||
#endif
|
||||
|
||||
return Pool;
|
||||
return _threads;
|
||||
}
|
||||
|
||||
public static void ReleaseParallelLeadership(ref MatrixParallelPool pool)
|
||||
public static void ReleaseParallelLeadership(ref CapacityFixedStack<MatrixParallelThread> pool)
|
||||
{
|
||||
#if NET9_0_OR_GREATER
|
||||
#else
|
||||
|
||||
@@ -51,77 +51,89 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly unsafe struct MatrixParallelTaskPackageNonGeneric
|
||||
internal unsafe struct MatrixParallelTaskPackageNonGeneric
|
||||
{
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void Copy(
|
||||
MatrixParallelTaskPackageNonGeneric* source,
|
||||
MatrixParallelTaskPackageNonGeneric* target)
|
||||
{
|
||||
Buffer.MemoryCopy(
|
||||
source,
|
||||
target,
|
||||
sizeof(MatrixParallelTaskPackageNonGeneric),
|
||||
sizeof(MatrixParallelTaskPackageNonGeneric));
|
||||
}
|
||||
|
||||
#region source
|
||||
|
||||
// Group 1: Left/Right matrix kernel base address
|
||||
public readonly IntPtr Left; // 8
|
||||
public readonly IntPtr Right; // 8
|
||||
public IntPtr Left; // 8
|
||||
public IntPtr Right; // 8
|
||||
|
||||
#endregion
|
||||
|
||||
#region ans
|
||||
|
||||
// Group 2: Result kernel base address + reserved
|
||||
public readonly IntPtr Result; // 8
|
||||
public readonly int ElementSize;
|
||||
public readonly int ReservedPtr0; // 4 (extension/reserved)
|
||||
public IntPtr Result; // 8
|
||||
public int ElementSize;
|
||||
public int ReservedPtr0; // 4 (extension/reserved)
|
||||
|
||||
#endregion
|
||||
|
||||
#region left enum
|
||||
|
||||
public readonly int LeftPrimOffset; // 4
|
||||
public readonly int LeftPrimStride; // 4
|
||||
public readonly int LeftSecOffset; // 4
|
||||
public readonly int LeftSecStride; // 4
|
||||
public int LeftPrimOffset; // 4
|
||||
public int LeftPrimStride; // 4
|
||||
public int LeftSecOffset; // 4
|
||||
public int LeftSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region right enum
|
||||
|
||||
public readonly int RightPrimOffset; // 4
|
||||
public readonly int RightPrimStride; // 4
|
||||
public readonly int RightSecOffset; // 4
|
||||
public readonly int RightSecStride; // 4
|
||||
public int RightPrimOffset; // 4
|
||||
public int RightPrimStride; // 4
|
||||
public int RightSecOffset; // 4
|
||||
public int RightSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region ans enum
|
||||
|
||||
public readonly int ResultPrimOffset; // 4
|
||||
public readonly int ResultPrimStride; // 4
|
||||
public readonly int ResultSecOffset; // 4
|
||||
public readonly int ResultSecStride; // 4
|
||||
public int ResultPrimOffset; // 4
|
||||
public int ResultPrimStride; // 4
|
||||
public int ResultSecOffset; // 4
|
||||
public int ResultSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region scalar/compute
|
||||
|
||||
public readonly ScalarPacket* Scalar; // 8
|
||||
public readonly IntPtr ComputeHandle; // 8
|
||||
public ScalarPacket* Scalar; // 8
|
||||
public IntPtr ComputeHandle; // 8
|
||||
|
||||
#endregion
|
||||
|
||||
#region global profile
|
||||
|
||||
public readonly int PrimCount; // 4
|
||||
public readonly int ComputeMode; // 4
|
||||
public readonly int SecCount; // 4
|
||||
private readonly int ReservedInt0; // 4
|
||||
public int PrimCount; // 4
|
||||
public int ComputeMode; // 4
|
||||
public int SecCount; // 4
|
||||
private int ReservedInt0; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region count offset
|
||||
|
||||
public readonly int PrimTileOffset; // 4
|
||||
public readonly int SecTileOffset; // 4
|
||||
public readonly int PrimCountInTile; // 4
|
||||
public readonly int SecCountInTile; // 4
|
||||
public int PrimTileOffset; // 4
|
||||
public int SecTileOffset; // 4
|
||||
public int PrimCountInTile; // 4
|
||||
public int SecCountInTile; // 4
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelThread"
|
||||
// Project: StgSharp
|
||||
// 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
|
||||
// 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 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 System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public class MatrixParallelThread : IDisposable
|
||||
{
|
||||
|
||||
private bool disposedValue;
|
||||
private ManualResetEventSlim _resetEvent;
|
||||
private Thread _managedThread;
|
||||
|
||||
public MatrixParallelThread()
|
||||
{
|
||||
_managedThread = new Thread(MatrixParallelWorker);
|
||||
_resetEvent = new ManualResetEventSlim(false);
|
||||
}
|
||||
|
||||
// ~MatrixComputeThread()
|
||||
// {
|
||||
// Dispose(disposing: false);
|
||||
// }
|
||||
|
||||
public ThreadPriority AfterWork
|
||||
{
|
||||
get => field;
|
||||
set => field = value;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing:true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void ExecuteTaskPackage(Queue<IntPtr> taskQueue)
|
||||
{
|
||||
while (taskQueue.TryDequeue(out nint handle))
|
||||
{
|
||||
MatrixParallelTaskPackageNonGeneric* p = (MatrixParallelTaskPackageNonGeneric*)handle;
|
||||
switch (p->ComputeMode)
|
||||
{
|
||||
case 1:
|
||||
MatrixComputeModel.Compute_Unary(p);
|
||||
break;
|
||||
case 2:
|
||||
MatrixComputeModel.Compute_UnaryScalar(p);
|
||||
break;
|
||||
case 3:
|
||||
MatrixComputeModel.Compute_Binary(p);
|
||||
break;
|
||||
case 4:
|
||||
MatrixComputeModel.Compute_BinaryScalar(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Restart()
|
||||
{
|
||||
_resetEvent.Set();
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_resetEvent.Dispose();
|
||||
_managedThread.Join();
|
||||
}
|
||||
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe void MatrixParallelWorker()
|
||||
{
|
||||
// Thread self = Thread.CurrentThread;
|
||||
do
|
||||
{
|
||||
_resetEvent.Wait();
|
||||
_resetEvent.Reset();
|
||||
_managedThread.Priority = ThreadPriority.Highest;
|
||||
switch (Role)
|
||||
{
|
||||
case 0://worker
|
||||
// _ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
ExecuteTaskPackage(Tasks);
|
||||
break;
|
||||
case 1://scheduler
|
||||
BindedWrap.PublishTaskToWorkers(Tasks);
|
||||
BindedWrap.SetAllWorker();
|
||||
ExecuteTaskPackage(Tasks);
|
||||
|
||||
break;
|
||||
case 2://leader, error because leader should be at outer thread but not here
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
_managedThread.Priority = AfterWork;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
#region wrap belong
|
||||
|
||||
internal MatrixParallelWrap BindedWrap { get; set; } = null!;
|
||||
|
||||
internal Queue<IntPtr> Tasks { get; } = [];
|
||||
|
||||
internal int Role
|
||||
{
|
||||
get;
|
||||
set
|
||||
{
|
||||
if ((Role & (~3u)) == 0)
|
||||
{
|
||||
field = value;
|
||||
} else
|
||||
{
|
||||
#if DEBUG
|
||||
throw new ArgumentOutOfRangeException(nameof(Role));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -39,44 +39,13 @@ namespace StgSharp.Mathematics.Numeric
|
||||
public unsafe class MatrixParallelWrap
|
||||
{
|
||||
|
||||
private readonly Handle[] _resetHandle;
|
||||
private readonly Thread[] _threads;
|
||||
private readonly MatrixParallelThread[] _threads;
|
||||
|
||||
public MatrixParallelWrap(int id, int wrapSize)
|
||||
public MatrixParallelWrap()
|
||||
{
|
||||
ID = id;
|
||||
_threads = new Thread[wrapSize];
|
||||
_resetHandle = new Handle[wrapSize];
|
||||
for (int i = 0; i < wrapSize; i++)
|
||||
{
|
||||
_threads[i] = new Thread(MatrixParallelWorker)
|
||||
{
|
||||
IsBackground = true,
|
||||
Priority = ThreadPriority.BelowNormal,
|
||||
Name = $"MPT#{ID}_{i}"
|
||||
};
|
||||
Handle handle = new Handle(i, this);
|
||||
_resetHandle[i] = handle;
|
||||
_threads[i].Start(handle);
|
||||
}
|
||||
_threads = new MatrixParallelThread[5];
|
||||
}
|
||||
|
||||
public bool IsAllAvailable
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach (Handle item in _resetHandle)
|
||||
{
|
||||
if (item.Role != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public int ID { get; init; }
|
||||
|
||||
public static SleepMode AfterWork
|
||||
{
|
||||
get;
|
||||
@@ -87,27 +56,39 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
internal ManualResetEventSlim SchedulerReset { get; } = new(false);
|
||||
|
||||
private int Count { get; set; }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetAllWorker()
|
||||
{
|
||||
foreach (Handle item in _resetHandle) {
|
||||
item.ResetEvent.Set();
|
||||
foreach (MatrixParallelThread t in _threads) {
|
||||
t.Restart();
|
||||
}
|
||||
}
|
||||
|
||||
internal bool TryGetLeaderHandle(out Handle? handle)
|
||||
internal void PublishTaskToWorkers(Queue<IntPtr> tasks)
|
||||
{
|
||||
foreach (Handle item in _resetHandle)
|
||||
while (tasks.TryDequeue(out nint handle))
|
||||
{
|
||||
if (item.Role != 2)
|
||||
{
|
||||
handle = item;
|
||||
item.Role = 2;
|
||||
return true;
|
||||
}
|
||||
MatrixParallelTaskPackageNonGeneric* task = (MatrixParallelTaskPackageNonGeneric*)handle;
|
||||
PublishTaskToWorkers(task);
|
||||
}
|
||||
}
|
||||
|
||||
internal void PublishTaskToWorkers(MatrixParallelTaskPackageNonGeneric* taskPackage)
|
||||
{
|
||||
int primCount = taskPackage->PrimCount;
|
||||
int secCount = taskPackage->SecCount;
|
||||
|
||||
int primAverage = primCount / Count;
|
||||
int secAverage = secCount / Count;
|
||||
bool shouldSliceSec = secAverage > 512;
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
Queue<IntPtr> tasks = _threads[i].Tasks;
|
||||
MatrixParallelTaskPackageNonGeneric* subTask = MatrixParallelFactory.FromExistPackage(taskPackage);
|
||||
subTask->PrimTileOffset = i * primAverage;
|
||||
}
|
||||
handle = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public class Handle(int inWrapIndex, MatrixParallelWrap wrap)
|
||||
@@ -141,71 +122,6 @@ namespace StgSharp.Mathematics.Numeric
|
||||
get => _threads.Length;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region ThreadOperation
|
||||
|
||||
private static unsafe void MatrixParallelWorker([AllowNull] object obj)
|
||||
{
|
||||
Handle handle = (obj as Handle)!;
|
||||
MatrixParallelWrap wrap = handle.Wrap;
|
||||
int inWrapIndex = handle.InWrapIndex;
|
||||
Thread self = Thread.CurrentThread;
|
||||
ManualResetEventSlim reset = handle.ResetEvent;
|
||||
do
|
||||
{
|
||||
reset.Wait();
|
||||
reset.Reset();
|
||||
Queue<IntPtr> taskQueue = handle.TaskCache;
|
||||
self.Priority = ThreadPriority.Highest;
|
||||
switch (handle.Role)
|
||||
{
|
||||
case 0://worker
|
||||
// _ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
ExecuteTaskPackage(handle.TaskCache);
|
||||
break;
|
||||
case 1://scheduler
|
||||
// TODO task publication to worker in wrap
|
||||
wrap.SetAllWorker();
|
||||
ExecuteTaskPackage(taskQueue);
|
||||
|
||||
break;
|
||||
case 2://leader, error because leader should be at outer thread but not here
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
self.Priority = (ThreadPriority)AfterWork;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void ExecuteTaskPackage(Queue<IntPtr> taskQueue)
|
||||
{
|
||||
while (taskQueue.TryDequeue(out nint handle))
|
||||
{
|
||||
MatrixParallelTaskPackageNonGeneric* p = (MatrixParallelTaskPackageNonGeneric*)handle;
|
||||
switch (p->ComputeMode)
|
||||
{
|
||||
case 1:
|
||||
MatrixComputeModel.Compute_Unary(p);
|
||||
break;
|
||||
case 2:
|
||||
MatrixComputeModel.Compute_UnaryScalar(p);
|
||||
break;
|
||||
case 3:
|
||||
MatrixComputeModel.Compute_Binary(p);
|
||||
break;
|
||||
case 4:
|
||||
MatrixComputeModel.Compute_BinaryScalar(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -217,18 +133,4 @@ namespace StgSharp.Mathematics.Numeric
|
||||
DeepSleep = ThreadPriority.Lowest,
|
||||
|
||||
}
|
||||
|
||||
public class MatrixComputeResource
|
||||
{
|
||||
|
||||
public IEnumerable<MatrixParallelWrap> Wraps { get; set; }
|
||||
|
||||
public int WorkingThreadCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Closure task leader should do to collect results.
|
||||
/// </summary>
|
||||
public unsafe IntPtr LeaderClosurePackage { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,27 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.TaskFactory.cs"
|
||||
// file="MatrixTaskFactory"
|
||||
// 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 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 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.
|
||||
// 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 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
@@ -85,6 +82,15 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return ptr;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe MatrixParallelTaskPackageNonGeneric* FromExistPackage(
|
||||
MatrixParallelTaskPackageNonGeneric* exist)
|
||||
{
|
||||
MatrixParallelTaskPackageNonGeneric* ptr = (MatrixParallelTaskPackageNonGeneric*)_wrapAllocator.Allocate();
|
||||
MatrixParallelTaskPackageNonGeneric.Copy(exist, ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
public static void Release(ScalarPacket* source)
|
||||
|
||||
Reference in New Issue
Block a user