Daily update on 20251109-030228 by Nitload Youmu. Additional Message : Dynamic wrapping is briefly ok, a special sorting is required.
Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallel.Await"
|
||||
// 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
|
||||
@@ -29,6 +29,7 @@ using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -37,7 +38,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
public static partial class MatrixParallel
|
||||
{
|
||||
|
||||
private static ConcurrentQueue<WaitHandle> _awaitingLeaderHandle;
|
||||
private static AwaitingQueue _awaitingLeaderHandle;
|
||||
|
||||
private static volatile int _createGeneration = 0;
|
||||
private static int _sortState;
|
||||
@@ -50,44 +51,52 @@ namespace StgSharp.Mathematics.Numeric
|
||||
if (_threads.Count < min)
|
||||
{
|
||||
WaitHandle handle = new WaitHandle
|
||||
{
|
||||
CreateGeneration = _createGeneration,
|
||||
LeastCount = min,
|
||||
BestCount = recommendCount
|
||||
};
|
||||
(
|
||||
_createGeneration,
|
||||
min,
|
||||
recommendCount
|
||||
);
|
||||
_awaitingLeaderHandle.Enqueue(handle);
|
||||
int cycleGeneration = _createGeneration;
|
||||
while (true)
|
||||
do
|
||||
{
|
||||
// TODO 排队等待,对前八个按照优先级排序,优先级最高的独占进行等待,其他继续排队
|
||||
Thread.Sleep(0);
|
||||
|
||||
if (_createGeneration != cycleGeneration)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _sortState, 1) == 0)
|
||||
{
|
||||
// TODO 排序
|
||||
if (_awaitingLeaderHandle.TryPopIfRefEqual(cycleGeneration, ref handle))
|
||||
{
|
||||
// Sort is implemented in TryPopIfRefEqual
|
||||
ret = _threads.PopRange(handle.BestCount);
|
||||
_createGeneration++;
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
return ret;
|
||||
}
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
} else
|
||||
{
|
||||
cycleGeneration = _createGeneration;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (_awaitingLeaderHandle.TryPeek(out WaitHandle? p) && p == handle)
|
||||
{
|
||||
// get the leader
|
||||
ret = _threads.PopRange(recommendCount);
|
||||
_awaitingLeaderHandle.TryDequeue(out _);
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(0);
|
||||
}
|
||||
} while (true);
|
||||
} else
|
||||
{
|
||||
// directly get the leader
|
||||
ret = _threads.PopRange(recommendCount);
|
||||
while (true)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _sortState, 1) == 0)
|
||||
{
|
||||
ret = _threads.PopRange(recommendCount);
|
||||
_createGeneration++;
|
||||
_ = Interlocked.Exchange(ref _sortState, 0);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = Interlocked.Increment(ref _createGeneration);
|
||||
return ret;
|
||||
|
||||
// return ret;
|
||||
}
|
||||
|
||||
public static void ReturnParallelResources(ref MatrixParallelThread[] pool)
|
||||
@@ -96,18 +105,21 @@ namespace StgSharp.Mathematics.Numeric
|
||||
pool = null!;
|
||||
}
|
||||
|
||||
private record class WaitHandle
|
||||
private record class WaitHandle(int generation, int least, int best)
|
||||
{
|
||||
|
||||
public int CreateGeneration { get; set; }
|
||||
public int CreateGeneration { get; init; } = generation;
|
||||
|
||||
public int LeastCount { get; set; }
|
||||
public int LeastCount { get; init; } = least;
|
||||
|
||||
public int BestCount { get; set; }
|
||||
public int BestCount { get; init; } = best;
|
||||
|
||||
public int CurrentPriority(int currentGeneration)
|
||||
public int Priority { get; private set; }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void UpdatePriority(int currentGeneration)
|
||||
{
|
||||
return (currentGeneration - CreateGeneration) * (MatrixParallel._threads.Capacity - LeastCount);
|
||||
Priority = (currentGeneration - CreateGeneration) * (MatrixParallel._threads.Capacity - LeastCount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user