working on matrix mul matrix, rebuilding task model defines in C#
This commit is contained in:
@@ -29,6 +29,7 @@ using StgSharp.HighPerformance;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -37,17 +38,23 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
public partial class MatrixCompute
|
public partial class MatrixCompute
|
||||||
{
|
{
|
||||||
|
|
||||||
private static unsafe void ComputeFmaPanelF32(
|
internal static unsafe void ComputeFmaPanelF32(MatrixParallelTaskPackage* p)
|
||||||
MatrixKernel* left,
|
|
||||||
MatrixKernel* right,
|
|
||||||
MatrixKernel* ans,
|
|
||||||
ScalarPacket* scalar)
|
|
||||||
{
|
{
|
||||||
const int eSize = 32;
|
MatrixPanel<float>* leftPanel = MatrixPanel.Create<float>();
|
||||||
int colum = scalar->Data<int>(0);
|
MatrixPanel<float>* rightPanel = MatrixPanel.Create<float>();
|
||||||
int row = scalar->Data<int>(1);
|
MatrixPanel<float>* ansPanel = MatrixPanel.Create<float>();
|
||||||
int k =
|
ref long offsetRef = ref Unsafe.As<int, long>(ref p->PrimTileOffset);
|
||||||
|
ref long lengthRef = ref Unsafe.As<int, long>(ref p->PrimCount);
|
||||||
|
int i_length = p->PrimCount;
|
||||||
|
int j_length = p->SecCount;
|
||||||
|
int k_length = 0;
|
||||||
|
for (long i = 0; i < lengthRef; i++)
|
||||||
|
{
|
||||||
|
for (int k = 0; k < k_length; k++) { }
|
||||||
|
}
|
||||||
|
MatrixPanel.Destroy(leftPanel);
|
||||||
|
MatrixPanel.Destroy(rightPanel);
|
||||||
|
MatrixPanel.Destroy(ansPanel);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
package->PrimCount = left.KernelColumnLength;
|
package->PrimCount = left.KernelColumnLength;
|
||||||
package->SecCount = left.KernelRowLength;
|
package->SecCount = left.KernelRowLength;
|
||||||
package->Scalar = scalar;
|
package->Scalar = scalar;
|
||||||
package->ComputeMode = new MatrixOpMode(MatrixOperationStyle.BUFFER_OP, MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM);
|
package->ComputeMode = new MatrixOpMode(MatrixOperationStyle.BUFFER_OP, MatrixOperationSpecial.SEQ_FMA);
|
||||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Intrinsic.f32_buffer_scalar_mul;
|
package->ComputeHandle = (IntPtr)NativeIntrinsic.Intrinsic.f32_buffer_scalar_mul;
|
||||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
internal static class MatrixComputeModel
|
internal static class MatrixComputeModel
|
||||||
{
|
{
|
||||||
|
|
||||||
internal static unsafe void BufferComputeBinary(MatrixParallelTaskPackage* p)
|
internal static unsafe void KernelComputeBinary(MatrixParallelTaskPackage* p)
|
||||||
{
|
{
|
||||||
int size = p->ElementSize * 16;
|
int size = p->ElementSize * 16;
|
||||||
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
||||||
@@ -47,7 +47,21 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
MatrixParallelFactory.Release(p);
|
MatrixParallelFactory.Release(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static unsafe void BufferComputeNoOperatorScalar(MatrixParallelTaskPackage* p)
|
internal static unsafe void KernelComputeBinaryScalar(MatrixParallelTaskPackage* p)
|
||||||
|
{
|
||||||
|
int size = p->ElementSize * 16;
|
||||||
|
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
||||||
|
long offset = Unsafe.As<int, long>(ref p->PrimTileOffset) * size;
|
||||||
|
ScalarPacket* scalar = p->Scalar;
|
||||||
|
((delegate* unmanaged[Cdecl]<void*, void*, void*,ScalarPacket*, long, void>)p->ComputeHandle)((void*)(p->Left + offset),
|
||||||
|
(void*)(p->Right + offset),
|
||||||
|
(void*)(p->Result + offset),
|
||||||
|
scalar,
|
||||||
|
count);
|
||||||
|
MatrixParallelFactory.Release(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static unsafe void KernelComputeNoOperatorScalar(MatrixParallelTaskPackage* p)
|
||||||
{
|
{
|
||||||
int size = p->ElementSize * 16;
|
int size = p->ElementSize * 16;
|
||||||
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
||||||
@@ -57,7 +71,7 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
MatrixParallelFactory.Release(p);
|
MatrixParallelFactory.Release(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static unsafe void BufferComputeUnary(MatrixParallelTaskPackage* p)
|
internal static unsafe void KernelComputeUnary(MatrixParallelTaskPackage* p)
|
||||||
{
|
{
|
||||||
int size = p->ElementSize * 16;
|
int size = p->ElementSize * 16;
|
||||||
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
||||||
@@ -69,7 +83,7 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
MatrixParallelFactory.Release(p);
|
MatrixParallelFactory.Release(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static unsafe void BufferComputeUnaryScalar(MatrixParallelTaskPackage* p)
|
internal static unsafe void KernelComputeUnaryScalar(MatrixParallelTaskPackage* p)
|
||||||
{
|
{
|
||||||
int size = p->ElementSize * 16;
|
int size = p->ElementSize * 16;
|
||||||
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
||||||
@@ -81,201 +95,19 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
MatrixParallelFactory.Release(p);
|
MatrixParallelFactory.Release(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
internal static unsafe void KernelComputeBinary(MatrixParallelTaskPackage* p)
|
|
||||||
{
|
|
||||||
int eSize = p->ElementSize;
|
|
||||||
|
|
||||||
// base pointer to matrix kernels
|
|
||||||
nint l = p->Left, r = p->Right, a = p->Result;
|
|
||||||
long
|
|
||||||
|
|
||||||
// set beginning value to counters for primary offsets
|
|
||||||
lCount = (long)p->LeftPrimOffset * p->LeftPrimStride,
|
|
||||||
rCount = (long)p->RightPrimOffset * p->RightPrimStride,
|
|
||||||
aCount = (long)p->ResultPrimOffset * p->ResultPrimStride,
|
|
||||||
pCount = int.Min(p->PrimCount, p->PrimColumnCountInTile + p->PrimTileOffset) /*_count of tile may be too much*/,
|
|
||||||
//edge to reset counters
|
|
||||||
lLimit = pCount - p->LeftPrimOffset,
|
|
||||||
rLimit = pCount - p->RightPrimOffset,
|
|
||||||
aLimit = pCount - p->ResultPrimOffset;
|
|
||||||
delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr, void> op =
|
|
||||||
(delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr, void>)p->ComputeHandle;
|
|
||||||
for (long i = p->PrimTileOffset % pCount; i < pCount; i++)
|
|
||||||
{
|
|
||||||
// update primary counters
|
|
||||||
// if reaches edge, reset to zero
|
|
||||||
lCount = i < lLimit ? lCount + p->LeftPrimStride : 0;
|
|
||||||
rCount = i < rLimit ? rCount + p->RightPrimStride : 0;
|
|
||||||
aCount = i < rLimit ? aCount + p->ResultPrimStride : 0;
|
|
||||||
|
|
||||||
// get kernel column pointers
|
|
||||||
nint left = l + (nint)(lCount * eSize);
|
|
||||||
nint right = r + (nint)(rCount * eSize);
|
|
||||||
nint ans = a + (nint)(aCount * eSize);
|
|
||||||
long
|
|
||||||
lc = (long)p->LeftSecOffset * p->LeftSecStride,
|
|
||||||
rc = (long)p->RightSecOffset * p->RightSecStride,
|
|
||||||
ac = (long)p->ResultSecOffset * p->ResultSecStride,
|
|
||||||
sCount = int.Min(p->SecCount, p->PrimColumnCountInTile * p->SecTileOffset);
|
|
||||||
long
|
|
||||||
ll = sCount - p->LeftSecOffset,
|
|
||||||
rl = sCount - p->RightSecOffset,
|
|
||||||
al = sCount - p->ResultSecOffset;
|
|
||||||
for (long j = p->SecTileOffset % sCount; j < sCount; j++)
|
|
||||||
{
|
|
||||||
lc = j < ll ? lc + p->LeftSecStride : 0;
|
|
||||||
rc = j < rl ? rc + p->RightSecStride : 0;
|
|
||||||
ac = j < al ? ac + p->ResultSecStride : 0;
|
|
||||||
op(left + (nint)lc, right + (nint)rc, ans + (nint)ac);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
MatrixParallelFactory.Release(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
internal static unsafe void KernelComputeBinaryScalar(MatrixParallelTaskPackage* p)
|
|
||||||
{
|
|
||||||
int eSize = p->ElementSize;
|
|
||||||
|
|
||||||
// base pointer to matrix kernels
|
|
||||||
nint l = p->Left, r = p->Right, a = p->Result;
|
|
||||||
long
|
|
||||||
|
|
||||||
// set beginning value to counters for primary offsets
|
|
||||||
lCount = (long)p->LeftPrimOffset * p->LeftPrimStride,
|
|
||||||
rCount = (long)p->RightPrimOffset * p->RightPrimStride,
|
|
||||||
aCount = (long)p->ResultPrimOffset * p->ResultPrimStride,
|
|
||||||
pCount = int.Min(p->PrimCount, p->PrimColumnCountInTile + p->PrimTileOffset) /*_count of tile may be too much*/,
|
|
||||||
//edge to reset counters
|
|
||||||
lLimit = pCount - p->LeftPrimOffset,
|
|
||||||
rLimit = pCount - p->RightPrimOffset,
|
|
||||||
aLimit = pCount - p->ResultPrimOffset;
|
|
||||||
delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr, ScalarPacket*, void> op =
|
|
||||||
(delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr, ScalarPacket*, void>)p->ComputeHandle;
|
|
||||||
for (long i = p->PrimTileOffset % pCount; i < pCount; i++)
|
|
||||||
{
|
|
||||||
// update primary counters
|
|
||||||
// if reaches edge, reset to zero
|
|
||||||
lCount = i < lLimit ? lCount + p->LeftPrimStride : 0;
|
|
||||||
rCount = i < rLimit ? rCount + p->RightPrimStride : 0;
|
|
||||||
aCount = i < rLimit ? aCount + p->ResultPrimStride : 0;
|
|
||||||
|
|
||||||
// get kernel column pointers
|
|
||||||
nint left = l + (nint)(lCount * eSize);
|
|
||||||
nint right = r + (nint)(rCount * eSize);
|
|
||||||
nint ans = a + (nint)(aCount * eSize);
|
|
||||||
long
|
|
||||||
lc = (long)p->LeftSecOffset * p->LeftSecStride,
|
|
||||||
rc = (long)p->RightSecOffset * p->RightSecStride,
|
|
||||||
ac = (long)p->ResultSecOffset * p->ResultSecStride,
|
|
||||||
sCount = int.Min(p->SecCount, p->PrimColumnCountInTile * p->SecTileOffset);
|
|
||||||
long
|
|
||||||
ll = sCount - p->LeftSecOffset,
|
|
||||||
rl = sCount - p->RightSecOffset,
|
|
||||||
al = sCount - p->ResultSecOffset;
|
|
||||||
for (long j = p->SecTileOffset % sCount; j < sCount; j++)
|
|
||||||
{
|
|
||||||
lc = j < ll ? lc + p->LeftSecStride : 0;
|
|
||||||
rc = j < rl ? rc + p->RightSecStride : 0;
|
|
||||||
ac = j < al ? ac + p->ResultSecStride : 0;
|
|
||||||
op(left + (nint)lc, right + (nint)rc, ans + (nint)ac, p->Scalar);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
MatrixParallelFactory.Release(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
internal static unsafe void KernelComputeUnary(MatrixParallelTaskPackage* p)
|
|
||||||
{
|
|
||||||
int eSize = p->ElementSize;
|
|
||||||
|
|
||||||
// base pointer to matrix kernels
|
|
||||||
nint r = p->Right, a = p->Result;
|
|
||||||
long
|
|
||||||
|
|
||||||
// set beginning value to counters for primary offsets
|
|
||||||
rCount = (long)p->RightPrimOffset * p->RightPrimStride,
|
|
||||||
aCount = (long)p->ResultPrimOffset * p->ResultPrimStride,
|
|
||||||
pCount = int.Min(p->PrimCount, p->PrimColumnCountInTile + p->PrimTileOffset) /*_count of tile may be too much*/,
|
|
||||||
//edge to reset counters
|
|
||||||
rLimit = pCount - p->RightPrimOffset,
|
|
||||||
aLimit = pCount - p->ResultPrimOffset;
|
|
||||||
delegate* unmanaged[Cdecl]<IntPtr, IntPtr, void> op =
|
|
||||||
(delegate* unmanaged[Cdecl]<IntPtr, IntPtr, void>)p->ComputeHandle;
|
|
||||||
for (long i = p->PrimTileOffset % pCount; i < pCount; i++)
|
|
||||||
{
|
|
||||||
// update primary counters
|
|
||||||
// if reaches edge, reset to zero
|
|
||||||
rCount = i < rLimit ? rCount + p->RightPrimStride : 0;
|
|
||||||
aCount = i < rLimit ? aCount + p->ResultPrimStride : 0;
|
|
||||||
|
|
||||||
// get kernel column pointers
|
|
||||||
nint right = r + (nint)(rCount * eSize);
|
|
||||||
nint ans = a + (nint)(aCount * eSize);
|
|
||||||
long
|
|
||||||
rc = (long)p->RightSecOffset * p->RightSecStride,
|
|
||||||
ac = (long)p->ResultSecOffset * p->ResultSecStride,
|
|
||||||
sCount = int.Min(p->SecCount, p->PrimColumnCountInTile * p->SecTileOffset);
|
|
||||||
long
|
|
||||||
rl = sCount - p->RightSecOffset,
|
|
||||||
al = sCount - p->ResultSecOffset;
|
|
||||||
for (long j = p->SecTileOffset % sCount; j < sCount; j++)
|
|
||||||
{
|
|
||||||
rc = j < rl ? rc + p->RightSecStride : 0;
|
|
||||||
ac = j < al ? ac + p->ResultSecStride : 0;
|
|
||||||
op(right + (nint)rc, ans + (nint)ac);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
MatrixParallelFactory.Release(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
internal static unsafe void KernelComputeUnaryScalar(MatrixParallelTaskPackage* p)
|
|
||||||
{
|
|
||||||
int eSize = p->ElementSize;
|
|
||||||
|
|
||||||
// base pointer to matrix kernels
|
|
||||||
nint r = p->Right, a = p->Result;
|
|
||||||
long
|
|
||||||
|
|
||||||
// set beginning value to counters for primary offsets
|
|
||||||
rCount = (long)p->RightPrimOffset * p->RightPrimStride,
|
|
||||||
aCount = (long)p->ResultPrimOffset * p->ResultPrimStride,
|
|
||||||
pCount = int.Min(p->PrimCount, p->PrimColumnCountInTile + p->PrimTileOffset) /*_count of tile may be too much*/,
|
|
||||||
//edge to reset counters
|
|
||||||
rLimit = pCount - p->RightPrimOffset,
|
|
||||||
aLimit = pCount - p->ResultPrimOffset;
|
|
||||||
delegate* unmanaged[Cdecl]<IntPtr, IntPtr, ScalarPacket*, void> op =
|
|
||||||
(delegate* unmanaged[Cdecl]<IntPtr, IntPtr, ScalarPacket*, void>)p->ComputeHandle;
|
|
||||||
for (long i = p->PrimTileOffset % pCount; i < pCount; i++)
|
|
||||||
{
|
|
||||||
// update primary counters
|
|
||||||
// if reaches edge, reset to zero
|
|
||||||
rCount = i < rLimit ? rCount + p->RightPrimStride : 0;
|
|
||||||
aCount = i < rLimit ? aCount + p->ResultPrimStride : 0;
|
|
||||||
|
|
||||||
// get kernel column pointers
|
|
||||||
nint right = r + (nint)(rCount * eSize);
|
|
||||||
nint ans = a + (nint)(aCount * eSize);
|
|
||||||
long
|
|
||||||
rc = (long)p->RightSecOffset * p->RightSecStride,
|
|
||||||
ac = (long)p->ResultSecOffset * p->ResultSecStride,
|
|
||||||
sCount = int.Min(p->SecCount, p->PrimColumnCountInTile * p->SecTileOffset);
|
|
||||||
long
|
|
||||||
rl = sCount - p->RightSecOffset,
|
|
||||||
al = sCount - p->ResultSecOffset;
|
|
||||||
for (long j = p->SecTileOffset % sCount; j < sCount; j++)
|
|
||||||
{
|
|
||||||
rc = j < rl ? rc + p->RightSecStride : 0;
|
|
||||||
ac = j < al ? ac + p->ResultSecStride : 0;
|
|
||||||
op(right + (nint)rc, ans + (nint)ac, p->Scalar);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
MatrixParallelFactory.Release(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static unsafe void PanelComputeBinary(MatrixParallelTaskPackage* p) { }
|
internal static unsafe void PanelComputeBinary(MatrixParallelTaskPackage* p) { }
|
||||||
|
|
||||||
|
internal static unsafe void SpecialCompute(MatrixParallelTaskPackage* p)
|
||||||
|
{
|
||||||
|
switch ((MatrixOperationSpecial)p->ComputeMode.OperationStyle)
|
||||||
|
{
|
||||||
|
case MatrixOperationSpecial.SEQ_FMA:
|
||||||
|
MatrixCompute.ComputeFmaPanelF32(p);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new NotSupportedException("The special matrix operation style is not supported.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="MatrixParallel.Publish"
|
// file="MatrixParallel.Publish"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
@@ -51,18 +51,15 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
MatrixParallelTaskPackage* nonGeneric = package;
|
MatrixParallelTaskPackage* nonGeneric = package;
|
||||||
switch (nonGeneric->ComputeMode.OperationStyle)
|
switch (nonGeneric->ComputeMode.OperationStyle)
|
||||||
{
|
{
|
||||||
case MatrixOperationStyle.PANEL_OP:
|
case MatrixOperationStyle.BUFFER_INDEX:
|
||||||
throw new NotImplementedException();
|
|
||||||
case MatrixOperationStyle.BUFFER_OP:
|
|
||||||
return ScheduleBufferTask(nonGeneric);
|
return ScheduleBufferTask(nonGeneric);
|
||||||
case MatrixOperationStyle.KERNEL_OP:
|
|
||||||
return ScheduleKernelTask(nonGeneric);
|
|
||||||
default:
|
default:
|
||||||
throw new InvalidOperationException($"Unknown mode {nonGeneric->ComputeMode.OperationStyle}.");
|
throw new InvalidOperationException($"Unknown mode {nonGeneric->ComputeMode.OperationStyle}.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static unsafe MatrixParallelHandle ScheduleBufferTask(MatrixParallelTaskPackage* package)
|
private static unsafe MatrixParallelHandle ScheduleBufferTask(
|
||||||
|
MatrixParallelTaskPackage* package)
|
||||||
{
|
{
|
||||||
int primCount = package->PrimCount;
|
int primCount = package->PrimCount;
|
||||||
int secCount = package->SecCount;
|
int secCount = package->SecCount;
|
||||||
@@ -113,7 +110,8 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static unsafe MatrixParallelHandle ScheduleKernelTask(MatrixParallelTaskPackage* package)
|
private static unsafe MatrixParallelHandle ScheduleKernelTask(
|
||||||
|
MatrixParallelTaskPackage* package)
|
||||||
{
|
{
|
||||||
int primCount = package->PrimCount;
|
int primCount = package->PrimCount;
|
||||||
int secCount = package->SecCount;
|
int secCount = package->SecCount;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="MatrixParallelTaskPackage"
|
// file="MatrixParallelTaskPackage"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
@@ -52,12 +52,17 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
* matrix_op_mode;
|
* matrix_op_mode;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
public enum MatrixOperationSpecial : short
|
||||||
|
{
|
||||||
|
|
||||||
|
SEQ_FMA = -1,
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public enum MatrixOperationStyle : short
|
public enum MatrixOperationStyle : short
|
||||||
{
|
{
|
||||||
|
|
||||||
PANEL_OP = 1,
|
BUFFER_INDEX = 1,
|
||||||
BUFFER_OP = 2,
|
|
||||||
KERNEL_OP = 3,
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,6 +84,9 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
public MatrixOperationParam ParameterStyle = paramLayout;
|
public MatrixOperationParam ParameterStyle = paramLayout;
|
||||||
public MatrixOperationStyle OperationStyle = style;
|
public MatrixOperationStyle OperationStyle = style;
|
||||||
|
|
||||||
|
public MatrixOpMode(MatrixOperationStyle style, MatrixOperationSpecial paramLayout)
|
||||||
|
: this(style, (MatrixOperationParam)paramLayout) { }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Explicit, Size = 128)]
|
[StructLayout(LayoutKind.Explicit, Size = 128)]
|
||||||
@@ -86,9 +94,12 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
{
|
{
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static unsafe void Copy(MatrixParallelTaskPackage* source, MatrixParallelTaskPackage* target)
|
public static unsafe void Copy(
|
||||||
|
MatrixParallelTaskPackage* source,
|
||||||
|
MatrixParallelTaskPackage* target)
|
||||||
{
|
{
|
||||||
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTaskPackage), sizeof(MatrixParallelTaskPackage));
|
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTaskPackage),
|
||||||
|
sizeof(MatrixParallelTaskPackage));
|
||||||
}
|
}
|
||||||
|
|
||||||
#region source
|
#region source
|
||||||
|
|||||||
@@ -85,26 +85,30 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
while (taskQueue.TryDequeue(out nint handle))
|
while (taskQueue.TryDequeue(out nint handle))
|
||||||
{
|
{
|
||||||
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)handle;
|
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)handle;
|
||||||
|
if ((int)p->ComputeMode.ParameterStyle < 0) {
|
||||||
|
MatrixComputeModel.SpecialCompute(p);
|
||||||
|
}
|
||||||
switch (p->ComputeMode.OperationStyle)
|
switch (p->ComputeMode.OperationStyle)
|
||||||
{
|
{
|
||||||
case MatrixOperationStyle.PANEL_OP:
|
case MatrixOperationStyle.PANEL_OP:
|
||||||
break;
|
|
||||||
case MatrixOperationStyle.BUFFER_OP:
|
|
||||||
switch (p->ComputeMode.ParameterStyle)
|
switch (p->ComputeMode.ParameterStyle)
|
||||||
{
|
{
|
||||||
case MatrixOperationParam.RIGHT_ANS_PARAM:
|
case MatrixOperationParam.RIGHT_ANS_PARAM:
|
||||||
MatrixComputeModel.BufferComputeUnary(p);
|
|
||||||
|
// MatrixComputeModel.PanelComputeUnary(p);
|
||||||
break;
|
break;
|
||||||
case MatrixOperationParam.LEFT_RIGHT_ANS_PARAM:
|
case MatrixOperationParam.LEFT_RIGHT_ANS_PARAM:
|
||||||
MatrixComputeModel.BufferComputeBinary(p);
|
MatrixComputeModel.PanelComputeBinary(p);
|
||||||
break;
|
break;
|
||||||
case MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM:
|
case MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM:
|
||||||
MatrixComputeModel.BufferComputeUnaryScalar(p);
|
|
||||||
|
// MatrixComputeModel.PanelComputeUnaryScalar(p);
|
||||||
break;
|
break;
|
||||||
case MatrixOperationParam.LEFT_RIGHT_ANS_SCALAR_PARAM:
|
case MatrixOperationParam.LEFT_RIGHT_ANS_SCALAR_PARAM:
|
||||||
break;
|
break;
|
||||||
case MatrixOperationParam.ANS_SCALAR_PARAM:
|
case MatrixOperationParam.ANS_SCALAR_PARAM:
|
||||||
MatrixComputeModel.BufferComputeNoOperatorScalar(p);
|
|
||||||
|
// MatrixComputeModel.PanelComputeNoOperatorScalar(p);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="MatrixParallelWrap"
|
// file="MatrixParallelWrap"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
@@ -104,18 +104,12 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
while (tasks.TryDequeue(out nint handle))
|
while (tasks.TryDequeue(out nint handle))
|
||||||
{
|
{
|
||||||
MatrixParallelTaskPackage* task = (MatrixParallelTaskPackage*)handle;
|
MatrixParallelTaskPackage* task = (MatrixParallelTaskPackage*)handle;
|
||||||
switch (task->ComputeMode.OperationStyle)
|
if ((int)task->ComputeMode.OperationStyle > 0)
|
||||||
{
|
{
|
||||||
case MatrixOperationStyle.PANEL_OP:
|
|
||||||
break;
|
|
||||||
case MatrixOperationStyle.BUFFER_OP:
|
|
||||||
PublishBufferTaskToWorkers(task);
|
PublishBufferTaskToWorkers(task);
|
||||||
break;
|
} else
|
||||||
case MatrixOperationStyle.KERNEL_OP:
|
{
|
||||||
PublishBufferTaskToWorkers(task);
|
throw new NotImplementedException();
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,73 +163,6 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
// Console.WriteLine(msg);
|
// Console.WriteLine(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PublishKernelTaskToWorkers(MatrixParallelTaskPackage* package)
|
|
||||||
{
|
|
||||||
int primCount = package->PrimCount;
|
|
||||||
int secCount = package->SecCount;
|
|
||||||
ReadOnlySpan<MatrixParallelThread> pool = Threads;
|
|
||||||
|
|
||||||
if (pool.Length == 1)
|
|
||||||
{
|
|
||||||
pool[0].Tasks.Enqueue((nint)package);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
throw new NotImplementedException();
|
|
||||||
/*
|
|
||||||
int threadCount = pool.Length;
|
|
||||||
int tilePerThreadLess = tileCount / threadCount; // tiles per thread without extra slice
|
|
||||||
int columnAfterBaseTile = tileCount % threadCount * tileWidth; // tile sliced into columns
|
|
||||||
int extraColumnPerThread = columnAfterBaseTile / threadCount;
|
|
||||||
int extraColumnCount = columnAfterBaseTile % threadCount; // columns to be sliced
|
|
||||||
|
|
||||||
int total = primCount;
|
|
||||||
Stack<MatrixParallelWrap> taskBunch = new Stack<MatrixParallelWrap>(threadCount);
|
|
||||||
MatrixParallelTaskPackageNonGeneric* current = package;
|
|
||||||
foreach (MatrixParallelWrap wrap in pool.Wraps)
|
|
||||||
{
|
|
||||||
bool hasSlice = columnAfterBaseTile > 0;
|
|
||||||
bool isLargeSlice = extraColumnCount > 0;
|
|
||||||
|
|
||||||
// _count tiles for wrap
|
|
||||||
int wrapWidth = wrap!.WrapTaskCapacity;
|
|
||||||
int columnFromSlicingCount = (hasSlice ? extraColumnPerThread : 0) * wrapWidth;
|
|
||||||
int baseColumn = wrapWidth * tilePerThreadLess * tileWidth;
|
|
||||||
int columnInWrap = baseColumn + columnFromSlicingCount + (isLargeSlice ? 1 : 0);
|
|
||||||
|
|
||||||
// set tasks
|
|
||||||
if (columnInWrap == 0)
|
|
||||||
{
|
|
||||||
#if DEBUG
|
|
||||||
throw new InvalidOperationException("Zero tasks published.");
|
|
||||||
#else
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
// update current task
|
|
||||||
current->PrimColumnCountInTile = columnInWrap;
|
|
||||||
current->SecColumnCountInTile = current->SecCount;
|
|
||||||
current->SecTileOffset = 0;
|
|
||||||
|
|
||||||
// remove tiles _count
|
|
||||||
extraColumnCount--;
|
|
||||||
columnAfterBaseTile -= columnFromSlicingCount;
|
|
||||||
total -= columnInWrap;
|
|
||||||
|
|
||||||
// set to wrap and push
|
|
||||||
wrap.WrapTask = current;
|
|
||||||
|
|
||||||
if (total <= 0)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
current = MatrixParallelFactory.FromExistPackage(current);
|
|
||||||
current->PrimTileOffset += columnInWrap;
|
|
||||||
}
|
|
||||||
/**/
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
#region wrap task
|
#region wrap task
|
||||||
|
|
||||||
internal unsafe Queue<nint> WrapTask { get; set; } = [];
|
internal unsafe Queue<nint> WrapTask { get; set; } = [];
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "StgSharpNative.h"
|
#include "StgSharpNative.h"
|
||||||
#include "sn_matkernel.h"
|
#include "sn_matkernel.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ INTERNAL void FORCEINLINE SN_DECL dot_41_sse(MAT_KERNEL(float) const *transpose,
|
|||||||
SSE_INSERT_SIMPLE(1, 4));
|
SSE_INSERT_SIMPLE(1, 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL void SN_DECL dot_41_avx(MAT_KERNEL(float) const *transpose, __m128 const *vector, __m128 *ans)
|
INTERNAL void SN_DECL dot_41_avx(MAT_KERNEL(float) const *transpose, __m128 const *vector,
|
||||||
|
__m128 *ans)
|
||||||
{
|
{
|
||||||
__m256 _double = _mm256_set_m128(vector[0], vector[0]);
|
__m256 _double = _mm256_set_m128(vector[0], vector[0]);
|
||||||
__m256 tmp = _mm256_dp_ps(transpose->ymm[0], _double, 0B11110001);
|
__m256 tmp = _mm256_dp_ps(transpose->ymm[0], _double, 0B11110001);
|
||||||
@@ -25,7 +26,8 @@ INTERNAL void SN_DECL dot_41_avx(MAT_KERNEL(float) const *transpose, __m128 cons
|
|||||||
_mm256_permutevar8x32_ps(tmp, _mm256_loadu_epi32(avxdot_indices)));
|
_mm256_permutevar8x32_ps(tmp, _mm256_loadu_epi32(avxdot_indices)));
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL void SN_DECL dot_42_sse(MAT_KERNEL(float) const *transpose, __m128 const *vector, __m128 *ans)
|
INTERNAL void SN_DECL dot_42_sse(MAT_KERNEL(float) const *transpose, __m128 const *vector,
|
||||||
|
__m128 *ans)
|
||||||
{
|
{
|
||||||
ans[0] = _mm_dp_ps(transpose->xmm[0], vector[0], 0B11110001);
|
ans[0] = _mm_dp_ps(transpose->xmm[0], vector[0], 0B11110001);
|
||||||
ans[0] = _mm_insert_ps(*ans, _mm_dp_ps(transpose->xmm[1], vector[0], 0B11110001),
|
ans[0] = _mm_insert_ps(*ans, _mm_dp_ps(transpose->xmm[1], vector[0], 0B11110001),
|
||||||
@@ -44,22 +46,20 @@ INTERNAL void SN_DECL dot_42_sse(MAT_KERNEL(float) const *transpose, __m128 cons
|
|||||||
SSE_INSERT_SIMPLE(1, 4));
|
SSE_INSERT_SIMPLE(1, 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL void SN_DECL dot_42_avx(MAT_KERNEL(float) const *transpose, __m128 const *vector, __m128 *ans)
|
INTERNAL void SN_DECL dot_42_avx(MAT_KERNEL(float) const *transpose, __m128 const *vector,
|
||||||
|
__m128 *ans)
|
||||||
{
|
{
|
||||||
__m256 temp = transpose->ymm[0];
|
__m256 temp = transpose->ymm[0];
|
||||||
__m256 vector_stream = _mm256_set_m128(vector[1], vector[0]);
|
__m256 vector_stream = _mm256_set_m128(vector[1], vector[0]);
|
||||||
|
|
||||||
__m256 result = _mm256_dp_ps(temp, vector_stream, 0B11110001);
|
__m256 result = _mm256_dp_ps(temp, vector_stream, 0B11110001);
|
||||||
temp = _mm256_permute2f128_ps(temp, temp, 0B00000001);
|
temp = _mm256_permute2f128_ps(temp, temp, 0B00000001);
|
||||||
result = _mm256_blend_ps(
|
result = _mm256_blend_ps(result, _mm256_dp_ps(temp, vector_stream, 0B11110010), 0B00100010);
|
||||||
result, _mm256_dp_ps(temp, vector_stream, 0B11110010), 0B00100010);
|
|
||||||
|
|
||||||
temp = transpose->ymm[1];
|
temp = transpose->ymm[1];
|
||||||
result = _mm256_blend_ps(
|
result = _mm256_blend_ps(result, _mm256_dp_ps(temp, vector_stream, 0B11110100), 0B01000100);
|
||||||
result, _mm256_dp_ps(temp, vector_stream, 0B11110100), 0B01000100);
|
|
||||||
temp = _mm256_permute2f128_ps(temp, temp, 0B00000001);
|
temp = _mm256_permute2f128_ps(temp, temp, 0B00000001);
|
||||||
result = _mm256_blend_ps(
|
result = _mm256_blend_ps(result, _mm256_dp_ps(temp, vector_stream, 0B11111000), 0B10001000);
|
||||||
result, _mm256_dp_ps(temp, vector_stream, 0B11111000), 0B10001000);
|
|
||||||
|
|
||||||
ans[0] = _mm256_castps256_ps128(result);
|
ans[0] = _mm256_castps256_ps128(result);
|
||||||
ans[1] = _mm256_extractf128_ps(result, 1);
|
ans[1] = _mm256_extractf128_ps(result, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user