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.Collections.Generic;
using System.Formats.Asn1;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics
{
@@ -98,7 +91,7 @@ namespace StgSharp.Mathematics
lLimit = pCount - p->LeftPrimOffset,
rLimit = pCount - p->RightPrimOffset,
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;
ScalarPacket* scalar = p->Scalar;
for (long i = 0; i < pCount; i++)
@@ -76,10 +76,22 @@ namespace StgSharp.Mathematics
package->ElementSize = sizeof(T);
package->ComputeMode = 3;
int
small_4 = c4 / length,
larget_4
if ((c3 | c4) > 0) { } else
{
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.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
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;
using System.Threading.Tasks;
namespace StgSharp.Mathematics
{
@@ -68,7 +57,6 @@ namespace StgSharp.Mathematics
{
private static Thread[] _threads;
private static MatrixParallelWrap[] _wraps;
private static readonly (int a, int b)[] LookupTable = [
(1, 1),
(0, 2),
@@ -87,9 +75,7 @@ namespace StgSharp.Mathematics
];
private static bool _meaningful;
private static int LargeWrapCount;
private static volatile int RemainThreadCount = 0;
private static int SmallWrapCount;
private static readonly object _lock = new();
private static ThreadPriority _AfterWork = ThreadPriority.Lowest;
@@ -139,25 +125,37 @@ namespace StgSharp.Mathematics
_threads = new Thread[count];
if (count < 6)
{
count = count == 3 ? 3 : 4;
_wraps = [ new MatrixParallelWrap(0, count, true) ];
(LargeWrapCount, SmallWrapCount) = count == 3 ? (1,0) : (0,1);
if (count == 3)
{
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)
{
_wraps = [
SmallWraps = [
new MatrixParallelWrap(0, 3, true),
new MatrixParallelWrap(3, 3, false)
];
LargeWraps = [];
(LargeWrapCount, SmallWrapCount) = (0, 2);
} else
{
(int _3wrap, int _4wrap) = FastDecompose(count);
_wraps = new MatrixParallelWrap[_3wrap + _4wrap];
int id = 0, idx = 0;
for (; idx < _3wrap; idx++, id += 3) {
_wraps[idx] = new MatrixParallelWrap(idx, 3, id == 0);
LargeWraps = new MatrixParallelWrap[ _4wrap];
SmallWraps = new MatrixParallelWrap[ _3wrap];
int 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) {
_wraps[idx] = new MatrixParallelWrap(id, 4, id == 0);
for (int idx = 0; idx < _3wrap + _4wrap; idx++, id += 4) {
LargeWraps[idx] = new MatrixParallelWrap(id, 4, id == 0);
}
}
LeaderHandle = new MatrixParallelWrap.Handle(0, _wraps[0]);
@@ -344,6 +342,14 @@ namespace StgSharp.Mathematics
#endregion
}
#region wrap field
private static MatrixParallelWrap[] LargeWraps;
private static MatrixParallelWrap[] SmallWraps;
private static int LargeWrapCount;
private static int SmallWrapCount;
#endregion
}
public enum SleepMode