From 1dee4672203e9462f62e85748745a1f81469f7da Mon Sep 17 00:00:00 2001 From: Nitload Date: Mon, 10 Nov 2025 00:23:39 +0800 Subject: [PATCH] Daily update on 20251110-002339 by Nitload Youmu. Additional Message : Dynamic wrapping is ok, going on test. Signed-off-by: Nitload --- .../ConcurrentCapacityFixedStack.cs | 4 +- .../Numeric/MatrixParallel.AwaitingQueue.cs | 132 +++++++++--------- .../Numeric/MatrixParallel.cs | 6 +- 3 files changed, 68 insertions(+), 74 deletions(-) diff --git a/StgSharp.Common/Collections/ConcurrentCapacityFixedStack.cs b/StgSharp.Common/Collections/ConcurrentCapacityFixedStack.cs index 92884837..9c62a4ba 100644 --- a/StgSharp.Common/Collections/ConcurrentCapacityFixedStack.cs +++ b/StgSharp.Common/Collections/ConcurrentCapacityFixedStack.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="ConcurrentCapacityFixedStack" // 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 diff --git a/StgSharp.Mathematics/Numeric/MatrixParallel.AwaitingQueue.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.AwaitingQueue.cs index 49bd62b0..bc4b6cfa 100644 --- a/StgSharp.Mathematics/Numeric/MatrixParallel.AwaitingQueue.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.AwaitingQueue.cs @@ -37,21 +37,18 @@ 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 readonly ConcurrentQueue _fallback = []; private Element _quickElements = new(); private volatile int _count; private volatile int _lastGeneration; - private readonly -#if NET9_0_OR_GREATER - Lock -#else - object -#endif - sync = new(); + private readonly Queue _fallback = []; public AwaitingQueue() { } @@ -63,17 +60,8 @@ namespace StgSharp.Mathematics.Numeric Interlocked.Increment(ref _count); return; } - lock (sync) - { - if (_count > 7) - { - _fallback.Enqueue(handle); - Interlocked.Increment(ref _count); - return; - } - _quickElements[_count] = handle; - Interlocked.Increment(ref _count); - } + _quickElements[_count] = handle; + Interlocked.Increment(ref _count); } public bool TryDequeue(int generation, out WaitHandle handle) @@ -84,25 +72,22 @@ namespace StgSharp.Mathematics.Numeric handle = null!; return false; } - lock (sync) + if (_lastGeneration == generation) { - if (_lastGeneration == generation) - { - for (int i = 0; i < _count; i++) { - _quickElements[i].UpdatePriority(generation); - } - Element.Sort(_quickElements); - } else - { - _lastGeneration = generation; + for (int i = 0; i < _count; i++) { + _quickElements[i].UpdatePriority(generation); } - handle = _quickElements[0]; - _count--; - if (_count > 7) { - _ = _fallback.TryDequeue(out _quickElements[0]!); - } - return true; + Element.Sort(_quickElements); + } else + { + _lastGeneration = generation; } + handle = _quickElements[0]; + _count--; + if (_count > 7) { + _ = _fallback.TryDequeue(out _quickElements[0]!); + } + return true; } public bool TryPeek(int generation, out WaitHandle handle) @@ -113,20 +98,17 @@ namespace StgSharp.Mathematics.Numeric handle = null!; return false; } - lock (sync) - { - for (int i = 0; i < _count; i++) { - _quickElements[i].UpdatePriority(generation); - } - Element.Sort(_quickElements); - _lastGeneration = generation; - handle = _quickElements[0]; - _count--; - if (_count > 7) { - _ = _fallback.TryDequeue(out _quickElements[0]!); - } - return true; + for (int i = 0; i < _count; i++) { + _quickElements[i].UpdatePriority(generation); } + Element.Sort(_quickElements); + _lastGeneration = generation; + handle = _quickElements[0]; + _count--; + if (_count > 7) { + _ = _fallback.TryDequeue(out _quickElements[0]!); + } + return true; } public bool TryPopIfRefEqual(int generation, ref WaitHandle handle) @@ -136,24 +118,21 @@ namespace StgSharp.Mathematics.Numeric handle = null!; return false; } - lock (sync) - { - for (int i = 0; i < _count; i++) { - _quickElements[i].UpdatePriority(generation); - } - Element.Sort(_quickElements); - _lastGeneration = generation; - handle = _quickElements[0]; - _count--; - if (!ReferenceEquals(_quickElements[0], handle)) { - return false; - } - _quickElements[0] = null!; - if (_count > 7) { - _ = _fallback.TryDequeue(out _quickElements[0]!); - } - return true; + for (int i = 0; i < _count; i++) { + _quickElements[i].UpdatePriority(generation); } + Element.Sort(ref _quickElements); + _lastGeneration = generation; + handle = _quickElements[0]; + _count--; + if (!ReferenceEquals(_quickElements[0], handle)) { + return false; + } + _quickElements[0] = null!; + if (_count > 7) { + _ = _fallback.TryDequeue(out _quickElements[0]!); + } + return true; } } @@ -164,11 +143,26 @@ namespace StgSharp.Mathematics.Numeric private WaitHandle value; - public static void Sort(Element e) + public static void Sort(ref Element e) { - /* - * yet to do - */ + int maxIdx = 0; + WaitHandle first = e[0]; + int maxPr = first?.Priority ?? int.MaxValue; + + 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]); + } } } diff --git a/StgSharp.Mathematics/Numeric/MatrixParallel.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.cs index 91d876ef..5b830baa 100644 --- a/StgSharp.Mathematics/Numeric/MatrixParallel.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.cs @@ -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 @@ -75,7 +75,7 @@ namespace StgSharp.Mathematics.Numeric for (int i = 0; i < count; i++) { _threads.Push(new()); } - _awaitingLeaderHandle = new ConcurrentQueue(); + _awaitingLeaderHandle = new AwaitingQueue(); IsInitialized = true; }