diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs new file mode 100644 index 00000000..1d9b4a90 --- /dev/null +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs @@ -0,0 +1,138 @@ +//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// file="MatrixParallel.Execute.cs" +// Project: StgSharp +// 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 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.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace StgSharp.Mathematics +{ + internal static partial class MatrixParallel + { + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static unsafe void Compute(MatrixKernel* right, MatrixKernel* ans, IntPtr computeHandle) + { + ((delegate*)computeHandle)(right, ans); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static unsafe void Compute( + MatrixKernel* left, + MatrixKernel* right, + MatrixKernel* ans, + IntPtr computeHandle) + { + ((delegate*)computeHandle)(left, right, ans); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static unsafe void Compute( + MatrixKernel* right, + MatrixKernel* ans, + ScalarDataPacket* scalar, + IntPtr computeHandle) + { + ((delegate*)computeHandle)(right, ans, scalar); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static unsafe void Compute( + MatrixKernel* left, + MatrixKernel* right, + MatrixKernel* ans, + ScalarDataPacket* scalar, + IntPtr computeHandle) + { + ((delegate*)computeHandle)(left, right, ans, scalar); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static unsafe void ComputeRow( + MatrixKernel* right, + MatrixKernel* ans, + IntPtr computeHandle, + MatrixParallelTaskPackage* task) + { + long l = task->LeftEnum.secStride, r = task->RightEnum.secStride, a = task->ResultEnum.secStride, li = 0, ri = 0, ai = 0; + for (int i = 0; i < task->secCount; i++) { + ((delegate*)computeHandle)(right + ri, ans + ai); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static unsafe void ComputeRow( + MatrixKernel* right, + MatrixKernel* ans, + ScalarDataPacket* scalar, + IntPtr computeHandle, + MatrixParallelTaskPackage* task) + { + long l = task->LeftEnum.secStride, r = task->RightEnum.secStride, a = task->ResultEnum.secStride, li = 0, ri = 0, ai = 0; + for (int i = 0; i < task->secCount; i++) { + ((delegate*)computeHandle)(right + ri, ans + ai, scalar); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static unsafe void ComputeRow( + MatrixKernel* left, + MatrixKernel* right, + MatrixKernel* ans, + IntPtr computeHandle, + MatrixParallelTaskPackage* task) + { + long l = task->LeftEnum.secStride, r = task->RightEnum.secStride, a = task->ResultEnum.secStride, li = 0, ri = 0, ai = 0; + for (int i = 0; i < task->secCount; i++) { + ((delegate*)computeHandle)(left + li, right + ri, ans + ai); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static unsafe void ComputeRow( + MatrixKernel* left, + MatrixKernel* right, + MatrixKernel* ans, + ScalarDataPacket* scalar, + IntPtr computeHandle, + MatrixParallelTaskPackage* task) + { + long l = task->LeftEnum.secStride, r = task->RightEnum.secStride, a = task->ResultEnum.secStride, li = 0, ri = 0, ai = 0; + for (int i = 0; i < task->secCount; i++) { + ((delegate*)computeHandle)(left + li, right + ri, ans + ai, scalar); + } + } + + } +} diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs index 302f0384..50b43cfe 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs @@ -49,10 +49,9 @@ namespace StgSharp.Mathematics [FieldOffset(08)] public int begin; [FieldOffset(12)] public int end; - [FieldOffset(16)] public int primCount; + [FieldOffset(16)] public int offset; [FieldOffset(20)] public int primStride; - [FieldOffset(24)] public int secCount; - [FieldOffset(28)] public int secStride; + [FieldOffset(24)] public int secStride; } @@ -60,18 +59,18 @@ namespace StgSharp.Mathematics public unsafe struct MatrixParallelTaskPackage { - [FieldOffset(72)] public MatrixKernel* kernel; [FieldOffset(0)] public MatrixKernel* Left; - [FieldOffset(32)] public MatrixKernel* Result; - [FieldOffset(64)] public MatrixKernel* Right; - [FieldOffset(96)] public ScalarDataPacket* scalerPacket; - [FieldOffset(100)] public int ComputeMode; - [FieldOffset(104)] public int count; + [FieldOffset(24)] public MatrixKernel* Result; + [FieldOffset(48)] public MatrixKernel* Right; + [FieldOffset(72)] public ScalarDataPacket* scalerPacket; + [FieldOffset(80)] public int ComputeMode; + [FieldOffset(84)] public int primCount; + [FieldOffset(88)] public int secCount; [FieldOffset(108)] public IntPtr executionHandle; [FieldOffset(0)] public MatrixParallelEnumeration LeftEnum; - [FieldOffset(32)] public MatrixParallelEnumeration ResultEnum; - [FieldOffset(64)] public MatrixParallelEnumeration RightEnum; + [FieldOffset(24)] public MatrixParallelEnumeration ResultEnum; + [FieldOffset(48)] public MatrixParallelEnumeration RightEnum; } diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs index 8a63df64..942035ad 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs @@ -62,7 +62,7 @@ namespace StgSharp.Mathematics * we allow each wrap to contain either 3 or 4 threads, providing flexibility while maintaining efficiency. */ - internal static class MatrixParallel + internal static partial class MatrixParallel { private static Thread[] _threads; @@ -214,48 +214,28 @@ namespace StgSharp.Mathematics private static unsafe void ExecuteTaskPackage(MatrixParallelTaskPackage* p) { - MatrixKernel* left = p->left, right = p->left, result = p->result; + MatrixKernel* left = p->Left, right = p->Left, result = p->Result; + ref MatrixParallelEnumeration leftParam = ref p->LeftEnum, rightParam = ref p->RightEnum, resultParam = ref p->ResultEnum; int count = p->count; ScalarDataPacket* scalar = p->scalerPacket; switch (p->ComputeMode) { - case 0: - delegate* op_0 = (delegate*)p->executionHandle; - for (int i = 0; i < count; i++) - { - op_0(left, result); - left += p->leftStride; - result += p->resultStride; - } - break; - case 1: - delegate* op_1 = (delegate*)p->executionHandle; - for (int i = 0; i < count; i++) - { - op_1(left, result, scalar); - left += p->leftStride; - result += p->resultStride; - } + case 1: // Unary operation + for (int i = 0; i < length; i++) { } break; case 2: - delegate* op_2 = (delegate*)p->executionHandle; - for (int i = 0; i < count; i++) - { - op_2(left, right, result); - right += p->rightStride; - left += p->leftStride; - result += p->resultStride; - } break; case 3: - delegate* op_3 = (delegate*)p->executionHandle; - for (int i = 0; i < count; i++) - { - op_3(left, right, result, scalar); - right += p->rightStride; - left += p->leftStride; - result += p->resultStride; - } + break; + case 4: + break; + case -1: + break; + case -2: + break; + case -3: + break; + case -4: break; default: break;