many things
This commit is contained in:
@@ -40,68 +40,6 @@ namespace StgSharp.Mathematics.Numeric
|
||||
public partial class MatrixCompute
|
||||
{
|
||||
|
||||
internal static unsafe void ComputeFmaPanelF32(
|
||||
MatrixParallelTaskPackage* p
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
MatrixKernel* leftBuffer = (MatrixKernel*)p->Left;
|
||||
MatrixKernel* rightBuffer = (MatrixKernel*)p->Right;
|
||||
MatrixKernel* ansBuffer = (MatrixKernel*)p->Answer;
|
||||
ref long offsetRef = ref Unsafe.As<int, long>(ref p->PrimTileOffset);
|
||||
ref long lengthRef = ref Unsafe.As<int, long>(ref p->PrimCount);
|
||||
int ansWidth = p->AnsPrimOffset;
|
||||
int ansHeight = p->AnsSecOffset;
|
||||
int commonK = p->LeftPrimOffset;
|
||||
|
||||
M128* enumeration = stackalloc M128[1];
|
||||
enumeration->Member<int>(2) = commonK;
|
||||
enumeration->Member<int>(3) = ansHeight;
|
||||
|
||||
// in this occasion size must be 1
|
||||
for (long c = offsetRef; c < offsetRef + lengthRef; c++)
|
||||
{
|
||||
int i = (int)(c / ansHeight);
|
||||
int j = (int)(c % ansHeight);
|
||||
enumeration->Member<int>(0) = i;
|
||||
enumeration->Member<int>(1) = j;
|
||||
NativeIntrinsic.Context.mat[f32].kernel_tile_fma(leftBuffer, rightBuffer, ansBuffer,
|
||||
enumeration);
|
||||
}
|
||||
}
|
||||
|
||||
internal static unsafe void ComputeFmaPanelF64(
|
||||
MatrixParallelTaskPackage* p
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
|
||||
// int size = 1;
|
||||
MatrixKernel* leftBuffer = (MatrixKernel*)p->Left;
|
||||
MatrixKernel* rightBuffer = (MatrixKernel*)p->Right;
|
||||
MatrixKernel* ansBuffer = (MatrixKernel*)p->Answer;
|
||||
ref long offsetRef = ref Unsafe.As<int, long>(ref p->PrimTileOffset);
|
||||
ref long lengthRef = ref Unsafe.As<int, long>(ref p->PrimCount);
|
||||
int ansWidth = p->AnsPrimOffset;
|
||||
int ansHeight = p->AnsSecOffset;
|
||||
int commonK = p->LeftPrimOffset;
|
||||
|
||||
M128* enumeration = stackalloc M128[1];
|
||||
enumeration->Member<int>(2) = commonK;
|
||||
enumeration->Member<int>(3) = ansHeight;
|
||||
|
||||
// in this occasion size must be 1
|
||||
for (long c = offsetRef; c < offsetRef + lengthRef; c++)
|
||||
{
|
||||
int i = (int)(c / ansHeight);
|
||||
int j = (int)(c % ansHeight);
|
||||
enumeration->Member<int>(0) = i;
|
||||
enumeration->Member<int>(1) = j;
|
||||
NativeIntrinsic.Context.mat[f64].kernel_tile_fma(leftBuffer, rightBuffer, ansBuffer,
|
||||
enumeration);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static unsafe MatrixKernel* GetKernelAddressUnsafe<T>(
|
||||
MatrixKernel<T>* buffer,
|
||||
|
||||
@@ -71,11 +71,9 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
long recommendCount = count / 1024;
|
||||
MatrixParallelThread[] pool = LeadParallel((int)recommendCount);
|
||||
MatrixParallelWrap handle = new MatrixParallelWrap(pool);
|
||||
|
||||
// TODO set parallel queue for handle
|
||||
handle.TaskQueue = MatrixParallelQueue.Rent(package);
|
||||
return handle;
|
||||
MatrixParallelWrap wrap = new MatrixParallelWrap(pool);
|
||||
wrap.TaskQueue = MatrixParallelQueue.Rent(package);
|
||||
return wrap;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -98,6 +98,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
|
||||
// TODO 构建全局的保底上下文
|
||||
foreach (MatrixParallelThread thread in _threads) { }
|
||||
if (contextMap.Keys.Count == 1)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
@@ -113,7 +114,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
|
||||
[LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
private static unsafe partial ulong CompareSIMDID(
|
||||
IntrinsicContext* context,
|
||||
|
||||
@@ -52,48 +52,12 @@ namespace StgSharp.Mathematics.Numeric
|
||||
* matrix_op_mode;
|
||||
*/
|
||||
|
||||
public enum MatrixOperationSpecial : short
|
||||
public enum MatrixIndexStyle : int
|
||||
{
|
||||
|
||||
SEQ_FMA = -1,
|
||||
|
||||
}
|
||||
|
||||
public enum MatrixIndexStyle : short
|
||||
{
|
||||
|
||||
SPECIAL = 0,
|
||||
NONE = 0,
|
||||
BUFFER_INDEX = 1,
|
||||
|
||||
}
|
||||
|
||||
public enum MatrixOperationParam : short
|
||||
{
|
||||
|
||||
RIGHT_ANS_PARAM = 1,
|
||||
LEFT_RIGHT_ANS_PARAM = 2,
|
||||
RIGHT_ANS_SCALAR_PARAM = 3,
|
||||
LEFT_RIGHT_ANS_SCALAR_PARAM = 4,
|
||||
ANS_SCALAR_PARAM = 5,
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct MatrixOpMode(
|
||||
MatrixIndexStyle style,
|
||||
MatrixOperationParam paramLayout
|
||||
)
|
||||
{
|
||||
|
||||
public MatrixIndexStyle OperationStyle = style;
|
||||
|
||||
public MatrixOperationParam ParameterStyle = paramLayout;
|
||||
|
||||
public MatrixOpMode(
|
||||
MatrixIndexStyle style,
|
||||
MatrixOperationSpecial paramLayout
|
||||
)
|
||||
: this(style, (MatrixOperationParam)paramLayout) { }
|
||||
SEQ_FMA = -1,
|
||||
|
||||
}
|
||||
|
||||
@@ -166,7 +130,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
[FieldOffset(96)]public int PrimCount; // 4
|
||||
[FieldOffset(100)]public int SecCount; // 4
|
||||
[FieldOffset(104)]public MatrixOpMode ComputeMode; // 4
|
||||
[FieldOffset(104)]public MatrixIndexStyle ComputeMode; // 4
|
||||
[FieldOffset(108)]private int ReservedInt0; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using StgSharp.HighPerformance;
|
||||
using StgSharp.Internal;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public partial class MatrixParallelThread
|
||||
{
|
||||
|
||||
internal static unsafe void ComputeFmaPanelF32(
|
||||
MatrixParallelTaskPackage* p
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
MatrixKernel* leftBuffer = (MatrixKernel*)p->Left;
|
||||
MatrixKernel* rightBuffer = (MatrixKernel*)p->Right;
|
||||
MatrixKernel* ansBuffer = (MatrixKernel*)p->Answer;
|
||||
ref long offsetRef = ref Unsafe.As<int, long>(ref p->PrimTileOffset);
|
||||
ref long lengthRef = ref Unsafe.As<int, long>(ref p->PrimCount);
|
||||
int ansWidth = p->AnsPrimOffset;
|
||||
int ansHeight = p->AnsSecOffset;
|
||||
int commonK = p->LeftPrimOffset;
|
||||
|
||||
M128* enumeration = stackalloc M128[1];
|
||||
enumeration->Member<int>(2) = commonK;
|
||||
enumeration->Member<int>(3) = ansHeight;
|
||||
|
||||
// in this occasion size must be 1
|
||||
for (long c = offsetRef; c < offsetRef + lengthRef; c++)
|
||||
{
|
||||
int i = (int)(c / ansHeight);
|
||||
int j = (int)(c % ansHeight);
|
||||
enumeration->Member<int>(0) = i;
|
||||
enumeration->Member<int>(1) = j;
|
||||
NativeIntrinsic.Context.mat[f32].kernel_tile_fma(leftBuffer, rightBuffer, ansBuffer,
|
||||
enumeration);
|
||||
}
|
||||
}
|
||||
|
||||
internal static unsafe void ComputeFmaPanelF64(
|
||||
MatrixParallelTaskPackage* p
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
|
||||
// int size = 1;
|
||||
MatrixKernel* leftBuffer = (MatrixKernel*)p->Left;
|
||||
MatrixKernel* rightBuffer = (MatrixKernel*)p->Right;
|
||||
MatrixKernel* ansBuffer = (MatrixKernel*)p->Answer;
|
||||
ref long offsetRef = ref Unsafe.As<int, long>(ref p->PrimTileOffset);
|
||||
ref long lengthRef = ref Unsafe.As<int, long>(ref p->PrimCount);
|
||||
int ansWidth = p->AnsPrimOffset;
|
||||
int ansHeight = p->AnsSecOffset;
|
||||
int commonK = p->LeftPrimOffset;
|
||||
|
||||
M128* enumeration = stackalloc M128[1];
|
||||
enumeration->Member<int>(2) = commonK;
|
||||
enumeration->Member<int>(3) = ansHeight;
|
||||
|
||||
// in this occasion size must be 1
|
||||
for (long c = offsetRef; c < offsetRef + lengthRef; c++)
|
||||
{
|
||||
int i = (int)(c / ansHeight);
|
||||
int j = (int)(c % ansHeight);
|
||||
enumeration->Member<int>(0) = i;
|
||||
enumeration->Member<int>(1) = j;
|
||||
NativeIntrinsic.Context.mat[f64].kernel_tile_fma(leftBuffer, rightBuffer, ansBuffer,
|
||||
enumeration);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,100 +34,41 @@ namespace StgSharp.Mathematics.Numeric
|
||||
public partial class MatrixParallelThread
|
||||
{
|
||||
|
||||
internal static unsafe void BufferComputeBinary(
|
||||
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;
|
||||
private const int KernelStealCount = 64;
|
||||
|
||||
((delegate* unmanaged[Cdecl]<void*, void*, void*, long, void>)p->ComputeHandle)((void*)(p->Left + offset),
|
||||
(void*)(p->Right + offset),
|
||||
(void*)(p->Answer + offset),
|
||||
count);
|
||||
MatrixParallelFactory.Release(p);
|
||||
}
|
||||
|
||||
internal static unsafe void BufferComputeBinaryScalar(
|
||||
MatrixParallelTaskPackage* p
|
||||
internal unsafe void BufferCompute(
|
||||
MatrixParallelTaskPackage* p
|
||||
)
|
||||
{
|
||||
// while () { }
|
||||
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->Answer + offset),
|
||||
scalar,
|
||||
count);
|
||||
MatrixParallelFactory.Release(p);
|
||||
IntPtr computeHandle = _context[p->ComputeHandle];
|
||||
((delegate* unmanaged[Cdecl]<void*, void*, void*, void*, long, void>)computeHandle)((void*)(p->Left + offset),
|
||||
(void*)(p->Right + offset),
|
||||
(void*)(p->Answer + offset),
|
||||
scalar,
|
||||
count);
|
||||
}
|
||||
|
||||
internal static unsafe void BufferComputeNoOperatorScalar(
|
||||
MatrixParallelTaskPackage* p
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe void SpecialBufferCompute(
|
||||
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;
|
||||
((delegate* unmanaged[Cdecl]<void*, ScalarPacket*, long, void>)p->ComputeHandle)((void*)(p->Answer + offset),
|
||||
p->Scalar, count);
|
||||
MatrixParallelFactory.Release(p);
|
||||
}
|
||||
|
||||
internal static unsafe void BufferComputeUnary(
|
||||
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;
|
||||
|
||||
((delegate* unmanaged[Cdecl]<void*, void*, long, void>)p->ComputeHandle)((void*)(p->Right + offset),
|
||||
(void*)(p->Answer + offset),
|
||||
count);
|
||||
MatrixParallelFactory.Release(p);
|
||||
}
|
||||
|
||||
internal static unsafe void BufferComputeUnaryScalar(
|
||||
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;
|
||||
((delegate* unmanaged[Cdecl]<void*, void*, ScalarPacket*, long, void>)p->ComputeHandle)((void*)(p->Right + offset),
|
||||
(void*)(p->Answer + offset),
|
||||
p->Scalar,
|
||||
count);
|
||||
MatrixParallelFactory.Release(p);
|
||||
}
|
||||
|
||||
internal static unsafe void SpecialBufferCompute(
|
||||
MatrixParallelTaskPackage* p
|
||||
)
|
||||
{
|
||||
switch ((MatrixOperationSpecial)p->ComputeMode.ParameterStyle)
|
||||
switch ((MatrixElementType)p->ElementSize)
|
||||
{
|
||||
case MatrixOperationSpecial.SEQ_FMA:
|
||||
switch ((MatrixElementType)p->ElementSize)
|
||||
{
|
||||
case MatrixElementType.F64:
|
||||
MatrixCompute.ComputeFmaPanelF64(p);
|
||||
return;
|
||||
case MatrixElementType.F32:
|
||||
MatrixCompute.ComputeFmaPanelF32(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
case MatrixElementType.F64:
|
||||
ComputeFmaPanelF64(p);
|
||||
return;
|
||||
case MatrixElementType.F32:
|
||||
ComputeFmaPanelF32(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
MatrixParallelFactory.Release(p);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -83,44 +83,6 @@ namespace StgSharp.Mathematics.Numeric
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void ExecuteTaskPackage(
|
||||
MatrixParallelQueue taskQueue
|
||||
)
|
||||
{
|
||||
MatrixParallelTaskPackage* p = taskQueue.Package;
|
||||
switch (p->ComputeMode.OperationStyle)
|
||||
{
|
||||
case MatrixIndexStyle.BUFFER_INDEX:
|
||||
switch (p->ComputeMode.ParameterStyle)
|
||||
{
|
||||
case MatrixOperationParam.RIGHT_ANS_PARAM:
|
||||
BufferComputeUnary(p);
|
||||
break;
|
||||
case MatrixOperationParam.LEFT_RIGHT_ANS_PARAM:
|
||||
BufferComputeBinary(p);
|
||||
break;
|
||||
case MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM:
|
||||
BufferComputeUnaryScalar(p);
|
||||
break;
|
||||
case MatrixOperationParam.LEFT_RIGHT_ANS_SCALAR_PARAM:
|
||||
BufferComputeBinaryScalar(p);
|
||||
break;
|
||||
case MatrixOperationParam.ANS_SCALAR_PARAM:
|
||||
BufferComputeNoOperatorScalar(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case MatrixIndexStyle.SPECIAL:
|
||||
SpecialBufferCompute(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Restart()
|
||||
{
|
||||
@@ -182,7 +144,19 @@ namespace StgSharp.Mathematics.Numeric
|
||||
// TODO 将外部线程绑定到当前线程上,并执行任务包
|
||||
} else
|
||||
{
|
||||
ExecuteTaskPackage(BindedWrap.TaskQueue);
|
||||
MatrixParallelQueue taskQueue = BindedWrap.TaskQueue;
|
||||
MatrixParallelTaskPackage* p = taskQueue.Package;
|
||||
if (0 > (int)p ->ComputeHandle) {
|
||||
SpecialBufferCompute(p);
|
||||
}
|
||||
switch (p->ComputeMode)
|
||||
{
|
||||
case MatrixIndexStyle.BUFFER_INDEX:
|
||||
BufferCompute(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
BindedWrap.ReportThreadAccomplished();
|
||||
}
|
||||
_managedThread.Priority = AfterWork;
|
||||
|
||||
Reference in New Issue
Block a user