This commit is contained in:
Youmu Liu
2025-09-26 20:45:28 +08:00
parent 0cafe9fd4d
commit 003f9d272e
3 changed files with 46 additions and 35 deletions
@@ -28,15 +28,8 @@
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.HighPerformance;
using System; using System;
using System.Collections.Generic;
using System.Formats.Asn1;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics namespace StgSharp.Mathematics
{ {
@@ -98,7 +91,7 @@ namespace StgSharp.Mathematics
lLimit = pCount - p->LeftPrimOffset, lLimit = pCount - p->LeftPrimOffset,
rLimit = pCount - p->RightPrimOffset, rLimit = pCount - p->RightPrimOffset,
aLimit = pCount - p->ResultPrimOffset; aLimit = pCount - p->ResultPrimOffset;
delegate* unmanaged[Cdecl]<nint,nint,nint, ScalarPacket*, void> op = delegate* unmanaged[Cdecl]<nint, nint, nint, ScalarPacket*, void> op =
(delegate* unmanaged[Cdecl]<nint, nint, nint, ScalarPacket*, void>)p->ComputeHandle; (delegate* unmanaged[Cdecl]<nint, nint, nint, ScalarPacket*, void>)p->ComputeHandle;
ScalarPacket* scalar = p->Scalar; ScalarPacket* scalar = p->Scalar;
for (long i = 0; i < pCount; i++) for (long i = 0; i < pCount; i++)
@@ -76,10 +76,22 @@ namespace StgSharp.Mathematics
package->ElementSize = sizeof(T); package->ElementSize = sizeof(T);
package->ComputeMode = 3; package->ComputeMode = 3;
int if ((c3 | c4) > 0) { } else
small_4 = c4 / length, {
larget_4 int
small_4_size = c4 / LargeWrapCount,
large_4_size = small_4_size + 1,
small_3_size = c3 / LargeWrapCount,
large_3_size = small_3_size + 1;
int
large_4_count = c4 % LargeWrapCount,
small_4_count = LargeWrapCount - large_4_count,
large_3_count = c3 % LargeWrapCount,
small_3_count = LargeWrapCount - large_3_count;
for (int i = 0; i < large_4_count; i++) { }
// TODO maybe a task slice method is good?
}
} }
} }
@@ -28,22 +28,11 @@
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.HighPerformance;
using StgSharp.HighPerformance.Memory;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.IO.Pipes;
using System.Linq;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace StgSharp.Mathematics namespace StgSharp.Mathematics
{ {
@@ -68,7 +57,6 @@ namespace StgSharp.Mathematics
{ {
private static Thread[] _threads; private static Thread[] _threads;
private static MatrixParallelWrap[] _wraps;
private static readonly (int a, int b)[] LookupTable = [ private static readonly (int a, int b)[] LookupTable = [
(1, 1), (1, 1),
(0, 2), (0, 2),
@@ -87,9 +75,7 @@ namespace StgSharp.Mathematics
]; ];
private static bool _meaningful; private static bool _meaningful;
private static int LargeWrapCount;
private static volatile int RemainThreadCount = 0; private static volatile int RemainThreadCount = 0;
private static int SmallWrapCount;
private static readonly object _lock = new(); private static readonly object _lock = new();
private static ThreadPriority _AfterWork = ThreadPriority.Lowest; private static ThreadPriority _AfterWork = ThreadPriority.Lowest;
@@ -139,25 +125,37 @@ namespace StgSharp.Mathematics
_threads = new Thread[count]; _threads = new Thread[count];
if (count < 6) if (count < 6)
{ {
count = count == 3 ? 3 : 4; if (count == 3)
_wraps = [ new MatrixParallelWrap(0, count, true) ]; {
(LargeWrapCount, SmallWrapCount) = count == 3 ? (1,0) : (0,1); SmallWraps = [ new MatrixParallelWrap(0, count, true) ];
LargeWraps = [];
(LargeWrapCount, SmallWrapCount) = (1, 0);
} else
{
count = 4;
LargeWraps = [ new MatrixParallelWrap(0, count, true) ];
SmallWraps = [];
(LargeWrapCount, SmallWrapCount) = (0, 1);
}
} else if (count == 6) } else if (count == 6)
{ {
_wraps = [ SmallWraps = [
new MatrixParallelWrap(0, 3, true), new MatrixParallelWrap(0, 3, true),
new MatrixParallelWrap(3, 3, false) new MatrixParallelWrap(3, 3, false)
]; ];
LargeWraps = [];
(LargeWrapCount, SmallWrapCount) = (0, 2);
} else } else
{ {
(int _3wrap, int _4wrap) = FastDecompose(count); (int _3wrap, int _4wrap) = FastDecompose(count);
_wraps = new MatrixParallelWrap[_3wrap + _4wrap]; LargeWraps = new MatrixParallelWrap[ _4wrap];
int id = 0, idx = 0; SmallWraps = new MatrixParallelWrap[ _3wrap];
for (; idx < _3wrap; idx++, id += 3) { int id = 0;
_wraps[idx] = new MatrixParallelWrap(idx, 3, id == 0); for (int idx = 0; idx < _3wrap; idx++, id += 3) {
SmallWraps[idx] = new MatrixParallelWrap(idx, 3, id == 0);
} }
for (; idx < _3wrap + _4wrap; idx++, id += 4) { for (int idx = 0; idx < _3wrap + _4wrap; idx++, id += 4) {
_wraps[idx] = new MatrixParallelWrap(id, 4, id == 0); LargeWraps[idx] = new MatrixParallelWrap(id, 4, id == 0);
} }
} }
LeaderHandle = new MatrixParallelWrap.Handle(0, _wraps[0]); LeaderHandle = new MatrixParallelWrap.Handle(0, _wraps[0]);
@@ -344,6 +342,14 @@ namespace StgSharp.Mathematics
#endregion #endregion
} }
#region wrap field
private static MatrixParallelWrap[] LargeWraps;
private static MatrixParallelWrap[] SmallWraps;
private static int LargeWrapCount;
private static int SmallWrapCount;
#endregion
} }
public enum SleepMode public enum SleepMode