back to matrix, but trouble in combining L4 predict with matrixparallelqueue

This commit is contained in:
Youmu Liu
2026-03-19 19:53:22 +08:00
parent fd82829788
commit e521bed222
10 changed files with 291 additions and 142 deletions
@@ -47,12 +47,12 @@ namespace StgSharp.Mathematics.Numeric
}
public class MatrixParallelWrap : IDisposable
public sealed class MatrixParallelWrap : IDisposable
{
private MatrixParallelThread[] _threads;
private bool _disposed = false;
private int _count;
private bool _disposed;
private readonly CountdownEvent _countdownEvent;
internal MatrixParallelWrap(
MatrixParallelThread[] threadArray
@@ -60,7 +60,7 @@ namespace StgSharp.Mathematics.Numeric
{
_threads = threadArray;
IsMultithread = threadArray.Length > 1;
_count = threadArray.Length;
_countdownEvent = new CountdownEvent(threadArray.Length);
}
public ReadOnlySpan<MatrixParallelThread> Threads
@@ -81,11 +81,8 @@ namespace StgSharp.Mathematics.Numeric
public void Dispose()
{
if (_disposed) {
return;
}
MatrixParallel.ReturnParallelResources(ref _threads);
_disposed = true;
Dispose(true);
GC.SuppressFinalize(this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -104,19 +101,30 @@ namespace StgSharp.Mathematics.Numeric
public void WaitAllWraps()
{
if (_disposed) {
throw new ObjectDisposedException(nameof(MatrixParallelWrap));
}
SpinWait.SpinUntil(() => Volatile.Read(ref _count) == 0);
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap));
_countdownEvent.Wait();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ReportThreadAccomplished()
{
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap));
_ = _countdownEvent.Signal();
}
private void Dispose(
bool disposing
)
{
if (_disposed) {
throw new ObjectDisposedException(nameof(MatrixParallelWrap));
return;
}
Interlocked.Decrement(ref _count);
if (disposing)
{
MatrixParallel.ReturnParallelResources(ref _threads);
_countdownEvent.Dispose();
}
_disposed = true;
}
}