working on matrix mul matrix

This commit is contained in:
Youmu Liu
2025-12-11 18:09:35 +08:00
parent 631bc2b6e8
commit 9ff438d3b9
13 changed files with 379 additions and 115 deletions
@@ -0,0 +1,54 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixCompute.Additional"
// 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 StgSharp.HighPerformance;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
public partial class MatrixCompute
{
private static unsafe void ComputeFmaPanelF32(
MatrixKernel* left,
MatrixKernel* right,
MatrixKernel* ans,
ScalarPacket* scalar)
{
const int eSize = 32;
int colum = scalar->Data<int>(0);
int row = scalar->Data<int>(1);
int k =
}
}
}
@@ -39,13 +39,10 @@ using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
public static class MatrixCompute
public static partial class MatrixCompute
{
public static unsafe Matrix<float> Add(
Matrix<float> left,
Matrix<float> right,
Matrix<float> ans)
public static unsafe void Add(Matrix<float> left, Matrix<float> right, Matrix<float> ans)
{
if (left.ColumnLength != right.ColumnLength ||
left.RowLength != right.RowLength ||
@@ -61,7 +58,7 @@ namespace StgSharp.Mathematics.Numeric
MatrixKernel<float>* ansPtr = ans.Buffer;
NativeIntrinsic.Intrinsic.f32_buffer_add(leftPtr, rightPtr, ansPtr, count);
return ans;
return;
} else if (count <= 1024)
{
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
@@ -72,22 +69,22 @@ namespace StgSharp.Mathematics.Numeric
NativeIntrinsic.Intrinsic.f32_buffer_add(leftPtr, rightPtr, ansPtr, count);
MatrixParallel.ReturnParallelResources(ref pool);
return ans;
return;
}
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
package->Left = (nint)left.Buffer;
package->Right = (nint)right.Buffer;
package->Result = (nint)ans.Buffer;
package->ElementSize = sizeof(float);
package->PrimCount = left.KernelColumnLength;
package->SecCount = left.KernelRowLength;
package->PrimCount = ans.KernelColumnLength;
package->SecCount = ans.KernelRowLength;
package->ComputeMode = new MatrixOpMode(MatrixOperationStyle.BUFFER_OP, MatrixOperationParam.LEFT_RIGHT_ANS_PARAM);
package->ComputeHandle = (IntPtr)NativeIntrinsic.Intrinsic.f32_buffer_add;
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
taskGroup.Dispose();
return ans;
return;
}
public static unsafe void Fill(Matrix<float> ans, float value)
@@ -120,10 +117,7 @@ namespace StgSharp.Mathematics.Numeric
taskGroup.Dispose();
}
public static unsafe Matrix<float> Mul(
Matrix<float> left,
Matrix<float> right,
Matrix<float> ans)
public static unsafe void Mul(Matrix<float> left, Matrix<float> right, Matrix<float> ans)
{
// For matrix multiplication (left: MxK) * (right: KxN) => ans: MxN
// ColumnLength = number of columns (N), RowLength = number of rows (M)
@@ -134,30 +128,68 @@ namespace StgSharp.Mathematics.Numeric
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
}
long count = (long)left.KernelColumnLength * left.KernelRowLength;
if (count <= 256)
if (count <= 1024)
{
MatrixPanel<float>* leftPanel = MatrixPanel<float>.Create();
MatrixPanel<float>* rightPanel = MatrixPanel<float>.Create();
MatrixPanel<float>* ansPanel = MatrixPanel<float>.Create();
int size = MatrixPanel<float>.KernelSideCount();
MatrixParallelThread[] pool = null;
if (count > 256) {
pool = MatrixParallel.LeadParallel(1);
}
MatrixPanel<float>* leftPanel = MatrixPanel.Create<float>();
MatrixPanel<float>* rightPanel = MatrixPanel.Create<float>();
MatrixPanel<float>* ansPanel = MatrixPanel.Create<float>();
int size = MatrixPanel.KernelSideCount<float>();
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_clear_panel((MatrixPanel*)ansPanel);
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);
NativeIntrinsic.Intrinsic
.f32_build_panel((MatrixKernel*)right.Buffer,
(MatrixPanel*)rightPanel,
right.ColumnLength, right.RowLength, k,
j);
NativeIntrinsic.Intrinsic
.f32_panel_fma((MatrixPanel*)leftPanel,
(MatrixPanel*)rightPanel,
(MatrixPanel*)ansPanel);
}
NativeIntrinsic.Intrinsic
.f32_store_panel((MatrixKernel*)ans.Buffer,
(MatrixPanel*)ansPanel, ans.ColumnLength,
ans.RowLength, i, j);
}
}
MatrixPanel.Destroy(leftPanel);
MatrixPanel.Destroy(rightPanel);
MatrixPanel.Destroy(ansPanel);
if (count > 256) {
MatrixParallel.ReturnParallelResources(ref pool!);
}
return;
}
throw new NotImplementedException();
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
package->Right = (nint)left.Buffer;
package->Result = (nint)ans.Buffer;
package->ElementSize = sizeof(float);
package->PrimCount = left.KernelColumnLength;
package->SecCount = left.KernelRowLength;
package->ComputeMode = new MatrixOpMode(MatrixOperationStyle.BUFFER_OP, MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM);
package->ComputeHandle = (IntPtr)NativeIntrinsic.Intrinsic.f32_buffer_scalar_mul;
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
taskGroup.Dispose();
return;
}
public static unsafe Matrix<float> Mul(Matrix<float> left, float right, Matrix<float> ans)
public static unsafe void Mul(Matrix<float> left, float right, Matrix<float> ans)
{
if (left.ColumnLength != ans.ColumnLength || left.RowLength != ans.RowLength) {
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
@@ -171,7 +203,7 @@ namespace StgSharp.Mathematics.Numeric
MatrixKernel<float>* ansPtr = ans.Buffer;
NativeIntrinsic.Intrinsic.f32_buffer_scalar_mul(leftPtr, ansPtr, scalar, count);
return ans;
return;
} else if (count <= 1024)
{
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
@@ -181,7 +213,7 @@ namespace StgSharp.Mathematics.Numeric
NativeIntrinsic.Intrinsic.f32_buffer_scalar_mul(leftPtr, ansPtr, scalar, count);
MatrixParallel.ReturnParallelResources(ref pool);
return ans;
return;
}
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
package->Right = (nint)left.Buffer;
@@ -197,13 +229,10 @@ namespace StgSharp.Mathematics.Numeric
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
taskGroup.Dispose();
MatrixParallelFactory.Release(scalar);
return ans;
return;
}
public static unsafe Matrix<float> Sub(
Matrix<float> left,
Matrix<float> right,
Matrix<float> ans)
public static unsafe void Sub(Matrix<float> left, Matrix<float> right, Matrix<float> ans)
{
if (left.ColumnLength != right.ColumnLength ||
left.RowLength != right.RowLength ||
@@ -219,7 +248,7 @@ namespace StgSharp.Mathematics.Numeric
MatrixKernel<float>* ansPtr = ans.Buffer;
NativeIntrinsic.Intrinsic.f32_buffer_sub(leftPtr, rightPtr, ansPtr, count);
return ans;
return;
} else if (count <= 1024)
{
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
@@ -230,7 +259,7 @@ namespace StgSharp.Mathematics.Numeric
NativeIntrinsic.Intrinsic.f32_buffer_sub(leftPtr, rightPtr, ansPtr, count);
MatrixParallel.ReturnParallelResources(ref pool);
return ans;
return;
}
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
package->Left = (nint)left.Buffer;
@@ -245,7 +274,7 @@ namespace StgSharp.Mathematics.Numeric
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
taskGroup.Dispose();
return ans;
return;
}
}
@@ -0,0 +1,37 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixCompute"
// 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
{
public static partial class MatrixCompute { }
}
@@ -1,4 +1,4 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixComputeModel"
// Project: StgSharp
@@ -76,12 +76,13 @@ namespace StgSharp.Mathematics.Numeric
long offset = Unsafe.As<int, long>(ref p->PrimTileOffset) * size;
((delegate* unmanaged[Cdecl]< void*, void*, ScalarPacket*, long, void>)p->ComputeHandle)((void*)(p->Right + offset),
(void*)(p->Result + offset),
p->Scalar, count);
p->Scalar,
count);
MatrixParallelFactory.Release(p);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe void Compute_Binary(MatrixParallelTaskPackage* p)
internal static unsafe void KernelComputeBinary(MatrixParallelTaskPackage* p)
{
int eSize = p->ElementSize;
@@ -133,7 +134,7 @@ namespace StgSharp.Mathematics.Numeric
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe void Compute_BinaryScalar(MatrixParallelTaskPackage* p)
internal static unsafe void KernelComputeBinaryScalar(MatrixParallelTaskPackage* p)
{
int eSize = p->ElementSize;
@@ -185,7 +186,7 @@ namespace StgSharp.Mathematics.Numeric
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe void Compute_Unary(MatrixParallelTaskPackage* p)
internal static unsafe void KernelComputeUnary(MatrixParallelTaskPackage* p)
{
int eSize = p->ElementSize;
@@ -230,7 +231,7 @@ namespace StgSharp.Mathematics.Numeric
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe void Compute_UnaryScalar(MatrixParallelTaskPackage* p)
internal static unsafe void KernelComputeUnaryScalar(MatrixParallelTaskPackage* p)
{
int eSize = p->ElementSize;
@@ -274,5 +275,7 @@ namespace StgSharp.Mathematics.Numeric
MatrixParallelFactory.Release(p);
}
internal static unsafe void PanelComputeBinary(MatrixParallelTaskPackage* p) { }
}
}
@@ -1,4 +1,4 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallel"
// Project: StgSharp
@@ -27,15 +27,14 @@
// -----------------------------------------------------------------------
using StgSharp.Collections;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
public static partial class MatrixParallel
public static class MatrixParallel
{
private static bool _meaningful;
@@ -97,4 +96,4 @@ namespace StgSharp.Mathematics.Numeric
}
}
}
}
@@ -1,4 +1,4 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallelThread"
// Project: StgSharp
@@ -114,16 +114,16 @@ namespace StgSharp.Mathematics.Numeric
switch (p->ComputeMode.ParameterStyle)
{
case MatrixOperationParam.RIGHT_ANS_PARAM:
MatrixComputeModel.Compute_Unary(p);
MatrixComputeModel.KernelComputeUnary(p);
break;
case MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM:
MatrixComputeModel.Compute_UnaryScalar(p);
MatrixComputeModel.KernelComputeUnaryScalar(p);
break;
case MatrixOperationParam.LEFT_RIGHT_ANS_PARAM:
MatrixComputeModel.Compute_Binary(p);
MatrixComputeModel.KernelComputeBinary(p);
break;
case MatrixOperationParam.LEFT_RIGHT_ANS_SCALAR_PARAM:
MatrixComputeModel.Compute_BinaryScalar(p);
MatrixComputeModel.KernelComputeBinaryScalar(p);
break;
default:
break;
@@ -400,7 +400,9 @@ namespace StgSharp.Mathematics.Numeric
private static extern IntPtr GetCurrentThread();
[DllImport("kernel32.dll")]
private static extern nuint SetThreadAffinityMask(IntPtr hThread, nuint dwThreadAffinityMask);
private static extern nuint SetThreadAffinityMask(
IntPtr hThread,
nuint dwThreadAffinityMask);
[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]