Daily update on 20251023-010006 by Nitload Youmu. Additional Message : Put matrix parallel pool and other supporting methods into individual classes.

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2025-10-23 01:00:06 +08:00
parent affaf03845
commit 3b23272f38
2 changed files with 34 additions and 34 deletions
+9 -13
View File
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixParallel"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -35,13 +35,13 @@ using System.Threading;
namespace StgSharp.Mathematics.Numeric
{
/*
* Thread wrapping to accelerate matrix operations by dividing threads into groups (wraps).
* For M wraps, each containing an average of N threads, we satisfy the condition M * N = T - 4,
* 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 wraps.
* - 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),
@@ -55,8 +55,7 @@ namespace StgSharp.Mathematics.Numeric
{
private static Thread[] _threads;
private static readonly (int a, int b)[] LookupTable = [
(1, 1), // 7
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
@@ -134,10 +133,7 @@ namespace StgSharp.Mathematics.Numeric
}
} else if (count == 6)
{
Pool = new(6, [
new MatrixParallelWrap(0, 3),
new MatrixParallelWrap(3, 3)
]);
Pool = new(6, [ new MatrixParallelWrap(0, 3), new MatrixParallelWrap(3, 3) ]);
(LargeWrapCount, SmallWrapCount) = (0, 2);
} else
{
@@ -199,13 +195,13 @@ namespace StgSharp.Mathematics.Numeric
return Pool;
}
public static void ReleaseParallelLeadership(ref CapacityFixedStack<MatrixParallelWrap> wraps)
public static void ReleaseParallelLeadership(ref MatrixParallelPool pool)
{
#if NET9_0_OR_GREATER
#else
Monitor.Exit(_lock);
#endif
wraps = null!;
pool = null!;
}
#endregion
@@ -1,27 +1,30 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallelPool"
// file="MatrixParallelPool.cs"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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.
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -42,13 +45,14 @@ namespace StgSharp.Mathematics.Numeric
{
throw new NotSupportedException();
}
/*
internal MatrixParallelPool(int threadCount, ReadOnlySpan<MatrixParallelWrap> wrapSpan)
{
SpareWrap = CapacityFixedStackBuilder.Create(wrapSpan);
SingleThreadAllocation = new CapacityFixedBag<MatrixParallelWrap>(wrapSpan.Length);
SpareThreadCount = threadCount;
}
/**/
internal MatrixParallelPool(int threadCount, CapacityFixedStack<MatrixParallelWrap> wrapSpan)
{