implemet wrap builder

This commit is contained in:
Youmu Liu
2025-09-15 09:06:23 +08:00
parent 4769986c37
commit 0a16f8317f
20 changed files with 33 additions and 96 deletions
+2 -2
View File
@@ -154,7 +154,7 @@ namespace StgSharp.Mathematics
private readonly int _secondaryStride = enumeration._secondaryStride;
private readonly int _stride = enumeration._stride;
public readonly Segment Current
public readonly MatrixSegment Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_current, _secondarySize, _secondaryStride);
@@ -170,7 +170,7 @@ namespace StgSharp.Mathematics
}
public readonly unsafe ref struct Segment(MatrixKernel* source, int size, int stride)
public readonly unsafe ref struct MatrixSegment(MatrixKernel* source, int size, int stride)
{
private readonly MatrixKernel* _source = source;
@@ -50,16 +50,6 @@ namespace StgSharp.Mathematics
return p;
}
public static MatrixParallelTaskPackage* FromTaskWrap(MatrixParallelTaskPackage* source, int offset)
{
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
*p = *source;
p->left += offset * p->leftStride;
p->right += offset * p->rightStride;
p->result += offset * p->resultStride;
return p;
}
public static void Release(ScalarPacket* source)
{
_scalarAllocator.Free((nuint)source);
@@ -67,35 +57,11 @@ namespace StgSharp.Mathematics
public static void Release(MatrixParallelTaskPackage* source)
{
if (source->scalerPacket != null) {
_scalarAllocator.Free((nuint)source->scalerPacket);
if (source->Scalar != null) {
_scalarAllocator.Free((nuint)source->Scalar);
}
_wrapAllocator.Free((nuint)source);
}
public static void SliceTask(MatrixParallelTaskPackage* source, Span<IntPtr> group)
{
int count = group.Length;
group[0] = (nint)source;
int l = source->leftStride, r = source->rightStride, res = source->resultStride, c = source->count / count;
source->leftStride *= count;
source->rightStride *= count;
source->resultStride *= count;
int remain = source->count - (c * count);
for (int i = 1; i < count; i++)
{
nint handle = (nint)_wrapAllocator.Allocate();
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)handle;
*p = *source;
p->left += l;
p->right += r;
p->result += res;
group[i] = handle;
}
for (int i = 0; i < remain; i++) {
((MatrixParallelTaskPackage*)group[^i])->count++;
}
}
}
}
@@ -107,9 +107,26 @@ namespace StgSharp.Mathematics
{
int count = Environment.ProcessorCount - 4;
count = count == 5 ? 4 : count;
int _3wrap = 0, _4wrap = 0;
_threads = new Thread[count];
if (count < 6) { } else if (count == 6) { } else { }
if (count < 6)
{
_wraps = [new MatrixParallelWrap(0, count, true)];
} else if (count == 6)
{
_wraps = [new MatrixParallelWrap(0, 3, true),
new MatrixParallelWrap(3, 3, false)];
} 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);
}
for (; idx < _3wrap + _4wrap; idx++, id += 4) {
_wraps[idx] = new MatrixParallelWrap(id, 4, id == 0);
}
}
LeaderHandle = new MatrixParallelWrap.Handle(0, _wraps[0]);
}