namespace arrange; final wrap scheduale solution
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Matrix.Basic.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 System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.AccessControl;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static class MatrixCompute
|
||||
{
|
||||
|
||||
public static Matrix<float> Add(Matrix<float> left, Matrix<float> right)
|
||||
{
|
||||
if (left.ColumnLength != right.ColumnLength || left.RowLength != right.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
MatrixParallel.LeadParallel();
|
||||
try
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
finally
|
||||
{
|
||||
MatrixParallel.EndParallel();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixMarshal.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 System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics
|
||||
{
|
||||
public readonly ref struct MatrixMarshal { }
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// 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 System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static unsafe void Compute_Binary(MatrixParallelTaskPackageNonGeneric* p)
|
||||
{
|
||||
int eSize = p->ElementSize;
|
||||
nint l = p->Left, r = p->Right, a = p->Result;
|
||||
long
|
||||
lCount = (long)p->LeftPrimOffset * p->LeftPrimStride,
|
||||
rCount = (long)p->RightPrimOffset * p->RightPrimStride,
|
||||
aCount = (long)p->ResultPrimOffset * p->ResultPrimStride,
|
||||
pCount = p->PrimCount,
|
||||
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 = 0; i < pCount; i++)
|
||||
{
|
||||
lCount = i < lLimit ? lCount + p->LeftPrimOffset : 0;
|
||||
rCount = i < rLimit ? rCount + p->RightPrimOffset : 0;
|
||||
aCount = i < rLimit ? aCount + p->ResultPrimOffset : 0;
|
||||
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 = p->SecCount;
|
||||
long
|
||||
ll = sCount - p->LeftSecOffset,
|
||||
rl = sCount - p->RightSecOffset,
|
||||
al = sCount - p->ResultSecOffset;
|
||||
for (int j = 0; j < sCount; j++)
|
||||
{
|
||||
lc = j < ll ? lc + p->LeftSecOffset : 0;
|
||||
rc = j < rl ? rc + p->RightSecOffset : 0;
|
||||
ac = j < al ? ac + p->ResultSecOffset : 0;
|
||||
op(left + (nint)lc, right + (nint)rc, ans + (nint)ac);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static unsafe void Compute_BinaryScalar(MatrixParallelTaskPackageNonGeneric* p)
|
||||
{
|
||||
nint l = p->Left, r = p->Right, a = p->Result;
|
||||
long
|
||||
lCount = p->LeftPrimOffset * p->LeftPrimStride,
|
||||
rCount = p->RightPrimOffset * p->RightPrimStride,
|
||||
aCount = p->ResultPrimOffset * p->ResultPrimStride,
|
||||
pCount = p->PrimCount;
|
||||
long
|
||||
lLimit = pCount - p->LeftPrimOffset,
|
||||
rLimit = pCount - p->RightPrimOffset,
|
||||
aLimit = pCount - p->ResultPrimOffset;
|
||||
delegate* unmanaged[Cdecl]<nint, nint, nint, ScalarPacket*, void> op =
|
||||
(delegate* unmanaged[Cdecl]<nint, nint, nint, ScalarPacket*, void>)p->ComputeHandle;
|
||||
ScalarPacket* scalar = p->Scalar;
|
||||
for (long i = 0; i < pCount; i++)
|
||||
{
|
||||
lCount = i < lLimit ? lCount + p->LeftPrimOffset : 0;
|
||||
rCount = i < rLimit ? rCount + p->RightPrimOffset : 0;
|
||||
aCount = i < rLimit ? aCount + p->ResultPrimOffset : 0;
|
||||
nint
|
||||
left = l + (nint)lCount,
|
||||
right = r + (nint)rCount,
|
||||
ans = a + (nint)aCount;
|
||||
long
|
||||
lc = p->LeftSecOffset * p->LeftSecStride,
|
||||
rc = p->RightSecOffset * p->RightSecStride,
|
||||
ac = p->ResultSecOffset * p->ResultSecStride,
|
||||
sCount = p->SecCount,
|
||||
ll = sCount - p->LeftSecOffset,
|
||||
rl = sCount - p->RightSecOffset,
|
||||
al = sCount - p->ResultSecOffset;
|
||||
for (int j = 0; j < sCount; j++)
|
||||
{
|
||||
lc = j < ll ? lc + p->LeftSecOffset : 0;
|
||||
rc = j < rl ? rc + p->RightSecOffset : 0;
|
||||
ac = j < al ? ac + p->ResultSecOffset : 0;
|
||||
op(left + (nint)lc, right + (nint)rc, ans + (nint)ac, scalar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static unsafe void Compute_Unary(MatrixParallelTaskPackageNonGeneric* p)
|
||||
{
|
||||
nint s = p->Right, a = p->Result;
|
||||
long rCount = p->RightPrimOffset * p->RightPrimStride,
|
||||
aCount = p->ResultPrimOffset * p->ResultPrimStride,
|
||||
pCount = p->PrimCount;
|
||||
long rLimit = pCount - p->RightPrimOffset,
|
||||
aLimit = pCount - p->ResultPrimOffset;
|
||||
delegate* unmanaged[Cdecl]<nint, nint, void> op =
|
||||
(delegate* unmanaged[Cdecl]<nint, nint, void>)p->ComputeHandle;
|
||||
for (long i = 0; i < pCount; i++)
|
||||
{
|
||||
rCount = i < rLimit ? rCount + p->RightPrimOffset : 0;
|
||||
aCount = i < rLimit ? aCount + p->ResultPrimOffset : 0;
|
||||
nint source = s + (nint)rCount, ans = a + (nint)aCount;
|
||||
long rc = p->RightSecOffset * p->RightSecStride,
|
||||
ac = p->ResultSecOffset * p->ResultSecStride,
|
||||
sCount = p->SecCount;
|
||||
long rl = sCount - p->RightSecOffset,
|
||||
al = sCount - p->ResultSecOffset;
|
||||
for (int j = 0; j < sCount; j++)
|
||||
{
|
||||
rc = j < rl ? rc + p->RightSecOffset : 0;
|
||||
ac = j < al ? ac + p->ResultSecOffset : 0;
|
||||
op(source + (nint)rc, ans + (nint)ac);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static unsafe void Compute_UnaryScalar(MatrixParallelTaskPackageNonGeneric* p)
|
||||
{
|
||||
nint s = p->Right, a = p->Result;
|
||||
long rCount = p->RightPrimOffset * p->RightPrimStride,
|
||||
aCount = p->ResultPrimOffset * p->ResultPrimStride,
|
||||
pCount = p->PrimCount;
|
||||
long rLimit = pCount - p->RightPrimOffset,
|
||||
aLimit = pCount - p->ResultPrimOffset;
|
||||
delegate* unmanaged[Cdecl]<nint, nint, ScalarPacket*, void> op =
|
||||
(delegate* unmanaged[Cdecl]<nint, nint, ScalarPacket*, void>)p->ComputeHandle;
|
||||
ScalarPacket* scalar = p->Scalar;
|
||||
for (long i = 0; i < pCount; i++)
|
||||
{
|
||||
rCount = i < rLimit ? rCount + p->RightPrimOffset : 0;
|
||||
aCount = i < rLimit ? aCount + p->ResultPrimOffset : 0;
|
||||
nint source = s + (nint)rCount;
|
||||
nint ans = a + (nint)aCount;
|
||||
long rc = p->RightSecOffset * p->RightSecStride,
|
||||
ac = p->ResultSecOffset * p->ResultSecStride,
|
||||
sCount = p->SecCount;
|
||||
long rl = sCount - p->RightSecOffset,
|
||||
al = sCount - p->ResultSecOffset;
|
||||
for (int j = 0; j < sCount; j++)
|
||||
{
|
||||
rc = j < rl ? rc + p->RightSecOffset : 0;
|
||||
ac = j < al ? ac + p->ResultSecOffset : 0;
|
||||
op(source + (nint)rc, ans + (nint)ac, scalar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,833 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.Publish.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.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
public static unsafe void PublicTask<T>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation)
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Right = source.Source;
|
||||
package->Result = ans.Source;
|
||||
package->RightPrimOffset = source.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->RightPrimStride = source.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->RightSecOffset = source.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->RightSecStride = source.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = source.PrimSize;
|
||||
package->SecCount = source.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 1; // Unary
|
||||
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation)
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Left = left.Source;
|
||||
package->Right = right.Source;
|
||||
package->Result = ans.Source;
|
||||
package->LeftPrimOffset = left.PrimOffset;
|
||||
package->RightPrimOffset = right.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->LeftPrimStride = left.PrimStride;
|
||||
package->RightPrimStride = right.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->LeftSecOffset = left.SecondaryOffset;
|
||||
package->RightSecOffset = right.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->LeftSecStride = left.SecondaryStride;
|
||||
package->RightSecStride = right.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = left.PrimSize;
|
||||
package->SecCount = left.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 3; // Binary
|
||||
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
// Unary with 1..8 scalars (each scalar is independent generic type)
|
||||
public static unsafe void PublicTask<T, TScalar0>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Right = source.Source;
|
||||
package->Result = ans.Source;
|
||||
package->RightPrimOffset = source.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->RightPrimStride = source.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->RightSecOffset = source.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->RightSecStride = source.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = source.PrimSize;
|
||||
package->SecCount = source.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 2; // UnaryScalar
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Right = source.Source;
|
||||
package->Result = ans.Source;
|
||||
package->RightPrimOffset = source.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->RightPrimStride = source.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->RightSecOffset = source.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->RightSecStride = source.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = source.PrimSize;
|
||||
package->SecCount = source.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 2;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
// Binary with 1..8 scalars (each scalar is independent generic type)
|
||||
public static unsafe void PublicTask<T, TScalar0>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Left = left.Source;
|
||||
package->Right = right.Source;
|
||||
package->Result = ans.Source;
|
||||
package->LeftPrimOffset = left.PrimOffset;
|
||||
package->RightPrimOffset = right.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->LeftPrimStride = left.PrimStride;
|
||||
package->RightPrimStride = right.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->LeftSecOffset = left.SecondaryOffset;
|
||||
package->RightSecOffset = right.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->LeftSecStride = left.SecondaryStride;
|
||||
package->RightSecStride = right.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = left.PrimSize;
|
||||
package->SecCount = left.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 4; // BinaryScalar
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Right = source.Source;
|
||||
package->Result = ans.Source;
|
||||
package->RightPrimOffset = source.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->RightPrimStride = source.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->RightSecOffset = source.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->RightSecStride = source.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = source.PrimSize;
|
||||
package->SecCount = source.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 2;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Left = left.Source;
|
||||
package->Right = right.Source;
|
||||
package->Result = ans.Source;
|
||||
package->LeftPrimOffset = left.PrimOffset;
|
||||
package->RightPrimOffset = right.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->LeftPrimStride = left.PrimStride;
|
||||
package->RightPrimStride = right.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->LeftSecOffset = left.SecondaryOffset;
|
||||
package->RightSecOffset = right.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->LeftSecStride = left.SecondaryStride;
|
||||
package->RightSecStride = right.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = left.PrimSize;
|
||||
package->SecCount = left.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 4;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
where TScalar3: unmanaged, INumber<TScalar3>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Right = source.Source;
|
||||
package->Result = ans.Source;
|
||||
package->RightPrimOffset = source.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->RightPrimStride = source.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->RightSecOffset = source.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->RightSecStride = source.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = source.PrimSize;
|
||||
package->SecCount = source.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 2;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
sp->Data<TScalar3>(3) = s3;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Left = left.Source;
|
||||
package->Right = right.Source;
|
||||
package->Result = ans.Source;
|
||||
package->LeftPrimOffset = left.PrimOffset;
|
||||
package->RightPrimOffset = right.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->LeftPrimStride = left.PrimStride;
|
||||
package->RightPrimStride = right.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->LeftSecOffset = left.SecondaryOffset;
|
||||
package->RightSecOffset = right.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->LeftSecStride = left.SecondaryStride;
|
||||
package->RightSecStride = right.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = left.PrimSize;
|
||||
package->SecCount = left.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 4;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
where TScalar3: unmanaged, INumber<TScalar3>
|
||||
where TScalar4: unmanaged, INumber<TScalar4>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Right = source.Source;
|
||||
package->Result = ans.Source;
|
||||
package->RightPrimOffset = source.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->RightPrimStride = source.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->RightSecOffset = source.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->RightSecStride = source.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = source.PrimSize;
|
||||
package->SecCount = source.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 2;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
sp->Data<TScalar3>(3) = s3;
|
||||
sp->Data<TScalar4>(4) = s4;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
where TScalar3: unmanaged, INumber<TScalar3>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Left = left.Source;
|
||||
package->Right = right.Source;
|
||||
package->Result = ans.Source;
|
||||
package->LeftPrimOffset = left.PrimOffset;
|
||||
package->RightPrimOffset = right.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->LeftPrimStride = left.PrimStride;
|
||||
package->RightPrimStride = right.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->LeftSecOffset = left.SecondaryOffset;
|
||||
package->RightSecOffset = right.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->LeftSecStride = left.SecondaryStride;
|
||||
package->RightSecStride = right.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = left.PrimSize;
|
||||
package->SecCount = left.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 4;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
sp->Data<TScalar3>(3) = s3;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
where TScalar3: unmanaged, INumber<TScalar3>
|
||||
where TScalar4: unmanaged, INumber<TScalar4>
|
||||
where TScalar5: unmanaged, INumber<TScalar5>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Right = source.Source;
|
||||
package->Result = ans.Source;
|
||||
package->RightPrimOffset = source.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->RightPrimStride = source.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->RightSecOffset = source.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->RightSecStride = source.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = source.PrimSize;
|
||||
package->SecCount = source.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 2;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
sp->Data<TScalar3>(3) = s3;
|
||||
sp->Data<TScalar4>(4) = s4;
|
||||
sp->Data<TScalar5>(5) = s5;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
where TScalar3: unmanaged, INumber<TScalar3>
|
||||
where TScalar4: unmanaged, INumber<TScalar4>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Left = left.Source;
|
||||
package->Right = right.Source;
|
||||
package->Result = ans.Source;
|
||||
package->LeftPrimOffset = left.PrimOffset;
|
||||
package->RightPrimOffset = right.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->LeftPrimStride = left.PrimStride;
|
||||
package->RightPrimStride = right.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->LeftSecOffset = left.SecondaryOffset;
|
||||
package->RightSecOffset = right.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->LeftSecStride = left.SecondaryStride;
|
||||
package->RightSecStride = right.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = left.PrimSize;
|
||||
package->SecCount = left.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 4;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
sp->Data<TScalar3>(3) = s3;
|
||||
sp->Data<TScalar4>(4) = s4;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
where TScalar3: unmanaged, INumber<TScalar3>
|
||||
where TScalar4: unmanaged, INumber<TScalar4>
|
||||
where TScalar5: unmanaged, INumber<TScalar5>
|
||||
where TScalar6: unmanaged, INumber<TScalar6>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Right = source.Source;
|
||||
package->Result = ans.Source;
|
||||
package->RightPrimOffset = source.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->RightPrimStride = source.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->RightSecOffset = source.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->RightSecStride = source.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = source.PrimSize;
|
||||
package->SecCount = source.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 2;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
sp->Data<TScalar3>(3) = s3;
|
||||
sp->Data<TScalar4>(4) = s4;
|
||||
sp->Data<TScalar5>(5) = s5;
|
||||
sp->Data<TScalar6>(6) = s6;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
where TScalar3: unmanaged, INumber<TScalar3>
|
||||
where TScalar4: unmanaged, INumber<TScalar4>
|
||||
where TScalar5: unmanaged, INumber<TScalar5>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Left = left.Source;
|
||||
package->Right = right.Source;
|
||||
package->Result = ans.Source;
|
||||
package->LeftPrimOffset = left.PrimOffset;
|
||||
package->RightPrimOffset = right.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->LeftPrimStride = left.PrimStride;
|
||||
package->RightPrimStride = right.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->LeftSecOffset = left.SecondaryOffset;
|
||||
package->RightSecOffset = right.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->LeftSecStride = left.SecondaryStride;
|
||||
package->RightSecStride = right.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = left.PrimSize;
|
||||
package->SecCount = left.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 4;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
sp->Data<TScalar3>(3) = s3;
|
||||
sp->Data<TScalar4>(4) = s4;
|
||||
sp->Data<TScalar5>(5) = s5;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6, TScalar7>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6,
|
||||
TScalar7 s7)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
where TScalar3: unmanaged, INumber<TScalar3>
|
||||
where TScalar4: unmanaged, INumber<TScalar4>
|
||||
where TScalar5: unmanaged, INumber<TScalar5>
|
||||
where TScalar6: unmanaged, INumber<TScalar6>
|
||||
where TScalar7: unmanaged, INumber<TScalar7>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Right = source.Source;
|
||||
package->Result = ans.Source;
|
||||
package->RightPrimOffset = source.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->RightPrimStride = source.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->RightSecOffset = source.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->RightSecStride = source.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = source.PrimSize;
|
||||
package->SecCount = source.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 2;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
sp->Data<TScalar3>(3) = s3;
|
||||
sp->Data<TScalar4>(4) = s4;
|
||||
sp->Data<TScalar5>(5) = s5;
|
||||
sp->Data<TScalar6>(6) = s6;
|
||||
sp->Data<TScalar7>(7) = s7;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
where TScalar3: unmanaged, INumber<TScalar3>
|
||||
where TScalar4: unmanaged, INumber<TScalar4>
|
||||
where TScalar5: unmanaged, INumber<TScalar5>
|
||||
where TScalar6: unmanaged, INumber<TScalar6>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Left = left.Source;
|
||||
package->Right = right.Source;
|
||||
package->Result = ans.Source;
|
||||
package->LeftPrimOffset = left.PrimOffset;
|
||||
package->RightPrimOffset = right.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->LeftPrimStride = left.PrimStride;
|
||||
package->RightPrimStride = right.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->LeftSecOffset = left.SecondaryOffset;
|
||||
package->RightSecOffset = right.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->LeftSecStride = left.SecondaryStride;
|
||||
package->RightSecStride = right.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = left.PrimSize;
|
||||
package->SecCount = left.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 4;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
sp->Data<TScalar3>(3) = s3;
|
||||
sp->Data<TScalar4>(4) = s4;
|
||||
sp->Data<TScalar5>(5) = s5;
|
||||
sp->Data<TScalar6>(6) = s6;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe void PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6, TScalar7>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6,
|
||||
TScalar7 s7)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
where TScalar2: unmanaged, INumber<TScalar2>
|
||||
where TScalar3: unmanaged, INumber<TScalar3>
|
||||
where TScalar4: unmanaged, INumber<TScalar4>
|
||||
where TScalar5: unmanaged, INumber<TScalar5>
|
||||
where TScalar6: unmanaged, INumber<TScalar6>
|
||||
where TScalar7: unmanaged, INumber<TScalar7>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
package->Left = left.Source;
|
||||
package->Right = right.Source;
|
||||
package->Result = ans.Source;
|
||||
package->LeftPrimOffset = left.PrimOffset;
|
||||
package->RightPrimOffset = right.PrimOffset;
|
||||
package->ResultPrimOffset = ans.PrimOffset;
|
||||
package->LeftPrimStride = left.PrimStride;
|
||||
package->RightPrimStride = right.PrimStride;
|
||||
package->ResultPrimStride = ans.PrimStride;
|
||||
package->LeftSecOffset = left.SecondaryOffset;
|
||||
package->RightSecOffset = right.SecondaryOffset;
|
||||
package->ResultSecOffset = ans.SecondaryOffset;
|
||||
package->LeftSecStride = left.SecondaryStride;
|
||||
package->RightSecStride = right.SecondaryStride;
|
||||
package->ResultSecStride = ans.SecondaryStride;
|
||||
package->PrimCount = left.PrimSize;
|
||||
package->SecCount = left.SecondarySize;
|
||||
package->ComputeHandle = (nint)Operation;
|
||||
package->ElementSize = sizeof(T);
|
||||
package->ComputeMode = 4;
|
||||
ScalarPacket* sp = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
sp->Data<TScalar0>(0) = s0;
|
||||
sp->Data<TScalar1>(1) = s1;
|
||||
sp->Data<TScalar2>(2) = s2;
|
||||
sp->Data<TScalar3>(3) = s3;
|
||||
sp->Data<TScalar4>(4) = s4;
|
||||
sp->Data<TScalar5>(5) = s5;
|
||||
sp->Data<TScalar6>(6) = s6;
|
||||
sp->Data<TScalar7>(7) = s7;
|
||||
package->Scalar = sp;
|
||||
PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
private static unsafe void PublicTaskPrivate<T>(MatrixParallelTaskPackage<T>* package)
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
int primCount = package->PrimCount;
|
||||
int secCount = package->SecCount;
|
||||
int tileWidth = (1024 / secCount) + 1;
|
||||
int tileCount = (primCount / tileWidth) + 1;
|
||||
|
||||
int tilePerThreadLess = tileCount / Wraps.Length;
|
||||
int tileToSlice = tileCount % Wraps.Length;
|
||||
|
||||
// TODO 这个方法依然是独占,需要一个新的缓存类型用来辅助复用空线程束
|
||||
for (int i = 0; i < Wraps.Length; i++) { }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.Task.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.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public unsafe struct ScalarPacket
|
||||
{
|
||||
|
||||
private fixed ulong data[8];
|
||||
|
||||
public ref T Data<T>(int index) where T: unmanaged,INumber<T>
|
||||
{
|
||||
if (index is < 0 or >= 8) {
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
|
||||
return ref Unsafe.As<ulong, T>(ref data[index]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly unsafe struct MatrixParallelTaskPackageNonGeneric
|
||||
{
|
||||
|
||||
#region source
|
||||
|
||||
// Group 1: Left/Right matrix kernel base address
|
||||
public readonly IntPtr Left; // 8
|
||||
public readonly IntPtr Right; // 8
|
||||
|
||||
#endregion
|
||||
|
||||
#region ans
|
||||
|
||||
// Group 2: Result kernel base address + reserved
|
||||
public readonly IntPtr Result; // 8
|
||||
public readonly int ElementSize;
|
||||
public readonly int ReservedPtr0; // 4 (extension/reserved)
|
||||
|
||||
#endregion
|
||||
|
||||
#region left enum
|
||||
|
||||
public readonly int LeftPrimOffset; // 4
|
||||
public readonly int LeftPrimStride; // 4
|
||||
public readonly int LeftSecOffset; // 4
|
||||
public readonly int LeftSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region right enum
|
||||
|
||||
public readonly int RightPrimOffset; // 4
|
||||
public readonly int RightPrimStride; // 4
|
||||
public readonly int RightSecOffset; // 4
|
||||
public readonly int RightSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region ans enum
|
||||
|
||||
public readonly int ResultPrimOffset; // 4
|
||||
public readonly int ResultPrimStride; // 4
|
||||
public readonly int ResultSecOffset; // 4
|
||||
public readonly int ResultSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region scalar/compute
|
||||
|
||||
public readonly ScalarPacket* Scalar; // 8
|
||||
public readonly IntPtr ComputeHandle; // 8
|
||||
|
||||
#endregion
|
||||
|
||||
#region global profile
|
||||
|
||||
public readonly int PrimCount; // 4
|
||||
public readonly int ComputeMode; // 4
|
||||
public readonly int SecCount; // 4
|
||||
private readonly int ReservedInt0; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region reserve
|
||||
|
||||
public readonly long ReservedLong0; // 8
|
||||
public readonly long ReservedLong1; // 8
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal unsafe struct MatrixParallelTaskPackage<T> where T: unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
public MatrixParallelTaskPackage()
|
||||
{
|
||||
Unsafe.SkipInit(out this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void Copy(MatrixParallelTaskPackage<T>* source, MatrixParallelTaskPackage<T>* target)
|
||||
{
|
||||
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTaskPackage<T>), sizeof(MatrixParallelTaskPackage<T>));
|
||||
}
|
||||
|
||||
#region source
|
||||
|
||||
// Group 1: Left/Right matrix kernel base address
|
||||
public MatrixKernel<T>* Left; // 8
|
||||
public MatrixKernel<T>* Right; // 8
|
||||
|
||||
#endregion
|
||||
|
||||
#region ans
|
||||
|
||||
// Group 2: Result kernel base address + reserved
|
||||
public MatrixKernel<T>* Result; // 8
|
||||
public int ElementSize = sizeof(T);
|
||||
public int ReservedPtr0; // 4 (extension/reserved)
|
||||
|
||||
#endregion
|
||||
|
||||
#region left enum
|
||||
|
||||
public int LeftPrimOffset; // 4
|
||||
public int LeftPrimStride; // 4
|
||||
public int LeftSecOffset; // 4
|
||||
public int LeftSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region right enum
|
||||
|
||||
public int RightPrimOffset; // 4
|
||||
public int RightPrimStride; // 4
|
||||
public int RightSecOffset; // 4
|
||||
public int RightSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region ans enum
|
||||
|
||||
public int ResultPrimOffset; // 4
|
||||
public int ResultPrimStride; // 4
|
||||
public int ResultSecOffset; // 4
|
||||
public int ResultSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region scalar/compute
|
||||
|
||||
public ScalarPacket* Scalar; // 8
|
||||
public IntPtr ComputeHandle; // 8
|
||||
|
||||
#endregion
|
||||
|
||||
#region global profile
|
||||
|
||||
public int PrimCount; // 4
|
||||
public int ComputeMode; // 4
|
||||
public int SecCount; // 4
|
||||
private readonly int ReservedInt0; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region reserve
|
||||
|
||||
public long ReservedLong0; // 8
|
||||
public long ReservedLong1; // 8
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.TaskFactory.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 StgSharp.HighPerformance.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using hlsfAllocator = StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
internal static unsafe class MatrixParallelFactory
|
||||
{
|
||||
|
||||
private static hlsfAllocator _intermediateResult;
|
||||
|
||||
private static
|
||||
#if NET9_0_OR_GREATER
|
||||
Lock
|
||||
#else
|
||||
object
|
||||
#endif
|
||||
_hlsfLock = new();
|
||||
|
||||
private static SlabAllocator<ScalarPacket> _scalarAllocator ;
|
||||
|
||||
private static SlabAllocator<MatrixParallelTaskPackageNonGeneric> _wrapAllocator;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static MatrixParallelTaskPackage<T>* CreateBaseTask<T>() where T: unmanaged, INumber<T>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* p = (MatrixParallelTaskPackage<T>*)_wrapAllocator.Allocate();
|
||||
Span<byte> s = new Span<byte>((byte*)p, sizeof(MatrixParallelTaskPackage<T>));
|
||||
s.Fill(0);
|
||||
return p;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ScalarPacket* CreateScalarPacket()
|
||||
{
|
||||
ScalarPacket* p = (ScalarPacket*)_scalarAllocator.Allocate();
|
||||
return p;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe MatrixParallelTaskPackage<T>* FromExistPackage<T>(MatrixParallelTaskPackage<T>* exist)
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* ptr = (MatrixParallelTaskPackage<T>*)_wrapAllocator.Allocate();
|
||||
MatrixParallelTaskPackage<T>.Copy(exist, ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
public static void Release(ScalarPacket* source)
|
||||
{
|
||||
_scalarAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Release(MatrixParallelTaskPackageNonGeneric* source)
|
||||
{
|
||||
if (source->Scalar != null) {
|
||||
_scalarAllocator.Free((nuint)source->Scalar);
|
||||
}
|
||||
_wrapAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Release<T>(MatrixParallelTaskPackage<T>* source) where T: unmanaged, INumber<T>
|
||||
{
|
||||
if (source->Scalar != null) {
|
||||
_scalarAllocator.Free((nuint)source->Scalar);
|
||||
}
|
||||
_wrapAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static void Init()
|
||||
{
|
||||
if (_intermediateResult is null)
|
||||
{
|
||||
_intermediateResult = new hlsfAllocator(64 * 16 * 16);
|
||||
_scalarAllocator = SlabAllocator<ScalarPacket>.Create(64 * 1024, SlabBufferLayout.Chunked);
|
||||
_wrapAllocator = SlabAllocator<MatrixParallelTaskPackageNonGeneric>.
|
||||
Create(128 * 1024, SlabBufferLayout.Chunked);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,362 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.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 System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
/*
|
||||
* Thread wrapping to accelerate matrix operations by dividing threads into groups (wraps).
|
||||
* For M wraps, each containing an average of N threads, we satisfy the condition M * N = T - 4,
|
||||
* where T is the total thread count of the CPU. Typically, for modern consumer CPUs, T is around 16.
|
||||
* For simplicity, let M * N = 16.
|
||||
*
|
||||
* The total time complexity for publishing tasks is O(M + N):
|
||||
* - O(M) accounts for distributing tasks across M wraps.
|
||||
* - O(N) accounts for threads within each wrap fetching tasks from the wrap queue.
|
||||
*
|
||||
* Using the inequality Sqrt(M * N) <= (M + N) / 2 (derived from the arithmetic-geometric mean inequality),
|
||||
* the optimal configuration occurs when M = N. For M * N = 16, this gives M = N = 4.
|
||||
*
|
||||
* To ensure compatibility across a wide range of CPU architectures,
|
||||
* we allow each wrap to contain either 3 or 4 threads, providing flexibility while maintaining efficiency.
|
||||
*/
|
||||
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private static Thread[] _threads;
|
||||
private static readonly (int a, int b)[] LookupTable = [
|
||||
(1, 1), // 7
|
||||
(0, 2), // 8 (no non-zero solution)
|
||||
(3, 0), // 9 (no non-zero solution)
|
||||
(2, 1), // 10
|
||||
(1, 2), // 11
|
||||
(0, 3), // 12 (no non-zero solution)
|
||||
(3, 1), // 13
|
||||
(2, 2), // 14
|
||||
(1, 3), // 15
|
||||
(4, 1), // 16
|
||||
(3, 2), // 17
|
||||
(2, 3), // 18
|
||||
(1, 4), // 19
|
||||
(4, 2), // 20
|
||||
];
|
||||
private static bool _meaningful;
|
||||
|
||||
private static volatile int RemainThreadCount = 0;
|
||||
private static readonly object _lock = new();
|
||||
private static ThreadPriority _AfterWork = ThreadPriority.Lowest;
|
||||
|
||||
public static bool SupportParallel { get; } = Environment.ProcessorCount > 6;
|
||||
|
||||
public static bool IsParallelMeaningful => Environment.ProcessorCount > 6 && IsInitialized;
|
||||
|
||||
public static MatrixParallelWrap.Handle LeaderHandle { get; private/**/ set; }
|
||||
|
||||
private static bool IsInitialized { get; set; } = false;
|
||||
|
||||
private static Queue<IntPtr> LeaderTask { get; set; }
|
||||
|
||||
public static void EndParallel()
|
||||
{
|
||||
Monitor.Exit(_lock);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static (int count_3, int count_4) FastDecompose(int c)
|
||||
{
|
||||
if (c < 7) {
|
||||
return (0, 0);
|
||||
}
|
||||
|
||||
int t = c - 7;
|
||||
int k = t / 14;
|
||||
int d = t % 14;
|
||||
|
||||
(int a0, int b0) = LookupTable[d];
|
||||
|
||||
return (a0 + (2 * k), b0 + (2 * k));
|
||||
}
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
if (IsInitialized) {
|
||||
return;
|
||||
}
|
||||
MatrixParallelFactory.Init();
|
||||
if (!SupportParallel)
|
||||
{
|
||||
IsInitialized = true;
|
||||
return;
|
||||
}
|
||||
int count = Environment.ProcessorCount - 4;
|
||||
count = count == 5 ? 4 : count;
|
||||
_threads = new Thread[count];
|
||||
if (count < 6)
|
||||
{
|
||||
if (count == 3)
|
||||
{
|
||||
Wraps = [ new MatrixParallelWrap(0, count, true) ];
|
||||
(LargeWrapCount, SmallWrapCount) = (0, 1);
|
||||
LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]);
|
||||
} else
|
||||
{
|
||||
count = 4;
|
||||
Wraps = [ new MatrixParallelWrap(0, count, true) ];
|
||||
(LargeWrapCount, SmallWrapCount) = (1, 0);
|
||||
LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]);
|
||||
}
|
||||
} else if (count == 6)
|
||||
{
|
||||
Wraps = [
|
||||
new MatrixParallelWrap(0, 3, true),
|
||||
new MatrixParallelWrap(3, 3, false)
|
||||
];
|
||||
(LargeWrapCount, SmallWrapCount) = (0, 2);
|
||||
LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]);
|
||||
} else
|
||||
{
|
||||
(int _3wrap, int _4wrap) = FastDecompose(count);
|
||||
Wraps = new MatrixParallelWrap[_4wrap + _3wrap];
|
||||
int id = 0,idx = 0;
|
||||
for (; idx < _3wrap; idx++, id += 3) {
|
||||
Wraps[idx] = new MatrixParallelWrap(idx, 3, id == 0);
|
||||
}
|
||||
for (; idx < _3wrap + _4wrap; idx++, id += 4) {
|
||||
Wraps[idx] = new MatrixParallelWrap(id, 4, id == 0);
|
||||
}
|
||||
LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]);
|
||||
}
|
||||
LeaderTask = LeaderHandle.Wrap.GetWorkerTaskQueue(0);
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
public static void LaunchParallel(IEnumerable<MatrixParallelWrap> wraps, SleepMode afterWork)
|
||||
{
|
||||
RemainThreadCount = _threads.Length;
|
||||
_AfterWork = (ThreadPriority)afterWork;
|
||||
SetAllWrap(wraps);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void LeadParallel()
|
||||
{
|
||||
Monitor.Enter(_lock);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void MatrixParallelLeaderWork()
|
||||
{
|
||||
MatrixParallelWrap.ExecuteTaskPackage(LeaderTask);
|
||||
_ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ResetThreadCount()
|
||||
{
|
||||
RemainThreadCount = _threads.Length;
|
||||
}
|
||||
|
||||
public static void SetAllWrap(IEnumerable<MatrixParallelWrap> wraps)
|
||||
{
|
||||
ResetThreadCount();
|
||||
foreach (MatrixParallelWrap item in wraps) {
|
||||
item.SchedulerReset.Set();
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe class MatrixParallelWrap
|
||||
{
|
||||
|
||||
private readonly Queue<IntPtr>[] _cache;
|
||||
private readonly Thread[] _threads;
|
||||
private readonly ManualResetEventSlim[] _workerReset;
|
||||
|
||||
public MatrixParallelWrap(int id, int wrapSize, bool isFirst)
|
||||
{
|
||||
ID = id;
|
||||
int begin = isFirst ? 1 : 0, end = wrapSize - 1;
|
||||
_cache = new Queue<IntPtr>[wrapSize];
|
||||
_workerReset = new ManualResetEventSlim[end];
|
||||
_threads = new Thread[wrapSize];
|
||||
for (int i = 0; i < end; i++) {
|
||||
_workerReset[i] = new ManualResetEventSlim(false);
|
||||
}
|
||||
for (int i = 0; i < _cache.Length; i++) {
|
||||
_cache[i] = new Queue<IntPtr>(8);
|
||||
}
|
||||
for (int i = begin; i < end; i++)
|
||||
{
|
||||
_threads[i] = new Thread(MatrixParallelWorker)
|
||||
{
|
||||
IsBackground = true,
|
||||
Priority = ThreadPriority.BelowNormal,
|
||||
Name = $"MPT#{ID}_{i}"
|
||||
};
|
||||
_threads[i].Start(new Handle(i, this));
|
||||
}
|
||||
_threads[end] = new Thread(MatrixParallelScheduler)
|
||||
{
|
||||
IsBackground = true,
|
||||
Priority = ThreadPriority.BelowNormal,
|
||||
Name = $"MPT#{ID}_{end}*"
|
||||
};
|
||||
_threads[end].Start(new Handle(end, this));
|
||||
}
|
||||
|
||||
public int ID { get; init; }
|
||||
|
||||
internal ManualResetEventSlim SchedulerReset { get; } = new(false);
|
||||
|
||||
public Span<IntPtr> EnqueuePackage(Span<IntPtr> packageSpan)
|
||||
{
|
||||
int idx = 0;
|
||||
for (; idx < _cache.Length; idx++) {
|
||||
_cache[idx].Enqueue(packageSpan[idx]);
|
||||
}
|
||||
return packageSpan[idx..];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ManualResetEventSlim GetWorkerResetEvent(int id)
|
||||
{
|
||||
return _workerReset[id];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Queue<IntPtr> GetWorkerTaskQueue(int idInWrap)
|
||||
{
|
||||
return _cache[idInWrap];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetAllWorker()
|
||||
{
|
||||
foreach (ManualResetEventSlim item in _workerReset) {
|
||||
item.Set();
|
||||
}
|
||||
}
|
||||
|
||||
public record class Handle(int InWrapIndex, MatrixParallelWrap Wrap);
|
||||
|
||||
#region ThreadOperation
|
||||
|
||||
private static unsafe void MatrixParallelWorker([AllowNull] object obj)
|
||||
{
|
||||
Handle handle = (obj as Handle)!;
|
||||
MatrixParallelWrap wrap = handle.Wrap;
|
||||
int inWrapIndex = handle.InWrapIndex;
|
||||
Thread self = Thread.CurrentThread;
|
||||
ManualResetEventSlim reset = wrap.GetWorkerResetEvent(inWrapIndex);
|
||||
do
|
||||
{
|
||||
reset.Wait();
|
||||
reset.Reset();
|
||||
self.Priority = ThreadPriority.Highest;
|
||||
ExecuteTaskPackage(wrap.GetWorkerTaskQueue(inWrapIndex));
|
||||
_ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
self.Priority = _AfterWork;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
private static unsafe void MatrixParallelScheduler([AllowNull] object obj)
|
||||
{
|
||||
Handle handle = (obj as Handle)!;
|
||||
MatrixParallelWrap wrap = handle.Wrap;
|
||||
int inWrapIndex = handle.InWrapIndex;
|
||||
Queue<IntPtr> taskQueue = wrap.GetWorkerTaskQueue(inWrapIndex);
|
||||
ManualResetEventSlim resetEvent = wrap.SchedulerReset;
|
||||
Thread self = Thread.CurrentThread;
|
||||
do
|
||||
{
|
||||
resetEvent.Wait();
|
||||
resetEvent.Reset();
|
||||
self.Priority = ThreadPriority.Highest;
|
||||
|
||||
// TODO task publication to worker in wrap
|
||||
wrap.SetAllWorker();
|
||||
ExecuteTaskPackage(taskQueue);
|
||||
_ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
self.Priority = _AfterWork;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void ExecuteTaskPackage(Queue<IntPtr> taskQueue)
|
||||
{
|
||||
while (taskQueue.TryDequeue(out nint handle))
|
||||
{
|
||||
MatrixParallelTaskPackageNonGeneric* p = (MatrixParallelTaskPackageNonGeneric*)handle;
|
||||
switch (p->ComputeMode)
|
||||
{
|
||||
case 1:
|
||||
Compute_Unary(p);
|
||||
break;
|
||||
case 2:
|
||||
Compute_UnaryScalar(p);
|
||||
break;
|
||||
case 3:
|
||||
Compute_Binary(p);
|
||||
break;
|
||||
case 4:
|
||||
Compute_BinaryScalar(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region wrap field
|
||||
|
||||
private static MatrixParallelWrap[] Wraps;
|
||||
private static int LargeWrapCount;
|
||||
private static int SmallWrapCount;
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public enum SleepMode
|
||||
{
|
||||
|
||||
QuickWakeup = ThreadPriority.AboveNormal,
|
||||
NormalSleep = ThreadPriority.BelowNormal,
|
||||
DeepSleep = ThreadPriority.Lowest,
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user