Daily update on 20260111-005714 by Nitload Youmu. Additional Message : Use optimized tile operation to accelerate matrix mul, still has problem
Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="M128"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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
|
||||
@@ -58,7 +58,7 @@ namespace StgSharp.HighPerformance
|
||||
Vec = v;
|
||||
}
|
||||
|
||||
public ref T AsRef<T>() where T : unmanaged, INumber<T>
|
||||
public ref T AsRef<T>() where T: unmanaged, INumber<T>
|
||||
{
|
||||
return ref Unsafe.As<byte, T>(ref Buffer[0]);
|
||||
}
|
||||
@@ -69,7 +69,7 @@ namespace StgSharp.HighPerformance
|
||||
return Vec.AsVector4();
|
||||
}
|
||||
|
||||
public void BroadCastFrom<T>(T value) where T : unmanaged, INumber<T>
|
||||
public void BroadCastFrom<T>(T value) where T: unmanaged, INumber<T>
|
||||
{
|
||||
Vec = Vector128.Create<T>(value).AsSingle();
|
||||
}
|
||||
@@ -85,7 +85,7 @@ namespace StgSharp.HighPerformance
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ref T Member<T>(int index) where T : unmanaged, INumber<T>
|
||||
public ref T Member<T>(int index) where T: unmanaged, INumber<T>
|
||||
{
|
||||
return ref Unsafe.Add(ref AsRef<T>(), index);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixPanel"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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
|
||||
@@ -41,41 +41,37 @@ namespace StgSharp.HighPerformance
|
||||
private byte Head;
|
||||
|
||||
// Allocate aligned memory for one panel; caller must free via Destroy.
|
||||
public static unsafe MatrixPanel<T>* Create<T>() where T : unmanaged, INumber<T>
|
||||
public static unsafe MatrixPanel<T>* Create<T>() where T: unmanaged, INumber<T>
|
||||
{
|
||||
return (MatrixPanel<T>*)NativeMemory.AlignedAlloc(Size<T>(), AlignmentBytes);
|
||||
}
|
||||
|
||||
public static unsafe void Destroy<T>(
|
||||
MatrixPanel<T>* panel
|
||||
) where T : unmanaged, INumber<T>
|
||||
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>
|
||||
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>
|
||||
public static int ElementSideLength<T>() where T: unmanaged, INumber<T>
|
||||
{
|
||||
return PanelSideLength<T>();
|
||||
}
|
||||
|
||||
public static int MaxElementCount<T>() where T : unmanaged, INumber<T>
|
||||
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>
|
||||
public static nuint Size<T>() where T: unmanaged, INumber<T>
|
||||
{
|
||||
return (nuint)(MaxElementCount<T>() * sizeof(T));
|
||||
}
|
||||
@@ -84,7 +80,7 @@ namespace StgSharp.HighPerformance
|
||||
/// Number of 4x4 kernels that fit in a panel for the given ISA and element T.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static int KernelCapacity<T>() where T : unmanaged, INumber<T>
|
||||
internal static int KernelCapacity<T>() where T: unmanaged, INumber<T>
|
||||
{
|
||||
int perSide = KernelSideCount<T>();
|
||||
return perSide * perSide;
|
||||
@@ -94,7 +90,7 @@ 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<T>() where T : unmanaged, INumber<T>
|
||||
internal static int KernelSideCount<T>() where T: unmanaged, INumber<T>
|
||||
{
|
||||
SIMDID id = NativeIntrinsic.IntrinsicMask;
|
||||
switch (id.MaskByte[0] & 0b_00001111)
|
||||
@@ -113,32 +109,31 @@ namespace StgSharp.HighPerformance
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static unsafe nuint Align64(
|
||||
void* p
|
||||
) => ((nuint)p + (AlignmentBytes - 1)) & ~(nuint)(AlignmentBytes - 1);
|
||||
private static unsafe nuint Align64(void* p)
|
||||
{
|
||||
return ((nuint)p + (AlignmentBytes - 1)) & ~(nuint)(AlignmentBytes - 1);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int KernelSideCountAvx2<T>() where T : unmanaged, INumber<T>
|
||||
private static int KernelSideCountAvx2<T>() where T: unmanaged, INumber<T>
|
||||
{
|
||||
return PanelSideFromBits<T>(256) / 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int KernelSideCountAvx512<T>() where T : unmanaged, INumber<T>
|
||||
private static int KernelSideCountAvx512<T>() where T: unmanaged, INumber<T>
|
||||
{
|
||||
return PanelSideFromBits<T>(512) / 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int KernelSideCountSse<T>() where T : unmanaged, INumber<T>
|
||||
private static int KernelSideCountSse<T>() where T: unmanaged, INumber<T>
|
||||
{
|
||||
return PanelSideFromBits<T>(128) / 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int PanelSideFromBits<T>(
|
||||
int vectorBits
|
||||
) where T : unmanaged, INumber<T>
|
||||
private static int PanelSideFromBits<T>(int vectorBits) where T: unmanaged, INumber<T>
|
||||
{
|
||||
int elemBits = sizeof(T) * 8;
|
||||
|
||||
@@ -150,7 +145,7 @@ namespace StgSharp.HighPerformance
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int PanelSideLength<T>() where T : unmanaged, INumber<T>
|
||||
private static int PanelSideLength<T>() where T: unmanaged, INumber<T>
|
||||
{
|
||||
SIMDID id = NativeIntrinsic.IntrinsicMask;
|
||||
switch (id.MaskByte[0] & 0b_00001111)
|
||||
@@ -171,15 +166,12 @@ 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>
|
||||
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
|
||||
]
|
||||
public ref T this[int column, int row]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref Unsafe.Add(ref Head, row * MatrixPanel.ElementSideLength<T>() + column);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="IntrinsicContext"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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
|
||||
@@ -46,7 +46,7 @@ namespace StgSharp.Internal.Intrinsic
|
||||
public delegate* unmanaged[Cdecl]<MatrixPanel*, MatrixKernel*, int, int, int, int, void> build_panel; //f32_build_panel
|
||||
public delegate* unmanaged[Cdecl]<MatrixPanel*, void> clear_panel; //f32_clear_panel
|
||||
public delegate* unmanaged[Cdecl]<void*, void*, void*, void> kernel_fma; //f32_panel_fma
|
||||
public delegate* unmanaged[Cdecl]<void*, void*, void*, M128*, void> kernel_tile_fma; //f32_panel_fma
|
||||
public delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, MatrixKernel*, M128*, void> kernel_tile_fma; //f32_panel_fma
|
||||
public delegate* unmanaged[Cdecl]<void*, ulong, ulong, ulong, void> pivot; //f32_panel_mul
|
||||
public delegate* unmanaged[Cdecl]<void*, void*, int, int, int, int, void> store_panel; //f32_store_panel
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="SIMDID"
|
||||
// Project: StgSharp
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute.Float"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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
|
||||
@@ -42,11 +42,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
public static partial class MatrixCompute
|
||||
{
|
||||
|
||||
public static unsafe void Add(
|
||||
Matrix<float> left,
|
||||
Matrix<float> right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
public static unsafe void Add(Matrix<float> left, Matrix<float> right, Matrix<float> ans)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
@@ -92,10 +88,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Fill(
|
||||
Matrix<float> ans,
|
||||
float value
|
||||
)
|
||||
public static unsafe void Fill(Matrix<float> ans, float value)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
long count = (long)ans.KernelColumnLength * ans.KernelRowLength;
|
||||
@@ -126,11 +119,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
taskGroup.Dispose();
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<float> left,
|
||||
Matrix<float> right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
public static unsafe void Mul(Matrix<float> left, Matrix<float> right, Matrix<float> ans)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
|
||||
@@ -155,28 +144,32 @@ namespace StgSharp.Mathematics.Numeric
|
||||
if (count > 256) {
|
||||
pool = MatrixParallel.LeadParallel(1);
|
||||
}
|
||||
M128* enumeration = stackalloc M128[4];
|
||||
for (int i = 0; i < ans.KernelRowLength; i++)
|
||||
{
|
||||
for (int j = 0; j < ans.KernelColumnLength; j++)
|
||||
{
|
||||
MatrixKernel<float>* ansPanel = GetKernelAddressUnsafe(ans.Buffer,
|
||||
ans.KernelColumnLength,
|
||||
i, j);
|
||||
enumeration->Member<int>(0) = i;
|
||||
enumeration->Member<int>(1) = j;
|
||||
enumeration->Member<int>(2) = ans.KernelRowLength;
|
||||
enumeration->Member<int>(3) = left.KernelColumnLength;
|
||||
|
||||
// NativeIntrinsic.Context.mat[f32].clear_panel((MatrixPanel*)ansPanel);
|
||||
for (int k = 0; k < left.KernelColumnLength; k++)
|
||||
{
|
||||
NativeIntrinsic.Context
|
||||
.mat[f32].kernel_tile_fma((MatrixKernel*)left.Buffer,
|
||||
(MatrixKernel*)right.Buffer,
|
||||
(MatrixKernel*)ans.Buffer, enumeration);
|
||||
/*
|
||||
for (int k = 0; k < left.KernelColumnLength; k++) {
|
||||
NativeIntrinsic.Context
|
||||
.mat[f32].kernel_fma((MatrixPanel*)GetKernelAddressUnsafe(left.Buffer,
|
||||
left.KernelColumnLength,
|
||||
i,
|
||||
k),
|
||||
(MatrixPanel*)GetKernelAddressUnsafe(right.Buffer,
|
||||
right.KernelColumnLength,
|
||||
k,
|
||||
j),
|
||||
(MatrixPanel*)ansPanel);
|
||||
.mat[f32].kernel_fma(GetKernelAddressUnsafe(left.Buffer,
|
||||
left.KernelColumnLength, i,
|
||||
k),
|
||||
GetKernelAddressUnsafe(right.Buffer,
|
||||
right.KernelColumnLength, k, j),
|
||||
GetKernelAddressUnsafe(ans.Buffer,
|
||||
ans.KernelColumnLength, i, j));
|
||||
}
|
||||
/**/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,18 +196,13 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<float> left,
|
||||
float right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
public static unsafe void Mul(Matrix<float> left, float right, Matrix<float> ans)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
if (left.ColumnLength != ans.ColumnLength || left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
if (left.Layout != MatrixLayout.DenseRectangle || ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
@@ -255,11 +243,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Sub(
|
||||
Matrix<float> left,
|
||||
Matrix<float> right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
public static unsafe void Sub(Matrix<float> left, Matrix<float> right, Matrix<float> ans)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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
|
||||
@@ -40,9 +40,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
public partial class MatrixCompute
|
||||
{
|
||||
|
||||
internal static unsafe void ComputeFmaPanelF32(
|
||||
MatrixParallelTaskPackage* p
|
||||
)
|
||||
internal static unsafe void ComputeFmaPanelF32(MatrixParallelTaskPackage* p)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
MatrixKernel* leftBuffer = (MatrixKernel*)p->Left;
|
||||
@@ -64,19 +62,14 @@ namespace StgSharp.Mathematics.Numeric
|
||||
for (int k = 0; k < commonK; k++) {
|
||||
NativeIntrinsic.Context
|
||||
.mat[f32].kernel_fma(
|
||||
(MatrixPanel*)GetKernelAddressUnsafe((MatrixKernel<float>*)leftBuffer,
|
||||
ansHeight, k, j),
|
||||
(MatrixPanel*)GetKernelAddressUnsafe((MatrixKernel<float>*)rightBuffer,
|
||||
commonK, j, k),
|
||||
(MatrixPanel*)GetKernelAddressUnsafe((MatrixKernel<float>*)ansBuffer,
|
||||
ansHeight, i, j));
|
||||
GetKernelAddressUnsafe((MatrixKernel<float>*)leftBuffer, ansHeight, k, j),
|
||||
GetKernelAddressUnsafe((MatrixKernel<float>*)rightBuffer, commonK, j, k),
|
||||
GetKernelAddressUnsafe((MatrixKernel<float>*)ansBuffer, ansHeight, i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static unsafe void ComputeFmaPanelF64(
|
||||
MatrixParallelTaskPackage* p
|
||||
)
|
||||
internal static unsafe void ComputeFmaPanelF64(MatrixParallelTaskPackage* p)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
|
||||
@@ -90,33 +83,39 @@ namespace StgSharp.Mathematics.Numeric
|
||||
int ansHeight = p->AnsSecOffset;
|
||||
int commonK = p->LeftPrimOffset;
|
||||
|
||||
M128* enumeration = stackalloc M128[1];
|
||||
|
||||
// in this occasion size must be 1
|
||||
for (long c = offsetRef; c < offsetRef + lengthRef; c++)
|
||||
{
|
||||
int i = (int)(c / ansWidth);
|
||||
int j = (int)(c % ansWidth);
|
||||
enumeration->Member<int>(0) = i;
|
||||
enumeration->Member<int>(1) = j;
|
||||
enumeration->Member<int>(2) = commonK;
|
||||
enumeration->Member<int>(3) = ansHeight;
|
||||
NativeIntrinsic.Context.mat[f64].kernel_tile_fma(leftBuffer, rightBuffer, ansBuffer, enumeration);
|
||||
/*
|
||||
for (int k = 0; k < commonK; k += 1) {
|
||||
NativeIntrinsic.Context
|
||||
.mat[f64].kernel_fma(
|
||||
(MatrixPanel*)GetKernelAddressUnsafe((MatrixKernel<double>*)leftBuffer,
|
||||
ansHeight, k, j),
|
||||
(MatrixPanel*)GetKernelAddressUnsafe((MatrixKernel<double>*)rightBuffer,
|
||||
commonK, j, k),
|
||||
(MatrixPanel*)GetKernelAddressUnsafe((MatrixKernel<double>*)ansBuffer,
|
||||
ansHeight, i, j));
|
||||
GetKernelAddressUnsafe((MatrixKernel<double>*)leftBuffer, ansHeight, k, j),
|
||||
GetKernelAddressUnsafe((MatrixKernel<double>*)rightBuffer, commonK, j, k),
|
||||
GetKernelAddressUnsafe((MatrixKernel<double>*)ansBuffer, ansHeight, i, j));
|
||||
}
|
||||
/**/
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static unsafe MatrixKernel<T>* GetKernelAddressUnsafe<T>(
|
||||
MatrixKernel<T>* buffer,
|
||||
int colLength,
|
||||
int x,
|
||||
int y
|
||||
) where T : unmanaged, INumber<T>
|
||||
internal static unsafe MatrixKernel* GetKernelAddressUnsafe<T>(
|
||||
MatrixKernel<T>* buffer,
|
||||
int colLength,
|
||||
int x,
|
||||
int y)
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
return buffer + (colLength * x + y);
|
||||
return (MatrixKernel*)(buffer + (colLength * x + y));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute.double"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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
|
||||
@@ -38,11 +38,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
public partial class MatrixCompute
|
||||
{
|
||||
|
||||
public static unsafe void Add(
|
||||
Matrix<double> left,
|
||||
Matrix<double> right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
public static unsafe void Add(Matrix<double> left, Matrix<double> right, Matrix<double> ans)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
@@ -88,10 +84,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Fill(
|
||||
Matrix<double> ans,
|
||||
double value
|
||||
)
|
||||
public static unsafe void Fill(Matrix<double> ans, double value)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
long count = (long)ans.KernelColumnLength * ans.KernelRowLength;
|
||||
@@ -122,11 +115,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
taskGroup.Dispose();
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<double> left,
|
||||
Matrix<double> right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
public static unsafe void Mul(Matrix<double> left, Matrix<double> right, Matrix<double> ans)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
|
||||
@@ -139,25 +128,42 @@ namespace StgSharp.Mathematics.Numeric
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
int size = MatrixPanel.KernelSideCount<double>();
|
||||
|
||||
// int size = MatrixPanel.KernelSideCount<double>();
|
||||
if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = null;
|
||||
if (count > 256) {
|
||||
pool = MatrixParallel.LeadParallel(1);
|
||||
}
|
||||
for (int i = 0; i < ans.KernelRowLength; i += size)
|
||||
M128* enumeration = stackalloc M128[1];
|
||||
for (int i = 0; i < ans.KernelRowLength; i++)
|
||||
{
|
||||
for (int j = 0; j < ans.KernelColumnLength; j += size)
|
||||
for (int j = 0; j < ans.KernelColumnLength; j += 1)
|
||||
{
|
||||
enumeration->Member<int>(0) = i;
|
||||
enumeration->Member<int>(1) = j;
|
||||
enumeration->Member<int>(2) = ans.KernelRowLength;
|
||||
enumeration->Member<int>(3) = left.KernelColumnLength;
|
||||
|
||||
NativeIntrinsic.Context
|
||||
.mat[f64].kernel_tile_fma((MatrixKernel*)left.Buffer,
|
||||
(MatrixKernel*)right.Buffer,
|
||||
(MatrixKernel*)ans.Buffer, enumeration);
|
||||
/*
|
||||
// Console.WriteLine($"{i}{j}");
|
||||
for (int k = 0; k < left.KernelColumnLength; k += size)
|
||||
for (int k = 0; k < left.KernelColumnLength; k++)
|
||||
{
|
||||
NativeIntrinsic.Context
|
||||
.mat[f64].kernel_fma((MatrixPanel*)left.GetKernelAddressUnsafe(i, k),
|
||||
(MatrixPanel*)right.GetKernelAddressUnsafe(k, j),
|
||||
(MatrixPanel*)ans.GetKernelAddressUnsafe(i, j));
|
||||
.mat[f64].kernel_fma(GetKernelAddressUnsafe(left.Buffer,
|
||||
left.KernelColumnLength, i,
|
||||
k),
|
||||
GetKernelAddressUnsafe(right.Buffer,
|
||||
right.KernelColumnLength, k, j),
|
||||
GetKernelAddressUnsafe(ans.Buffer,
|
||||
ans.KernelColumnLength, i, j));
|
||||
}
|
||||
/**/
|
||||
}
|
||||
}
|
||||
if (count > 256) {
|
||||
@@ -172,8 +178,8 @@ namespace StgSharp.Mathematics.Numeric
|
||||
package->AnsPrimOffset = ans.KernelRowLength;
|
||||
package->AnsSecOffset = ans.KernelColumnLength;
|
||||
package->LeftPrimOffset = left.KernelColumnLength;
|
||||
package->PrimCount = ans.KernelColumnLength / size;
|
||||
package->SecCount = ans.KernelRowLength / size;
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ElementSize = (int)MatrixElementType.F64;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationSpecial.SEQ_FMA);
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
@@ -183,18 +189,13 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<double> left,
|
||||
double right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
public static unsafe void Mul(Matrix<double> left, double right, Matrix<double> ans)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
if (left.ColumnLength != ans.ColumnLength || left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
if (left.Layout != MatrixLayout.DenseRectangle || ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
@@ -235,11 +236,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Sub(
|
||||
Matrix<double> left,
|
||||
Matrix<double> right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
public static unsafe void Sub(Matrix<double> left, Matrix<double> right, Matrix<double> ans)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "sn_target.h"
|
||||
#include "sn_target.h"
|
||||
|
||||
#if SN_IS_ARCH(SN_ARCH_X86_64)
|
||||
|
||||
@@ -293,7 +293,7 @@ DECLARE_KERTILE_PROC_LEFT_RIGHT_ANS(float, 512, , fma)
|
||||
a_hi = _mm_load_pd(left_cur + (3 * 2 + 1)); \
|
||||
b = _mm_shuffle_pd(b_hi, b_hi, 0x3); \
|
||||
c##i##_lo = _mm_add_pd(c##i##_lo, _mm_mul_pd(a_lo, b)); \
|
||||
c##i##_hi = _mm_add_pd(c##i##_hi, _mm_mul_pd(a_hi, b));
|
||||
c##i##_hi = _mm_add_pd(c##i##_hi, _mm_mul_pd(a_hi, b));\
|
||||
|
||||
DECLARE_KERTILE_PROC_LEFT_RIGHT_ANS(double, sse, , fma)
|
||||
{
|
||||
@@ -316,10 +316,12 @@ DECLARE_KERTILE_PROC_LEFT_RIGHT_ANS(double, sse, , fma)
|
||||
const *left_cur = left + GET_INDEX(ansHeight, k, j),
|
||||
const *right_cur = right + GET_INDEX(commonK, j, k); // GetKernelAddressUnsafe
|
||||
|
||||
DOUBLE_KERNEL_FMA_CYCLE(0)
|
||||
DOUBLE_KERNEL_FMA_CYCLE(1)
|
||||
DOUBLE_KERNEL_FMA_CYCLE(2)
|
||||
DOUBLE_KERNEL_FMA_CYCLE(3)
|
||||
{DOUBLE_KERNEL_FMA_CYCLE(0)}
|
||||
{DOUBLE_KERNEL_FMA_CYCLE(1)}
|
||||
{DOUBLE_KERNEL_FMA_CYCLE(2)}
|
||||
{
|
||||
DOUBLE_KERNEL_FMA_CYCLE(3)
|
||||
}
|
||||
}
|
||||
|
||||
_mm_store_pd(ans->f64_x + (0 * 2 + 0), c0_lo);
|
||||
@@ -519,8 +521,8 @@ DECLARE_KERTILE_PROC_LEFT_RIGHT_ANS(double, 512, , fma)
|
||||
c23 = _mm512_fmadd_pd(a01, b23_perm, c23);
|
||||
|
||||
//group 1
|
||||
register __m512d a12 = _mm512_permute2f128_pd(a01, a23, 0x21);
|
||||
register __m512d a30 = _mm512_permute2f128_pd(a01, a23, 0x30);
|
||||
register __m512d a12 = _mm512_shuffle_f64x2(a01, a23, 0x21);
|
||||
register __m512d a30 = _mm512_shuffle_f64x2(a01, a23, 0x30);
|
||||
b01_perm = _mm512_shuffle_pd(b01, b01, _FMA_SELECT(1));
|
||||
b23_perm = _mm512_shuffle_pd(b23, b23, _FMA_SELECT(1));
|
||||
c01 = _mm512_fmadd_pd(a12, b01_perm, c01);
|
||||
@@ -534,50 +536,6 @@ DECLARE_KERTILE_PROC_LEFT_RIGHT_ANS(double, 512, , fma)
|
||||
}
|
||||
_mm512_store_pd((double *)&ans_cur->f64_z[0], c01);
|
||||
_mm512_store_pd((double *)&ans_cur->f64_z[1], c23);
|
||||
/*
|
||||
static const __m512i b01_shift = { .m512i_i64 = { 0, 1, 2, 3, 5, 6, 7, 4 } };
|
||||
static const __m512i b23_shift = { .m512i_i64 = { 2, 3, 0, 1, 7, 4, 5, 6 } };
|
||||
|
||||
register __m512d a01 = _mm512_load_pd((double *)&left->f64_z[0]);
|
||||
register __m512d a23 = _mm512_load_pd((double *)&left->f64_z[1]);
|
||||
|
||||
register __m512d b01 = _mm512_load_pd((double *)&right->f64_z[0]);
|
||||
register __m512d b23 = _mm512_load_pd((double *)&right->f64_z[1]);
|
||||
b01 = _mm512_permutexvar_pd(b01_shift, b01);
|
||||
b23 = _mm512_permutexvar_pd(b23_shift, b23);
|
||||
|
||||
register __m512d c01 = _mm512_load_pd((float *)&ans->f64_z[0]);
|
||||
register __m512d c23 = _mm512_load_pd((float *)&ans->f64_z[1]);
|
||||
|
||||
//group 0
|
||||
register __m512d b01_perm = _mm512_shuffle_pd(b01, b01, _FMA_SELECT(0));
|
||||
register __m512d b23_perm = _mm512_shuffle_pd(b23, b23, _FMA_SELECT(0));
|
||||
c01 = _mm512_fmadd_pd(a01, b01_perm, c01);
|
||||
c23 = _mm512_fmadd_pd(a23, b23_perm, c23);
|
||||
|
||||
//group 2
|
||||
b01_perm = _mm512_shuffle_pd(b01, b01, _FMA_SELECT(2));
|
||||
b23_perm = _mm512_shuffle_pd(b23, b23, _FMA_SELECT(2));
|
||||
c01 = _mm512_fmadd_pd(a23, b01_perm, c01);
|
||||
c23 = _mm512_fmadd_pd(a01, b23_perm, c23);
|
||||
|
||||
//group 1
|
||||
register __m512d a12 = _mm512_shuffle_f64x2(a01, a23, _MM_SHUFFLE(1, 0, 3, 2));
|
||||
register __m512d a30 = _mm512_shuffle_f64x2(a23, a01, _MM_SHUFFLE(1, 0, 3, 2));
|
||||
b01_perm = _mm512_shuffle_pd(b01, b01, _FMA_SELECT(1));
|
||||
b23_perm = _mm512_shuffle_pd(b23, b23, _FMA_SELECT(1));
|
||||
c01 = _mm512_fmadd_pd(a12, b01_perm, c01);
|
||||
c23 = _mm512_fmadd_pd(a30, b23_perm, c23);
|
||||
|
||||
//group 3
|
||||
b01_perm = _mm512_shuffle_pd(b01, b01, _FMA_SELECT(3));
|
||||
b23_perm = _mm512_shuffle_pd(b23, b23, _FMA_SELECT(3));
|
||||
c01 = _mm512_fmadd_pd(a30, b01_perm, c01);
|
||||
c23 = _mm512_fmadd_pd(a12, b23_perm, c23);
|
||||
|
||||
ans->f64_z[0] = c01;
|
||||
ans->f64_z[1] = c23;
|
||||
/**/
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Program"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. 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
|
||||
@@ -39,16 +39,11 @@ namespace StgSharp
|
||||
|
||||
private static string testExpPath = @"D:\Programmer\stepcode\data\ap203\ap203.exp";
|
||||
|
||||
internal static unsafe void Main(
|
||||
string[] args
|
||||
)
|
||||
internal static unsafe void Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
World.Initialize()
|
||||
.UseModule<NumericalModule>()
|
||||
.UseModule<GraphicModule>()
|
||||
.InitializeModule();
|
||||
World.Initialize().UseModule<NumericalModule>().UseModule<GraphicModule>().InitializeModule();
|
||||
|
||||
// TestRender();
|
||||
/**/
|
||||
@@ -56,14 +51,11 @@ namespace StgSharp
|
||||
unchecked {
|
||||
hlsf = new((nuint)(64 * (ulong)10000 * 10000));
|
||||
}
|
||||
Matrix<double> m = Matrix<double>.Create(4000, 4000, MatrixLayout.DenseRectangle,
|
||||
hlsf);
|
||||
Matrix<double> m = Matrix<double>.Create(40, 40, MatrixLayout.DenseRectangle, hlsf);
|
||||
MatrixCompute.Fill(m, 1.0f);
|
||||
Matrix<double> n = Matrix<double>.Create(4000, 4000, MatrixLayout.DenseRectangle,
|
||||
hlsf);
|
||||
Matrix<double> n = Matrix<double>.Create(40, 40, MatrixLayout.DenseRectangle, hlsf);
|
||||
MatrixCompute.Fill(n, 1.0f);
|
||||
Matrix<double> p = Matrix<double>.Create(4000, 4000, MatrixLayout.DenseRectangle,
|
||||
hlsf);
|
||||
Matrix<double> p = Matrix<double>.Create(40, 40, MatrixLayout.DenseRectangle, hlsf);
|
||||
MatrixCompute.Fill(p, 0.0f);
|
||||
|
||||
/**/
|
||||
|
||||
Reference in New Issue
Block a user