This commit is contained in:
Youmu Liu
2026-03-26 18:02:13 +08:00
parent d960d02972
commit 06e3aa119b
20 changed files with 198 additions and 169 deletions
+15 -15
View File
@@ -64,7 +64,10 @@ namespace StgSharp.Mathematics.Numeric
{
get
{
if (y < 0 || y > _size.ElementColumnLength || x < 0 || x > _size.ElementRowLength) {
if ((y < 0) ||
(y > _size.ElementColumnLength) ||
(x < 0) ||
(x > _size.ElementRowLength)) {
throw new ArgumentOutOfRangeException($"Index [{x},{y}] is out of matrix index range.");
}
long offset = GetElementOffset(x, y);
@@ -75,7 +78,10 @@ namespace StgSharp.Mathematics.Numeric
}
set
{
if (y < 0 || y > _size.ElementColumnLength || x < 0 || x > _size.ElementRowLength) {
if ((y < 0) ||
(y > _size.ElementColumnLength) ||
(x < 0) ||
(x > _size.ElementRowLength)) {
throw new ArgumentOutOfRangeException($"Index [{x},{y}] is out of matrix index range.");
}
long offset = GetElementOffset(x, y);
@@ -106,8 +112,8 @@ namespace StgSharp.Mathematics.Numeric
ReadOnlySpan<MatrixIndex> row
]
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
/// <summary>
@@ -122,11 +128,7 @@ namespace StgSharp.Mathematics.Numeric
public MatrixLayout Layout { get; init; }
internal MatrixKernel<T>* Buffer
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (MatrixKernel<T>*)_buffer.BufferPointer;
}
internal MatrixKernel<T>* Buffer => (MatrixKernel<T>*)_buffer.BufferPointer;
protected internal int KernelColumnLength => _size.KernelColumnLength;
@@ -138,9 +140,9 @@ namespace StgSharp.Mathematics.Numeric
int y
)
{
return ref _boarder.IsInBuffer(x, y, in _size) ?
ref _buffer[_boarder.GetIndexOffset(x, y, in _size)] :
ref Unsafe.NullRef<MatrixKernel<T>>();
return ref (_boarder.IsInBuffer(x, y, in _size) ?
ref _buffer[_boarder.GetIndexOffset(x, y, in _size)] :
ref Unsafe.NullRef<MatrixKernel<T>>());
}
internal long GetElementOffset(
@@ -158,7 +160,7 @@ namespace StgSharp.Mathematics.Numeric
}
long kernelOffset = _boarder.GetIndexOffset(k_r, k_c, in _size);
return kernelOffset * 16 + 4 * r + c;
return (kernelOffset * 16) + (4 * r) + c;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -195,8 +197,6 @@ namespace StgSharp.Mathematics.Numeric
// Caller guarantees: indices in range, layout is dense square.
int k_c = y / 4;
int k_r = x / 4;
int c = y % 4;
int r = x % 4;
return ref _buffer[_boarder.GetIndexOffset(k_c, k_r, in _size)];
}
@@ -30,6 +30,7 @@ using StgSharp.Mathematics.Numeric.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
@@ -71,34 +72,45 @@ namespace StgSharp.Mathematics.Numeric.Linear
t->Mat1 = (MatrixKernel*)leftPtr;
t->Mat2 = (MatrixKernel*)rightPtr;
t->Mat3 = (MatrixKernel*)ansPtr;
t->ElementType = MatrixElementType.F64;
t->ElementType = f32;
t->ComputeHandle = MatrixIntrinsicHandle.Add;
t->Scalar.Data<long>(0) = count;
SequentialOperation(t, count);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void SequentialOperation(
MatrixParallelTask* task,
long count
)
{
MatrixElementType elementType = task->ElementType;
MatrixParallelThread[] pool;
MkProc op = (MkProc)NumericalModule.GlobalContext.
mat_mk[elementType.IntrinsicNode].
Get(task->ComputeHandle);
if (count <= 256)
{
add(t);
// .buffer_add(leftPtr, rightPtr, ansPtr, count);
op(task);
return;
} else if (count <= 1024)
{
pool = MatrixParallel.LeadParallel(1);
add(t);
op(task);
MatrixParallel.ReturnParallelResources(ref pool);
return;
}
pool = MatrixParallel.LeadParallel((int)(count / 1024));
MatrixParallelWrap taskGroup = MatrixParallel.ScheduleTask<SequentialPredictor>(pool, t);
pool = MatrixParallel.LeadParallel(count / 1024);
MatrixParallelWrap taskGroup = MatrixParallel.ScheduleTask<SequentialPredictor>(pool, task);
taskGroup.LaunchParallel(SleepMode.DeepSleep);
taskGroup.Dispose();
return;
}
private struct SequentialPredictor : IMatrixPredictor<SequentialPredictor>
{
public static PredictPolicy Policy => PredictPolicy.Sequential;
public bool Predict(
Span<CacheLinePrediction> node,
out int count