SIMD経路の採用を初回フレームの実測で自動判定

このコミットが含まれているのは:
routersys committed 2026-07-02 19:52:59 +09:00
1 親 8ca66bceb8
コミット 55c8bffb70
2ファイル変更
+180 -4

検索条件に一致するファイルはありません

+58 -4
ファイルの表示
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Runtime.Intrinsics;
using System.Threading.Tasks;
@@ -8,18 +9,71 @@ namespace LuaScript.Engine.Kernel
public delegate void RowDelegate(byte[] buffer, int rowBase, int xStart, int xEnd, double y, double[] uniforms);
private const int ParallelThreshold = 64;
private const int WarmupRunsPerPath = 1;
private const int TimedRunsPerPath = 3;
private readonly RowDelegate _row = row;
private readonly RowDelegate? _block = block;
private long _rowTicks;
private long _blockTicks;
private int _rowRuns;
private int _blockRuns;
private bool _nextIsBlock = true;
private bool _decided;
private bool _useBlock;
public bool? BlockAdopted => _decided ? _useBlock : null;
public void Execute(byte[] buffer, int width, int height, double[] uniforms)
{
if (_block is null || !Vector256.IsHardwareAccelerated)
{
Run(buffer, width, height, uniforms, false);
return;
}
if (_decided)
{
Run(buffer, width, height, uniforms, _useBlock);
return;
}
bool useBlock = _nextIsBlock;
_nextIsBlock = !_nextIsBlock;
long start = Stopwatch.GetTimestamp();
Run(buffer, width, height, uniforms, useBlock);
long elapsed = Stopwatch.GetTimestamp() - start;
if (useBlock)
{
if (_blockRuns >= WarmupRunsPerPath)
_blockTicks += elapsed;
_blockRuns++;
}
else
{
if (_rowRuns >= WarmupRunsPerPath)
_rowTicks += elapsed;
_rowRuns++;
}
if (_rowRuns >= WarmupRunsPerPath + TimedRunsPerPath &&
_blockRuns >= WarmupRunsPerPath + TimedRunsPerPath)
{
_useBlock = _blockTicks < _rowTicks;
_decided = true;
}
}
private void Run(byte[] buffer, int width, int height, double[] uniforms, bool useBlock)
{
var row = _row;
var block = Vector256.IsHardwareAccelerated ? _block : null;
var block = useBlock ? _block : null;
int stride = width * 4;
int blockWidth = block is null ? 0 : width & ~3;
void Run(int y)
void RunRow(int y)
{
int rowBase = y * stride;
if (blockWidth > 0)
@@ -31,11 +85,11 @@ namespace LuaScript.Engine.Kernel
if (height < ParallelThreshold)
{
for (int y = 0; y < height; y++)
Run(y);
RunRow(y);
return;
}
Parallel.For(0, height, Run);
Parallel.For(0, height, RunRow);
}
}
}