From 35849274c63d6839e6b36f151d9df83add869fd6 Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 3 Oct 2026 08:57:28 +0900 Subject: [PATCH] =?UTF-8?q?FFT=E3=81=AB=E3=82=88=E3=82=8B=E7=95=B3?= =?UTF-8?q?=E3=81=BF=E8=BE=BC=E3=81=BF=E3=81=AE=E8=AA=A4=E5=B7=AE=E3=81=AE?= =?UTF-8?q?=E4=B8=8A=E9=99=90=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConvolutionBoundTests.cs | 95 +++++++++++++++++++ .../SpectralConvolution/ConvolutionBound.cs | 67 +++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 SpectralConvolution/SpectralConvolution.Tests/ConvolutionBoundTests.cs create mode 100644 SpectralConvolution/SpectralConvolution/ConvolutionBound.cs diff --git a/SpectralConvolution/SpectralConvolution.Tests/ConvolutionBoundTests.cs b/SpectralConvolution/SpectralConvolution.Tests/ConvolutionBoundTests.cs new file mode 100644 index 0000000..2078352 --- /dev/null +++ b/SpectralConvolution/SpectralConvolution.Tests/ConvolutionBoundTests.cs @@ -0,0 +1,95 @@ +namespace SpectralConvolution.Tests; + +public sealed class ConvolutionBoundTests +{ + [Theory] + [InlineData(64, 0.296, 0.166)] + [InlineData(128, 0.688, 0.385)] + [InlineData(256, 1.566, 0.877)] + [InlineData(512, 3.514, 1.966)] + public void TheWorstCaseTileStaysWithinTheDerivedNumberOfLevels(int size, double gpuLevels, double cpuLevels) + { + var norm = Math.Sqrt(2d * size * size); + + Assert.Equal(gpuLevels, ConvolutionBound.Relative(size, ConvolutionBound.GpuOperationError) * norm * 255d, 3); + Assert.Equal(cpuLevels, ConvolutionBound.Relative(size, ConvolutionBound.CpuOperationError) * norm * 255d, 3); + } + + [Fact] + public void TheBoundGrowsWithTheTileAndTheOperationError() + { + int[] sizes = [64, 128, 256, 512]; + for (var index = 1; index < sizes.Length; index++) + { + Assert.True(ConvolutionBound.Relative(sizes[index], ConvolutionBound.GpuOperationError) + > ConvolutionBound.Relative(sizes[index - 1], ConvolutionBound.GpuOperationError)); + Assert.True(ConvolutionBound.FlushedOperations(sizes[index]) > ConvolutionBound.FlushedOperations(sizes[index - 1])); + } + + foreach (var size in sizes) + Assert.True(ConvolutionBound.Relative(size, ConvolutionBound.GpuOperationError) > ConvolutionBound.Relative(size, ConvolutionBound.CpuOperationError)); + } + + [Theory] + [InlineData(64)] + [InlineData(512)] + public void TheSpectrumErrorIsDominatedBySingleRounding(int size) + { + var error = ConvolutionBound.SpectrumError(size); + + Assert.True(error > ConvolutionBound.SingleRounding); + Assert.True(error < ConvolutionBound.SingleRounding + 1e-10); + } + + [Fact] + public void TheTwiddleErrorCoversRoundingToSinglePrecision() + { + Assert.True(ConvolutionBound.SingleTwiddleError > ConvolutionBound.SingleRounding); + Assert.True(ConvolutionBound.DoubleTwiddleError > ConvolutionBound.TwiddleAngleError); + Assert.True(ConvolutionBound.DoubleTwiddleError < 1e-14); + } + + [Fact] + public void GammaMatchesItsDefinition() + { + Assert.Equal(0d, ConvolutionBound.Gamma(0, ConvolutionBound.GpuOperationError)); + var unit = ConvolutionBound.GpuOperationError; + Assert.Equal(3d * unit / (1d - 3d * unit), ConvolutionBound.Gamma(3, unit)); + } + + [Theory] + [InlineData(-1, 0.1)] + [InlineData(10, 0.1)] + [InlineData(1, -0.1)] + [InlineData(1, double.NaN)] + public void GammaRejectsMeaninglessArguments(int operations, double unit) + { + Assert.Throws(() => ConvolutionBound.Gamma(operations, unit)); + } + + [Fact] + public void FlushingAddsFarLessThanOneLevel() + { + Assert.True(ConvolutionBound.FlushedOperations(512) * ConvolutionBound.SmallestNormal < 1e-30); + Assert.True(ConvolutionBound.Absolute(512, ConvolutionBound.GpuOperationError, 0d) > 0d); + } + + [Fact] + public void TheAbsoluteBoundScalesWithTheNorm() + { + var size = 128; + var flush = ConvolutionBound.FlushedOperations(size) * ConvolutionBound.SmallestNormal; + var relative = ConvolutionBound.Relative(size, ConvolutionBound.GpuOperationError); + + Assert.Equal(relative * 3d + flush, ConvolutionBound.Absolute(size, ConvolutionBound.GpuOperationError, 3d)); + } + + [Theory] + [InlineData(-1d)] + [InlineData(double.NaN)] + [InlineData(double.PositiveInfinity)] + public void AnInvalidNormIsRejected(double norm) + { + Assert.Throws(() => ConvolutionBound.Absolute(128, ConvolutionBound.GpuOperationError, norm)); + } +} diff --git a/SpectralConvolution/SpectralConvolution/ConvolutionBound.cs b/SpectralConvolution/SpectralConvolution/ConvolutionBound.cs new file mode 100644 index 0000000..6b8ca80 --- /dev/null +++ b/SpectralConvolution/SpectralConvolution/ConvolutionBound.cs @@ -0,0 +1,67 @@ +namespace SpectralConvolution; + +internal static class ConvolutionBound +{ + public static readonly double GpuOperationError = Math.ScaleB(1d, -23); + public static readonly double CpuOperationError = Math.ScaleB(1d, -24); + public static readonly double SingleRounding = Math.ScaleB(1d, -24); + public static readonly double DoubleRounding = Math.ScaleB(1d, -53); + public static readonly double SmallestNormal = Math.ScaleB(1d, -126); + public static readonly double TrigonometricError = Math.ScaleB(1d, -51); + + public static double TwiddleAngleError => Math.PI * TrigonometricError; + + public static double DoubleTwiddleError => TwiddleAngleError + Math.Sqrt(2d) * TrigonometricError; + + public static double SingleTwiddleError => SingleRounding * (1d + DoubleTwiddleError) + DoubleTwiddleError; + + public static double Gamma(int operations, double unit) + { + ArgumentOutOfRangeException.ThrowIfNegative(operations); + if (!double.IsFinite(unit) || unit < 0d) + throw new ArgumentOutOfRangeException(nameof(unit)); + var product = operations * unit; + if (product >= 1d) + throw new ArgumentOutOfRangeException(nameof(operations)); + return product / (1d - product); + } + + public static double ComplexMultiplication(double unit) => Math.Sqrt(2d) * Gamma(2, unit); + + public static double Stage(double unit, double twiddleError) + => (1d + unit) * (twiddleError + (1d + twiddleError) * ComplexMultiplication(unit)) + unit; + + public static double Transform(int stages, double unit, double twiddleError) + => double.ExpM1(stages * double.LogP1(Stage(unit, twiddleError))); + + public static double SpectrumError(int size) + { + var plan = TilePlan.Create(size, 0, 0, 0, 1, 1); + var transform = Transform(2 * plan.Log2Size, DoubleRounding, DoubleTwiddleError) * size; + return SingleRounding * (1d + transform) + transform + Math.Sqrt(2d) * SmallestNormal; + } + + public static double Relative(int size, double operationError) + { + var plan = TilePlan.Create(size, 0, 0, 0, 1, 1); + var multiplication = ComplexMultiplication(operationError); + var transform = Transform(2 * plan.Log2Size, operationError, SingleTwiddleError); + var spectrum = SpectrumError(size); + var product = transform * (1d + spectrum) * (1d + multiplication) + spectrum * (1d + multiplication) + multiplication; + var convolution = product + transform * (1d + product); + return convolution + SingleRounding * (1d + convolution); + } + + public static int FlushedOperations(int size) + { + var plan = TilePlan.Create(size, 0, 0, 0, 1, 1); + return (int)Math.Ceiling(2d * (4 * plan.Log2Size + 2) * 4d * Math.Sqrt(2d) * size); + } + + public static double Absolute(int size, double operationError, double norm) + { + if (!double.IsFinite(norm) || norm < 0d) + throw new ArgumentOutOfRangeException(nameof(norm)); + return Relative(size, operationError) * norm + FlushedOperations(size) * SmallestNormal; + } +}