CPUの畳み込みのスレッドと作業領域をエフェクト間で共有する

・CPUの経路で、エフェクトごとに持っていた作業スレッドと作業領域を共有にして、使用メモリを削減-M
このコミットが含まれているのは:
routersys committed 2026-10-09 22:09:29 +09:00
1 親 29585d8924
コミット c832ba3dd3
4ファイル変更
+141 -8

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

+15
ファイルの表示
@@ -11,6 +11,21 @@ public sealed class ChromaticCpuConvolverTests
return (output, convolved);
}
[Fact]
public void ASharedConvolverGivesTheResultOfADedicatedOne()
{
var scene = ChromaticConvolutionScene.Create(ConvolutionScene.RandomSource(200, 120, 17, 0.5), 200, 120, 14, 9, 64);
var dedicated = Run(scene, Environment.ProcessorCount, 1.5f);
var output = new byte[scene.RegionLength];
var convolved = new float[scene.RegionLength];
using var shared = CpuTileConvolver.CreateShared();
shared.Convolve(scene.Source, scene.SourceX, scene.SourceY, scene.SourceWidth, scene.SourceHeight, scene.Spectrum, scene.Plan, output, 1.5f, convolved, scene.Light);
Assert.Equal(dedicated.Output, output);
Assert.Equal(dedicated.Convolved, convolved);
}
[Theory]
[InlineData(64, 5, 150, 97, 11)]
[InlineData(128, 23, 140, 90, 24)]
+93
ファイルの表示
@@ -239,6 +239,99 @@ public sealed class CpuTileConvolverTests
Assert.Equal(3, CpuTileConvolver.ActiveThreads(3, size));
}
static (byte[] Output, float[] Convolved) RunShared(ConvolutionScene scene, float gain)
{
var output = new byte[scene.RegionLength];
var convolved = new float[scene.RegionLength];
using var convolver = CpuTileConvolver.CreateShared();
convolver.Convolve(scene.Source, scene.SourceX, scene.SourceY, scene.SourceWidth, scene.SourceHeight, scene.Spectrum, scene.Plan, output, gain, convolved);
return (output, convolved);
}
[Fact]
public void ASharedConvolverGivesTheResultOfADedicatedOne()
{
var scene = ConvolutionScene.Create(ConvolutionScene.RandomSource(300, 170, 5, 0.6), 300, 170, 16, 9, 64);
var dedicated = Run(scene, Environment.ProcessorCount, 1.75f);
var shared = RunShared(scene, 1.75f);
Assert.Equal(dedicated.Output, shared.Output);
Assert.Equal(dedicated.Convolved, shared.Convolved);
}
[Fact]
public void SharedConvolversShareOneWorkingArea()
{
static void Convolve(CpuTileConvolver convolver)
{
var spectrum = new KernelSpectrum();
spectrum.Update(ConvolutionScene.AsymmetricKernel(24), 24, 512);
var plan = TilePlan.Create(512, 24, 0, 0, 2000, 2000);
convolver.Convolve(ConvolutionScene.Uniform(16, 16, 200), 40, 40, 16, 16, spectrum, plan, new byte[plan.RegionWidth * plan.RegionHeight * 4], 1f, null);
}
using var first = CpuTileConvolver.CreateShared();
using var second = CpuTileConvolver.CreateShared();
Convolve(first);
var bytes = first.WorkingBytes;
Convolve(second);
var active = CpuTileConvolver.ActiveThreads(first.Threads, 512);
var tilesAndLines = ((long)512 * 512 * 4 + 512 * 4 * 2) * sizeof(float);
var withChromaticLines = tilesAndLines + 512L * 4 * 2 * sizeof(float);
Assert.InRange(bytes, active * tilesAndLines, active * withChromaticLines);
Assert.Equal(bytes, first.WorkingBytes);
Assert.Equal(bytes, second.WorkingBytes);
}
[Fact]
public void DisposingASharedConvolverLeavesTheSharedThreadsToTheOthers()
{
var scene = ConvolutionScene.Create(ConvolutionScene.RandomSource(120, 80, 3, 0.5), 120, 80, 8, 5, 64);
var expected = Run(scene, 2, 1f);
var gone = CpuTileConvolver.CreateShared();
gone.Dispose();
var survivor = RunShared(scene, 1f);
Assert.Equal(expected.Output, survivor.Output);
Assert.Throws<ObjectDisposedException>(() => gone.Convolve(scene.Source, scene.SourceX, scene.SourceY, scene.SourceWidth, scene.SourceHeight, scene.Spectrum, scene.Plan, new byte[scene.RegionLength], 1f, null));
}
[Fact]
public void SharedConvolversUsedFromSeveralThreadsEachGetTheirOwnResult()
{
var scenes = new[]
{
ConvolutionScene.Create(ConvolutionScene.RandomSource(200, 130, 11, 0.5), 200, 130, 12, 9, 64),
ConvolutionScene.Create(ConvolutionScene.RandomSource(150, 160, 12, 0.6), 150, 160, 30, 23, 128),
ConvolutionScene.Create(ConvolutionScene.RandomSource(90, 70, 13, 0.7), 90, 70, 40, 31, 512),
};
var expected = scenes.Select(scene => Run(scene, 2, 1.25f)).ToArray();
var failures = new List<string>();
var threads = scenes.Select((scene, index) => new Thread(() =>
{
for (var round = 0; round < 6; round++)
{
var actual = RunShared(scene, 1.25f);
if (!actual.Output.AsSpan().SequenceEqual(expected[index].Output) || !actual.Convolved.AsSpan().SequenceEqual(expected[index].Convolved))
{
lock (failures)
failures.Add($"scene {index} round {round}");
}
}
})).ToArray();
foreach (var thread in threads)
thread.Start();
foreach (var thread in threads)
thread.Join();
Assert.Empty(failures);
}
[Fact]
public void OnlyTheThreadsThatComputeKeepAWorkingAreaOfTheCurrentSize()
{
+32 -7
ファイルの表示
@@ -11,8 +11,11 @@ internal sealed class CpuTileConvolver : IDisposable
public const int StoredChunkPixels = 16384;
const int Channels = ByteColor.Channels;
static readonly Lazy<Engine> SharedEngine = new(static () => new Engine(Environment.ProcessorCount));
readonly Worker[] workers;
readonly WorkerPool pool;
readonly bool sharesEngine;
readonly ConvolveJob convolveJob;
readonly RenderJob renderJob;
bool disposed;
@@ -44,16 +47,21 @@ internal sealed class CpuTileConvolver : IDisposable
}
public CpuTileConvolver(int threadCount)
: this(new Engine(threadCount), false)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(threadCount);
workers = new Worker[threadCount];
for (var index = 0; index < threadCount; index++)
workers[index] = new Worker();
pool = new WorkerPool(threadCount - 1);
}
CpuTileConvolver(Engine engine, bool sharesEngine)
{
workers = engine.Workers;
pool = engine.Pool;
this.sharesEngine = sharesEngine;
convolveJob = new ConvolveJob(this);
renderJob = new RenderJob(this);
}
public static CpuTileConvolver CreateShared() => new(SharedEngine.Value, true);
public int Threads => workers.Length;
public long WorkingBytes
@@ -220,7 +228,8 @@ internal sealed class CpuTileConvolver : IDisposable
if (disposed)
return;
disposed = true;
pool.Dispose();
if (!sharesEngine)
pool.Dispose();
}
void EnsureReversal(int size, int log2)
@@ -235,7 +244,7 @@ internal sealed class CpuTileConvolver : IDisposable
void EnsureWorkingSize(int size)
{
if (workingSize == size)
if (sharesEngine || workingSize == size)
return;
foreach (var worker in workers)
worker.Release();
@@ -473,6 +482,22 @@ internal sealed class CpuTileConvolver : IDisposable
public void Execute(int index, int worker) => owner.RenderChunk(owner.stored!, index);
}
sealed class Engine
{
public Engine(int threadCount)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(threadCount);
Workers = new Worker[threadCount];
for (var index = 0; index < threadCount; index++)
Workers[index] = new Worker();
Pool = new WorkerPool(threadCount - 1);
}
public Worker[] Workers { get; }
public WorkerPool Pool { get; }
}
sealed class Worker
{
public float[] Tile { get; private set; } = [];
+1 -1
ファイルの表示
@@ -21,8 +21,8 @@ internal sealed class WaveOpticsCpuPipeline : IDisposable
private float[] _store = [];
public WaveOpticsCpuPipeline()
: this(Environment.ProcessorCount)
{
_convolver = CpuTileConvolver.CreateShared();
}
public WaveOpticsCpuPipeline(int threads)