diff --git a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs index 93eb471..5d00762 100644 --- a/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs +++ b/SpectralConvolution/SpectralConvolution.Tests/CpuTileConvolverTests.cs @@ -99,6 +99,74 @@ public sealed class CpuTileConvolverTests Assert.Equal(expected, output); } + [Theory] + [InlineData(1f)] + [InlineData(2.75f)] + [InlineData(0.3f)] + public void RenderingTheStoreMatchesTheConvolutionBitForBit(float gain) + { + var scene = ConvolutionScene.Create(ConvolutionScene.RandomSource(130, 90, 13, 0.6), 130, 90, 8, 4, 64); + var (expected, _) = Run(scene, 3, gain); + var (_, stored) = Run(scene, 3, 1f); + var output = new byte[scene.RegionLength]; + using var convolver = new CpuTileConvolver(3); + + convolver.RenderStored(stored, scene.Plan.RegionWidth * scene.Plan.RegionHeight, gain, output); + + Assert.Equal(expected, output); + } + + [Fact] + public void RenderingTheStoreDoesNotDependOnTheNumberOfThreads() + { + var pixels = CpuTileConvolver.StoredChunkPixels * 3 + 17; + var random = new Random(3); + var stored = new float[pixels * 4]; + for (var index = 0; index < stored.Length; index++) + stored[index] = (float)(random.NextDouble() * 1.2 - 0.1); + var single = new byte[stored.Length]; + var several = new byte[stored.Length]; + using (var convolver = new CpuTileConvolver(1)) + convolver.RenderStored(stored, pixels, 1.3f, single); + using (var convolver = new CpuTileConvolver(6)) + convolver.RenderStored(stored, pixels, 1.3f, several); + + Assert.Equal(single, several); + for (var pixel = 0; pixel < pixels; pixel++) + { + Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4 + 2] * 1.3f), single[pixel * 4]); + Assert.Equal(CpuTileConvolver.ToUnorm(stored[pixel * 4] * 1.3f), single[pixel * 4 + 2]); + } + } + + [Fact] + public void RenderingTheStoreRejectsShortBuffers() + { + using var convolver = new CpuTileConvolver(2); + + Assert.Throws(() => convolver.RenderStored(new float[7], 2, 1f, new byte[8])); + Assert.Throws(() => convolver.RenderStored(new float[8], 2, 1f, new byte[7])); + Assert.Throws(() => convolver.RenderStored(new float[8], 2, float.PositiveInfinity, new byte[8])); + convolver.RenderStored([], 0, 1f, []); + } + + [Fact] + public void AWarmStoredRenderAllocatesNothingOnTheCallingThread() + { + var pixels = CpuTileConvolver.StoredChunkPixels * 4; + var stored = new float[pixels * 4]; + var output = new byte[pixels * 4]; + using var convolver = new CpuTileConvolver(4); + for (var warmUp = 0; warmUp < 3; warmUp++) + convolver.RenderStored(stored, pixels, 1f, output); + + var before = GC.GetAllocatedBytesForCurrentThread(); + for (var run = 0; run < 20; run++) + convolver.RenderStored(stored, pixels, 1f, output); + + Assert.Equal(0L, GC.GetAllocatedBytesForCurrentThread() - before); + } + [Theory] [InlineData(float.NaN, 0)] [InlineData(-0.25f, 0)] diff --git a/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs b/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs index 7b39647..07f1db8 100644 --- a/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs +++ b/SpectralConvolution/SpectralConvolution/CpuTileConvolver.cs @@ -6,6 +6,7 @@ namespace SpectralConvolution; internal sealed class CpuTileConvolver : IDisposable { public const long WorkingBudgetBytes = 64L << 20; + public const int StoredChunkPixels = 16384; const int Channels = 4; readonly Worker[] workers; @@ -29,6 +30,9 @@ internal sealed class CpuTileConvolver : IDisposable byte[] output = []; float gain; float[]? convolved; + float[]? stored; + int storedPixels; + int storedChunks; public CpuTileConvolver() : this(Environment.ProcessorCount) @@ -109,16 +113,9 @@ internal sealed class CpuTileConvolver : IDisposable output = outputPixels; gain = outputGain; convolved = convolvedValues; - next = 0; - claimed = 0; - failure = null; - countdown.Reset(active); try { - if (active > 1) - start.Release(active - 1); - Drain(); - countdown.Wait(); + Dispatch(active); } finally { @@ -127,6 +124,51 @@ internal sealed class CpuTileConvolver : IDisposable output = []; convolved = null; } + } + + public void RenderStored(float[] storedValues, int pixels, float outputGain, byte[] outputPixels) + { + ArgumentNullException.ThrowIfNull(storedValues); + ArgumentNullException.ThrowIfNull(outputPixels); + ArgumentOutOfRangeException.ThrowIfNegative(pixels); + if (storedValues.Length < (long)pixels * Channels) + throw new ArgumentException(null, nameof(storedValues)); + if (outputPixels.Length < (long)pixels * Channels) + throw new ArgumentException(null, nameof(outputPixels)); + if (!float.IsFinite(outputGain)) + throw new ArgumentOutOfRangeException(nameof(outputGain)); + + ObjectDisposedException.ThrowIf(disposed, this); + var chunks = (pixels + StoredChunkPixels - 1) / StoredChunkPixels; + if (chunks == 0) + return; + + stored = storedValues; + storedPixels = pixels; + storedChunks = chunks; + output = outputPixels; + gain = outputGain; + try + { + Dispatch(Math.Min(workers.Length, chunks)); + } + finally + { + stored = null; + output = []; + } + } + + void Dispatch(int active) + { + next = 0; + claimed = 0; + failure = null; + countdown.Reset(active); + if (active > 1) + start.Release(active - 1); + Drain(); + countdown.Wait(); if (failure is { } exception) ExceptionDispatchInfo.Throw(exception); @@ -188,11 +230,20 @@ internal sealed class CpuTileConvolver : IDisposable { try { - var worker = workers[Interlocked.Increment(ref claimed) - 1]; - worker.Prepare(plan.Size); - int tile; - while ((tile = Interlocked.Increment(ref next) - 1) < plan.TileCount) - ConvolveTile(tile, worker); + if (stored is { } values) + { + int chunk; + while ((chunk = Interlocked.Increment(ref next) - 1) < storedChunks) + RenderChunk(values, chunk); + } + else + { + var worker = workers[Interlocked.Increment(ref claimed) - 1]; + worker.Prepare(plan.Size); + int tile; + while ((tile = Interlocked.Increment(ref next) - 1) < plan.TileCount) + ConvolveTile(tile, worker); + } } catch (Exception exception) { @@ -305,6 +356,20 @@ internal sealed class CpuTileConvolver : IDisposable } } + void RenderChunk(float[] values, int chunk) + { + var first = chunk * StoredChunkPixels; + var last = Math.Min(first + StoredChunkPixels, storedPixels); + for (var pixel = first; pixel < last; pixel++) + { + var index = pixel * Channels; + output[index] = ToUnorm(values[index + 2] * gain); + output[index + 1] = ToUnorm(values[index + 1] * gain); + output[index + 2] = ToUnorm(values[index] * gain); + output[index + 3] = ToUnorm(values[index + 3] * gain); + } + } + bool LoadRow(Span line, ReadOnlySpan order, int sourceRow, int left, int first, int last) { line.Clear();