working on matrix mul matrix
This commit is contained in:
@@ -34,82 +34,63 @@ using System.Runtime.InteropServices;
|
||||
namespace StgSharp.HighPerformance
|
||||
{
|
||||
public unsafe struct MatrixPanel
|
||||
{
|
||||
|
||||
private byte Head;
|
||||
|
||||
}
|
||||
|
||||
// Single POD-style panel: ZMM-compatible size. Alignment handled by allocator, not by the struct.
|
||||
public unsafe struct MatrixPanel<T> where T : unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
public const int AlignmentBytes = 64;
|
||||
|
||||
internal T Head; // first element of the panel buffer
|
||||
|
||||
public ref T this[int column, int row]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref Unsafe.Add(ref Head, row * ElementSideLength + column);
|
||||
}
|
||||
|
||||
public static int ElementSideLength
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => Math.Max(8, AlignmentBytes / sizeof(T));
|
||||
}
|
||||
|
||||
public static int MaxElementCount
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ElementSideLength * ElementSideLength;
|
||||
}
|
||||
|
||||
public static nuint Size
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => (nuint)(MaxElementCount * sizeof(T));
|
||||
}
|
||||
|
||||
public Span<T> Elements
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ref T first = ref Head;
|
||||
unsafe
|
||||
{
|
||||
void* p = Unsafe.AsPointer(ref first);
|
||||
return new Span<T>(p, MaxElementCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
private byte Head;
|
||||
|
||||
// Allocate aligned memory for one panel; caller must free via Destroy.
|
||||
public static unsafe MatrixPanel<T>* Create()
|
||||
public static unsafe MatrixPanel<T>* Create<T>() where T : unmanaged, INumber<T>
|
||||
{
|
||||
return (MatrixPanel<T>*)NativeMemory.AlignedAlloc(Size, AlignmentBytes);
|
||||
return (MatrixPanel<T>*)NativeMemory.AlignedAlloc(Size<T>(), AlignmentBytes);
|
||||
}
|
||||
|
||||
public static unsafe void Destroy(MatrixPanel<T>* panel)
|
||||
public static unsafe void Destroy<T>(MatrixPanel<T>* panel) where T : unmanaged, INumber<T>
|
||||
{
|
||||
NativeMemory.AlignedFree(panel);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe Span<T> Elements<T>(ref T head) where T : unmanaged, INumber<T>
|
||||
{
|
||||
void* p = Unsafe.AsPointer(ref head);
|
||||
return new Span<T>(p, MaxElementCount<T>());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int ElementSideLength<T>() where T : unmanaged, INumber<T>
|
||||
{
|
||||
return Math.Max(8, AlignmentBytes / sizeof(T));
|
||||
}
|
||||
|
||||
// View an existing pointer; caller guarantees lifetime/alignment.
|
||||
public static unsafe MatrixPanel<T>* FromPointer(void* ptr)
|
||||
public static unsafe MatrixPanel<T>* FromPointer<T>(void* ptr)
|
||||
where T : unmanaged, INumber<T>
|
||||
{
|
||||
return (MatrixPanel<T>*)Align64(ptr);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int MaxElementCount<T>() where T : unmanaged, INumber<T>
|
||||
{
|
||||
int side = ElementSideLength<T>();
|
||||
return side * side;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static nuint Size<T>() where T : unmanaged, INumber<T>
|
||||
{
|
||||
return (nuint)(MaxElementCount<T>() * sizeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Number of 4x4 kernels that fit in a panel for the given ISA and element T.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static int KernelCapacity()
|
||||
internal static int KernelCapacity<T>() where T : unmanaged, INumber<T>
|
||||
{
|
||||
int perSide = KernelSideCount();
|
||||
int perSide = KernelSideCount<T>();
|
||||
return perSide * perSide;
|
||||
}
|
||||
|
||||
@@ -117,13 +98,13 @@ namespace StgSharp.HighPerformance
|
||||
/// How many 4x4 kernels fit along a single panel dimension for the given ISA.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static int KernelSideCount()
|
||||
internal static int KernelSideCount<T>() where T : unmanaged, INumber<T>
|
||||
{
|
||||
return NativeIntrinsic.IntrinsicLevel switch
|
||||
{
|
||||
IntrinsicLevel.SSE => KernelSideCountSse(),
|
||||
IntrinsicLevel.AVX2 => KernelSideCountAvx2(),
|
||||
IntrinsicLevel.AVX512 => KernelSideCountAvx512(),
|
||||
IntrinsicLevel.SSE => KernelSideCountSse<T>(),
|
||||
IntrinsicLevel.AVX2 => KernelSideCountAvx2<T>(),
|
||||
IntrinsicLevel.AVX512 => KernelSideCountAvx512<T>(),
|
||||
_ => 0
|
||||
};
|
||||
}
|
||||
@@ -132,7 +113,7 @@ namespace StgSharp.HighPerformance
|
||||
private static unsafe nuint Align64(void* p) => ((nuint)p + (AlignmentBytes - 1)) & ~(nuint)(AlignmentBytes - 1);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int KernelSideCountAvx2()
|
||||
private static int KernelSideCountAvx2<T>() where T : unmanaged, INumber<T>
|
||||
{
|
||||
const int minQ = 8;
|
||||
int lanes = 32 / sizeof(T);
|
||||
@@ -141,7 +122,7 @@ namespace StgSharp.HighPerformance
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int KernelSideCountAvx512()
|
||||
private static int KernelSideCountAvx512<T>() where T : unmanaged, INumber<T>
|
||||
{
|
||||
const int minQ = 8;
|
||||
int lanes = 64 / sizeof(T);
|
||||
@@ -150,7 +131,7 @@ namespace StgSharp.HighPerformance
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int KernelSideCountSse()
|
||||
private static int KernelSideCountSse<T>() where T : unmanaged, INumber<T>
|
||||
{
|
||||
const int minQ = 4;
|
||||
int lanes = 16 / sizeof(T);
|
||||
@@ -159,4 +140,25 @@ namespace StgSharp.HighPerformance
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Single POD-style panel: ZMM-compatible size. Alignment handled by allocator, not by the struct.
|
||||
public unsafe struct MatrixPanel<T> where T : unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
internal T Head; // first element of the panel buffer
|
||||
|
||||
public ref T this[int column, int row]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref Unsafe.Add(ref Head, row * MatrixPanel.ElementSideLength<T>() + column);
|
||||
}
|
||||
|
||||
public Span<T> Elements
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => MatrixPanel.Elements(ref Head);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ namespace StgSharp.Internal.Intrinsic
|
||||
public delegate* unmanaged[Cdecl]<void*, void*, void*, long, void> f32_buffer_sub; //f32_kernel_sub
|
||||
public delegate* unmanaged[Cdecl]<void*, void*, void> f32_buffer_transpose; //f32_kernel_transpose
|
||||
public delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixPanel*, int, int, int, int, void> f32_build_panel; //f32_build_panel
|
||||
public delegate* unmanaged[Cdecl]<MatrixPanel*, void> f32_clear_panel;
|
||||
public delegate* unmanaged[Cdecl]<Vector4*, Vector4*, void> f32_normalize_3; //f32_normalize_3
|
||||
public delegate* unmanaged[Cdecl]<void*, void*, void*, void> f32_panel_fma; //f32_panel_fma
|
||||
public delegate* unmanaged[Cdecl]<void*, void*, int, int, int, int, void> f32_store_panel; //f32_store_panel
|
||||
|
||||
Reference in New Issue
Block a user