FFTの作業領域で実部と虚部の間隔を空ける
・点像分布関数とカーネルのスペクトルの変換の作業領域を見直して高速化-M
このコミットが含まれているのは:
5ファイル変更
+124
-40
検索条件に一致するファイルはありません
@@ -0,0 +1,68 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace SpectralConvolution.Tests;
|
||||
|
||||
public sealed class ComplexLinesTests
|
||||
{
|
||||
static long Distance(ComplexLines lines)
|
||||
=> Unsafe.ByteOffset(ref MemoryMarshal.GetReference(lines.Real), ref MemoryMarshal.GetReference(lines.Imaginary));
|
||||
|
||||
[Theory]
|
||||
[InlineData(128)]
|
||||
[InlineData(512)]
|
||||
[InlineData(31 * 128)]
|
||||
[InlineData(81 * 256)]
|
||||
[InlineData(81 * 512)]
|
||||
[InlineData(256 * 256)]
|
||||
[InlineData(512 * 512)]
|
||||
public void TheImaginaryPartStartsAtAFixedOffsetWithinTheFourKilobytePeriod(int count)
|
||||
{
|
||||
var lines = new ComplexLines();
|
||||
|
||||
lines.Ensure(count);
|
||||
|
||||
Assert.Equal(1536L, Distance(lines) % 4096);
|
||||
Assert.True(Distance(lines) >= (count + 64L) * sizeof(double), $"{Distance(lines)}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TheTwoPartsDoNotOverlap()
|
||||
{
|
||||
var lines = new ComplexLines();
|
||||
lines.Ensure(1000);
|
||||
|
||||
lines.Real.Fill(1d);
|
||||
lines.Imaginary.Fill(2d);
|
||||
|
||||
Assert.All(lines.Real.ToArray(), value => Assert.Equal(1d, value));
|
||||
Assert.All(lines.Imaginary.ToArray(), value => Assert.Equal(2d, value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ABufferThatIsLargeEnoughIsKeptAndAGrownOneKeepsTheOffset()
|
||||
{
|
||||
var lines = new ComplexLines();
|
||||
lines.Ensure(100);
|
||||
|
||||
lines.Ensure(50);
|
||||
|
||||
Assert.Equal(100, lines.Real.Length);
|
||||
Assert.Equal(100, lines.Imaginary.Length);
|
||||
|
||||
lines.Ensure(4096);
|
||||
|
||||
Assert.Equal(4096, lines.Real.Length);
|
||||
Assert.Equal(4096, lines.Imaginary.Length);
|
||||
Assert.Equal(1536L, Distance(lines) % 4096);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnUnusedBufferIsEmpty()
|
||||
{
|
||||
var lines = new ComplexLines();
|
||||
|
||||
Assert.True(lines.Real.IsEmpty);
|
||||
Assert.True(lines.Imaginary.IsEmpty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace SpectralConvolution;
|
||||
|
||||
internal sealed class ComplexLines
|
||||
{
|
||||
const int Period = 512;
|
||||
const int Offset = 192;
|
||||
const int MinimumSeparation = 64;
|
||||
|
||||
double[] storage = [];
|
||||
int length;
|
||||
int separation;
|
||||
|
||||
public Span<double> Real => storage.AsSpan(0, length);
|
||||
|
||||
public Span<double> Imaginary => storage.AsSpan(length + separation, length);
|
||||
|
||||
public void Ensure(int count)
|
||||
{
|
||||
if (length >= count)
|
||||
return;
|
||||
|
||||
length = count;
|
||||
separation = (Offset - count % Period + Period) % Period;
|
||||
if (separation < MinimumSeparation)
|
||||
separation += Period;
|
||||
storage = new double[count * 2 + separation];
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,9 @@ namespace SpectralConvolution;
|
||||
|
||||
internal sealed class KernelSpectrum
|
||||
{
|
||||
readonly ComplexLines rows = new();
|
||||
readonly ComplexLines column = new();
|
||||
double[] kernel = [];
|
||||
double[] rowReal = [];
|
||||
double[] rowImaginary = [];
|
||||
double[] columnReal = [];
|
||||
double[] columnImaginary = [];
|
||||
Float2[] twiddles = [];
|
||||
Float2[] spectrum = [];
|
||||
|
||||
@@ -47,16 +45,8 @@ internal sealed class KernelSpectrum
|
||||
Radius = 0;
|
||||
if (kernel.Length < kernelArea)
|
||||
kernel = new double[kernelArea];
|
||||
if (rowReal.Length < rowArea)
|
||||
{
|
||||
rowReal = new double[rowArea];
|
||||
rowImaginary = new double[rowArea];
|
||||
}
|
||||
if (columnReal.Length < size)
|
||||
{
|
||||
columnReal = new double[size];
|
||||
columnImaginary = new double[size];
|
||||
}
|
||||
rows.Ensure(rowArea);
|
||||
column.Ensure(size);
|
||||
if (spectrum.Length < size * size)
|
||||
spectrum = new Float2[size * size];
|
||||
if (twiddles.Length < size / 2)
|
||||
@@ -72,10 +62,12 @@ internal sealed class KernelSpectrum
|
||||
}
|
||||
|
||||
var mask = size - 1;
|
||||
var rowReal = rows.Real;
|
||||
var rowImaginary = rows.Imaginary;
|
||||
for (var row = 0; row < kernelSize; row++)
|
||||
{
|
||||
var realLine = rowReal.AsSpan(row * size, size);
|
||||
var imaginaryLine = rowImaginary.AsSpan(row * size, size);
|
||||
var realLine = rowReal.Slice(row * size, size);
|
||||
var imaginaryLine = rowImaginary.Slice(row * size, size);
|
||||
realLine.Clear();
|
||||
imaginaryLine.Clear();
|
||||
for (var offsetX = -radius; offsetX <= radius; offsetX++)
|
||||
@@ -85,8 +77,8 @@ internal sealed class KernelSpectrum
|
||||
}
|
||||
|
||||
var half = size / 2;
|
||||
var real = columnReal.AsSpan(0, size);
|
||||
var imaginary = columnImaginary.AsSpan(0, size);
|
||||
var real = column.Real[..size];
|
||||
var imaginary = column.Imaginary[..size];
|
||||
for (var frequencyX = 0; frequencyX <= half; frequencyX++)
|
||||
{
|
||||
real.Clear();
|
||||
|
||||
@@ -4,11 +4,9 @@ namespace WaveOptics.Optics;
|
||||
|
||||
internal sealed class FraunhoferKernelSampler
|
||||
{
|
||||
double[] real = [];
|
||||
double[] imaginary = [];
|
||||
readonly ComplexLines grid = new();
|
||||
readonly ComplexLines line = new();
|
||||
double[] intensity = [];
|
||||
double[] columnReal = [];
|
||||
double[] columnImaginary = [];
|
||||
bool[] sampledColumns = [];
|
||||
|
||||
public bool TrySample(in PsfSpecification specification, Span<double> kernel)
|
||||
@@ -20,6 +18,10 @@ internal sealed class FraunhoferKernelSampler
|
||||
var gridSize = specification.PupilGridSize;
|
||||
EnsureCapacity(gridSize);
|
||||
|
||||
var real = grid.Real;
|
||||
var imaginary = grid.Imaginary;
|
||||
var columnReal = line.Real;
|
||||
var columnImaginary = line.Imaginary;
|
||||
var center = gridSize / 2;
|
||||
var reach = (int)(pupilDiameter / 2d);
|
||||
var firstRow = Math.Max(center - reach, 0);
|
||||
@@ -28,7 +30,7 @@ internal sealed class FraunhoferKernelSampler
|
||||
return false;
|
||||
|
||||
for (var row = firstRow; row <= lastRow; row++)
|
||||
FastFourierTransform.Forward(real.AsSpan(row * gridSize, gridSize), imaginary.AsSpan(row * gridSize, gridSize));
|
||||
FastFourierTransform.Forward(real.Slice(row * gridSize, gridSize), imaginary.Slice(row * gridSize, gridSize));
|
||||
|
||||
MarkSampledColumns(in specification, center, positionScales);
|
||||
for (var shiftedColumn = 0; shiftedColumn < gridSize; shiftedColumn++)
|
||||
@@ -37,17 +39,17 @@ internal sealed class FraunhoferKernelSampler
|
||||
continue;
|
||||
|
||||
var column = (shiftedColumn + center) % gridSize;
|
||||
columnReal.AsSpan(0, firstRow).Clear();
|
||||
columnImaginary.AsSpan(0, firstRow).Clear();
|
||||
columnReal[..firstRow].Clear();
|
||||
columnImaginary[..firstRow].Clear();
|
||||
for (var row = firstRow; row <= lastRow; row++)
|
||||
{
|
||||
columnReal[row] = real[row * gridSize + column];
|
||||
columnImaginary[row] = imaginary[row * gridSize + column];
|
||||
}
|
||||
columnReal.AsSpan(lastRow + 1, gridSize - lastRow - 1).Clear();
|
||||
columnImaginary.AsSpan(lastRow + 1, gridSize - lastRow - 1).Clear();
|
||||
columnReal.Slice(lastRow + 1, gridSize - lastRow - 1).Clear();
|
||||
columnImaginary.Slice(lastRow + 1, gridSize - lastRow - 1).Clear();
|
||||
|
||||
FastFourierTransform.Forward(columnReal.AsSpan(0, gridSize), columnImaginary.AsSpan(0, gridSize));
|
||||
FastFourierTransform.Forward(columnReal[..gridSize], columnImaginary[..gridSize]);
|
||||
var split = gridSize - center;
|
||||
for (var row = 0; row < split; row++)
|
||||
{
|
||||
@@ -101,17 +103,11 @@ internal sealed class FraunhoferKernelSampler
|
||||
void EnsureCapacity(int gridSize)
|
||||
{
|
||||
var area = gridSize * gridSize;
|
||||
if (real.Length < area)
|
||||
{
|
||||
real = new double[area];
|
||||
imaginary = new double[area];
|
||||
grid.Ensure(area);
|
||||
line.Ensure(gridSize);
|
||||
if (intensity.Length < area)
|
||||
intensity = new double[area];
|
||||
}
|
||||
if (columnReal.Length < gridSize)
|
||||
{
|
||||
columnReal = new double[gridSize];
|
||||
columnImaginary = new double[gridSize];
|
||||
if (sampledColumns.Length < gridSize)
|
||||
sampledColumns = new bool[gridSize];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,10 +74,10 @@ public sealed class FraunhoferPsfGenerator : IPsfGenerator
|
||||
}
|
||||
}
|
||||
|
||||
internal static int BuildPupil(in PsfSpecification specification, double[] real, double[] imaginary, int firstRow, int lastRow)
|
||||
internal static int BuildPupil(in PsfSpecification specification, Span<double> real, Span<double> imaginary, int firstRow, int lastRow)
|
||||
=> BuildPupil(in specification, specification.PupilDiameterSamples, 1d, real, imaginary, firstRow, lastRow);
|
||||
|
||||
internal static int BuildPupil(in PsfSpecification specification, double pupilDiameter, double phaseScale, double[] real, double[] imaginary, int firstRow, int lastRow)
|
||||
internal static int BuildPupil(in PsfSpecification specification, double pupilDiameter, double phaseScale, Span<double> real, Span<double> imaginary, int firstRow, int lastRow)
|
||||
{
|
||||
var gridSize = specification.PupilGridSize;
|
||||
var center = gridSize / 2;
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする