Huge progress on Matrix Parallel, a parallel pool has its basic structure.
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
|
||||
using System;
|
||||
@@ -35,6 +36,8 @@ using System.Collections.Generic;
|
||||
using System.IO.Pipes;
|
||||
using System.Linq;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Channels;
|
||||
@@ -45,40 +48,220 @@ namespace StgSharp.Mathematics
|
||||
internal static class MatrixParallel
|
||||
{
|
||||
|
||||
private static readonly Channel<IntPtr> _taskChannel = Channel.CreateUnbounded<IntPtr>();
|
||||
private static int _paraState;
|
||||
// TODO 解决多线程协调问题,barrier性能不好,MRES时机不好控制,可能可以使用双MRES
|
||||
private static Thread[] _threads;
|
||||
private static MatrixParallelWrap[] _wraps;
|
||||
private static readonly (int a, int b)[] LookupTable =
|
||||
[(1, 1), (0, 2), (3, 0), (2, 1), (1, 2),
|
||||
(0, 3), (3, 1), (2, 2), (1, 3), (4, 1),
|
||||
(3, 2), (2, 3), (1, 4), (0, 5),];
|
||||
private static volatile int RemainThreadCount = 0;
|
||||
|
||||
private static IReadOnlyList<Thread> _threads;
|
||||
private static readonly ManualResetEventSlim _resetEvent = new(false);
|
||||
private static readonly object _lock = new();
|
||||
private static ThreadPriority _AfterWork = ThreadPriority.Lowest;
|
||||
|
||||
public static bool SupportParallel { get; } = Environment.ProcessorCount > 6;
|
||||
|
||||
public static void EndParallel()
|
||||
{
|
||||
Monitor.Exit(_lock);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static (int a, int b) FastDecompose(int c)
|
||||
{
|
||||
if (c < 7) {
|
||||
return (0, 0);
|
||||
}
|
||||
c -= 7;
|
||||
int c_prime = c / 14;
|
||||
int d = c % 14;
|
||||
|
||||
(int a_prime, int b_prime) = LookupTable[d];
|
||||
|
||||
return (a_prime + c_prime, b_prime + c_prime);
|
||||
}
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
int count = Environment.ProcessorCount - 4;
|
||||
count = count < 0 ? 1 : count;
|
||||
List<Thread> _t = new(count);
|
||||
_threads = _t;
|
||||
for (int i = 0; i < count; i++)
|
||||
count = count == 5 ? 4 : count;
|
||||
int _3wrap = 0, _4wrap = 0;
|
||||
_threads = new Thread[count];
|
||||
if (count < 6)
|
||||
{
|
||||
_t.Add(new(MatrixParallelCycle)
|
||||
_wraps = new MatrixParallelWrap[1];
|
||||
_wraps[0] = new MatrixParallelWrap(count);
|
||||
for (int i = 1; i < count; i++) //first is reverse for leader thread
|
||||
{
|
||||
_threads[i] = new Thread(MatrixParallelCycle)
|
||||
{
|
||||
IsBackground = true
|
||||
});
|
||||
_t[i].Start();
|
||||
IsBackground = true,
|
||||
Priority = ThreadPriority.Highest,
|
||||
Name = $"StgSharp Matrix Parallel Worker {i}"
|
||||
};
|
||||
_threads[i].Start(new MatrixParallelWrap.Handle(i, _wraps[0]));
|
||||
}
|
||||
} else if (count == 6)
|
||||
{
|
||||
_3wrap = 2;
|
||||
_threads = new Thread[6];
|
||||
_wraps = new MatrixParallelWrap[2];
|
||||
_wraps[0] = new(3);
|
||||
_wraps[1] = new(3);
|
||||
for (int i = 1; i < 3; i++) //first is reverse for leader thread
|
||||
{
|
||||
_threads[i] = new Thread(MatrixParallelCycle)
|
||||
{
|
||||
IsBackground = true,
|
||||
Priority = ThreadPriority.Highest,
|
||||
Name = $"StgSharp Matrix Parallel Worker {i}"
|
||||
};
|
||||
_threads[i].Start(new MatrixParallelWrap.Handle(i, _wraps[0]));
|
||||
}
|
||||
for (int i = 3; i < 6; i++)
|
||||
{
|
||||
_threads[i] = new Thread(MatrixParallelCycle)
|
||||
{
|
||||
IsBackground = true,
|
||||
Priority = ThreadPriority.Highest,
|
||||
Name = $"StgSharp Matrix Parallel Worker {i}"
|
||||
};
|
||||
_threads[i].Start(new MatrixParallelWrap.Handle(i - 3, _wraps[1]));
|
||||
}
|
||||
} else
|
||||
{
|
||||
(_3wrap, _4wrap) = FastDecompose(count);
|
||||
_wraps = new MatrixParallelWrap[_3wrap + _4wrap];
|
||||
int i = 0, wi = 0;
|
||||
for (; i < 3 * _3wrap; i += 3)
|
||||
{
|
||||
MatrixParallelWrap wrap = new(3);
|
||||
_wraps[wi] = wrap;
|
||||
wi++;
|
||||
for (int j = i; j < i + 3; j++)
|
||||
{
|
||||
if (j == 0)
|
||||
{
|
||||
continue; //first is reverse for leader thread
|
||||
}
|
||||
_threads[j] = new Thread(MatrixParallelCycle)
|
||||
{
|
||||
IsBackground = true,
|
||||
Priority = ThreadPriority.Highest,
|
||||
Name = $"StgSharp Matrix Parallel Worker {j}"
|
||||
};
|
||||
_threads[j].Start(new MatrixParallelWrap.Handle(j, wrap));
|
||||
}
|
||||
}
|
||||
for (; i < count; i += 4)
|
||||
{
|
||||
MatrixParallelWrap wrap = new(4);
|
||||
_wraps[wi] = wrap;
|
||||
wi++;
|
||||
for (int j = i; j < i + 4; j++)
|
||||
{
|
||||
_threads[j] = new Thread(MatrixParallelCycle)
|
||||
{
|
||||
IsBackground = true,
|
||||
Priority = ThreadPriority.Highest,
|
||||
Name = $"StgSharp Matrix Parallel Worker {j}"
|
||||
};
|
||||
_threads[j].Start(new MatrixParallelWrap.Handle(j, wrap));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void LaunchParallel(SleepMode afterWork)
|
||||
{
|
||||
_resetEvent.Set();
|
||||
RemainThreadCount = _threads.Length;
|
||||
_AfterWork = (ThreadPriority)afterWork;
|
||||
_resetEvent.Reset();
|
||||
}
|
||||
|
||||
public static void LeadParallel()
|
||||
{
|
||||
Monitor.Enter(_lock);
|
||||
}
|
||||
|
||||
public static void PublishTask() { }
|
||||
|
||||
public static void Terminate()
|
||||
private static unsafe void MatrixParallelCycle(object obj)
|
||||
{
|
||||
_ = Interlocked.Exchange(ref _paraState, 0);
|
||||
}
|
||||
|
||||
private static unsafe void MatrixParallelCycle()
|
||||
{
|
||||
Thread t_cur = Thread.CurrentThread;
|
||||
int index = _threads.ToList().IndexOf(t_cur);
|
||||
while (_taskChannel.Reader.TryRead(out IntPtr ptr)) { }
|
||||
MatrixParallelWrap.Handle handle = (obj as MatrixParallelWrap.Handle)!;
|
||||
MatrixParallelWrap wrap = handle.Wrap;
|
||||
int inWrapIndex = handle.InWrapIndex;
|
||||
Thread self = Thread.CurrentThread;
|
||||
while (true)
|
||||
{
|
||||
while (wrap.TryGetNextWrapTask(inWrapIndex, out IntPtr ptr))
|
||||
{
|
||||
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)ptr;
|
||||
MatrixKernel* left = p->left, right = p->left, result = p->result;
|
||||
int count = p->count;
|
||||
ScalarDataPacket* scalar = p->scalerPacket;
|
||||
switch (p->ComputeMode)
|
||||
{
|
||||
case 0:
|
||||
delegate*<MatrixKernel*, MatrixKernel*, void> op_0 = (delegate*<MatrixKernel*, MatrixKernel*, void>)p->executionHandle;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
op_0(left, result);
|
||||
left += p->leftStride;
|
||||
result += p->resultStride;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
delegate*<MatrixKernel*, MatrixKernel*, ScalarDataPacket*, void> op_1 = (delegate*<MatrixKernel*, MatrixKernel*, ScalarDataPacket*, void>)p->executionHandle;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
op_1(left, result, scalar);
|
||||
left += p->leftStride;
|
||||
result += p->resultStride;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
delegate*<MatrixKernel*, MatrixKernel*, MatrixKernel*, void> op_2 = (delegate*<MatrixKernel*, MatrixKernel*, MatrixKernel*, void>)p->executionHandle;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
op_2(left, right, result);
|
||||
right += p->rightStride;
|
||||
left += p->leftStride;
|
||||
result += p->resultStride;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
delegate*<MatrixKernel*, MatrixKernel*, MatrixKernel*, ScalarDataPacket*, void> op_3 = (delegate*<MatrixKernel*, MatrixKernel*, MatrixKernel*, ScalarDataPacket*, void>)p->executionHandle;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
op_3(left, right, result, scalar);
|
||||
right += p->rightStride;
|
||||
left += p->leftStride;
|
||||
result += p->resultStride;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
self.Priority = _AfterWork;
|
||||
_resetEvent.Wait();
|
||||
self.Priority = ThreadPriority.Highest;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum SleepMode
|
||||
{
|
||||
|
||||
QuickWakeup = ThreadPriority.AboveNormal,
|
||||
NormalSleep = ThreadPriority.BelowNormal,
|
||||
DeepSleep = ThreadPriority.Lowest,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user