optimize para performance of buffered stack

This commit is contained in:
Youmu Liu
2025-11-05 20:33:58 +08:00
parent 9c4c71dc5b
commit ea0e6f3ff6
10 changed files with 516 additions and 91 deletions
@@ -91,6 +91,7 @@ namespace StgSharp.Mathematics.Numeric
* wait
*/
}
MatrixParallel.ReleaseParallelLeadership(ref pool);
MatrixKernel<float>* leftPtr = left.Buffer;
MatrixKernel<float>* rightPtr = right.Buffer;
MatrixKernel<float>* ansPtr = ans.Buffer;
@@ -98,6 +99,8 @@ namespace StgSharp.Mathematics.Numeric
NativeIntrinsic.Intrinsic.f32_sub(leftPtr + i, rightPtr + i, ansPtr + i);
}
// thread.
return ans;
}
Stack<MatrixParallelWrap> taskGroup = MatrixParallel.PublicTask(left.GetColumnEnumeration(), right.GetColumnEnumeration(), ans.GetColumnEnumeration(),
@@ -820,9 +820,9 @@ namespace StgSharp.Mathematics.Numeric
int tileWidth = (1024 / secCount) + 1;
int tileCount = (primCount + tileWidth - 1) / tileWidth;
MatrixParallelPool pool = LeadParallel();
CapacityFixedStack<MatrixParallelThread> pool = LeadParallel();
int threadCount = pool.SpareThreadCount;
int threadCount = pool.Count;
int tilePerThreadLess = tileCount / threadCount; // tiles per thread without extra slice
int columnAfterBaseTile = tileCount % threadCount * tileWidth; // tile sliced into columns
int extraColumnPerThread = columnAfterBaseTile / threadCount;
@@ -833,7 +833,7 @@ namespace StgSharp.Mathematics.Numeric
MatrixParallelTaskPackage<T>* current = package;
while (true)
{
if (!pool.TryRequestWrap(out MatrixParallelWrap? wrap) || wrap is null)
if (!pool.TryPop(out MatrixParallelThread? wrap) || wrap is null)
{
#if DEBUG
throw new InvalidOperationException("Someone steal parallel wraps from the pool.");
@@ -846,7 +846,7 @@ namespace StgSharp.Mathematics.Numeric
bool isLargeSlice = extraColumnCount > 0;
// count tiles for wrap
int wrapWidth = wrap!.WrapTaskCapacity;
int wrapWidth =/**/ 3/* wrap!.WrapTaskCapacity/**/;
int columnFromSlicingCount = (hasSlice ? extraColumnPerThread : 0) * wrapWidth;
int baseColumn = wrapWidth * tilePerThreadLess * tileWidth;
int columnInWrap = baseColumn + columnFromSlicingCount + (isLargeSlice ? 1 : 0);
@@ -872,8 +872,9 @@ namespace StgSharp.Mathematics.Numeric
total -= columnInWrap;
// set to wrap and push
wrap.WrapTask = (MatrixParallelTaskPackageNonGeneric*)current;
taskBunch.Push(wrap);
wrap.Tasks.Enqueue((IntPtr)current);
// taskBunch.Push(wrap);
if (total <= 0)
@@ -65,6 +65,7 @@ namespace StgSharp.Mathematics.Numeric
internal CapacityFixedStack<MatrixParallelWrap> SpareWrap { get; set; }
/*
public void ReturnIndividualHandle(ref MatrixParallelWrap.Handle handle)
{
MatrixParallelWrap wrap = handle.Wrap;
@@ -108,6 +109,6 @@ namespace StgSharp.Mathematics.Numeric
wrap = null!;
return false;
}
/**/
}
}
@@ -96,6 +96,11 @@ namespace StgSharp.Mathematics.Numeric
_resetEvent.Set();
}
public void ReturnToPool()
{
// MatrixParallel.(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)