mat queue and mat predictor confuses. i need time to design and organize

This commit is contained in:
Youmu Liu
2026-05-06 18:07:31 +08:00
parent 1338bf2801
commit e636410ba6
5 changed files with 112 additions and 42 deletions
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="IntrinsicContext" // file="IntrinsicContext"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload Space // AuthorGroup: Nitload
// Copyright (c) Nitload Space. All rights reserved. // Copyright (c) Nitload. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -68,7 +68,9 @@ namespace StgSharp.Mathematics.Internal
[FieldOffset(0)] public readonly ushort Size; [FieldOffset(0)] public readonly ushort Size;
private MatrixElementType(int mask) private MatrixElementType(
int mask
)
{ {
Unsafe.SkipInit(out this); Unsafe.SkipInit(out this);
Unsafe.SkipInit(out Size); Unsafe.SkipInit(out Size);
@@ -76,6 +78,24 @@ namespace StgSharp.Mathematics.Internal
this.Mask = mask; this.Mask = mask;
} }
[Obsolete("Do not use this constructor. Use pre-defined ones from static fields.", true)]
public MatrixElementType()
{
throw new NotSupportedException("Do not use this constructor. Use pre-defined ones from static fields.");
}
public override readonly bool Equals(
object? obj
)
{
return (obj is MatrixElementType type) && (Mask == type.Mask);
}
public override readonly int GetHashCode()
{
return Mask;
}
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
@@ -105,14 +125,19 @@ namespace StgSharp.Mathematics.Internal
private nint handle; //f32_panel_mul private nint handle; //f32_panel_mul
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Call(MatrixIntrinsicHandle index, MatrixParallelTask* task) public readonly void Call(
MatrixIntrinsicHandle index,
MatrixParallelTask* task
)
{ {
nint ptr = this[(int)index]; nint ptr = this[(int)index];
((delegate* unmanaged[Cdecl]<MatrixParallelTask*, void>)ptr)(task); ((delegate* unmanaged[Cdecl]<MatrixParallelTask*, void>)ptr)(task);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly nint Get(MatrixIntrinsicHandle index) public readonly nint Get(
MatrixIntrinsicHandle index
)
{ {
return this[(int)index]; return this[(int)index];
} }
@@ -143,15 +168,20 @@ namespace StgSharp.Mathematics.Internal
MatrixKernel* left, MatrixKernel* left,
MatrixKernel* right, MatrixKernel* right,
MatrixKernel* ans, MatrixKernel* ans,
ulong scalar) ulong scalar
)
{ {
nint ptr = this[(int)index]; nint ptr = this[(int)index];
((delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, MatrixKernel*, ulong, void>)ptr)(left, right, ((delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, MatrixKernel*, ulong, void>)ptr)(left,
ans, scalar); right,
ans,
scalar);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly nint Get(MatrixIntrinsicHandle index) public readonly nint Get(
MatrixIntrinsicHandle index
)
{ {
return this[(int)index]; return this[(int)index];
} }
@@ -71,7 +71,7 @@ namespace StgSharp.Mathematics.Numeric
op(task); op(task);
return; return;
} }
MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask<BufferQueueLeftRightAns>(task); MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask<BufferQueueLeftRightAns_FullNuma>(task);
taskWrap.LaunchParallel(SleepMode.DeepSleep); taskWrap.LaunchParallel(SleepMode.DeepSleep);
taskWrap.Dispose(); taskWrap.Dispose();
} }
@@ -33,6 +33,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.JavaScript;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -42,12 +43,6 @@ namespace StgSharp.Mathematics.Numeric
internal unsafe interface IMatrixParallelQueue<TSelf> where TSelf : IMatrixParallelQueue<TSelf> internal unsafe interface IMatrixParallelQueue<TSelf> where TSelf : IMatrixParallelQueue<TSelf>
{ {
static abstract TSelf Create(
MatrixParallelTask* baseTask,
L4 buffer,
int numaCount
);
void Return( void Return(
MatrixParallelTask* task, MatrixParallelTask* task,
in L4CacheLine left, in L4CacheLine left,
@@ -64,32 +59,35 @@ namespace StgSharp.Mathematics.Numeric
} }
internal sealed unsafe class BufferQueueLeftRightAns : IMatrixParallelQueue<BufferQueueLeftRightAns> internal interface IBufferQueueLeftRightAns : IMatrixParallelQueue<IBufferQueueLeftRightAns> { }
internal sealed unsafe class BufferQueueLeftRightAns_FullNuma : IMatrixParallelQueue<IBufferQueueLeftRightAns>
{ {
private MatrixParallelTask* _baseTask; private MatrixParallelTask* _baseTask;
private bool _needNuma;
private L4 _buffer; private L4 _buffer;
private readonly nuint _byteSize;
private readonly nuint _count;
private volatile nuint _cursor; private volatile nuint _cursor;
private readonly nuint Count;
private BufferQueueLeftRightAns() { } private volatile nuint _mat1;
private volatile nuint _mat2;
private volatile nuint _mat3;
private BufferQueueLeftRightAns( private BufferQueueLeftRightAns_FullNuma() { }
MatrixParallelTask* baseTask
internal BufferQueueLeftRightAns_FullNuma(
MatrixParallelTask* baseTask
) )
{ {
_baseTask = baseTask; _baseTask = baseTask;
Count = ((nuint)_baseTask->CommonX) * ((nuint)_baseTask->CommonY); _count = ((nuint)_baseTask->CommonX) * ((nuint)_baseTask->CommonY);
} _byteSize = ((nuint)_baseTask->Mat1) + (((nuint)_baseTask->CommonX) * ((nuint)_baseTask->CommonY) * _baseTask->ElementType.Size);
public static unsafe BufferQueueLeftRightAns Create(
MatrixParallelTask* baseTask, _mat1 = (nuint)_baseTask->Mat1;
L4 buffer, _mat2 = (nuint)_baseTask->Mat2;
int numaCount _mat3 = (nuint)_baseTask->Mat3;
)
{
return new BufferQueueLeftRightAns(baseTask);
} }
public unsafe void Return( public unsafe void Return(
@@ -121,11 +119,11 @@ namespace StgSharp.Mathematics.Numeric
} }
private class Predictor( private class Predictor(
BufferQueueLeftRightAns host BufferQueueLeftRightAns_FullNuma host
) : IL4Predict ) : IL4Predict
{ {
private readonly BufferQueueLeftRightAns _host; private readonly BufferQueueLeftRightAns_FullNuma _host;
public PredictResult Predict( public PredictResult Predict(
Span<CacheLineDescription> node, Span<CacheLineDescription> node,
@@ -136,16 +134,16 @@ namespace StgSharp.Mathematics.Numeric
nuint mat1 = (nuint)_host._baseTask->Mat1; nuint mat1 = (nuint)_host._baseTask->Mat1;
nuint mat2 = (nuint)_host._baseTask->Mat2; nuint mat2 = (nuint)_host._baseTask->Mat2;
nuint mat3 = (nuint)_host._baseTask->Mat3; nuint mat3 = (nuint)_host._baseTask->Mat3;
nuint _cur = begin * _host._baseTask->ElementType.Size; nuint _cur = 16 * begin * _host._baseTask->ElementType.Size;
nuint mat1_cur = ((nuint)_host._baseTask->Mat1) + _cur; nuint mat1_cur = ((nuint)_host._baseTask->Mat1) + _cur;
nuint mat2_cur = ((nuint)_host._baseTask->Mat2) + _cur; nuint mat2_cur = ((nuint)_host._baseTask->Mat2) + _cur;
nuint mat3_cur = ((nuint)_host._baseTask->Mat3) + _cur; nuint mat3_cur = ((nuint)_host._baseTask->Mat3) + _cur;
nuint end = ((nuint)_host._baseTask->Mat1) + (((nuint)_host._baseTask->CommonX) * ((nuint)_host._baseTask->CommonY) * _host._baseTask->ElementType.Size); nuint end = _host._byteSize;
nuint mat1_end = ((nuint)_host._baseTask->Mat1) + end; nuint mat1_end = ((nuint)_host._baseTask->Mat1) + end;
nuint mat2_end = ((nuint)_host._baseTask->Mat2) + end; nuint mat2_end = ((nuint)_host._baseTask->Mat2) + end;
nuint mat3_end = ((nuint)_host._baseTask->Mat3) + end; nuint mat3_end = ((nuint)_host._baseTask->Mat3) + end;
nuint size = 1024 * 16 * _host._baseTask->ElementType.Size; nuint size = 1024 * 16 * ((nuint)_host._baseTask->ElementType.Size);
for (int i = 0; i < node.Length; i += 3) for (int i = 0; i < node.Length; i += 3)
{ {
int cycle = i / 3; int cycle = i / 3;
@@ -52,20 +52,20 @@ namespace StgSharp.Mathematics.Numeric.Runtime
[FieldOffset(8)] public MatrixKernel* Mat2; [FieldOffset(8)] public MatrixKernel* Mat2;
[FieldOffset(16)] public MatrixKernel* Mat3; [FieldOffset(16)] public MatrixKernel* Mat3;
/// <summary> /// <summary>
/// Being offset of first kernel in a L4 cache line.
/// </summary>
[FieldOffset(24)] public nuint CacheIndex;
/// <summary>
/// Count of kernels to be calculated in a L4 cache line. /// Count of kernels to be calculated in a L4 cache line.
/// </summary> /// </summary>
[FieldOffset(28)] public int CacheLength; [FieldOffset(28)] public int CacheLength;
[FieldOffset(32)] public int CommonK; [FieldOffset(32)] public int CommonK;
[FieldOffset(36)] public int CommonX; [FieldOffset(36)] public int CommonX;
[FieldOffset(40)] public int CommonY; [FieldOffset(40)] public int CommonY;
[FieldOffset(44)] public int SpanLength; [FieldOffset(44)] public int VectorLength;
[FieldOffset(48)] public MatrixElementType ElementType; [FieldOffset(48)] public MatrixElementType ElementType;
[FieldOffset(52)] public MatrixIntrinsicHandle ComputeHandle; [FieldOffset(52)] public MatrixIntrinsicHandle ComputeHandle;
[FieldOffset(56)] public nint Span; /// <summary>
/// Beginning offset of first kernel in a L4 cache line.
/// </summary>
[FieldOffset(24)] public nuint Offset;
[FieldOffset(56)] public nuint Vector;
[FieldOffset(64)] public ScalarPacket Scalar; [FieldOffset(64)] public ScalarPacket Scalar;
public MatrixParallelTask() public MatrixParallelTask()
@@ -0,0 +1,42 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="QueueFactory"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
internal interface IMatrixQueueFactory
{
IMatrixParallelQueue<IMatrixParallelQueue> CreateSequential();
}
}