From 55c8bffb706c7d4612d3fff87f27ff96a25f1698 Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 2 Jul 2026 19:52:41 +0900 Subject: [PATCH] =?UTF-8?q?SIMD=E7=B5=8C=E8=B7=AF=E3=81=AE=E6=8E=A1?= =?UTF-8?q?=E7=94=A8=E3=82=92=E5=88=9D=E5=9B=9E=E3=83=95=E3=83=AC=E3=83=BC?= =?UTF-8?q?=E3=83=A0=E3=81=AE=E5=AE=9F=E6=B8=AC=E3=81=A7=E8=87=AA=E5=8B=95?= =?UTF-8?q?=E5=88=A4=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Tests/KernelSimdAdoptionTests.cs | 122 +++++++++++++++++++++ LuaScript/Engine/Kernel/CpuKernel.cs | 62 ++++++++++- 2 files changed, 180 insertions(+), 4 deletions(-) create mode 100644 LuaScript.Tests/KernelSimdAdoptionTests.cs diff --git a/LuaScript.Tests/KernelSimdAdoptionTests.cs b/LuaScript.Tests/KernelSimdAdoptionTests.cs new file mode 100644 index 0000000..5a678f6 --- /dev/null +++ b/LuaScript.Tests/KernelSimdAdoptionTests.cs @@ -0,0 +1,122 @@ +using System; +using System.Runtime.Intrinsics; +using System.Threading; +using LuaScript.Engine.Kernel; + +namespace LuaScript.Tests +{ + public class KernelSimdAdoptionTests + { + private const int CalibrationExecutes = 8; + + private static CpuKernel.RowDelegate Path(Action onCall, int delayMs) => + (buffer, rowBase, xStart, xEnd, y, uniforms) => + { + onCall(); + if (delayMs > 0) + Thread.Sleep(delayMs); + }; + + [Fact] + public void SlowerBlock_SettlesOnScalar() + { + if (!Vector256.IsHardwareAccelerated) + return; + + int rowCalls = 0, blockCalls = 0; + var kernel = new CpuKernel(Path(() => rowCalls++, 0), Path(() => blockCalls++, 5)); + var buffer = new byte[8 * 4]; + var uniforms = new double[1]; + + for (int i = 0; i < CalibrationExecutes - 1; i++) + kernel.Execute(buffer, 8, 1, uniforms); + Assert.Null(kernel.BlockAdopted); + + kernel.Execute(buffer, 8, 1, uniforms); + Assert.True(kernel.BlockAdopted.HasValue); + Assert.False(kernel.BlockAdopted.Value); + + int blockCallsAtDecision = blockCalls; + int rowCallsAtDecision = rowCalls; + for (int i = 0; i < 5; i++) + kernel.Execute(buffer, 8, 1, uniforms); + + Assert.Equal(blockCallsAtDecision, blockCalls); + Assert.Equal(rowCallsAtDecision + 5, rowCalls); + } + + [Fact] + public void FasterBlock_SettlesOnBlock() + { + if (!Vector256.IsHardwareAccelerated) + return; + + int rowCalls = 0, blockCalls = 0; + var kernel = new CpuKernel(Path(() => rowCalls++, 5), Path(() => blockCalls++, 0)); + var buffer = new byte[8 * 4]; + var uniforms = new double[1]; + + for (int i = 0; i < CalibrationExecutes; i++) + kernel.Execute(buffer, 8, 1, uniforms); + Assert.True(kernel.BlockAdopted.HasValue); + Assert.True(kernel.BlockAdopted.Value); + + int rowCallsAtDecision = rowCalls; + int blockCallsAtDecision = blockCalls; + for (int i = 0; i < 5; i++) + kernel.Execute(buffer, 8, 1, uniforms); + + Assert.Equal(rowCallsAtDecision, rowCalls); + Assert.Equal(blockCallsAtDecision + 5, blockCalls); + } + + [Fact] + public void CalibrationAlternatesBothPaths() + { + if (!Vector256.IsHardwareAccelerated) + return; + + int rowCalls = 0, blockCalls = 0; + var kernel = new CpuKernel(Path(() => rowCalls++, 0), Path(() => blockCalls++, 0)); + var buffer = new byte[8 * 4]; + var uniforms = new double[1]; + + for (int i = 0; i < CalibrationExecutes; i++) + kernel.Execute(buffer, 8, 1, uniforms); + + Assert.Equal(CalibrationExecutes / 2, rowCalls); + Assert.Equal(CalibrationExecutes / 2, blockCalls); + } + + [Fact] + public void WithoutBlock_RunsScalarWithoutDecision() + { + int rowCalls = 0; + var kernel = new CpuKernel(Path(() => rowCalls++, 0), null); + var buffer = new byte[8 * 4]; + var uniforms = new double[1]; + + for (int i = 0; i < CalibrationExecutes + 2; i++) + kernel.Execute(buffer, 8, 1, uniforms); + + Assert.Null(kernel.BlockAdopted); + Assert.Equal(CalibrationExecutes + 2, rowCalls); + } + + [Fact] + public void BlockPath_UsesScalarRowForRemainder() + { + if (!Vector256.IsHardwareAccelerated) + return; + + int rowCalls = 0, blockCalls = 0; + var kernel = new CpuKernel(Path(() => rowCalls++, 0), Path(() => blockCalls++, 0)); + var buffer = new byte[10 * 4]; + var uniforms = new double[1]; + + kernel.Execute(buffer, 10, 1, uniforms); + Assert.Equal(1, blockCalls); + Assert.Equal(1, rowCalls); + } + } +} diff --git a/LuaScript/Engine/Kernel/CpuKernel.cs b/LuaScript/Engine/Kernel/CpuKernel.cs index aaeed4c..d45a506 100644 --- a/LuaScript/Engine/Kernel/CpuKernel.cs +++ b/LuaScript/Engine/Kernel/CpuKernel.cs @@ -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); } } }