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:
@@ -2,8 +2,8 @@
|
|||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="MatrixParallel"
|
// file="MatrixParallel"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
// AuthorGroup: Nitload
|
// AuthorGroup: Nitload Space
|
||||||
// Copyright (c) Nitload. All rights reserved.
|
// Copyright (c) Nitload Space. All rights reserved.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
@@ -35,13 +35,13 @@ using System.Threading;
|
|||||||
namespace StgSharp.Mathematics.Numeric
|
namespace StgSharp.Mathematics.Numeric
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Thread wrapping to accelerate matrix operations by dividing threads into groups (wraps).
|
* Thread wrapping to accelerate matrix operations by dividing threads into groups (pool).
|
||||||
* For M wraps, each containing an average of N threads, we satisfy the condition M * N = T - 4,
|
* 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.
|
* 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.
|
* For simplicity, let M * N = 16.
|
||||||
*
|
*
|
||||||
* The total time complexity for publishing tasks is O(M + N):
|
* 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.
|
* - 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),
|
* 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 Thread[] _threads;
|
||||||
private static readonly (int a, int b)[] LookupTable = [
|
private static readonly (int a, int b)[] LookupTable = [ (1, 1), // 7
|
||||||
(1, 1), // 7
|
|
||||||
(0, 2), // 8 (no non-zero solution)
|
(0, 2), // 8 (no non-zero solution)
|
||||||
(3, 0), // 9 (no non-zero solution)
|
(3, 0), // 9 (no non-zero solution)
|
||||||
(2, 1), // 10
|
(2, 1), // 10
|
||||||
@@ -134,10 +133,7 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
}
|
}
|
||||||
} else if (count == 6)
|
} else if (count == 6)
|
||||||
{
|
{
|
||||||
Pool = new(6, [
|
Pool = new(6, [ new MatrixParallelWrap(0, 3), new MatrixParallelWrap(3, 3) ]);
|
||||||
new MatrixParallelWrap(0, 3),
|
|
||||||
new MatrixParallelWrap(3, 3)
|
|
||||||
]);
|
|
||||||
(LargeWrapCount, SmallWrapCount) = (0, 2);
|
(LargeWrapCount, SmallWrapCount) = (0, 2);
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
@@ -199,13 +195,13 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
return Pool;
|
return Pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ReleaseParallelLeadership(ref CapacityFixedStack<MatrixParallelWrap> wraps)
|
public static void ReleaseParallelLeadership(ref MatrixParallelPool pool)
|
||||||
{
|
{
|
||||||
#if NET9_0_OR_GREATER
|
#if NET9_0_OR_GREATER
|
||||||
#else
|
#else
|
||||||
Monitor.Exit(_lock);
|
Monitor.Exit(_lock);
|
||||||
#endif
|
#endif
|
||||||
wraps = null!;
|
pool = null!;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -1,27 +1,30 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="MatrixParallelPool"
|
// file="MatrixParallelPool.cs"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
// AuthorGroup: Nitload
|
// AuthorGroup: Nitload Space
|
||||||
// Copyright (c) Nitload. All rights reserved.
|
// Copyright (c) Nitload Space. All rights reserved.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
// Permission is hereby granted, free of charge, to any person
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
// obtaining a copy of this software and associated documentation
|
||||||
// in the Software without restriction, including without limitation the rights
|
// files (the “Software”), to deal in the Software without restriction,
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
// including without limitation the rights to use, copy, modify, merge,
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||||
// furnished to do so, subject to the following conditions:
|
// 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 above copyright notice and
|
||||||
//
|
// this permission notice shall be included in all copies
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
// or substantial portions of the Software.
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
//
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
// SOFTWARE.
|
// 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();
|
throw new NotSupportedException();
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
internal MatrixParallelPool(int threadCount, ReadOnlySpan<MatrixParallelWrap> wrapSpan)
|
internal MatrixParallelPool(int threadCount, ReadOnlySpan<MatrixParallelWrap> wrapSpan)
|
||||||
{
|
{
|
||||||
SpareWrap = CapacityFixedStackBuilder.Create(wrapSpan);
|
SpareWrap = CapacityFixedStackBuilder.Create(wrapSpan);
|
||||||
SingleThreadAllocation = new CapacityFixedBag<MatrixParallelWrap>(wrapSpan.Length);
|
SingleThreadAllocation = new CapacityFixedBag<MatrixParallelWrap>(wrapSpan.Length);
|
||||||
SpareThreadCount = threadCount;
|
SpareThreadCount = threadCount;
|
||||||
}
|
}
|
||||||
|
/**/
|
||||||
|
|
||||||
internal MatrixParallelPool(int threadCount, CapacityFixedStack<MatrixParallelWrap> wrapSpan)
|
internal MatrixParallelPool(int threadCount, CapacityFixedStack<MatrixParallelWrap> wrapSpan)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user