HLSF use long as aloloc size
This commit is contained in:
@@ -1,160 +1,160 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute.Factorize"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixCompute
|
||||
{
|
||||
|
||||
public static unsafe void FactorizePLU(
|
||||
Matrix<float> matrix,
|
||||
Span<int> pivot,
|
||||
Matrix<float> lower,
|
||||
Matrix<float> upper
|
||||
)
|
||||
{
|
||||
if (matrix.ColumnLength != matrix.RowLength) {
|
||||
throw new InvalidOperationException("Matrix must be square.");
|
||||
}
|
||||
if (matrix.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense square matrices are supported.");
|
||||
}
|
||||
int n = matrix.RowLength;
|
||||
if (pivot.Length < n) {
|
||||
throw new ArgumentException("Pivot span length is insufficient.", nameof(pivot));
|
||||
}
|
||||
if (lower.RowLength != n ||
|
||||
lower.ColumnLength != n ||
|
||||
lower.Layout != MatrixLayout.UpperTriangle) {
|
||||
throw new ArgumentException("Lower matrix must be lower-triangular with same dimensions as input.", nameof(lower));
|
||||
}
|
||||
if (upper.RowLength != n ||
|
||||
upper.ColumnLength != n ||
|
||||
upper.Layout != MatrixLayout.UpperTriangle) {
|
||||
throw new ArgumentException("Upper matrix must be upper-triangular with same dimensions as input.", nameof(upper));
|
||||
}
|
||||
|
||||
int size = matrix.KernelColumnLength * matrix.KernelRowLength;
|
||||
if (size < 256)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
pivot[i] = i; // identity permutation
|
||||
}
|
||||
|
||||
// Aggressive: always call native pivot to fetch four-column candidates
|
||||
ulong* pivots = stackalloc ulong[4];
|
||||
for (int kBlock = 0; kBlock < n; kBlock += 4)
|
||||
{
|
||||
NativeIntrinsic.Context
|
||||
.mat[(int)MatrixElementType.F32].pivot(matrix.Buffer,
|
||||
(ulong)(kBlock >> 2),
|
||||
(ulong)(matrix.KernelRowLength - (kBlock >> 2)),
|
||||
(ulong)pivots);
|
||||
int colsInBlock = Math.Min(4, n - kBlock);
|
||||
for (int offset = 0; offset < colsInBlock; offset++)
|
||||
{
|
||||
int k = kBlock + offset;
|
||||
int pivotRow = k;
|
||||
float maxAbs;
|
||||
|
||||
int candidateRow = (int)pivots[offset];
|
||||
int idx = -1;
|
||||
for (int r = k; r < n; r++)
|
||||
{
|
||||
if (pivot[r] == candidateRow)
|
||||
{
|
||||
idx = r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (idx != -1)
|
||||
{
|
||||
pivotRow = idx;
|
||||
maxAbs = MathF.Abs(matrix.GetElementUnsafe(pivot[idx], k));
|
||||
} else
|
||||
{
|
||||
maxAbs = MathF.Abs(matrix.GetElementUnsafe(pivot[k], k));
|
||||
for (int r = k + 1; r < n; r++)
|
||||
{
|
||||
float val = MathF.Abs(matrix.GetElementUnsafe(pivot[r], k));
|
||||
if (val > maxAbs)
|
||||
{
|
||||
maxAbs = val;
|
||||
pivotRow = r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (maxAbs == 0f) {
|
||||
throw new InvalidOperationException("Zero pivot encountered in PLU.");
|
||||
}
|
||||
if (pivotRow != k) {
|
||||
(pivot[k], pivot[pivotRow]) = (pivot[pivotRow], pivot[k]);
|
||||
}
|
||||
lower[k, k] = 1.0f; // unit diagonal for L
|
||||
|
||||
// Compute U[k, j] for j >= k
|
||||
for (int j = k; j < n; j++)
|
||||
{
|
||||
float sum = 0f;
|
||||
for (int s = 0; s < k; s++) {
|
||||
sum += lower[k, s] * upper[s, j];
|
||||
}
|
||||
upper[k, j] = matrix.GetElementUnsafe(pivot[k], j) - sum;
|
||||
}
|
||||
|
||||
// Compute L[i, k] for i > k
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
float sum = 0f;
|
||||
for (int s = 0; s < k; s++) {
|
||||
sum += lower[i, s] * upper[s, k];
|
||||
}
|
||||
float u = upper[k, k];
|
||||
if (u == 0f) {
|
||||
throw new InvalidOperationException("Zero pivot encountered in PLU.");
|
||||
}
|
||||
lower[i, k] = (matrix.GetElementUnsafe(pivot[i], k) - sum) / u;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: For larger kernels, consider calling native pivot helper to extract 4 pivots at once.
|
||||
} else if (size < 1024) { } else { }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute.Factorize"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixCompute
|
||||
{
|
||||
|
||||
public static unsafe void FactorizePLU(
|
||||
Matrix<float> matrix,
|
||||
Span<int> pivot,
|
||||
Matrix<float> lower,
|
||||
Matrix<float> upper
|
||||
)
|
||||
{
|
||||
if (matrix.ColumnLength != matrix.RowLength) {
|
||||
throw new InvalidOperationException("Matrix must be square.");
|
||||
}
|
||||
if (matrix.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense square matrices are supported.");
|
||||
}
|
||||
int n = matrix.RowLength;
|
||||
if (pivot.Length < n) {
|
||||
throw new ArgumentException("Pivot span length is insufficient.", nameof(pivot));
|
||||
}
|
||||
if (lower.RowLength != n ||
|
||||
lower.ColumnLength != n ||
|
||||
lower.Layout != MatrixLayout.UpperTriangle) {
|
||||
throw new ArgumentException("Lower matrix must be lower-triangular with same dimensions as input.", nameof(lower));
|
||||
}
|
||||
if (upper.RowLength != n ||
|
||||
upper.ColumnLength != n ||
|
||||
upper.Layout != MatrixLayout.UpperTriangle) {
|
||||
throw new ArgumentException("Upper matrix must be upper-triangular with same dimensions as input.", nameof(upper));
|
||||
}
|
||||
|
||||
int size = matrix.KernelColumnLength * matrix.KernelRowLength;
|
||||
if (size < 256)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
pivot[i] = i; // identity permutation
|
||||
}
|
||||
|
||||
// Aggressive: always call native pivot to fetch four-column candidates
|
||||
ulong* pivots = stackalloc ulong[4];
|
||||
for (int kBlock = 0; kBlock < n; kBlock += 4)
|
||||
{
|
||||
NativeIntrinsic.Context
|
||||
.mat[(int)MatrixElementType.F32].pivot(matrix.Buffer,
|
||||
(ulong)(kBlock >> 2),
|
||||
(ulong)(matrix.KernelRowLength - (kBlock >> 2)),
|
||||
(ulong)pivots);
|
||||
int colsInBlock = Math.Min(4, n - kBlock);
|
||||
for (int offset = 0; offset < colsInBlock; offset++)
|
||||
{
|
||||
int k = kBlock + offset;
|
||||
int pivotRow = k;
|
||||
float maxAbs;
|
||||
|
||||
int candidateRow = (int)pivots[offset];
|
||||
int idx = -1;
|
||||
for (int r = k; r < n; r++)
|
||||
{
|
||||
if (pivot[r] == candidateRow)
|
||||
{
|
||||
idx = r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (idx != -1)
|
||||
{
|
||||
pivotRow = idx;
|
||||
maxAbs = MathF.Abs(matrix.GetElementUnsafe(pivot[idx], k));
|
||||
} else
|
||||
{
|
||||
maxAbs = MathF.Abs(matrix.GetElementUnsafe(pivot[k], k));
|
||||
for (int r = k + 1; r < n; r++)
|
||||
{
|
||||
float val = MathF.Abs(matrix.GetElementUnsafe(pivot[r], k));
|
||||
if (val > maxAbs)
|
||||
{
|
||||
maxAbs = val;
|
||||
pivotRow = r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (maxAbs == 0f) {
|
||||
throw new InvalidOperationException("Zero pivot encountered in PLU.");
|
||||
}
|
||||
if (pivotRow != k) {
|
||||
(pivot[k], pivot[pivotRow]) = (pivot[pivotRow], pivot[k]);
|
||||
}
|
||||
lower[k, k] = 1.0f; // unit diagonal for L
|
||||
|
||||
// Compute U[k, j] for j >= k
|
||||
for (int j = k; j < n; j++)
|
||||
{
|
||||
float sum = 0f;
|
||||
for (int s = 0; s < k; s++) {
|
||||
sum += lower[k, s] * upper[s, j];
|
||||
}
|
||||
upper[k, j] = matrix.GetElementUnsafe(pivot[k], j) - sum;
|
||||
}
|
||||
|
||||
// Compute L[i, k] for i > k
|
||||
for (int i = k + 1; i < n; i++)
|
||||
{
|
||||
float sum = 0f;
|
||||
for (int s = 0; s < k; s++) {
|
||||
sum += lower[i, s] * upper[s, k];
|
||||
}
|
||||
float u = upper[k, k];
|
||||
if (u == 0f) {
|
||||
throw new InvalidOperationException("Zero pivot encountered in PLU.");
|
||||
}
|
||||
lower[i, k] = (matrix.GetElementUnsafe(pivot[i], k) - sum) / u;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: For larger kernels, consider calling native pivot helper to extract 4 pivots at once.
|
||||
} else if (size < 1024) { } else { }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,307 +1,307 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute.Float"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Collections;
|
||||
using StgSharp.HighPerformance;
|
||||
using StgSharp.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Security.AccessControl;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixCompute
|
||||
{
|
||||
|
||||
public static unsafe void Add(
|
||||
Matrix<float> left,
|
||||
Matrix<float> right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
left.RowLength != right.RowLength ||
|
||||
left.ColumnLength != ans.ColumnLength ||
|
||||
left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* rightPtr = right.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_add(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* rightPtr = right.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_add(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(float);
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.LEFT_RIGHT_ANS_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f32].buffer_add;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Fill(
|
||||
Matrix<float> ans,
|
||||
float value
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
long count = (long)ans.KernelColumnLength * ans.KernelRowLength;
|
||||
ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket();
|
||||
scalar->Data<float>(0) = value;
|
||||
if (count <= 256)
|
||||
{
|
||||
NativeIntrinsic.Context.mat[f32].buffer_fill(ans.Buffer, scalar, count);
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
NativeIntrinsic.Context.mat[f32].buffer_fill(ans.Buffer, scalar, count);
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(float);
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.ANS_SCALAR_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f32].buffer_fill;
|
||||
package->Scalar = scalar;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
MatrixParallelFactory.Release(scalar);
|
||||
taskGroup.Dispose();
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<float> left,
|
||||
Matrix<float> right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
|
||||
// For matrix multiplication (left: MxK) * (right: KxN) => ans: MxN
|
||||
// ColumnLength = number of columns (N), RowLength = number of rows (M)
|
||||
if (left.ColumnLength != right.RowLength ||
|
||||
ans.ColumnLength != right.ColumnLength ||
|
||||
ans.RowLength != left.RowLength)
|
||||
{
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
right.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
|
||||
if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = null;
|
||||
if (count > 256) {
|
||||
pool = MatrixParallel.LeadParallel(1);
|
||||
}
|
||||
M128* enumeration = stackalloc M128[1];
|
||||
enumeration->Member<int>(2) = left.KernelColumnLength;
|
||||
enumeration->Member<int>(3) = ans.KernelColumnLength;
|
||||
for (int i = 0; i < ans.KernelRowLength; i++)
|
||||
{
|
||||
for (int j = 0; j < ans.KernelColumnLength; j++)
|
||||
{
|
||||
enumeration->Member<int>(0) = i;
|
||||
enumeration->Member<int>(1) = j;
|
||||
|
||||
NativeIntrinsic.Context
|
||||
.mat[f32].kernel_tile_fma((MatrixKernel*)left.Buffer,
|
||||
(MatrixKernel*)right.Buffer,
|
||||
(MatrixKernel*)ans.Buffer,
|
||||
enumeration);
|
||||
}
|
||||
}
|
||||
|
||||
if (count > 256) {
|
||||
MatrixParallel.ReturnParallelResources(ref pool!);
|
||||
}
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->AnsPrimOffset = ans.KernelRowLength;
|
||||
package->AnsSecOffset = ans.KernelColumnLength;
|
||||
package->LeftPrimOffset = left.KernelColumnLength;
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ElementSize = (int)MatrixElementType.F32;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationSpecial.SEQ_FMA);
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<float> left,
|
||||
float right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
if (left.ColumnLength != ans.ColumnLength || left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket();
|
||||
scalar->Data<float>(0) = right;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_scalar_mul(leftPtr, ansPtr, scalar, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_scalar_mul(leftPtr, ansPtr, scalar, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
|
||||
package->Right = (nint)left.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(float);
|
||||
package->PrimCount = left.KernelColumnLength;
|
||||
package->SecCount = left.KernelRowLength;
|
||||
package->Scalar = scalar;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f32].buffer_scalar_mul;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
MatrixParallelFactory.Release(scalar);
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Sub(
|
||||
Matrix<float> left,
|
||||
Matrix<float> right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
left.RowLength != right.RowLength ||
|
||||
left.ColumnLength != ans.ColumnLength ||
|
||||
left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
right.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* rightPtr = right.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_sub(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* rightPtr = right.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_sub(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(float);
|
||||
package->PrimCount = left.KernelColumnLength;
|
||||
package->SecCount = left.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.LEFT_RIGHT_ANS_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f32].buffer_sub;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute.Float"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Collections;
|
||||
using StgSharp.HighPerformance;
|
||||
using StgSharp.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Security.AccessControl;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixCompute
|
||||
{
|
||||
|
||||
public static unsafe void Add(
|
||||
Matrix<float> left,
|
||||
Matrix<float> right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
left.RowLength != right.RowLength ||
|
||||
left.ColumnLength != ans.ColumnLength ||
|
||||
left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* rightPtr = right.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_add(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* rightPtr = right.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_add(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(float);
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.LEFT_RIGHT_ANS_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f32].buffer_add;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Fill(
|
||||
Matrix<float> ans,
|
||||
float value
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
long count = (long)ans.KernelColumnLength * ans.KernelRowLength;
|
||||
ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket();
|
||||
scalar->Data<float>(0) = value;
|
||||
if (count <= 256)
|
||||
{
|
||||
NativeIntrinsic.Context.mat[f32].buffer_fill(ans.Buffer, scalar, count);
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
NativeIntrinsic.Context.mat[f32].buffer_fill(ans.Buffer, scalar, count);
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(float);
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.ANS_SCALAR_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f32].buffer_fill;
|
||||
package->Scalar = scalar;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
MatrixParallelFactory.Release(scalar);
|
||||
taskGroup.Dispose();
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<float> left,
|
||||
Matrix<float> right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
|
||||
// For matrix multiplication (left: MxK) * (right: KxN) => ans: MxN
|
||||
// ColumnLength = number of columns (N), RowLength = number of rows (M)
|
||||
if (left.ColumnLength != right.RowLength ||
|
||||
ans.ColumnLength != right.ColumnLength ||
|
||||
ans.RowLength != left.RowLength)
|
||||
{
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
right.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
|
||||
if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = null;
|
||||
if (count > 256) {
|
||||
pool = MatrixParallel.LeadParallel(1);
|
||||
}
|
||||
M128* enumeration = stackalloc M128[1];
|
||||
enumeration->Member<int>(2) = left.KernelColumnLength;
|
||||
enumeration->Member<int>(3) = ans.KernelColumnLength;
|
||||
for (int i = 0; i < ans.KernelRowLength; i++)
|
||||
{
|
||||
for (int j = 0; j < ans.KernelColumnLength; j++)
|
||||
{
|
||||
enumeration->Member<int>(0) = i;
|
||||
enumeration->Member<int>(1) = j;
|
||||
|
||||
NativeIntrinsic.Context
|
||||
.mat[f32].kernel_tile_fma((MatrixKernel*)left.Buffer,
|
||||
(MatrixKernel*)right.Buffer,
|
||||
(MatrixKernel*)ans.Buffer,
|
||||
enumeration);
|
||||
}
|
||||
}
|
||||
|
||||
if (count > 256) {
|
||||
MatrixParallel.ReturnParallelResources(ref pool!);
|
||||
}
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->AnsPrimOffset = ans.KernelRowLength;
|
||||
package->AnsSecOffset = ans.KernelColumnLength;
|
||||
package->LeftPrimOffset = left.KernelColumnLength;
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ElementSize = (int)MatrixElementType.F32;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationSpecial.SEQ_FMA);
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<float> left,
|
||||
float right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
if (left.ColumnLength != ans.ColumnLength || left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket();
|
||||
scalar->Data<float>(0) = right;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_scalar_mul(leftPtr, ansPtr, scalar, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_scalar_mul(leftPtr, ansPtr, scalar, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
|
||||
package->Right = (nint)left.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(float);
|
||||
package->PrimCount = left.KernelColumnLength;
|
||||
package->SecCount = left.KernelRowLength;
|
||||
package->Scalar = scalar;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f32].buffer_scalar_mul;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
MatrixParallelFactory.Release(scalar);
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Sub(
|
||||
Matrix<float> left,
|
||||
Matrix<float> right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
{
|
||||
int f32 = (int)MatrixElementType.F32;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
left.RowLength != right.RowLength ||
|
||||
left.ColumnLength != ans.ColumnLength ||
|
||||
left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
right.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* rightPtr = right.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_sub(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* rightPtr = right.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f32].buffer_sub(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<float>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(float);
|
||||
package->PrimCount = left.KernelColumnLength;
|
||||
package->SecCount = left.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.LEFT_RIGHT_ANS_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f32].buffer_sub;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,117 +1,117 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using StgSharp.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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,
|
||||
int colLength,
|
||||
int x,
|
||||
int y
|
||||
) where T : unmanaged, INumber<T>
|
||||
{
|
||||
return (MatrixKernel*)(buffer + (colLength * x + y));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using StgSharp.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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,
|
||||
int colLength,
|
||||
int x,
|
||||
int y
|
||||
) where T : unmanaged, INumber<T>
|
||||
{
|
||||
return (MatrixKernel*)(buffer + (colLength * x + y));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,302 +1,302 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute.double"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using StgSharp.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public partial class MatrixCompute
|
||||
{
|
||||
|
||||
public static unsafe void Add(
|
||||
Matrix<double> left,
|
||||
Matrix<double> right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
left.RowLength != right.RowLength ||
|
||||
left.ColumnLength != ans.ColumnLength ||
|
||||
left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* rightPtr = right.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_add(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* rightPtr = right.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_add(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<double>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(double);
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.LEFT_RIGHT_ANS_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f64].buffer_add;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Fill(
|
||||
Matrix<double> ans,
|
||||
double value
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
long count = (long)ans.KernelColumnLength * ans.KernelRowLength;
|
||||
ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket();
|
||||
scalar->Data<double>(0) = value;
|
||||
if (count <= 256)
|
||||
{
|
||||
NativeIntrinsic.Context.mat[f64].buffer_fill(ans.Buffer, scalar, count);
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
NativeIntrinsic.Context.mat[f64].buffer_fill(ans.Buffer, scalar, count);
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<double>();
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(double);
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.ANS_SCALAR_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f64].buffer_fill;
|
||||
package->Scalar = scalar;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
MatrixParallelFactory.Release(scalar);
|
||||
taskGroup.Dispose();
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<double> left,
|
||||
Matrix<double> right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
|
||||
// For matrix multiplication (left: MxK) * (right: KxN) => ans: MxN
|
||||
// ColumnLength = number of columns (N), RowLength = number of rows (M)
|
||||
if (left.ColumnLength != right.RowLength ||
|
||||
ans.ColumnLength != right.ColumnLength ||
|
||||
ans.RowLength != left.RowLength)
|
||||
{
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
|
||||
// int size = MatrixPanel.KernelSideCount<double>();
|
||||
if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = null;
|
||||
if (count > 256) {
|
||||
pool = MatrixParallel.LeadParallel(1);
|
||||
}
|
||||
M128* enumeration = stackalloc M128[1];
|
||||
enumeration->Member<int>(2) = left.KernelColumnLength;
|
||||
enumeration->Member<int>(3) = ans.KernelColumnLength;
|
||||
for (int i = 0; i < ans.KernelRowLength; i++)
|
||||
{
|
||||
for (int j = 0; j < ans.KernelColumnLength; j++)
|
||||
{
|
||||
enumeration->Member<int>(0) = i;
|
||||
enumeration->Member<int>(1) = j;
|
||||
|
||||
NativeIntrinsic.Context
|
||||
.mat[f64].kernel_tile_fma((MatrixKernel*)left.Buffer,
|
||||
(MatrixKernel*)right.Buffer,
|
||||
(MatrixKernel*)ans.Buffer,
|
||||
enumeration);
|
||||
|
||||
// Console.WriteLine($"{i},{j},{ans[4*i,4*j]:0.0E00}\t");
|
||||
}
|
||||
|
||||
// Console.WriteLine();
|
||||
}
|
||||
if (count > 256) {
|
||||
MatrixParallel.ReturnParallelResources(ref pool!);
|
||||
}
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<double>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->AnsPrimOffset = ans.KernelRowLength;
|
||||
package->AnsSecOffset = ans.KernelColumnLength;
|
||||
package->LeftPrimOffset = left.KernelColumnLength;
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ElementSize = (int)MatrixElementType.F64;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationSpecial.SEQ_FMA);
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<double> left,
|
||||
double right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
if (left.ColumnLength != ans.ColumnLength || left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket();
|
||||
scalar->Data<double>(0) = right;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_scalar_mul(leftPtr, ansPtr, scalar, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_scalar_mul(leftPtr, ansPtr, scalar, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<double>();
|
||||
package->Right = (nint)left.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(double);
|
||||
package->PrimCount = left.KernelColumnLength;
|
||||
package->SecCount = left.KernelRowLength;
|
||||
package->Scalar = scalar;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f64].buffer_scalar_mul;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
MatrixParallelFactory.Release(scalar);
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Sub(
|
||||
Matrix<double> left,
|
||||
Matrix<double> right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
left.RowLength != right.RowLength ||
|
||||
left.ColumnLength != ans.ColumnLength ||
|
||||
left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
right.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* rightPtr = right.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_sub(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* rightPtr = right.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_sub(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<double>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(double);
|
||||
package->PrimCount = left.KernelColumnLength;
|
||||
package->SecCount = left.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.LEFT_RIGHT_ANS_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f64].buffer_sub;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute.double"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using StgSharp.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public partial class MatrixCompute
|
||||
{
|
||||
|
||||
public static unsafe void Add(
|
||||
Matrix<double> left,
|
||||
Matrix<double> right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
left.RowLength != right.RowLength ||
|
||||
left.ColumnLength != ans.ColumnLength ||
|
||||
left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* rightPtr = right.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_add(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* rightPtr = right.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_add(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<double>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(double);
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.LEFT_RIGHT_ANS_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f64].buffer_add;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Fill(
|
||||
Matrix<double> ans,
|
||||
double value
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
long count = (long)ans.KernelColumnLength * ans.KernelRowLength;
|
||||
ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket();
|
||||
scalar->Data<double>(0) = value;
|
||||
if (count <= 256)
|
||||
{
|
||||
NativeIntrinsic.Context.mat[f64].buffer_fill(ans.Buffer, scalar, count);
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
NativeIntrinsic.Context.mat[f64].buffer_fill(ans.Buffer, scalar, count);
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<double>();
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(double);
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.ANS_SCALAR_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f64].buffer_fill;
|
||||
package->Scalar = scalar;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
MatrixParallelFactory.Release(scalar);
|
||||
taskGroup.Dispose();
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<double> left,
|
||||
Matrix<double> right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
|
||||
// For matrix multiplication (left: MxK) * (right: KxN) => ans: MxN
|
||||
// ColumnLength = number of columns (N), RowLength = number of rows (M)
|
||||
if (left.ColumnLength != right.RowLength ||
|
||||
ans.ColumnLength != right.ColumnLength ||
|
||||
ans.RowLength != left.RowLength)
|
||||
{
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
|
||||
// int size = MatrixPanel.KernelSideCount<double>();
|
||||
if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = null;
|
||||
if (count > 256) {
|
||||
pool = MatrixParallel.LeadParallel(1);
|
||||
}
|
||||
M128* enumeration = stackalloc M128[1];
|
||||
enumeration->Member<int>(2) = left.KernelColumnLength;
|
||||
enumeration->Member<int>(3) = ans.KernelColumnLength;
|
||||
for (int i = 0; i < ans.KernelRowLength; i++)
|
||||
{
|
||||
for (int j = 0; j < ans.KernelColumnLength; j++)
|
||||
{
|
||||
enumeration->Member<int>(0) = i;
|
||||
enumeration->Member<int>(1) = j;
|
||||
|
||||
NativeIntrinsic.Context
|
||||
.mat[f64].kernel_tile_fma((MatrixKernel*)left.Buffer,
|
||||
(MatrixKernel*)right.Buffer,
|
||||
(MatrixKernel*)ans.Buffer,
|
||||
enumeration);
|
||||
|
||||
// Console.WriteLine($"{i},{j},{ans[4*i,4*j]:0.0E00}\t");
|
||||
}
|
||||
|
||||
// Console.WriteLine();
|
||||
}
|
||||
if (count > 256) {
|
||||
MatrixParallel.ReturnParallelResources(ref pool!);
|
||||
}
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<double>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->AnsPrimOffset = ans.KernelRowLength;
|
||||
package->AnsSecOffset = ans.KernelColumnLength;
|
||||
package->LeftPrimOffset = left.KernelColumnLength;
|
||||
package->PrimCount = ans.KernelColumnLength;
|
||||
package->SecCount = ans.KernelRowLength;
|
||||
package->ElementSize = (int)MatrixElementType.F64;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationSpecial.SEQ_FMA);
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Mul(
|
||||
Matrix<double> left,
|
||||
double right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
if (left.ColumnLength != ans.ColumnLength || left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket();
|
||||
scalar->Data<double>(0) = right;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_scalar_mul(leftPtr, ansPtr, scalar, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_scalar_mul(leftPtr, ansPtr, scalar, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<double>();
|
||||
package->Right = (nint)left.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(double);
|
||||
package->PrimCount = left.KernelColumnLength;
|
||||
package->SecCount = left.KernelRowLength;
|
||||
package->Scalar = scalar;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f64].buffer_scalar_mul;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
MatrixParallelFactory.Release(scalar);
|
||||
return;
|
||||
}
|
||||
|
||||
public static unsafe void Sub(
|
||||
Matrix<double> left,
|
||||
Matrix<double> right,
|
||||
Matrix<double> ans
|
||||
)
|
||||
{
|
||||
int f64 = (int)MatrixElementType.F64;
|
||||
if (left.ColumnLength != right.ColumnLength ||
|
||||
left.RowLength != right.RowLength ||
|
||||
left.ColumnLength != ans.ColumnLength ||
|
||||
left.RowLength != ans.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
if (left.Layout != MatrixLayout.DenseRectangle ||
|
||||
right.Layout != MatrixLayout.DenseRectangle ||
|
||||
ans.Layout != MatrixLayout.DenseRectangle) {
|
||||
throw new NotSupportedException("Only dense rectangle matrices are supported.");
|
||||
}
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
if (count <= 256)
|
||||
{
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* rightPtr = right.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_sub(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
|
||||
MatrixKernel<double>* leftPtr = left.Buffer;
|
||||
MatrixKernel<double>* rightPtr = right.Buffer;
|
||||
MatrixKernel<double>* ansPtr = ans.Buffer;
|
||||
NativeIntrinsic.Context.mat[f64].buffer_sub(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelTaskPackage* package = MatrixParallelFactory.CreateBaseTask<double>();
|
||||
package->Left = (nint)left.Buffer;
|
||||
package->Right = (nint)right.Buffer;
|
||||
package->Answer = (nint)ans.Buffer;
|
||||
package->ElementSize = sizeof(double);
|
||||
package->PrimCount = left.KernelColumnLength;
|
||||
package->SecCount = left.KernelRowLength;
|
||||
package->ComputeMode = new MatrixOpMode(MatrixIndexStyle.BUFFER_INDEX, MatrixOperationParam.LEFT_RIGHT_ANS_PARAM);
|
||||
package->ComputeHandle = (IntPtr)NativeIntrinsic.Context.mat[f64].buffer_sub;
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.ScheduleTask(package);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,134 +1,134 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixComputeModel"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
internal static class MatrixComputeModel
|
||||
{
|
||||
|
||||
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;
|
||||
|
||||
((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
|
||||
)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
internal static unsafe void BufferComputeNoOperatorScalar(
|
||||
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)
|
||||
{
|
||||
case MatrixOperationSpecial.SEQ_FMA:
|
||||
switch ((MatrixElementType)p->ElementSize)
|
||||
{
|
||||
case MatrixElementType.F64:
|
||||
MatrixCompute.ComputeFmaPanelF64(p);
|
||||
return;
|
||||
case MatrixElementType.F32:
|
||||
MatrixCompute.ComputeFmaPanelF32(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
MatrixParallelFactory.Release(p);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixComputeModel"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
internal static class MatrixComputeModel
|
||||
{
|
||||
|
||||
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;
|
||||
|
||||
((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
|
||||
)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
internal static unsafe void BufferComputeNoOperatorScalar(
|
||||
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)
|
||||
{
|
||||
case MatrixOperationSpecial.SEQ_FMA:
|
||||
switch ((MatrixElementType)p->ElementSize)
|
||||
{
|
||||
case MatrixElementType.F64:
|
||||
MatrixCompute.ComputeFmaPanelF64(p);
|
||||
return;
|
||||
case MatrixElementType.F32:
|
||||
MatrixCompute.ComputeFmaPanelF32(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
MatrixParallelFactory.Release(p);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,146 +1,146 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.Await"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private static AwaitingQueue _awaitingLeaderHandle;
|
||||
|
||||
private static volatile int _createGeneration = 0;
|
||||
private static int _sortState;
|
||||
|
||||
public static MatrixParallelThread[] LeadParallel(int recommendCount)
|
||||
{
|
||||
int min = int.Min(recommendCount, _threads.Capacity);
|
||||
min /= 2;
|
||||
MatrixParallelThread[] ret;
|
||||
if (_threads.Count < min)
|
||||
{
|
||||
WaitHandle handle = new WaitHandle
|
||||
(
|
||||
_createGeneration,
|
||||
min,
|
||||
recommendCount
|
||||
);
|
||||
do
|
||||
{
|
||||
if (Interlocked.Exchange(ref _sortState, 1) == 0)
|
||||
{
|
||||
_awaitingLeaderHandle.Enqueue(handle);
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
Interlocked.Increment(ref _createGeneration);
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
int cycleGeneration = _createGeneration;
|
||||
do
|
||||
{
|
||||
Thread.Sleep(0);
|
||||
|
||||
if (_createGeneration != cycleGeneration)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _sortState, 1) == 0)
|
||||
{
|
||||
if (_awaitingLeaderHandle.TryPopIfRefEqual(cycleGeneration, in handle))
|
||||
{
|
||||
// Sort is implemented in TryPopIfRefEqual
|
||||
ret = _threads.PopRange(handle.BestCount);
|
||||
_createGeneration++;
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
return ret;
|
||||
}
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
} else
|
||||
{
|
||||
cycleGeneration = _createGeneration;
|
||||
}
|
||||
}
|
||||
} while (true);
|
||||
} else
|
||||
{
|
||||
// directly get the leader
|
||||
while (true)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _sortState, 1) == 0)
|
||||
{
|
||||
ret = _threads.PopRange(recommendCount);
|
||||
_createGeneration++;
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return ret;
|
||||
}
|
||||
|
||||
public static void ReturnParallelResources(ref MatrixParallelThread[] pool)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _sortState, 1) == 0)
|
||||
{
|
||||
_threads.PushRange(pool);
|
||||
Interlocked.Exchange(ref _sortState, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pool = null!;
|
||||
}
|
||||
|
||||
private record class WaitHandle(int generation, int least, int best)
|
||||
{
|
||||
|
||||
public int CreateGeneration { get; init; } = generation;
|
||||
|
||||
public int LeastCount { get; init; } = least;
|
||||
|
||||
public int BestCount { get; init; } = best;
|
||||
|
||||
public int Priority { get; private set; }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void UpdatePriority(int currentGeneration)
|
||||
{
|
||||
Priority = (currentGeneration - CreateGeneration) * (MatrixParallel._threads.Capacity - LeastCount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.Await"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private static AwaitingQueue _awaitingLeaderHandle;
|
||||
|
||||
private static volatile int _createGeneration = 0;
|
||||
private static int _sortState;
|
||||
|
||||
public static MatrixParallelThread[] LeadParallel(int recommendCount)
|
||||
{
|
||||
int min = int.Min(recommendCount, _threads.Capacity);
|
||||
min /= 2;
|
||||
MatrixParallelThread[] ret;
|
||||
if (_threads.Count < min)
|
||||
{
|
||||
WaitHandle handle = new WaitHandle
|
||||
(
|
||||
_createGeneration,
|
||||
min,
|
||||
recommendCount
|
||||
);
|
||||
do
|
||||
{
|
||||
if (Interlocked.Exchange(ref _sortState, 1) == 0)
|
||||
{
|
||||
_awaitingLeaderHandle.Enqueue(handle);
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
Interlocked.Increment(ref _createGeneration);
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
int cycleGeneration = _createGeneration;
|
||||
do
|
||||
{
|
||||
Thread.Sleep(0);
|
||||
|
||||
if (_createGeneration != cycleGeneration)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _sortState, 1) == 0)
|
||||
{
|
||||
if (_awaitingLeaderHandle.TryPopIfRefEqual(cycleGeneration, in handle))
|
||||
{
|
||||
// Sort is implemented in TryPopIfRefEqual
|
||||
ret = _threads.PopRange(handle.BestCount);
|
||||
_createGeneration++;
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
return ret;
|
||||
}
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
} else
|
||||
{
|
||||
cycleGeneration = _createGeneration;
|
||||
}
|
||||
}
|
||||
} while (true);
|
||||
} else
|
||||
{
|
||||
// directly get the leader
|
||||
while (true)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _sortState, 1) == 0)
|
||||
{
|
||||
ret = _threads.PopRange(recommendCount);
|
||||
_createGeneration++;
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return ret;
|
||||
}
|
||||
|
||||
public static void ReturnParallelResources(ref MatrixParallelThread[] pool)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _sortState, 1) == 0)
|
||||
{
|
||||
_threads.PushRange(pool);
|
||||
Interlocked.Exchange(ref _sortState, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pool = null!;
|
||||
}
|
||||
|
||||
private record class WaitHandle(int generation, int least, int best)
|
||||
{
|
||||
|
||||
public int CreateGeneration { get; init; } = generation;
|
||||
|
||||
public int LeastCount { get; init; } = least;
|
||||
|
||||
public int BestCount { get; init; } = best;
|
||||
|
||||
public int Priority { get; private set; }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void UpdatePriority(int currentGeneration)
|
||||
{
|
||||
Priority = (currentGeneration - CreateGeneration) * (MatrixParallel._threads.Capacity - LeastCount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,156 +1,156 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.AwaitingQueue"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
/*
|
||||
* Because caller of this class has its own sync object
|
||||
* this queue will use no sync object internally.
|
||||
*/
|
||||
|
||||
private class AwaitingQueue
|
||||
{
|
||||
|
||||
private Element _quickElements = new();
|
||||
private volatile int _count;
|
||||
private readonly Queue<WaitHandle> _fallback = [];
|
||||
|
||||
public AwaitingQueue() { }
|
||||
|
||||
public void Enqueue(WaitHandle handle)
|
||||
{
|
||||
if (_count > 7)
|
||||
{
|
||||
_fallback.Enqueue(handle);
|
||||
Interlocked.Increment(ref _count);
|
||||
return;
|
||||
}
|
||||
_quickElements[_count] = handle;
|
||||
Interlocked.Increment(ref _count);
|
||||
}
|
||||
|
||||
public bool TryDequeue(int generation, out WaitHandle handle)
|
||||
{
|
||||
if (_count == 0)
|
||||
{
|
||||
handle = null!;
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < _count; i++) {
|
||||
_quickElements[i].UpdatePriority(generation);
|
||||
}
|
||||
Element.Sort(ref _quickElements);
|
||||
handle = _quickElements[0];
|
||||
_count--;
|
||||
_quickElements[0] = default!;
|
||||
if (_count > 7) {
|
||||
_ = _fallback.TryDequeue(out _quickElements[0]!);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryPeek(int generation, out WaitHandle handle)
|
||||
{
|
||||
if (_count == 0)
|
||||
{
|
||||
handle = null!;
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < _count; i++) {
|
||||
_quickElements[i].UpdatePriority(generation);
|
||||
}
|
||||
|
||||
Element.Sort(ref _quickElements);
|
||||
handle = _quickElements[0];
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryPopIfRefEqual(int generation, in WaitHandle handle)
|
||||
{
|
||||
if (_count == 0) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < _count; i++) {
|
||||
_quickElements[i].UpdatePriority(generation);
|
||||
}
|
||||
Element.Sort(ref _quickElements);
|
||||
if (!ReferenceEquals(_quickElements[0], handle)) {
|
||||
return false;
|
||||
}
|
||||
_count--;
|
||||
_quickElements[0] = null!;
|
||||
if (_count > 7) {
|
||||
_ = _fallback.TryDequeue(out _quickElements[0]!);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[InlineArray(8)]
|
||||
private struct Element
|
||||
{
|
||||
|
||||
private WaitHandle value;
|
||||
|
||||
public static void Sort(ref Element e)
|
||||
{
|
||||
int maxIdx = 0;
|
||||
WaitHandle first = e[0];
|
||||
int maxPr = first?.Priority ?? int.MinValue;
|
||||
|
||||
for (int i = 1; i < 8; i++)
|
||||
{
|
||||
WaitHandle cur = e[i];
|
||||
int p = cur?.Priority ?? int.MinValue;
|
||||
if (p > maxPr)
|
||||
{
|
||||
maxPr = p;
|
||||
maxIdx = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (maxIdx != 0) {
|
||||
(e[0], e[maxIdx]) = (e[maxIdx], e[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.AwaitingQueue"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
/*
|
||||
* Because caller of this class has its own sync object
|
||||
* this queue will use no sync object internally.
|
||||
*/
|
||||
|
||||
private class AwaitingQueue
|
||||
{
|
||||
|
||||
private Element _quickElements = new();
|
||||
private volatile int _count;
|
||||
private readonly Queue<WaitHandle> _fallback = [];
|
||||
|
||||
public AwaitingQueue() { }
|
||||
|
||||
public void Enqueue(WaitHandle handle)
|
||||
{
|
||||
if (_count > 7)
|
||||
{
|
||||
_fallback.Enqueue(handle);
|
||||
Interlocked.Increment(ref _count);
|
||||
return;
|
||||
}
|
||||
_quickElements[_count] = handle;
|
||||
Interlocked.Increment(ref _count);
|
||||
}
|
||||
|
||||
public bool TryDequeue(int generation, out WaitHandle handle)
|
||||
{
|
||||
if (_count == 0)
|
||||
{
|
||||
handle = null!;
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < _count; i++) {
|
||||
_quickElements[i].UpdatePriority(generation);
|
||||
}
|
||||
Element.Sort(ref _quickElements);
|
||||
handle = _quickElements[0];
|
||||
_count--;
|
||||
_quickElements[0] = default!;
|
||||
if (_count > 7) {
|
||||
_ = _fallback.TryDequeue(out _quickElements[0]!);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryPeek(int generation, out WaitHandle handle)
|
||||
{
|
||||
if (_count == 0)
|
||||
{
|
||||
handle = null!;
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < _count; i++) {
|
||||
_quickElements[i].UpdatePriority(generation);
|
||||
}
|
||||
|
||||
Element.Sort(ref _quickElements);
|
||||
handle = _quickElements[0];
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryPopIfRefEqual(int generation, in WaitHandle handle)
|
||||
{
|
||||
if (_count == 0) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < _count; i++) {
|
||||
_quickElements[i].UpdatePriority(generation);
|
||||
}
|
||||
Element.Sort(ref _quickElements);
|
||||
if (!ReferenceEquals(_quickElements[0], handle)) {
|
||||
return false;
|
||||
}
|
||||
_count--;
|
||||
_quickElements[0] = null!;
|
||||
if (_count > 7) {
|
||||
_ = _fallback.TryDequeue(out _quickElements[0]!);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[InlineArray(8)]
|
||||
private struct Element
|
||||
{
|
||||
|
||||
private WaitHandle value;
|
||||
|
||||
public static void Sort(ref Element e)
|
||||
{
|
||||
int maxIdx = 0;
|
||||
WaitHandle first = e[0];
|
||||
int maxPr = first?.Priority ?? int.MinValue;
|
||||
|
||||
for (int i = 1; i < 8; i++)
|
||||
{
|
||||
WaitHandle cur = e[i];
|
||||
int p = cur?.Priority ?? int.MinValue;
|
||||
if (p > maxPr)
|
||||
{
|
||||
maxPr = p;
|
||||
maxIdx = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (maxIdx != 0) {
|
||||
(e[0], e[maxIdx]) = (e[maxIdx], e[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,114 +1,114 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.Publish"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Collections;
|
||||
using StgSharp.HighPerformance;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private const int MIN_TASK_PER_THREAD = 256;
|
||||
private const int REC_TASK_PER_THREAD = 1024;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
internal static unsafe MatrixParallelHandle ScheduleTask(MatrixParallelTaskPackage* package)
|
||||
{
|
||||
MatrixParallelTaskPackage* nonGeneric = package;
|
||||
switch (nonGeneric->ComputeMode.OperationStyle)
|
||||
{
|
||||
case MatrixIndexStyle.BUFFER_INDEX:
|
||||
return ScheduleBufferTask(nonGeneric);
|
||||
default:
|
||||
throw new InvalidOperationException($"Unknown mode {nonGeneric->ComputeMode.OperationStyle}.");
|
||||
}
|
||||
}
|
||||
|
||||
private static unsafe MatrixParallelHandle ScheduleBufferTask(
|
||||
MatrixParallelTaskPackage* package)
|
||||
{
|
||||
int primCount = package->PrimCount;
|
||||
int secCount = package->SecCount;
|
||||
long count = (long)primCount * secCount;
|
||||
|
||||
// Console.WriteLine($"amount of kernels is {count}");
|
||||
|
||||
long recommendCount = count / 1024;
|
||||
MatrixParallelThread[] pool = LeadParallel((int)recommendCount);
|
||||
MatrixParallelHandle handle = new MatrixParallelHandle(pool);
|
||||
if (handle.Wraps.Length == 1)
|
||||
{
|
||||
// Console.WriteLine($"Scheduling into {pool.Length} threads.");
|
||||
ref long offsetRef = ref Unsafe.As<int, long>(ref package->PrimTileOffset);
|
||||
offsetRef = 0;
|
||||
ref long lengthRef = ref Unsafe.As<int, long>(ref package->PrimCount);
|
||||
lengthRef = count;
|
||||
handle.Wraps[0].WrapTask.Enqueue((nint)package);
|
||||
|
||||
// Console.WriteLine($"Scheduling {count} kernels into thread");
|
||||
return handle;
|
||||
}
|
||||
long actual = count / pool.Length;
|
||||
long extra = count % pool.Length;
|
||||
|
||||
long offset = 0;
|
||||
MatrixParallelTaskPackage* current = package;
|
||||
|
||||
// Console.WriteLine($"Scheduling into {pool.Length} threads.");
|
||||
foreach (MatrixParallelWrap wrap in handle.Wraps)
|
||||
{
|
||||
long wrapTaskLength = actual * wrap.WrapTaskCapacity;
|
||||
long ex = Math.Min(extra, wrap.WrapTaskCapacity);
|
||||
long length = wrapTaskLength + ex;
|
||||
extra -= ex;
|
||||
ref long offsetRef = ref Unsafe.As<int, long>(ref current->PrimTileOffset);
|
||||
offsetRef = offset;
|
||||
ref long lengthRef = ref Unsafe.As<int, long>(ref current->PrimCount);
|
||||
lengthRef = length;
|
||||
wrap.WrapTask.Enqueue((nint) current);
|
||||
|
||||
// Console.WriteLine($"Offset is {offset}");
|
||||
// Console.WriteLine($"Scheduling {length} kernels into {wrap.WrapTaskCapacity} threads");
|
||||
// Console.WriteLine();
|
||||
current = MatrixParallelFactory.FromExistPackage(current);
|
||||
offset += length;
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.Publish"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Collections;
|
||||
using StgSharp.HighPerformance;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private const int MIN_TASK_PER_THREAD = 256;
|
||||
private const int REC_TASK_PER_THREAD = 1024;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
internal static unsafe MatrixParallelHandle ScheduleTask(MatrixParallelTaskPackage* package)
|
||||
{
|
||||
MatrixParallelTaskPackage* nonGeneric = package;
|
||||
switch (nonGeneric->ComputeMode.OperationStyle)
|
||||
{
|
||||
case MatrixIndexStyle.BUFFER_INDEX:
|
||||
return ScheduleBufferTask(nonGeneric);
|
||||
default:
|
||||
throw new InvalidOperationException($"Unknown mode {nonGeneric->ComputeMode.OperationStyle}.");
|
||||
}
|
||||
}
|
||||
|
||||
private static unsafe MatrixParallelHandle ScheduleBufferTask(
|
||||
MatrixParallelTaskPackage* package)
|
||||
{
|
||||
int primCount = package->PrimCount;
|
||||
int secCount = package->SecCount;
|
||||
long count = (long)primCount * secCount;
|
||||
|
||||
// Console.WriteLine($"amount of kernels is {count}");
|
||||
|
||||
long recommendCount = count / 1024;
|
||||
MatrixParallelThread[] pool = LeadParallel((int)recommendCount);
|
||||
MatrixParallelHandle handle = new MatrixParallelHandle(pool);
|
||||
if (handle.Wraps.Length == 1)
|
||||
{
|
||||
// Console.WriteLine($"Scheduling into {pool.Length} threads.");
|
||||
ref long offsetRef = ref Unsafe.As<int, long>(ref package->PrimTileOffset);
|
||||
offsetRef = 0;
|
||||
ref long lengthRef = ref Unsafe.As<int, long>(ref package->PrimCount);
|
||||
lengthRef = count;
|
||||
handle.Wraps[0].WrapTask.Enqueue((nint)package);
|
||||
|
||||
// Console.WriteLine($"Scheduling {count} kernels into thread");
|
||||
return handle;
|
||||
}
|
||||
long actual = count / pool.Length;
|
||||
long extra = count % pool.Length;
|
||||
|
||||
long offset = 0;
|
||||
MatrixParallelTaskPackage* current = package;
|
||||
|
||||
// Console.WriteLine($"Scheduling into {pool.Length} threads.");
|
||||
foreach (MatrixParallelWrap wrap in handle.Wraps)
|
||||
{
|
||||
long wrapTaskLength = actual * wrap.WrapTaskCapacity;
|
||||
long ex = Math.Min(extra, wrap.WrapTaskCapacity);
|
||||
long length = wrapTaskLength + ex;
|
||||
extra -= ex;
|
||||
ref long offsetRef = ref Unsafe.As<int, long>(ref current->PrimTileOffset);
|
||||
offsetRef = offset;
|
||||
ref long lengthRef = ref Unsafe.As<int, long>(ref current->PrimCount);
|
||||
lengthRef = length;
|
||||
wrap.WrapTask.Enqueue((nint) current);
|
||||
|
||||
// Console.WriteLine($"Offset is {offset}");
|
||||
// Console.WriteLine($"Scheduling {length} kernels into {wrap.WrapTaskCapacity} threads");
|
||||
// Console.WriteLine();
|
||||
current = MatrixParallelFactory.FromExistPackage(current);
|
||||
offset += length;
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,99 +1,99 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel"
|
||||
// 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.Collections;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private static bool _meaningful;
|
||||
|
||||
private static CapacityFixedStack<MatrixParallelThread> _threads;
|
||||
|
||||
private static readonly
|
||||
#if NET10_0_OR_GREATER
|
||||
Lock
|
||||
#else
|
||||
object
|
||||
#endif
|
||||
_lock = new();
|
||||
|
||||
private static ThreadPriority _AfterWork = ThreadPriority.Lowest;
|
||||
|
||||
public static bool SupportParallel { get; } = Environment.ProcessorCount > 6;
|
||||
|
||||
public static bool IsParallelMeaningful => Environment.ProcessorCount > 6 && IsInitialized;
|
||||
|
||||
private static bool IsInitialized { get; set; } = false;
|
||||
|
||||
private static Queue<IntPtr> LeaderTask { get; set; }
|
||||
|
||||
public static void EndParallel()
|
||||
{
|
||||
Monitor.Exit(_lock);
|
||||
}
|
||||
|
||||
public static void LaunchParallel(MatrixParallelHandle handle, SleepMode afterWork)
|
||||
{
|
||||
_AfterWork = (ThreadPriority)afterWork;
|
||||
MatrixParallelThread leader = handle.GetLeader();
|
||||
foreach (MatrixParallelWrap w in handle.Wraps) {
|
||||
w.SetScheduler();
|
||||
}
|
||||
leader.MatrixParallelLeader();
|
||||
handle.WaitAllWraps();
|
||||
}
|
||||
|
||||
internal static void Init()
|
||||
{
|
||||
if (IsInitialized) {
|
||||
return;
|
||||
}
|
||||
MatrixParallelFactory.Init();
|
||||
if (!SupportParallel)
|
||||
{
|
||||
IsInitialized = true;
|
||||
return;
|
||||
}
|
||||
int count = Environment.ProcessorCount - 4;
|
||||
_threads = new(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
_threads.Push(new());
|
||||
}
|
||||
_awaitingLeaderHandle = new AwaitingQueue();
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel"
|
||||
// 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.Collections;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private static bool _meaningful;
|
||||
|
||||
private static CapacityFixedStack<MatrixParallelThread> _threads;
|
||||
|
||||
private static readonly
|
||||
#if NET10_0_OR_GREATER
|
||||
Lock
|
||||
#else
|
||||
object
|
||||
#endif
|
||||
_lock = new();
|
||||
|
||||
private static ThreadPriority _AfterWork = ThreadPriority.Lowest;
|
||||
|
||||
public static bool SupportParallel { get; } = Environment.ProcessorCount > 6;
|
||||
|
||||
public static bool IsParallelMeaningful => Environment.ProcessorCount > 6 && IsInitialized;
|
||||
|
||||
private static bool IsInitialized { get; set; } = false;
|
||||
|
||||
private static Queue<IntPtr> LeaderTask { get; set; }
|
||||
|
||||
public static void EndParallel()
|
||||
{
|
||||
Monitor.Exit(_lock);
|
||||
}
|
||||
|
||||
public static void LaunchParallel(MatrixParallelHandle handle, SleepMode afterWork)
|
||||
{
|
||||
_AfterWork = (ThreadPriority)afterWork;
|
||||
MatrixParallelThread leader = handle.GetLeader();
|
||||
foreach (MatrixParallelWrap w in handle.Wraps) {
|
||||
w.SetScheduler();
|
||||
}
|
||||
leader.MatrixParallelLeader();
|
||||
handle.WaitAllWraps();
|
||||
}
|
||||
|
||||
internal static void Init()
|
||||
{
|
||||
if (IsInitialized) {
|
||||
return;
|
||||
}
|
||||
MatrixParallelFactory.Init();
|
||||
if (!SupportParallel)
|
||||
{
|
||||
IsInitialized = true;
|
||||
return;
|
||||
}
|
||||
int count = Environment.ProcessorCount - 4;
|
||||
_threads = new(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
_threads.Push(new());
|
||||
}
|
||||
_awaitingLeaderHandle = new AwaitingQueue();
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,197 +1,197 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelHandle"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Collections;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public class MatrixParallelHandle : IDisposable
|
||||
{
|
||||
|
||||
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 MatrixParallelThread[] _threads;
|
||||
private MatrixParallelWrap[] _wraps;
|
||||
private bool _disposed = false;
|
||||
private int _count;
|
||||
private ManualResetEventSlim _leaderEvent = new ManualResetEventSlim(false);
|
||||
|
||||
internal MatrixParallelHandle(MatrixParallelThread[] threadArray)
|
||||
{
|
||||
_threads = threadArray;
|
||||
IsMultithread = threadArray.Length > 1;
|
||||
SliceToWraps();
|
||||
}
|
||||
|
||||
public ReadOnlySpan<MatrixParallelThread> Threads
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||
}
|
||||
return _threads;
|
||||
}
|
||||
}
|
||||
|
||||
internal ReadOnlySpan<MatrixParallelWrap> Wraps
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||
}
|
||||
return _wraps;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsMultithread { get; init; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) {
|
||||
return;
|
||||
}
|
||||
MatrixParallel.ReturnParallelResources(ref _threads);
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
public MatrixParallelThread GetLeader()
|
||||
{
|
||||
MatrixParallelThread leader = Threads[1];
|
||||
leader.Role = MatrixParallelThread.RoleType.Leader;
|
||||
leader.LeaderEvent = _leaderEvent;
|
||||
return leader;
|
||||
}
|
||||
|
||||
public void WaitAllWraps()
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||
}
|
||||
SpinWait.SpinUntil(() => Volatile.Read(ref _count) == 0);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void ReportWrapAccomplished()
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||
}
|
||||
Interlocked.Decrement(ref _count);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static (int count_3, int count_4) FastDecompose(int c)
|
||||
{
|
||||
/*
|
||||
* Thread wrapping to accelerate matrix operations by dividing threads into groups (pool).
|
||||
* For M pool, 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 pool.
|
||||
* - 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.
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
||||
private void SliceToWraps()
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||
}
|
||||
if (_wraps is not null) {
|
||||
return;
|
||||
}
|
||||
int count = _threads.Length;
|
||||
if (count < 6)
|
||||
{
|
||||
MatrixParallelWrap wrap = new MatrixParallelWrap(this, 0, count);
|
||||
_wraps = [ wrap ];
|
||||
} else
|
||||
{
|
||||
(int c3, int c4) = FastDecompose(_threads.Length);
|
||||
_wraps = new MatrixParallelWrap[c3 + c4];
|
||||
int begin = 0;
|
||||
int i = 0;
|
||||
for (; i < c3; i++)
|
||||
{
|
||||
_wraps[i] = new MatrixParallelWrap(this, begin, 3);
|
||||
begin += 3;
|
||||
}
|
||||
for (; i < c3 + c4; i++)
|
||||
{
|
||||
_wraps[i] = new MatrixParallelWrap(this, begin, 4);
|
||||
begin += 4;
|
||||
}
|
||||
}
|
||||
_count = _wraps.Length;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelHandle"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Collections;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public class MatrixParallelHandle : IDisposable
|
||||
{
|
||||
|
||||
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 MatrixParallelThread[] _threads;
|
||||
private MatrixParallelWrap[] _wraps;
|
||||
private bool _disposed = false;
|
||||
private int _count;
|
||||
private ManualResetEventSlim _leaderEvent = new ManualResetEventSlim(false);
|
||||
|
||||
internal MatrixParallelHandle(MatrixParallelThread[] threadArray)
|
||||
{
|
||||
_threads = threadArray;
|
||||
IsMultithread = threadArray.Length > 1;
|
||||
SliceToWraps();
|
||||
}
|
||||
|
||||
public ReadOnlySpan<MatrixParallelThread> Threads
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||
}
|
||||
return _threads;
|
||||
}
|
||||
}
|
||||
|
||||
internal ReadOnlySpan<MatrixParallelWrap> Wraps
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||
}
|
||||
return _wraps;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsMultithread { get; init; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) {
|
||||
return;
|
||||
}
|
||||
MatrixParallel.ReturnParallelResources(ref _threads);
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
public MatrixParallelThread GetLeader()
|
||||
{
|
||||
MatrixParallelThread leader = Threads[1];
|
||||
leader.Role = MatrixParallelThread.RoleType.Leader;
|
||||
leader.LeaderEvent = _leaderEvent;
|
||||
return leader;
|
||||
}
|
||||
|
||||
public void WaitAllWraps()
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||
}
|
||||
SpinWait.SpinUntil(() => Volatile.Read(ref _count) == 0);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void ReportWrapAccomplished()
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||
}
|
||||
Interlocked.Decrement(ref _count);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static (int count_3, int count_4) FastDecompose(int c)
|
||||
{
|
||||
/*
|
||||
* Thread wrapping to accelerate matrix operations by dividing threads into groups (pool).
|
||||
* For M pool, 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 pool.
|
||||
* - 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.
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
||||
private void SliceToWraps()
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||
}
|
||||
if (_wraps is not null) {
|
||||
return;
|
||||
}
|
||||
int count = _threads.Length;
|
||||
if (count < 6)
|
||||
{
|
||||
MatrixParallelWrap wrap = new MatrixParallelWrap(this, 0, count);
|
||||
_wraps = [ wrap ];
|
||||
} else
|
||||
{
|
||||
(int c3, int c4) = FastDecompose(_threads.Length);
|
||||
_wraps = new MatrixParallelWrap[c3 + c4];
|
||||
int begin = 0;
|
||||
int i = 0;
|
||||
for (; i < c3; i++)
|
||||
{
|
||||
_wraps[i] = new MatrixParallelWrap(this, begin, 3);
|
||||
begin += 3;
|
||||
}
|
||||
for (; i < c3 + c4; i++)
|
||||
{
|
||||
_wraps[i] = new MatrixParallelWrap(this, begin, 4);
|
||||
begin += 4;
|
||||
}
|
||||
}
|
||||
_count = _wraps.Length;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,182 +1,182 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelTaskPackage"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
/*
|
||||
* #define PANEL_OP 1
|
||||
* #define BUFFER_OP 2
|
||||
* #define KERNEL_OP 3
|
||||
*
|
||||
* #define RIGHT_ANS_PARAM 1
|
||||
* #define LEFT_RIGHT_ANS_PARAM 2
|
||||
* #define RIGHT_ANS_SCALAR_PARAM 3
|
||||
* #define LEFT_RIGHT_ANS_SCALAR_PARAM 4
|
||||
* #define ANS_SCALAR_PARAM 5
|
||||
*
|
||||
* typedef struct matrix_op_mode {
|
||||
* int16_t operation_style; // PANEL_OP, BUFFER_OP, KERNEL_OP
|
||||
* int16_t parameter_style; // RIGHT_ANS_PARAM, LEFT_RIGHT_ANS_PARAM, e.g.
|
||||
* }
|
||||
* matrix_op_mode;
|
||||
*/
|
||||
|
||||
public enum MatrixOperationSpecial : short
|
||||
{
|
||||
|
||||
SEQ_FMA = -1,
|
||||
|
||||
}
|
||||
|
||||
public enum MatrixIndexStyle : short
|
||||
{
|
||||
|
||||
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) { }
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit, Size = 128)]
|
||||
public unsafe struct MatrixParallelTaskPackage
|
||||
{
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void Copy(
|
||||
MatrixParallelTaskPackage* source,
|
||||
MatrixParallelTaskPackage* target
|
||||
)
|
||||
{
|
||||
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTaskPackage),
|
||||
sizeof(MatrixParallelTaskPackage));
|
||||
}
|
||||
|
||||
#region source
|
||||
|
||||
// Group 1: Left/Right matrix kernel base address
|
||||
[FieldOffset(0)] public IntPtr Left; // 8
|
||||
[FieldOffset(8)] public IntPtr Right; // 8
|
||||
|
||||
#endregion
|
||||
|
||||
#region ans
|
||||
|
||||
// Group 2: Result kernel base address + reserved
|
||||
[FieldOffset(16)] public IntPtr Answer; // 8
|
||||
[FieldOffset(24)] public int ElementSize;
|
||||
[FieldOffset(28)] public int ReservedPtr0; // 4 (extension/reserved)
|
||||
|
||||
#endregion
|
||||
|
||||
#region left enum
|
||||
|
||||
[FieldOffset(32)]public int LeftPrimOffset; // 4
|
||||
[FieldOffset(36)]public int LeftPrimStride; // 4
|
||||
[FieldOffset(40)]public int LeftSecOffset; // 4
|
||||
[FieldOffset(44)]public int LeftSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region right enum
|
||||
|
||||
[FieldOffset(48)]public int RightPrimOffset; // 4
|
||||
[FieldOffset(52)]public int RightPrimStride; // 4
|
||||
[FieldOffset(56)]public int RightSecOffset; // 4
|
||||
[FieldOffset(60)]public int RightSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region ans enum
|
||||
|
||||
[FieldOffset(64)]public int AnsPrimOffset; // 4
|
||||
[FieldOffset(68)]public int AnsPrimStride; // 4
|
||||
[FieldOffset(72)]public int AnsSecOffset; // 4
|
||||
[FieldOffset(76)] public int AnsSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region scalar/compute
|
||||
|
||||
[FieldOffset(80)]public ScalarPacket* Scalar; // 8
|
||||
[FieldOffset(88)]public IntPtr ComputeHandle; // 8
|
||||
|
||||
#endregion
|
||||
|
||||
#region global profile
|
||||
|
||||
[FieldOffset(96)]public int PrimCount; // 4
|
||||
[FieldOffset(100)]public int SecCount; // 4
|
||||
[FieldOffset(104)]public MatrixOpMode ComputeMode; // 4
|
||||
[FieldOffset(108)]private int ReservedInt0; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region count offset
|
||||
|
||||
[FieldOffset(112)]public int PrimTileOffset; // 4
|
||||
[FieldOffset(116)]public int SecTileOffset; // 4
|
||||
[FieldOffset(120)] public int PrimColumnCountInTile; // 4
|
||||
[FieldOffset(124)] public int SecColumnCountInTile; // 4
|
||||
|
||||
#endregion
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelTaskPackage"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
/*
|
||||
* #define PANEL_OP 1
|
||||
* #define BUFFER_OP 2
|
||||
* #define KERNEL_OP 3
|
||||
*
|
||||
* #define RIGHT_ANS_PARAM 1
|
||||
* #define LEFT_RIGHT_ANS_PARAM 2
|
||||
* #define RIGHT_ANS_SCALAR_PARAM 3
|
||||
* #define LEFT_RIGHT_ANS_SCALAR_PARAM 4
|
||||
* #define ANS_SCALAR_PARAM 5
|
||||
*
|
||||
* typedef struct matrix_op_mode {
|
||||
* int16_t operation_style; // PANEL_OP, BUFFER_OP, KERNEL_OP
|
||||
* int16_t parameter_style; // RIGHT_ANS_PARAM, LEFT_RIGHT_ANS_PARAM, e.g.
|
||||
* }
|
||||
* matrix_op_mode;
|
||||
*/
|
||||
|
||||
public enum MatrixOperationSpecial : short
|
||||
{
|
||||
|
||||
SEQ_FMA = -1,
|
||||
|
||||
}
|
||||
|
||||
public enum MatrixIndexStyle : short
|
||||
{
|
||||
|
||||
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) { }
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit, Size = 128)]
|
||||
public unsafe struct MatrixParallelTaskPackage
|
||||
{
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void Copy(
|
||||
MatrixParallelTaskPackage* source,
|
||||
MatrixParallelTaskPackage* target
|
||||
)
|
||||
{
|
||||
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTaskPackage),
|
||||
sizeof(MatrixParallelTaskPackage));
|
||||
}
|
||||
|
||||
#region source
|
||||
|
||||
// Group 1: Left/Right matrix kernel base address
|
||||
[FieldOffset(0)] public IntPtr Left; // 8
|
||||
[FieldOffset(8)] public IntPtr Right; // 8
|
||||
|
||||
#endregion
|
||||
|
||||
#region ans
|
||||
|
||||
// Group 2: Result kernel base address + reserved
|
||||
[FieldOffset(16)] public IntPtr Answer; // 8
|
||||
[FieldOffset(24)] public int ElementSize;
|
||||
[FieldOffset(28)] public int ReservedPtr0; // 4 (extension/reserved)
|
||||
|
||||
#endregion
|
||||
|
||||
#region left enum
|
||||
|
||||
[FieldOffset(32)]public int LeftPrimOffset; // 4
|
||||
[FieldOffset(36)]public int LeftPrimStride; // 4
|
||||
[FieldOffset(40)]public int LeftSecOffset; // 4
|
||||
[FieldOffset(44)]public int LeftSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region right enum
|
||||
|
||||
[FieldOffset(48)]public int RightPrimOffset; // 4
|
||||
[FieldOffset(52)]public int RightPrimStride; // 4
|
||||
[FieldOffset(56)]public int RightSecOffset; // 4
|
||||
[FieldOffset(60)]public int RightSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region ans enum
|
||||
|
||||
[FieldOffset(64)]public int AnsPrimOffset; // 4
|
||||
[FieldOffset(68)]public int AnsPrimStride; // 4
|
||||
[FieldOffset(72)]public int AnsSecOffset; // 4
|
||||
[FieldOffset(76)] public int AnsSecStride; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region scalar/compute
|
||||
|
||||
[FieldOffset(80)]public ScalarPacket* Scalar; // 8
|
||||
[FieldOffset(88)]public IntPtr ComputeHandle; // 8
|
||||
|
||||
#endregion
|
||||
|
||||
#region global profile
|
||||
|
||||
[FieldOffset(96)]public int PrimCount; // 4
|
||||
[FieldOffset(100)]public int SecCount; // 4
|
||||
[FieldOffset(104)]public MatrixOpMode ComputeMode; // 4
|
||||
[FieldOffset(108)]private int ReservedInt0; // 4
|
||||
|
||||
#endregion
|
||||
|
||||
#region count offset
|
||||
|
||||
[FieldOffset(112)]public int PrimTileOffset; // 4
|
||||
[FieldOffset(116)]public int SecTileOffset; // 4
|
||||
[FieldOffset(120)] public int PrimColumnCountInTile; // 4
|
||||
[FieldOffset(124)] public int SecColumnCountInTile; // 4
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,440 +1,440 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelThread"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public partial class MatrixParallelThread : IDisposable
|
||||
{
|
||||
|
||||
// Affinity binding info for this thread
|
||||
private AffinityBinding _affinity;
|
||||
private bool _affinityApplied;
|
||||
|
||||
private bool disposedValue;
|
||||
private readonly ManualResetEventSlim _resetEvent;
|
||||
private readonly Thread _managedThread;
|
||||
|
||||
public MatrixParallelThread()
|
||||
{
|
||||
// Allocate a unique logical processor for this worker (mutually exclusive)
|
||||
_affinity = AffinityAllocator.Allocate();
|
||||
_resetEvent = new ManualResetEventSlim(false);
|
||||
_managedThread = new Thread(MatrixParallelWorker);
|
||||
_managedThread.Start();
|
||||
}
|
||||
|
||||
// Global runtime switch: true => UMA/no-affinity; false => NUMA/affinity (auto-detected)
|
||||
public static bool UseUmaAffinityPath { get; } = DetectUmaAffinityPath();
|
||||
|
||||
// ~MatrixComputeThread()
|
||||
// {
|
||||
// Dispose(disposing: false);
|
||||
// }
|
||||
|
||||
public ThreadPriority AfterWork
|
||||
{
|
||||
get => field;
|
||||
set => field = value;
|
||||
}
|
||||
|
||||
internal ManualResetEventSlim? LeaderEvent { get; set; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing:true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void ExecuteTaskPackage(
|
||||
Queue<IntPtr> taskQueue
|
||||
)
|
||||
{
|
||||
while (taskQueue.TryDequeue(out nint handle))
|
||||
{
|
||||
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)handle;
|
||||
if ((short)p->ComputeMode.ParameterStyle < 0)
|
||||
{
|
||||
MatrixComputeModel.SpecialBufferCompute(p);
|
||||
} else
|
||||
{
|
||||
switch (p->ComputeMode.OperationStyle)
|
||||
{
|
||||
case MatrixIndexStyle.BUFFER_INDEX:
|
||||
switch (p->ComputeMode.ParameterStyle)
|
||||
{
|
||||
case MatrixOperationParam.RIGHT_ANS_PARAM:
|
||||
MatrixComputeModel.BufferComputeUnary(p);
|
||||
break;
|
||||
case MatrixOperationParam.LEFT_RIGHT_ANS_PARAM:
|
||||
MatrixComputeModel.BufferComputeBinary(p);
|
||||
break;
|
||||
case MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM:
|
||||
MatrixComputeModel.BufferComputeUnaryScalar(p);
|
||||
break;
|
||||
case MatrixOperationParam.LEFT_RIGHT_ANS_SCALAR_PARAM:
|
||||
MatrixComputeModel.BufferComputeBinaryScalar(p);
|
||||
break;
|
||||
case MatrixOperationParam.ANS_SCALAR_PARAM:
|
||||
MatrixComputeModel.BufferComputeNoOperatorScalar(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Restart()
|
||||
{
|
||||
if (Role == RoleType.Leader)
|
||||
{
|
||||
LeaderEvent?.Set();
|
||||
} else
|
||||
{
|
||||
_resetEvent.Set();
|
||||
}
|
||||
}
|
||||
|
||||
internal void MatrixParallelLeader()
|
||||
{
|
||||
Thread current = Thread.CurrentThread;
|
||||
current.Priority = ThreadPriority.Highest;
|
||||
LeaderEvent?.Wait();
|
||||
LeaderEvent?.Reset();
|
||||
|
||||
// Console.WriteLine("Leader is computing");
|
||||
#if WINDOWS
|
||||
if (!UseUmaAffinityPath)
|
||||
{
|
||||
// NUMA/affinity path: bind to this thread's reserved mask, then restore
|
||||
nuint previousMask = SetThreadAffinityMask(GetCurrentThread(), _affinity.Mask);
|
||||
bool affinityChanged = previousMask != 0; // nonzero => success
|
||||
Thread.Sleep(0);
|
||||
ExecuteTaskPackage(Tasks);
|
||||
if (affinityChanged) {
|
||||
_ = SetThreadAffinityMask(GetCurrentThread(), previousMask);
|
||||
}
|
||||
} else
|
||||
{
|
||||
// UMA path: no affinity binding
|
||||
ExecuteTaskPackage(Tasks);
|
||||
}
|
||||
#else
|
||||
// Non-Windows: no affinity control
|
||||
ExecuteTaskPackage(Tasks);
|
||||
#endif
|
||||
|
||||
BindedWrap.ReportThreadAccomplished();
|
||||
|
||||
// Console.WriteLine("leader's computing is done");
|
||||
Role = RoleType.Worker;
|
||||
LeaderEvent = null;
|
||||
current.Priority = ThreadPriority.Normal;
|
||||
}
|
||||
|
||||
protected virtual void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_resetEvent.Dispose();
|
||||
_managedThread.Join();
|
||||
}
|
||||
|
||||
// Release the reserved logical processor back to the pool
|
||||
AffinityAllocator.Release(_affinity.LogicalProcessor);
|
||||
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool DetectUmaAffinityPath()
|
||||
{
|
||||
#if WINDOWS
|
||||
try
|
||||
{
|
||||
// Quick heuristic: single CPU looks like UMA
|
||||
if (Environment.ProcessorCount <= 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Query NUMA topology: highest node number 0 => single node => UMA
|
||||
uint highest = 0;
|
||||
if (GetNumaHighestNodeNumber(out highest)) {
|
||||
return highest == 0;
|
||||
}
|
||||
|
||||
// If query fails, prefer UMA (safer: no affinity changes)
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
// Non-Windows: treat as UMA (no affinity API available)
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
private unsafe void MatrixParallelWorker()
|
||||
{
|
||||
// Bind OS-thread affinity once when the worker starts running
|
||||
ApplyAffinityIfNeeded();
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Thread self = Thread.CurrentThread;
|
||||
_managedThread.Priority = ThreadPriority.Highest;
|
||||
_resetEvent.Wait();
|
||||
_resetEvent.Reset();
|
||||
switch (Role)
|
||||
{
|
||||
case RoleType.Worker://worker
|
||||
// _ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
// Console.WriteLine($"worker {Environment.CurrentManagedThreadId} start");
|
||||
ExecuteTaskPackage(Tasks);
|
||||
BindedWrap.ReportThreadAccomplished();
|
||||
|
||||
// Console.WriteLine($"worker {Environment.CurrentManagedThreadId} end");
|
||||
break;
|
||||
case RoleType.Scheduler://scheduler
|
||||
// Console.WriteLine($"scheduler {Environment.CurrentManagedThreadId} start");
|
||||
BindedWrap.PublishTaskToWorkers();
|
||||
BindedWrap.SetAllWorker();
|
||||
|
||||
// Console.WriteLine($"scheduler {Environment.CurrentManagedThreadId} done");
|
||||
ExecuteTaskPackage(Tasks);
|
||||
BindedWrap.ReportThreadAccomplished();
|
||||
BindedWrap.WaitAllWorker();
|
||||
BindedWrap.ReportWrapAccomplished();
|
||||
Role = RoleType.Worker;
|
||||
break;
|
||||
case RoleType.Leader://leader, error because leader should be at outer thread but not here
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
_managedThread.Priority = AfterWork;
|
||||
}
|
||||
}
|
||||
|
||||
#region core affinity
|
||||
|
||||
/*
|
||||
* data to record core affinity
|
||||
* preparation for future NUMA affinity setting
|
||||
*/
|
||||
|
||||
// Represents the binding information for a worker thread
|
||||
internal readonly struct AffinityBinding
|
||||
{
|
||||
|
||||
public readonly int LogicalProcessor;
|
||||
public readonly nuint Mask;
|
||||
|
||||
public AffinityBinding(
|
||||
int logicalProcessor,
|
||||
nuint mask
|
||||
)
|
||||
{
|
||||
LogicalProcessor = logicalProcessor;
|
||||
Mask = mask;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Thread-safe allocator that hands out unique logical processors (mutually exclusive)
|
||||
internal static class AffinityAllocator
|
||||
{
|
||||
|
||||
// Bitset of reserved logical processors (0..63). One bit per CPU.
|
||||
private static long s_reservedMask; // treat as unsigned bits
|
||||
|
||||
public static AffinityBinding Allocate()
|
||||
{
|
||||
int cpuCount = Environment.ProcessorCount;
|
||||
if (cpuCount > 64)
|
||||
{
|
||||
cpuCount = 64; // SetThreadAffinityMask supports up to 64-bit mask
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
long snapshot = Volatile.Read(ref s_reservedMask);
|
||||
|
||||
// Find first zero bit
|
||||
int candidate = -1;
|
||||
long bit = 0;
|
||||
for (int i = 0; i < cpuCount; i++)
|
||||
{
|
||||
bit = 1L << i;
|
||||
if ((snapshot & bit) == 0)
|
||||
{
|
||||
candidate = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (candidate < 0)
|
||||
{
|
||||
// No free CPU found: fall back to CPU 0 (shared)
|
||||
return new AffinityBinding(0, (nuint)1);
|
||||
}
|
||||
|
||||
long desired = snapshot | bit;
|
||||
if (Interlocked.CompareExchange(ref s_reservedMask, desired, snapshot) == snapshot)
|
||||
{
|
||||
// Successfully reserved candidate
|
||||
nuint mask = (nuint)(1UL << candidate);
|
||||
return new AffinityBinding(candidate, mask);
|
||||
}
|
||||
|
||||
// else: another thread raced us; retry
|
||||
}
|
||||
}
|
||||
|
||||
public static void Release(
|
||||
int logicalProcessor
|
||||
)
|
||||
{
|
||||
if ((uint)logicalProcessor >= 64u) {
|
||||
return;
|
||||
}
|
||||
|
||||
long bit = 1L << logicalProcessor;
|
||||
while (true)
|
||||
{
|
||||
long snapshot = Volatile.Read(ref s_reservedMask);
|
||||
if ((snapshot & bit) == 0)
|
||||
{
|
||||
return; // already free
|
||||
}
|
||||
|
||||
long desired = snapshot & ~bit;
|
||||
if (Interlocked.CompareExchange(ref s_reservedMask, desired, snapshot) == snapshot) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ApplyAffinityIfNeeded()
|
||||
{
|
||||
if (_affinityApplied) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if WINDOWS
|
||||
try
|
||||
{
|
||||
// On Windows pin the current native thread to the reserved logical processor
|
||||
SetThreadAffinityMask(GetCurrentThread(), _affinity.Mask);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore binding errors; continue without affinity
|
||||
}
|
||||
#elif LINUX
|
||||
|
||||
#endif
|
||||
|
||||
_affinityApplied = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#if WINDOWS
|
||||
[DllImport("kernel32.dll")]
|
||||
private static extern IntPtr GetCurrentThread();
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
private static extern nuint SetThreadAffinityMask(
|
||||
IntPtr hThread,
|
||||
nuint dwThreadAffinityMask
|
||||
);
|
||||
|
||||
[LibraryImport("kernel32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static partial bool GetNumaHighestNodeNumber(
|
||||
out uint HighestNodeNumber
|
||||
);
|
||||
|
||||
#elif LINUX
|
||||
|
||||
#endif
|
||||
|
||||
#region wrap belong
|
||||
|
||||
internal MatrixParallelWrap BindedWrap { get; set; } = null!;
|
||||
|
||||
internal Queue<IntPtr> Tasks { get; } = [];
|
||||
|
||||
public RoleType Role
|
||||
{
|
||||
get;
|
||||
internal set
|
||||
{
|
||||
// Console.WriteLine($"Thread {_managedThread.ManagedThreadId} set to {value}, from thread {Environment.CurrentManagedThreadId}");
|
||||
field = value;
|
||||
}
|
||||
}
|
||||
|
||||
public enum RoleType : int
|
||||
{
|
||||
|
||||
Worker = 0,
|
||||
Scheduler = 1,
|
||||
Leader = 2,
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelThread"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public partial class MatrixParallelThread : IDisposable
|
||||
{
|
||||
|
||||
// Affinity binding info for this thread
|
||||
private AffinityBinding _affinity;
|
||||
private bool _affinityApplied;
|
||||
|
||||
private bool disposedValue;
|
||||
private readonly ManualResetEventSlim _resetEvent;
|
||||
private readonly Thread _managedThread;
|
||||
|
||||
public MatrixParallelThread()
|
||||
{
|
||||
// Allocate a unique logical processor for this worker (mutually exclusive)
|
||||
_affinity = AffinityAllocator.Allocate();
|
||||
_resetEvent = new ManualResetEventSlim(false);
|
||||
_managedThread = new Thread(MatrixParallelWorker);
|
||||
_managedThread.Start();
|
||||
}
|
||||
|
||||
// Global runtime switch: true => UMA/no-affinity; false => NUMA/affinity (auto-detected)
|
||||
public static bool UseUmaAffinityPath { get; } = DetectUmaAffinityPath();
|
||||
|
||||
// ~MatrixComputeThread()
|
||||
// {
|
||||
// Dispose(disposing: false);
|
||||
// }
|
||||
|
||||
public ThreadPriority AfterWork
|
||||
{
|
||||
get => field;
|
||||
set => field = value;
|
||||
}
|
||||
|
||||
internal ManualResetEventSlim? LeaderEvent { get; set; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing:true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void ExecuteTaskPackage(
|
||||
Queue<IntPtr> taskQueue
|
||||
)
|
||||
{
|
||||
while (taskQueue.TryDequeue(out nint handle))
|
||||
{
|
||||
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)handle;
|
||||
if ((short)p->ComputeMode.ParameterStyle < 0)
|
||||
{
|
||||
MatrixComputeModel.SpecialBufferCompute(p);
|
||||
} else
|
||||
{
|
||||
switch (p->ComputeMode.OperationStyle)
|
||||
{
|
||||
case MatrixIndexStyle.BUFFER_INDEX:
|
||||
switch (p->ComputeMode.ParameterStyle)
|
||||
{
|
||||
case MatrixOperationParam.RIGHT_ANS_PARAM:
|
||||
MatrixComputeModel.BufferComputeUnary(p);
|
||||
break;
|
||||
case MatrixOperationParam.LEFT_RIGHT_ANS_PARAM:
|
||||
MatrixComputeModel.BufferComputeBinary(p);
|
||||
break;
|
||||
case MatrixOperationParam.RIGHT_ANS_SCALAR_PARAM:
|
||||
MatrixComputeModel.BufferComputeUnaryScalar(p);
|
||||
break;
|
||||
case MatrixOperationParam.LEFT_RIGHT_ANS_SCALAR_PARAM:
|
||||
MatrixComputeModel.BufferComputeBinaryScalar(p);
|
||||
break;
|
||||
case MatrixOperationParam.ANS_SCALAR_PARAM:
|
||||
MatrixComputeModel.BufferComputeNoOperatorScalar(p);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Restart()
|
||||
{
|
||||
if (Role == RoleType.Leader)
|
||||
{
|
||||
LeaderEvent?.Set();
|
||||
} else
|
||||
{
|
||||
_resetEvent.Set();
|
||||
}
|
||||
}
|
||||
|
||||
internal void MatrixParallelLeader()
|
||||
{
|
||||
Thread current = Thread.CurrentThread;
|
||||
current.Priority = ThreadPriority.Highest;
|
||||
LeaderEvent?.Wait();
|
||||
LeaderEvent?.Reset();
|
||||
|
||||
// Console.WriteLine("Leader is computing");
|
||||
#if WINDOWS
|
||||
if (!UseUmaAffinityPath)
|
||||
{
|
||||
// NUMA/affinity path: bind to this thread's reserved mask, then restore
|
||||
nuint previousMask = SetThreadAffinityMask(GetCurrentThread(), _affinity.Mask);
|
||||
bool affinityChanged = previousMask != 0; // nonzero => success
|
||||
Thread.Sleep(0);
|
||||
ExecuteTaskPackage(Tasks);
|
||||
if (affinityChanged) {
|
||||
_ = SetThreadAffinityMask(GetCurrentThread(), previousMask);
|
||||
}
|
||||
} else
|
||||
{
|
||||
// UMA path: no affinity binding
|
||||
ExecuteTaskPackage(Tasks);
|
||||
}
|
||||
#else
|
||||
// Non-Windows: no affinity control
|
||||
ExecuteTaskPackage(Tasks);
|
||||
#endif
|
||||
|
||||
BindedWrap.ReportThreadAccomplished();
|
||||
|
||||
// Console.WriteLine("leader's computing is done");
|
||||
Role = RoleType.Worker;
|
||||
LeaderEvent = null;
|
||||
current.Priority = ThreadPriority.Normal;
|
||||
}
|
||||
|
||||
protected virtual void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_resetEvent.Dispose();
|
||||
_managedThread.Join();
|
||||
}
|
||||
|
||||
// Release the reserved logical processor back to the pool
|
||||
AffinityAllocator.Release(_affinity.LogicalProcessor);
|
||||
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool DetectUmaAffinityPath()
|
||||
{
|
||||
#if WINDOWS
|
||||
try
|
||||
{
|
||||
// Quick heuristic: single CPU looks like UMA
|
||||
if (Environment.ProcessorCount <= 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Query NUMA topology: highest node number 0 => single node => UMA
|
||||
uint highest = 0;
|
||||
if (GetNumaHighestNodeNumber(out highest)) {
|
||||
return highest == 0;
|
||||
}
|
||||
|
||||
// If query fails, prefer UMA (safer: no affinity changes)
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
// Non-Windows: treat as UMA (no affinity API available)
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
private unsafe void MatrixParallelWorker()
|
||||
{
|
||||
// Bind OS-thread affinity once when the worker starts running
|
||||
ApplyAffinityIfNeeded();
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Thread self = Thread.CurrentThread;
|
||||
_managedThread.Priority = ThreadPriority.Highest;
|
||||
_resetEvent.Wait();
|
||||
_resetEvent.Reset();
|
||||
switch (Role)
|
||||
{
|
||||
case RoleType.Worker://worker
|
||||
// _ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
// Console.WriteLine($"worker {Environment.CurrentManagedThreadId} start");
|
||||
ExecuteTaskPackage(Tasks);
|
||||
BindedWrap.ReportThreadAccomplished();
|
||||
|
||||
// Console.WriteLine($"worker {Environment.CurrentManagedThreadId} end");
|
||||
break;
|
||||
case RoleType.Scheduler://scheduler
|
||||
// Console.WriteLine($"scheduler {Environment.CurrentManagedThreadId} start");
|
||||
BindedWrap.PublishTaskToWorkers();
|
||||
BindedWrap.SetAllWorker();
|
||||
|
||||
// Console.WriteLine($"scheduler {Environment.CurrentManagedThreadId} done");
|
||||
ExecuteTaskPackage(Tasks);
|
||||
BindedWrap.ReportThreadAccomplished();
|
||||
BindedWrap.WaitAllWorker();
|
||||
BindedWrap.ReportWrapAccomplished();
|
||||
Role = RoleType.Worker;
|
||||
break;
|
||||
case RoleType.Leader://leader, error because leader should be at outer thread but not here
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
_managedThread.Priority = AfterWork;
|
||||
}
|
||||
}
|
||||
|
||||
#region core affinity
|
||||
|
||||
/*
|
||||
* data to record core affinity
|
||||
* preparation for future NUMA affinity setting
|
||||
*/
|
||||
|
||||
// Represents the binding information for a worker thread
|
||||
internal readonly struct AffinityBinding
|
||||
{
|
||||
|
||||
public readonly int LogicalProcessor;
|
||||
public readonly nuint Mask;
|
||||
|
||||
public AffinityBinding(
|
||||
int logicalProcessor,
|
||||
nuint mask
|
||||
)
|
||||
{
|
||||
LogicalProcessor = logicalProcessor;
|
||||
Mask = mask;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Thread-safe allocator that hands out unique logical processors (mutually exclusive)
|
||||
internal static class AffinityAllocator
|
||||
{
|
||||
|
||||
// Bitset of reserved logical processors (0..63). One bit per CPU.
|
||||
private static long s_reservedMask; // treat as unsigned bits
|
||||
|
||||
public static AffinityBinding Allocate()
|
||||
{
|
||||
int cpuCount = Environment.ProcessorCount;
|
||||
if (cpuCount > 64)
|
||||
{
|
||||
cpuCount = 64; // SetThreadAffinityMask supports up to 64-bit mask
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
long snapshot = Volatile.Read(ref s_reservedMask);
|
||||
|
||||
// Find first zero bit
|
||||
int candidate = -1;
|
||||
long bit = 0;
|
||||
for (int i = 0; i < cpuCount; i++)
|
||||
{
|
||||
bit = 1L << i;
|
||||
if ((snapshot & bit) == 0)
|
||||
{
|
||||
candidate = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (candidate < 0)
|
||||
{
|
||||
// No free CPU found: fall back to CPU 0 (shared)
|
||||
return new AffinityBinding(0, (nuint)1);
|
||||
}
|
||||
|
||||
long desired = snapshot | bit;
|
||||
if (Interlocked.CompareExchange(ref s_reservedMask, desired, snapshot) == snapshot)
|
||||
{
|
||||
// Successfully reserved candidate
|
||||
nuint mask = (nuint)(1UL << candidate);
|
||||
return new AffinityBinding(candidate, mask);
|
||||
}
|
||||
|
||||
// else: another thread raced us; retry
|
||||
}
|
||||
}
|
||||
|
||||
public static void Release(
|
||||
int logicalProcessor
|
||||
)
|
||||
{
|
||||
if ((uint)logicalProcessor >= 64u) {
|
||||
return;
|
||||
}
|
||||
|
||||
long bit = 1L << logicalProcessor;
|
||||
while (true)
|
||||
{
|
||||
long snapshot = Volatile.Read(ref s_reservedMask);
|
||||
if ((snapshot & bit) == 0)
|
||||
{
|
||||
return; // already free
|
||||
}
|
||||
|
||||
long desired = snapshot & ~bit;
|
||||
if (Interlocked.CompareExchange(ref s_reservedMask, desired, snapshot) == snapshot) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ApplyAffinityIfNeeded()
|
||||
{
|
||||
if (_affinityApplied) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if WINDOWS
|
||||
try
|
||||
{
|
||||
// On Windows pin the current native thread to the reserved logical processor
|
||||
SetThreadAffinityMask(GetCurrentThread(), _affinity.Mask);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore binding errors; continue without affinity
|
||||
}
|
||||
#elif LINUX
|
||||
|
||||
#endif
|
||||
|
||||
_affinityApplied = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#if WINDOWS
|
||||
[DllImport("kernel32.dll")]
|
||||
private static extern IntPtr GetCurrentThread();
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
private static extern nuint SetThreadAffinityMask(
|
||||
IntPtr hThread,
|
||||
nuint dwThreadAffinityMask
|
||||
);
|
||||
|
||||
[LibraryImport("kernel32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static partial bool GetNumaHighestNodeNumber(
|
||||
out uint HighestNodeNumber
|
||||
);
|
||||
|
||||
#elif LINUX
|
||||
|
||||
#endif
|
||||
|
||||
#region wrap belong
|
||||
|
||||
internal MatrixParallelWrap BindedWrap { get; set; } = null!;
|
||||
|
||||
internal Queue<IntPtr> Tasks { get; } = [];
|
||||
|
||||
public RoleType Role
|
||||
{
|
||||
get;
|
||||
internal set
|
||||
{
|
||||
// Console.WriteLine($"Thread {_managedThread.ManagedThreadId} set to {value}, from thread {Environment.CurrentManagedThreadId}");
|
||||
field = value;
|
||||
}
|
||||
}
|
||||
|
||||
public enum RoleType : int
|
||||
{
|
||||
|
||||
Worker = 0,
|
||||
Scheduler = 1,
|
||||
Leader = 2,
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,189 +1,189 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelWrap"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Net.WebSockets;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public unsafe class MatrixParallelWrap
|
||||
{
|
||||
|
||||
private int _begin;
|
||||
|
||||
private int _remainThreadCount;
|
||||
private readonly MatrixParallelHandle _handle;
|
||||
|
||||
internal MatrixParallelWrap(
|
||||
MatrixParallelHandle handle,
|
||||
int begin,
|
||||
int count
|
||||
)
|
||||
{
|
||||
WrapTaskCapacity = count;
|
||||
_handle = handle;
|
||||
_begin = begin;
|
||||
for (int i = 0; i < WrapTaskCapacity; i++)
|
||||
{
|
||||
Threads[i].Role = MatrixParallelThread.RoleType.Worker;
|
||||
Threads[i].BindedWrap = this;
|
||||
}
|
||||
Threads[0].Role = MatrixParallelThread.RoleType.Scheduler;
|
||||
}
|
||||
|
||||
public static SleepMode AfterWork
|
||||
{
|
||||
get;
|
||||
set => Interlocked.Exchange(ref Unsafe.As<SleepMode, int>(ref field), (int)value);
|
||||
}
|
||||
|
||||
internal int Magic { get; set; }
|
||||
|
||||
private ReadOnlySpan<MatrixParallelThread> Threads
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _handle.Threads[_begin..(_begin + WrapTaskCapacity)];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void ReportThreadAccomplished()
|
||||
{
|
||||
Interlocked.Decrement(ref _remainThreadCount);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void ReportWrapAccomplished()
|
||||
{
|
||||
_handle.ReportWrapAccomplished();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetAllWorker()
|
||||
{
|
||||
for (int i = 1; i < WrapTaskCapacity; i++) {
|
||||
Threads[i].Restart();
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WaitAllWorker()
|
||||
{
|
||||
SpinWait.SpinUntil(() => Volatile.Read(ref _remainThreadCount) == 0);
|
||||
}
|
||||
|
||||
internal void PublishTaskToWorkers()
|
||||
{
|
||||
Queue<nint> tasks = WrapTask;
|
||||
while (tasks.TryDequeue(out nint handle))
|
||||
{
|
||||
MatrixParallelTaskPackage* task = (MatrixParallelTaskPackage*)handle;
|
||||
if ((int)task->ComputeMode.OperationStyle > 0)
|
||||
{
|
||||
PublishBufferTaskToWorkers(task);
|
||||
} else
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void SetScheduler()
|
||||
{
|
||||
MatrixParallelThread thread = Threads[0];
|
||||
thread.Restart();
|
||||
_ = Interlocked.Exchange(ref _remainThreadCount, WrapTaskCapacity);
|
||||
}
|
||||
|
||||
private void PublishBufferTaskToWorkers(
|
||||
MatrixParallelTaskPackage* package
|
||||
)
|
||||
{
|
||||
ReadOnlySpan<MatrixParallelThread> pool = Threads;
|
||||
if (pool.Length == 1)
|
||||
{
|
||||
pool[0].Tasks.Enqueue((nint)package);
|
||||
return;
|
||||
}
|
||||
long count = Unsafe.As<int, long>(ref package->PrimCount);
|
||||
long actual = count / pool.Length;
|
||||
long extra = count % pool.Length;
|
||||
|
||||
long offset = 0;
|
||||
MatrixParallelTaskPackage* current = package;
|
||||
/*
|
||||
string msg = $"""
|
||||
Total: count={count}, length={Threads.Length}{Environment.NewLine}
|
||||
""";
|
||||
/**/
|
||||
foreach (MatrixParallelThread thread in pool)
|
||||
{
|
||||
long wrapTaskLength = actual;
|
||||
long ex = extra > 0 ? 1 : 0;
|
||||
long length = wrapTaskLength + ex;
|
||||
extra -= ex;
|
||||
ref long offsetRef = ref Unsafe.As<int, long>(ref current->PrimTileOffset);
|
||||
offsetRef += offset;
|
||||
ref long lengthRef = ref Unsafe.As<int, long>(ref current->PrimCount);
|
||||
lengthRef = length;
|
||||
thread.Tasks.Enqueue((nint)current);
|
||||
/*
|
||||
msg += $"""
|
||||
Slice to thread: total offset={offsetRef} local offset={offset}, length={lengthRef}{Environment.NewLine}
|
||||
""";
|
||||
/**/
|
||||
current = MatrixParallelFactory.FromExistPackage(current);
|
||||
offset = length;
|
||||
}
|
||||
|
||||
// Console.WriteLine(msg);
|
||||
}
|
||||
|
||||
#region wrap task
|
||||
|
||||
internal unsafe Queue<nint> WrapTask { get; set; } = [];
|
||||
|
||||
internal int WrapTaskCapacity { get ; init; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public enum SleepMode : int
|
||||
{
|
||||
|
||||
QuickWakeup = ThreadPriority.AboveNormal,
|
||||
NormalSleep = ThreadPriority.BelowNormal,
|
||||
DeepSleep = ThreadPriority.Lowest,
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelWrap"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Net.WebSockets;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public unsafe class MatrixParallelWrap
|
||||
{
|
||||
|
||||
private int _begin;
|
||||
|
||||
private int _remainThreadCount;
|
||||
private readonly MatrixParallelHandle _handle;
|
||||
|
||||
internal MatrixParallelWrap(
|
||||
MatrixParallelHandle handle,
|
||||
int begin,
|
||||
int count
|
||||
)
|
||||
{
|
||||
WrapTaskCapacity = count;
|
||||
_handle = handle;
|
||||
_begin = begin;
|
||||
for (int i = 0; i < WrapTaskCapacity; i++)
|
||||
{
|
||||
Threads[i].Role = MatrixParallelThread.RoleType.Worker;
|
||||
Threads[i].BindedWrap = this;
|
||||
}
|
||||
Threads[0].Role = MatrixParallelThread.RoleType.Scheduler;
|
||||
}
|
||||
|
||||
public static SleepMode AfterWork
|
||||
{
|
||||
get;
|
||||
set => Interlocked.Exchange(ref Unsafe.As<SleepMode, int>(ref field), (int)value);
|
||||
}
|
||||
|
||||
internal int Magic { get; set; }
|
||||
|
||||
private ReadOnlySpan<MatrixParallelThread> Threads
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _handle.Threads[_begin..(_begin + WrapTaskCapacity)];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void ReportThreadAccomplished()
|
||||
{
|
||||
Interlocked.Decrement(ref _remainThreadCount);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void ReportWrapAccomplished()
|
||||
{
|
||||
_handle.ReportWrapAccomplished();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetAllWorker()
|
||||
{
|
||||
for (int i = 1; i < WrapTaskCapacity; i++) {
|
||||
Threads[i].Restart();
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WaitAllWorker()
|
||||
{
|
||||
SpinWait.SpinUntil(() => Volatile.Read(ref _remainThreadCount) == 0);
|
||||
}
|
||||
|
||||
internal void PublishTaskToWorkers()
|
||||
{
|
||||
Queue<nint> tasks = WrapTask;
|
||||
while (tasks.TryDequeue(out nint handle))
|
||||
{
|
||||
MatrixParallelTaskPackage* task = (MatrixParallelTaskPackage*)handle;
|
||||
if ((int)task->ComputeMode.OperationStyle > 0)
|
||||
{
|
||||
PublishBufferTaskToWorkers(task);
|
||||
} else
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void SetScheduler()
|
||||
{
|
||||
MatrixParallelThread thread = Threads[0];
|
||||
thread.Restart();
|
||||
_ = Interlocked.Exchange(ref _remainThreadCount, WrapTaskCapacity);
|
||||
}
|
||||
|
||||
private void PublishBufferTaskToWorkers(
|
||||
MatrixParallelTaskPackage* package
|
||||
)
|
||||
{
|
||||
ReadOnlySpan<MatrixParallelThread> pool = Threads;
|
||||
if (pool.Length == 1)
|
||||
{
|
||||
pool[0].Tasks.Enqueue((nint)package);
|
||||
return;
|
||||
}
|
||||
long count = Unsafe.As<int, long>(ref package->PrimCount);
|
||||
long actual = count / pool.Length;
|
||||
long extra = count % pool.Length;
|
||||
|
||||
long offset = 0;
|
||||
MatrixParallelTaskPackage* current = package;
|
||||
/*
|
||||
string msg = $"""
|
||||
Total: count={count}, length={Threads.Length}{Environment.NewLine}
|
||||
""";
|
||||
/**/
|
||||
foreach (MatrixParallelThread thread in pool)
|
||||
{
|
||||
long wrapTaskLength = actual;
|
||||
long ex = extra > 0 ? 1 : 0;
|
||||
long length = wrapTaskLength + ex;
|
||||
extra -= ex;
|
||||
ref long offsetRef = ref Unsafe.As<int, long>(ref current->PrimTileOffset);
|
||||
offsetRef += offset;
|
||||
ref long lengthRef = ref Unsafe.As<int, long>(ref current->PrimCount);
|
||||
lengthRef = length;
|
||||
thread.Tasks.Enqueue((nint)current);
|
||||
/*
|
||||
msg += $"""
|
||||
Slice to thread: total offset={offsetRef} local offset={offset}, length={lengthRef}{Environment.NewLine}
|
||||
""";
|
||||
/**/
|
||||
current = MatrixParallelFactory.FromExistPackage(current);
|
||||
offset = length;
|
||||
}
|
||||
|
||||
// Console.WriteLine(msg);
|
||||
}
|
||||
|
||||
#region wrap task
|
||||
|
||||
internal unsafe Queue<nint> WrapTask { get; set; } = [];
|
||||
|
||||
internal int WrapTaskCapacity { get ; init; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public enum SleepMode : int
|
||||
{
|
||||
|
||||
QuickWakeup = ThreadPriority.AboveNormal,
|
||||
NormalSleep = ThreadPriority.BelowNormal,
|
||||
DeepSleep = ThreadPriority.Lowest,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,131 +1,131 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixTaskFactory"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using 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<MatrixParallelTaskPackage> _wrapAllocator;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static MatrixParallelTaskPackage* CreateBaseTask<T>() where T : unmanaged, INumber<T>
|
||||
{
|
||||
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
|
||||
Span<byte> s = new Span<byte>((byte*)p, sizeof(MatrixParallelTaskPackage));
|
||||
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* FromExistPackage<T>(MatrixParallelTaskPackage* exist)
|
||||
where T : unmanaged, INumber<T>
|
||||
{
|
||||
MatrixParallelTaskPackage* ptr = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
|
||||
MatrixParallelTaskPackage.Copy(exist, ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe MatrixParallelTaskPackage* FromExistPackage(MatrixParallelTaskPackage* exist)
|
||||
{
|
||||
MatrixParallelTaskPackage* ptr = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
|
||||
MatrixParallelTaskPackage.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(MatrixParallelTaskPackage* source)
|
||||
{
|
||||
if (source->Scalar != null) {
|
||||
_scalarAllocator.Free((nuint)source->Scalar);
|
||||
}
|
||||
_wrapAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Release<T>(MatrixParallelTaskPackage* 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<MatrixParallelTaskPackage>.
|
||||
Create(128 * 1024, SlabBufferLayout.Chunked);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixTaskFactory"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using 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<MatrixParallelTaskPackage> _wrapAllocator;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static MatrixParallelTaskPackage* CreateBaseTask<T>() where T : unmanaged, INumber<T>
|
||||
{
|
||||
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
|
||||
Span<byte> s = new Span<byte>((byte*)p, sizeof(MatrixParallelTaskPackage));
|
||||
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* FromExistPackage<T>(MatrixParallelTaskPackage* exist)
|
||||
where T : unmanaged, INumber<T>
|
||||
{
|
||||
MatrixParallelTaskPackage* ptr = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
|
||||
MatrixParallelTaskPackage.Copy(exist, ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe MatrixParallelTaskPackage* FromExistPackage(MatrixParallelTaskPackage* exist)
|
||||
{
|
||||
MatrixParallelTaskPackage* ptr = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
|
||||
MatrixParallelTaskPackage.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(MatrixParallelTaskPackage* source)
|
||||
{
|
||||
if (source->Scalar != null) {
|
||||
_scalarAllocator.Free((nuint)source->Scalar);
|
||||
}
|
||||
_wrapAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Release<T>(MatrixParallelTaskPackage* 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<MatrixParallelTaskPackage>.
|
||||
Create(128 * 1024, SlabBufferLayout.Chunked);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,47 +1,47 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="NumericalModule"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public class NumericalModule : IStaticModule
|
||||
{
|
||||
|
||||
public string ModuleName => "Math.Numerical";
|
||||
|
||||
public void InitializeModule()
|
||||
{
|
||||
MatrixParallel.Init();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="NumericalModule"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public class NumericalModule : IStaticModule
|
||||
{
|
||||
|
||||
public string ModuleName => "Math.Numerical";
|
||||
|
||||
public void InitializeModule()
|
||||
{
|
||||
MatrixParallel.Init();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user