working on dynamic wrapping, a good awaiting is on way
This commit is contained in:
@@ -47,6 +47,16 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
Matrix<float> ans = Matrix<float>.FromDefault(left.ColumnLength, left.RowLength);
|
||||
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;
|
||||
for (int i = 0; i < count; i++) {
|
||||
NativeIntrinsic.Intrinsic.f32_add(leftPtr + i, rightPtr + i, ansPtr + i);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
if (count <= 1024)
|
||||
{
|
||||
ReadOnlySpan<MatrixParallelThread> pool = MatrixParallel.LeadParallel(1);
|
||||
@@ -64,50 +74,14 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
Stack<MatrixParallelWrap> taskGroup = MatrixParallel.PublicTask(left.GetColumnEnumeration(), right.GetColumnEnumeration(), ans.GetColumnEnumeration(),
|
||||
MatrixParallelHandle taskGroup = MatrixParallel.PublicTask(left.GetColumnEnumeration(), right.GetColumnEnumeration(), ans.GetColumnEnumeration(),
|
||||
(delegate*<MatrixKernel<float>*, MatrixKernel<float>*, MatrixKernel<float>*, void>)NativeIntrinsic.Intrinsic.f32_add);
|
||||
|
||||
MatrixParallel.LaunchParallel(taskGroup, SleepMode.DeepSleep);
|
||||
/*
|
||||
* run parallel
|
||||
*/
|
||||
return ans;
|
||||
}
|
||||
|
||||
public static unsafe Matrix<float> Sub(Matrix<float> left, Matrix<float> right)
|
||||
{
|
||||
if (left.ColumnLength != right.ColumnLength || left.RowLength != right.RowLength) {
|
||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||
}
|
||||
Matrix<float> ans = Matrix<float>.FromDefault(left.ColumnLength, left.RowLength);
|
||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||
if (count <= 1024)
|
||||
{
|
||||
// TODO: this is very awkward, blocking thread renting, need a better way to handle small tasks
|
||||
ReadOnlySpan<MatrixParallelThread> pool = MatrixParallel.LeadParallel(1);
|
||||
if (pool.Length == 0)
|
||||
{
|
||||
/*
|
||||
* wait
|
||||
*/
|
||||
}
|
||||
MatrixParallel.ReleaseParallelLeadership(ref pool);
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* rightPtr = right.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
for (int i = 0; i < count; i++) {
|
||||
NativeIntrinsic.Intrinsic.f32_sub(leftPtr + i, rightPtr + i, ansPtr + i);
|
||||
}
|
||||
|
||||
// thread.
|
||||
|
||||
return ans;
|
||||
}
|
||||
Stack<MatrixParallelWrap> taskGroup = MatrixParallel.PublicTask(left.GetColumnEnumeration(), right.GetColumnEnumeration(), ans.GetColumnEnumeration(),
|
||||
(delegate*<MatrixKernel<float>*, MatrixKernel<float>*, MatrixKernel<float>*, void>)NativeIntrinsic.Intrinsic.f32_sub);
|
||||
/*
|
||||
* run parallel
|
||||
*/
|
||||
taskGroup.Dispose();
|
||||
return ans;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// 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.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private static ConcurrentQueue<WaitHandle> _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 = _createGeneration,
|
||||
LeastCount = min,
|
||||
BestCount = recommendCount
|
||||
};
|
||||
_awaitingLeaderHandle.Enqueue(handle);
|
||||
int cycleGeneration = _createGeneration;
|
||||
while (true)
|
||||
{
|
||||
// TODO 排队等待,对前八个按照优先级排序,优先级最高的独占进行等待,其他继续排队
|
||||
if (_createGeneration != cycleGeneration)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _sortState, 1) == 0)
|
||||
{
|
||||
// TODO 排序
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
} else
|
||||
{
|
||||
cycleGeneration = _createGeneration;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (_awaitingLeaderHandle.TryPeek(out WaitHandle? p) && p == handle)
|
||||
{
|
||||
// get the leader
|
||||
ret = _threads.PopRange(recommendCount);
|
||||
_awaitingLeaderHandle.TryDequeue(out _);
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(0);
|
||||
}
|
||||
} else
|
||||
{
|
||||
// directly get the leader
|
||||
ret = _threads.PopRange(recommendCount);
|
||||
}
|
||||
_ = Interlocked.Increment(ref _createGeneration);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static void ReturnParallelResources(ref MatrixParallelThread[] pool)
|
||||
{
|
||||
_threads.PushRange(pool);
|
||||
pool = null!;
|
||||
}
|
||||
|
||||
private record class WaitHandle
|
||||
{
|
||||
|
||||
public int CreateGeneration { get; set; }
|
||||
|
||||
public int LeastCount { get; set; }
|
||||
|
||||
public int BestCount { get; set; }
|
||||
|
||||
public int CurrentPriority(int currentGeneration)
|
||||
{
|
||||
return (currentGeneration - CreateGeneration) * (MatrixParallel._threads.Capacity - LeastCount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -40,10 +40,10 @@ namespace StgSharp.Mathematics.Numeric
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation)
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
@@ -66,11 +66,11 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation)
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
MatrixParallelTaskPackage<T>* package = MatrixParallelFactory.CreateBaseTask<T>();
|
||||
@@ -99,11 +99,11 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
|
||||
// Unary with 1..8 scalars (each scalar is independent generic type)
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
{
|
||||
@@ -130,12 +130,12 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -165,12 +165,12 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
|
||||
// Binary with 1..8 scalars (each scalar is independent generic type)
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
{
|
||||
@@ -202,13 +202,13 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -239,13 +239,13 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -279,14 +279,14 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -319,14 +319,14 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -362,15 +362,15 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -405,15 +405,15 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -451,16 +451,16 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -497,16 +497,16 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -546,17 +546,17 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -595,17 +595,17 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -647,18 +647,18 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6, TScalar7>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6,
|
||||
TScalar7 s7)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6, TScalar7>(
|
||||
MatrixSegmentEnumeration<T> source,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6,
|
||||
TScalar7 s7)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -699,18 +699,18 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -754,19 +754,19 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
public static unsafe Stack<MatrixParallelWrap> PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6, TScalar7>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6,
|
||||
TScalar7 s7)
|
||||
public static unsafe MatrixParallelHandle PublicTask<T, TScalar0, TScalar1, TScalar2, TScalar3, TScalar4, TScalar5, TScalar6, TScalar7>(
|
||||
MatrixSegmentEnumeration<T> left,
|
||||
MatrixSegmentEnumeration<T> right,
|
||||
MatrixSegmentEnumeration<T> ans,
|
||||
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation,
|
||||
TScalar0 s0,
|
||||
TScalar1 s1,
|
||||
TScalar2 s2,
|
||||
TScalar3 s3,
|
||||
TScalar4 s4,
|
||||
TScalar5 s5,
|
||||
TScalar6 s6,
|
||||
TScalar7 s7)
|
||||
where T: unmanaged, INumber<T>
|
||||
where TScalar0: unmanaged, INumber<TScalar0>
|
||||
where TScalar1: unmanaged, INumber<TScalar1>
|
||||
@@ -812,7 +812,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return PublicTaskPrivate<T>(package);
|
||||
}
|
||||
|
||||
private static unsafe Stack<MatrixParallelWrap> PublicTaskPrivate<T>(MatrixParallelTaskPackage<T>* package)
|
||||
private static unsafe MatrixParallelHandle PublicTaskPrivate<T>(MatrixParallelTaskPackage<T>* package)
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
int primCount = package->PrimCount;
|
||||
@@ -820,15 +820,21 @@ namespace StgSharp.Mathematics.Numeric
|
||||
int tileWidth = (1024 / secCount) + 1;
|
||||
int tileCount = (primCount + tileWidth - 1) / tileWidth;
|
||||
|
||||
ReadOnlySpan<MatrixParallelThread> pool = LeadParallel(tileCount);
|
||||
MatrixParallelThread[] pool = LeadParallel(tileCount);
|
||||
|
||||
int threadCount = pool.Length;
|
||||
if (pool.Length == 0)
|
||||
{
|
||||
/*
|
||||
* wait until has enough threads
|
||||
*/
|
||||
}
|
||||
MatrixParallelHandle handle = new MatrixParallelHandle(pool);
|
||||
if (handle.Wraps.Count() == 1)
|
||||
{
|
||||
handle.Wraps.First().WrapTask = (MatrixParallelTaskPackageNonGeneric*)package;
|
||||
return handle;
|
||||
}
|
||||
int threadCount = pool.Length;
|
||||
int tilePerThreadLess = tileCount / threadCount; // tiles per thread without extra slice
|
||||
int columnAfterBaseTile = tileCount % threadCount * tileWidth; // tile sliced into columns
|
||||
int extraColumnPerThread = columnAfterBaseTile / threadCount;
|
||||
@@ -837,13 +843,13 @@ namespace StgSharp.Mathematics.Numeric
|
||||
int total = primCount;
|
||||
Stack<MatrixParallelWrap> taskBunch = new Stack<MatrixParallelWrap>(threadCount);
|
||||
MatrixParallelTaskPackage<T>* current = package;
|
||||
foreach (MatrixParallelThread thread in pool)
|
||||
foreach (MatrixParallelWrap wrap in handle.Wraps)
|
||||
{
|
||||
bool hasSlice = columnAfterBaseTile > 0;
|
||||
bool isLargeSlice = extraColumnCount > 0;
|
||||
|
||||
// count tiles for wrap
|
||||
int wrapWidth =/**/ 3/* wrap!.WrapTaskCapacity/**/;
|
||||
int wrapWidth = wrap!.WrapTaskCapacity/**/;
|
||||
int columnFromSlicingCount = (hasSlice ? extraColumnPerThread : 0) * wrapWidth;
|
||||
int baseColumn = wrapWidth * tilePerThreadLess * tileWidth;
|
||||
int columnInWrap = baseColumn + columnFromSlicingCount + (isLargeSlice ? 1 : 0);
|
||||
@@ -869,10 +875,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
total -= columnInWrap;
|
||||
|
||||
// set to wrap and push
|
||||
// wrap.Tasks.Enqueue((IntPtr)current);
|
||||
|
||||
// taskBunch.Push(wrap);
|
||||
|
||||
wrap.WrapTask = (MatrixParallelTaskPackageNonGeneric*)current;
|
||||
|
||||
if (total <= 0)
|
||||
{
|
||||
@@ -882,7 +885,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
current->PrimTileOffset += columnInWrap;
|
||||
}
|
||||
|
||||
return taskBunch;
|
||||
return handle;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Collections;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
@@ -74,46 +75,18 @@ namespace StgSharp.Mathematics.Numeric
|
||||
for (int i = 0; i < count; i++) {
|
||||
_threads.Push(new());
|
||||
}
|
||||
_awaitingLeaderHandle = new ConcurrentQueue<WaitHandle>();
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
public static void LaunchParallel(IEnumerable<MatrixParallelWrap> wraps, SleepMode afterWork)
|
||||
public static void LaunchParallel(MatrixParallelHandle handle, SleepMode afterWork)
|
||||
{
|
||||
RemainThreadCount = _threads.Count;
|
||||
_AfterWork = (ThreadPriority)afterWork;
|
||||
ResetThreadCount();
|
||||
foreach (MatrixParallelWrap w in wraps) {
|
||||
foreach (MatrixParallelWrap w in handle.Wraps) {
|
||||
w.SchedulerReset.Set();
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ResetThreadCount()
|
||||
{
|
||||
RemainThreadCount = _threads.Count;
|
||||
}
|
||||
|
||||
#region wrap field
|
||||
|
||||
public static MatrixParallelThread[] LeadParallel(int recommendCount)
|
||||
{
|
||||
#if NET9_0_OR_GREATER
|
||||
#else
|
||||
Monitor.Enter(_lock);
|
||||
#endif
|
||||
|
||||
return _threads.PopRange(recommendCount);
|
||||
}
|
||||
|
||||
public static void ReleaseParallelLeadership(ref MatrixParallelThread[] pool)
|
||||
{
|
||||
#if NET9_0_OR_GREATER
|
||||
#else
|
||||
Monitor.Exit(_lock);
|
||||
#endif
|
||||
pool = null!;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -78,6 +78,17 @@ namespace StgSharp.Mathematics.Numeric
|
||||
}
|
||||
}
|
||||
|
||||
internal IEnumerable<MatrixParallelWrap> Wraps
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||
}
|
||||
return _wraps;
|
||||
}
|
||||
}
|
||||
|
||||
internal ReadOnlySpan<MatrixParallelThread> ThreadsUnsafe
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -91,10 +102,16 @@ namespace StgSharp.Mathematics.Numeric
|
||||
if (_disposed) {
|
||||
return;
|
||||
}
|
||||
MatrixParallel.ReleaseParallelLeadership(ref _threads);
|
||||
MatrixParallel.ReturnParallelResources(ref _threads);
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
public MatrixParallelThread GetLeader()
|
||||
{
|
||||
Threads[0].Role = 2;
|
||||
return Threads[0];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static (int count_3, int count_4) FastDecompose(int c)
|
||||
{
|
||||
@@ -139,11 +156,22 @@ namespace StgSharp.Mathematics.Numeric
|
||||
int count = _threads.Length;
|
||||
if (count < 6)
|
||||
{
|
||||
MatrixParallelWrap wrap = new MatrixParallelWrap(_threads);
|
||||
MatrixParallelWrap wrap = new MatrixParallelWrap(_threads, 0, count);
|
||||
_wraps = [ wrap ];
|
||||
}
|
||||
(int c3,int c4) = FastDecompose(_threads.Length);
|
||||
_wraps = new(c3 + c4);
|
||||
int begin = 0;
|
||||
for (int i = 0; i < c3; i++)
|
||||
{
|
||||
_wraps.Push(new MatrixParallelWrap(_threads, begin, 3));
|
||||
begin += 3;
|
||||
}
|
||||
for (int i = 0; i < c4; i++)
|
||||
{
|
||||
_wraps.Push(new MatrixParallelWrap(_threads, begin, 4));
|
||||
begin += 4;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -118,30 +118,27 @@ namespace StgSharp.Mathematics.Numeric
|
||||
private unsafe void MatrixParallelWorker()
|
||||
{
|
||||
// Thread self = Thread.CurrentThread;
|
||||
do
|
||||
_resetEvent.Wait();
|
||||
_resetEvent.Reset();
|
||||
_managedThread.Priority = ThreadPriority.Highest;
|
||||
switch (Role)
|
||||
{
|
||||
_resetEvent.Wait();
|
||||
_resetEvent.Reset();
|
||||
_managedThread.Priority = ThreadPriority.Highest;
|
||||
switch (Role)
|
||||
{
|
||||
case 0://worker
|
||||
// _ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
ExecuteTaskPackage(Tasks);
|
||||
break;
|
||||
case 1://scheduler
|
||||
BindedWrap.PublishTaskToWorkers(Tasks);
|
||||
BindedWrap.SetAllWorker();
|
||||
ExecuteTaskPackage(Tasks);
|
||||
case 0://worker
|
||||
// _ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
ExecuteTaskPackage(Tasks);
|
||||
break;
|
||||
case 1://scheduler
|
||||
BindedWrap.PublishTaskToWorkers(Tasks);
|
||||
BindedWrap.SetAllWorker();
|
||||
ExecuteTaskPackage(Tasks);
|
||||
|
||||
break;
|
||||
case 2://leader, error because leader should be at outer thread but not here
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
_managedThread.Priority = AfterWork;
|
||||
} while (true);
|
||||
break;
|
||||
case 2://leader, error because leader should be at outer thread but not here
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
_managedThread.Priority = AfterWork;
|
||||
}
|
||||
|
||||
#region wrap belong
|
||||
@@ -150,10 +147,10 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
internal Queue<IntPtr> Tasks { get; } = [];
|
||||
|
||||
internal int Role
|
||||
public int Role
|
||||
{
|
||||
get;
|
||||
set
|
||||
internal set
|
||||
{
|
||||
if ((Role & (~3u)) == 0)
|
||||
{
|
||||
|
||||
@@ -42,10 +42,12 @@ namespace StgSharp.Mathematics.Numeric
|
||||
private readonly MatrixParallelThread[] _threads;
|
||||
private int _begin;
|
||||
|
||||
internal MatrixParallelWrap(MatrixParallelThread[] threads)
|
||||
internal MatrixParallelWrap(MatrixParallelThread[] threads, int begin, int count)
|
||||
{
|
||||
_threads = threads;
|
||||
_begin = begin;
|
||||
Count = threads.Length;
|
||||
_threads[_begin].Role = 1;
|
||||
}
|
||||
|
||||
public MatrixParallelWrap()
|
||||
@@ -90,7 +92,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
int primAverage = primCount / Count;
|
||||
int secAverage = secCount / Count;
|
||||
bool shouldSliceSec = secAverage > 512;
|
||||
for (int i = 0; i < Count; i++)
|
||||
for (int i = _begin; i < _begin + Count; i++)
|
||||
{
|
||||
Queue<IntPtr> tasks = _threads[i].Tasks;
|
||||
MatrixParallelTaskPackageNonGeneric* subTask = MatrixParallelFactory.FromExistPackage(taskPackage);
|
||||
|
||||
Reference in New Issue
Block a user