working on matrix mul matrix

This commit is contained in:
Youmu Liu
2025-12-10 17:52:17 +08:00
parent 7bcb460a39
commit 631bc2b6e8
8 changed files with 151 additions and 221 deletions
@@ -1,4 +1,4 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixKernel" // file="MatrixKernel"
// Project: StgSharp // Project: StgSharp
@@ -32,9 +32,16 @@ using System.Runtime.InteropServices;
namespace StgSharp.HighPerformance namespace StgSharp.HighPerformance
{ {
public unsafe struct MatrixKernel
{
private byte Head;
}
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
[InlineArray(16)] [InlineArray(16)]
public unsafe struct MatrixKernel<T> where T: unmanaged, INumber<T> public unsafe struct MatrixKernel<T> where T : unmanaged, INumber<T>
{ {
private T c0r0; private T c0r0;
+67 -203
View File
@@ -1,4 +1,4 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixPanel" // file="MatrixPanel"
// Project: StgSharp // Project: StgSharp
@@ -25,6 +25,7 @@
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.Internal;
using System; using System;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@@ -32,267 +33,130 @@ using System.Runtime.InteropServices;
namespace StgSharp.HighPerformance namespace StgSharp.HighPerformance
{ {
#pragma warning disable CA1000 public unsafe struct MatrixPanel
public interface IMatrixPanel<T> : IDisposable where T : unmanaged,INumber<T>
{ {
ref T this[int column, int row] { get; } private byte Head;
static abstract int MaxElementCount { get; }
static abstract int ElementSideLength { get; }
static abstract nuint Size { get; }
void Dispose();
} }
/// <summary> // Single POD-style panel: ZMM-compatible size. Alignment handled by allocator, not by the struct.
/// Buffer for SSE targeted matrix panel, size of a panel is 4x4 elements, and no smaller than public unsafe struct MatrixPanel<T> where T : unmanaged, INumber<T>
/// 16 bytes.
/// </summary>
/// <typeparam name="T">
/// Type of matrix element.
/// </typeparam>
public unsafe struct MatrixPanelSSE<T> : IMatrixPanel<T> where T : unmanaged, INumber<T>
{ {
private T element; public const int AlignmentBytes = 64;
[Obsolete("Compute kernel should not be created from new(), use Create instead", true)] internal T Head; // first element of the panel buffer
public MatrixPanelSSE() { }
public ref T this[int column, int row] public ref T this[int column, int row]
{ {
get [MethodImpl(MethodImplOptions.AggressiveInlining)]
{ get => ref Unsafe.Add(ref Head, row * ElementSideLength + column);
int index = row * ElementSideLength + column;
return ref Unsafe.Add(ref element, index);
}
} }
public static int ElementSideLength public static int ElementSideLength
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get; get => Math.Max(8, AlignmentBytes / sizeof(T));
} = int.Max(4, 16 / sizeof(T)); }
public static int MaxElementCount public static int MaxElementCount
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get; get => ElementSideLength * ElementSideLength;
} = ElementSideLength * ElementSideLength; }
public static unsafe nuint Size { get; } = (nuint)(MaxElementCount * sizeof(T)); public static nuint Size
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ChunkCount() => (ElementSideLength + LanesPerVector() - 1) / LanesPerVector();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MatrixPanelSSE<T>* Create()
{ {
MatrixPanelSSE<T>* kernel = (MatrixPanelSSE<T>*)NativeMemory.AlignedAlloc(Size, 16);
return kernel;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Destroy(MatrixPanelSSE<T>* kernel) get => (nuint)(MaxElementCount * sizeof(T));
{
NativeMemory.AlignedFree(kernel);
} }
public void Dispose() public Span<T> Elements
{ {
fixed (MatrixPanelSSE<T>* thisPtr = &this) {
NativeMemory.AlignedFree(thisPtr);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref M128 GetVectorChunk(int column, int chunk) get
{ {
int lanes = LanesPerVector(); ref T first = ref Head;
int rowStart = chunk * lanes; unsafe
ref T colBase = ref this[column, 0]; {
return ref Unsafe.As<T, M128>(ref Unsafe.Add(ref colBase, rowStart)); void* p = Unsafe.AsPointer(ref first);
return new Span<T>(p, MaxElementCount);
}
}
} }
// SIMD access helpers (column-major) // Allocate aligned memory for one panel; caller must free via Destroy.
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe MatrixPanel<T>* Create()
public static int LanesPerVector() => 16 / sizeof(T);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetVectorChunk(int column, int chunk, in M128 value)
{ {
GetVectorChunk(column, chunk) = value; return (MatrixPanel<T>*)NativeMemory.AlignedAlloc(Size, AlignmentBytes);
} }
public static unsafe void Destroy(MatrixPanel<T>* panel)
{
NativeMemory.AlignedFree(panel);
}
// View an existing pointer; caller guarantees lifetime/alignment.
public static unsafe MatrixPanel<T>* FromPointer(void* ptr)
{
return (MatrixPanel<T>*)Align64(ptr);
} }
/// <summary> /// <summary>
/// Buffer for AVX targeted matrix panel, size of a panel is 4x4 elements, and no smaller than /// Number of 4x4 kernels that fit in a panel for the given ISA and element T.
/// 16 bytes.
/// </summary> /// </summary>
/// <typeparam name="T">
/// Type of matrix element.
/// </typeparam>
public unsafe struct MatrixPanelAVX<T> : IMatrixPanel<T> where T : unmanaged, INumber<T>
{
private T element;
[Obsolete("Compute kernel should not be created from new(), use Create instead", true)]
public MatrixPanelAVX() { }
public ref T this[int column, int row]
{
get
{
int index = row * ElementSideLength + column;
return ref Unsafe.Add(ref element, index);
}
}
public static int ElementSideLength
{
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get; internal static int KernelCapacity()
} = int.Max(8, 32 / sizeof(T));
public static unsafe int MaxElementCount
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] int perSide = KernelSideCount();
get; return perSide * perSide;
} = ElementSideLength * ElementSideLength;
public static unsafe nuint Size { get; } = (nuint)(MaxElementCount * sizeof(T));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ChunkCount() => (ElementSideLength + LanesPerVector() - 1) / LanesPerVector();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MatrixPanelAVX<T>* Create()
{
MatrixPanelAVX<T>* kernel = (MatrixPanelAVX<T>*)NativeMemory.AlignedAlloc(Size, 32);
return kernel;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Destroy(MatrixPanelAVX<T>* kernel)
{
NativeMemory.AlignedFree(kernel);
}
public void Dispose()
{
fixed (MatrixPanelAVX<T>* thisPtr = &this) {
NativeMemory.AlignedFree(thisPtr);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref M256 GetVectorChunk(int column, int chunk)
{
int lanes = LanesPerVector();
int rowStart = chunk * lanes;
ref T colBase = ref this[column, 0];
return ref Unsafe.As<T, M256>(ref Unsafe.Add(ref colBase, rowStart));
}
// SIMD access helpers for AVX (column-major)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int LanesPerVector() => 32 / sizeof(T);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetVectorChunk(int column, int chunk, in M256 value)
{
GetVectorChunk(column, chunk) = value;
}
} }
/// <summary> /// <summary>
/// Buffer for AVX512 targeted matrix panel, size of a panel is 4x4 elements, and no smaller /// How many 4x4 kernels fit along a single panel dimension for the given ISA.
/// than 16 bytes.
/// </summary> /// </summary>
/// <typeparam name="T">
/// Type of matrix element.
/// </typeparam>
public unsafe struct MatrixComputeKernelAVX512<T> : IMatrixPanel<T>
where T : unmanaged, INumber<T>
{
private T element;
[Obsolete("Compute kernel should not be created from new(), use Create instead", true)]
public MatrixComputeKernelAVX512() { }
public ref T this[int column, int row]
{
get
{
int index = row * ElementSideLength + column;
return ref Unsafe.Add(ref element, index);
}
}
public static int ElementSideLength
{
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get; internal static int KernelSideCount()
} = int.Max(8, 64 / sizeof(T));
public static unsafe int MaxElementCount
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] return NativeIntrinsic.IntrinsicLevel switch
get;
} = ElementSideLength * ElementSideLength;
public static unsafe nuint Size { get; } = (nuint)(MaxElementCount * sizeof(T));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ChunkCount() => (ElementSideLength + LanesPerVector() - 1) / LanesPerVector();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MatrixComputeKernelAVX512<T>* Create()
{ {
MatrixComputeKernelAVX512<T>* kernel = (MatrixComputeKernelAVX512<T>*)NativeMemory.AlignedAlloc(Size, 64); IntrinsicLevel.SSE => KernelSideCountSse(),
return kernel; IntrinsicLevel.AVX2 => KernelSideCountAvx2(),
IntrinsicLevel.AVX512 => KernelSideCountAvx512(),
_ => 0
};
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Destroy(MatrixComputeKernelAVX512<T>* kernel) private static unsafe nuint Align64(void* p) => ((nuint)p + (AlignmentBytes - 1)) & ~(nuint)(AlignmentBytes - 1);
{
NativeMemory.AlignedFree(kernel);
}
public void Dispose() [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int KernelSideCountAvx2()
{ {
fixed (MatrixComputeKernelAVX512<T>* thisPtr = &this) { const int minQ = 8;
NativeMemory.AlignedFree(thisPtr); int lanes = 32 / sizeof(T);
} int side = Math.Max(lanes, minQ);
return side / 4;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref M512 GetVectorChunk(int column, int chunk) private static int KernelSideCountAvx512()
{ {
int lanes = LanesPerVector(); const int minQ = 8;
int rowStart = chunk * lanes; int lanes = 64 / sizeof(T);
ref T colBase = ref this[column, 0]; int side = Math.Max(lanes, minQ);
return ref Unsafe.As<T, M512>(ref Unsafe.Add(ref colBase, rowStart)); return side / 4;
} }
// SIMD access helpers for AVX-512 (column-major)
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int LanesPerVector() => 64 / sizeof(T); private static int KernelSideCountSse()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetVectorChunk(int column, int chunk, in M512 value)
{ {
GetVectorChunk(column, chunk) = value; const int minQ = 4;
int lanes = 16 / sizeof(T);
int side = Math.Max(lanes, minQ);
return side / 4;
} }
#pragma warning restore CA1000
} }
} }
@@ -1,4 +1,4 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="IntrinsicContext" // file="IntrinsicContext"
// Project: StgSharp // Project: StgSharp
@@ -44,7 +44,7 @@ namespace StgSharp.Internal.Intrinsic
public delegate* unmanaged[Cdecl]<void*, void*, ScalarPacket*, long, void> f32_buffer_scalar_mul; //f32_buffer_s_m public delegate* unmanaged[Cdecl]<void*, void*, ScalarPacket*, long, void> f32_buffer_scalar_mul; //f32_buffer_s_m
public delegate* unmanaged[Cdecl]<void*, void*, void*, long, void> f32_buffer_sub; //f32_kernel_sub 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]<void*, void*, void> f32_buffer_transpose; //f32_kernel_transpose
public delegate* unmanaged[Cdecl]<void*, void*, int, int, int, int, void> f32_build_panel; //f32_build_panel public delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixPanel*, int, int, int, int, void> f32_build_panel; //f32_build_panel
public delegate* unmanaged[Cdecl]<Vector4*, Vector4*, void> f32_normalize_3; //f32_normalize_3 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*, 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 public delegate* unmanaged[Cdecl]<void*, void*, int, int, int, int, void> f32_store_panel; //f32_store_panel
@@ -59,7 +59,7 @@ namespace StgSharp.Internal
{ {
case 0: case 0:
// Dialogue.PostError(new PlatformNotSupportedException("No SIMD instructions supported on current hardware platform")); Dialogue.PostError(new PlatformNotSupportedException("No SIMD instructions supported on current hardware platform"));
Environment.Exit(-1); Environment.Exit(-1);
break; break;
default: default:
@@ -1,4 +1,4 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixCompute.Basic" // file="MatrixCompute.Basic"
// Project: StgSharp // Project: StgSharp
@@ -30,6 +30,7 @@ using StgSharp.HighPerformance;
using StgSharp.Internal; using StgSharp.Internal;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Security.AccessControl; using System.Security.AccessControl;
@@ -124,11 +125,35 @@ namespace StgSharp.Mathematics.Numeric
Matrix<float> right, Matrix<float> right,
Matrix<float> ans) Matrix<float> ans)
{ {
if (left.RowLength != right.ColumnLength || // For matrix multiplication (left: MxK) * (right: KxN) => ans: MxN
left.RowLength != ans.RowLength || // ColumnLength = number of columns (N), RowLength = number of rows (M)
right.ColumnLength != ans.ColumnLength) { if (left.ColumnLength != right.RowLength ||
ans.ColumnLength != right.ColumnLength ||
ans.RowLength != left.RowLength)
{
throw new IndexOutOfRangeException("Matrix dimensions must agree."); throw new IndexOutOfRangeException("Matrix dimensions must agree.");
} }
long count = (long)left.KernelColumnLength * left.KernelRowLength;
if (count <= 256)
{
MatrixPanel<float>* leftPanel = MatrixPanel<float>.Create();
MatrixPanel<float>* rightPanel = MatrixPanel<float>.Create();
MatrixPanel<float>* ansPanel = MatrixPanel<float>.Create();
int size = MatrixPanel<float>.KernelSideCount();
for (int i = 0; i < left.RowLength; i += size)
{
for (int j = 0; j < right.ColumnLength; j += size)
{
for (int k = 0; k < left.ColumnLength; k += size) {
NativeIntrinsic.Intrinsic
.f32_build_panel((MatrixKernel*)left.Buffer,
(MatrixPanel*)leftPanel,
left.ColumnLength, left.RowLength, i,
k);
}
}
}
}
throw new NotImplementedException(); throw new NotImplementedException();
} }
+43
View File
@@ -52,6 +52,49 @@ DECLARE_PNL_PROC_LEFT_RIGHT_ANS(float, sse, fma)
PANEL_COL_VEC((*ans), 3, 0) = c3; PANEL_COL_VEC((*ans), 3, 0) = c3;
} }
// ans = left * right + ans
// right is not transposed
DECLARE_PNL_PROC_LEFT_RIGHT_ANS(float, avx_fma, fma)
{
static const __m256i idx0 = { 1, 1, 1, 1, 1, 1, 1, 1 };
for (int col = 0; col < 8; ++col) {
register __m256 bcol = _mm256_load_ps((float *)&right->vec[col][0]);
register __m256 ccol = _mm256_load_ps((float *)&ans->vec[col][0]);
register __m256i idx = _mm256_setzero_si256();
register __m256i increament = _mm256_load_si256(&idx0);
register __m256 a0 = _mm256_load_ps((float *)&left->vec[0][0]);
register __m256 a1 = _mm256_load_ps((float *)&left->vec[1][0]);
register __m256 a2 = _mm256_load_ps((float *)&left->vec[2][0]);
register __m256 a3 = _mm256_load_ps((float *)&left->vec[3][0]);
ccol = _mm256_fmadd_ps(a0, _mm256_permutevar8x32_ps(bcol, idx), ccol);
idx = _mm256_add_epi32(idx, increament);
ccol = _mm256_fmadd_ps(a1, _mm256_permutevar8x32_ps(bcol, idx), ccol);
idx = _mm256_add_epi32(idx, increament);
ccol = _mm256_fmadd_ps(a2, _mm256_permutevar8x32_ps(bcol, idx), ccol);
idx = _mm256_add_epi32(idx, increament);
ccol = _mm256_fmadd_ps(a3, _mm256_permutevar8x32_ps(bcol, idx), ccol);
idx = _mm256_add_epi32(idx, increament);
a0 = _mm256_load_ps((float *)&left->vec[4][0]);
a1 = _mm256_load_ps((float *)&left->vec[5][0]);
a2 = _mm256_load_ps((float *)&left->vec[6][0]);
a3 = _mm256_load_ps((float *)&left->vec[7][0]);
ccol = _mm256_fmadd_ps(a0, _mm256_permutevar8x32_ps(bcol, idx), ccol);
idx = _mm256_add_epi32(idx, increament);
ccol = _mm256_fmadd_ps(a1, _mm256_permutevar8x32_ps(bcol, idx), ccol);
idx = _mm256_add_epi32(idx, increament);
ccol = _mm256_fmadd_ps(a2, _mm256_permutevar8x32_ps(bcol, idx), ccol);
idx = _mm256_add_epi32(idx, increament);
ccol = _mm256_fmadd_ps(a3, _mm256_permutevar8x32_ps(bcol, idx), ccol);
_mm256_store_ps((float *)&ans->vec[col][0], ccol);
}
}
// ans = left * right + ans // ans = left * right + ans
// right is not transposed // right is not transposed
DECLARE_PNL_PROC_LEFT_RIGHT_ANS(float, avx, fma) DECLARE_PNL_PROC_LEFT_RIGHT_ANS(float, avx, fma)
-9
View File
@@ -1,9 +0,0 @@
public const uint GL_ACTIVE_SUBROUTINES = 0x8DE5
public const uint GL_ACTIVE_SUBROUTINE_UNIFORMS = 0x8DE6
public const uint GL_MAX_SUBROUTINES = 0x8DE7
public const uint GL_MAX_SUBROUTINE_UNIFORM_LOCATIONS = 0x8DE8
public const uint GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS = 0x8E47
public const uint GL_ACTIVE_SUBROUTINE_MAX_LENGTH = 0x8E48
public const uint GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH = 0x8E49
public const uint GL_NUM_COMPATIBLE_SUBROUTINES = 0x8E4A
public const uint GL_COMPATIBLE_SUBROUTINES = 0x8E4B