mirror of
https://github.com/DarthAffe/HPPH.git
synced 2025-12-12 13:28:37 +00:00
Improved performance of Sum on Images
This commit is contained in:
parent
723649053c
commit
f4c1eaddd6
@ -21,4 +21,18 @@ internal static class BenchmarkHelper
|
||||
|
||||
return colors;
|
||||
}
|
||||
|
||||
public static List<IImage<T>> GetSampleDataImages<T>()
|
||||
where T : struct, IColor
|
||||
{
|
||||
if (!Directory.Exists(SAMPLE_DATA_DIR)) throw new Exception("sample data not found!");
|
||||
|
||||
List<IImage<T>> colors = [];
|
||||
|
||||
IEnumerable<string> files = Directory.EnumerateFiles(SAMPLE_DATA_DIR, "*.png", SearchOption.AllDirectories);
|
||||
foreach (string file in files)
|
||||
colors.Add(ImageHelper.LoadImage(file).ConvertTo<T>());
|
||||
|
||||
return colors;
|
||||
}
|
||||
}
|
||||
@ -13,6 +13,8 @@ public class SumBenchmarks
|
||||
|
||||
private readonly List<ColorRGB[]> _colors3bpp;
|
||||
private readonly List<ColorRGBA[]> _colors4bpp;
|
||||
private readonly List<IImage<ColorRGB>> _images3bpp;
|
||||
private readonly List<IImage<ColorRGBA>> _images4bpp;
|
||||
|
||||
#endregion
|
||||
|
||||
@ -22,6 +24,9 @@ public class SumBenchmarks
|
||||
{
|
||||
_colors3bpp = BenchmarkHelper.GetSampleData<ColorRGB>();
|
||||
_colors4bpp = BenchmarkHelper.GetSampleData<ColorRGBA>();
|
||||
|
||||
_images3bpp = BenchmarkHelper.GetSampleDataImages<ColorRGB>();
|
||||
_images4bpp = BenchmarkHelper.GetSampleDataImages<ColorRGBA>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -48,6 +53,26 @@ public class SumBenchmarks
|
||||
return sums;
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public ISum[] PixelHelper_3BPP_Image()
|
||||
{
|
||||
ISum[] sums = new ISum[_colors3bpp.Count];
|
||||
for (int i = 0; i < _images3bpp.Count; i++)
|
||||
sums[i] = _images3bpp[i].Sum();
|
||||
|
||||
return sums;
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public ISum[] PixelHelper_4BPP_Image()
|
||||
{
|
||||
ISum[] sums = new ISum[_images4bpp.Count];
|
||||
for (int i = 0; i < _images4bpp.Count; i++)
|
||||
sums[i] = _images4bpp[i].Sum();
|
||||
|
||||
return sums;
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public ISum[] Reference_3BPP()
|
||||
{
|
||||
|
||||
@ -106,6 +106,7 @@ internal class Sum : IGeneratorFeature
|
||||
return $$"""
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace HPPH;
|
||||
@ -114,7 +115,8 @@ internal class Sum : IGeneratorFeature
|
||||
{
|
||||
#region Methods
|
||||
|
||||
unsafe ISum IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum<Color{{colorFormat.Format}}, Sum{{colorFormat.Format}}>(MemoryMarshal.Cast<byte, Color{{colorFormat.Format}}>(data));
|
||||
unsafe Generic4LongData IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum(MemoryMarshal.Cast<byte, Generic{{colorFormat.Bpp}}ByteData>(data));
|
||||
unsafe ISum IColorFormat.ToSum(Generic4LongData data) => Unsafe.BitCast<Generic4LongData, Sum{{colorFormat.Format}}>(data);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -130,7 +132,8 @@ internal class Sum : IGeneratorFeature
|
||||
|
||||
public partial interface IColorFormat
|
||||
{
|
||||
internal ISum Sum(ReadOnlySpan<byte> data);
|
||||
internal Generic4LongData Sum(ReadOnlySpan<byte> data);
|
||||
internal ISum ToSum(Generic4LongData data);
|
||||
}
|
||||
""";
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace HPPH;
|
||||
@ -8,7 +9,8 @@ public sealed partial class ColorFormatABGR
|
||||
{
|
||||
#region Methods
|
||||
|
||||
unsafe ISum IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum<ColorABGR, SumABGR>(MemoryMarshal.Cast<byte, ColorABGR>(data));
|
||||
unsafe Generic4LongData IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum(MemoryMarshal.Cast<byte, Generic4ByteData>(data));
|
||||
unsafe ISum IColorFormat.ToSum(Generic4LongData data) => Unsafe.BitCast<Generic4LongData, SumABGR>(data);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace HPPH;
|
||||
@ -8,7 +9,8 @@ public sealed partial class ColorFormatARGB
|
||||
{
|
||||
#region Methods
|
||||
|
||||
unsafe ISum IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum<ColorARGB, SumARGB>(MemoryMarshal.Cast<byte, ColorARGB>(data));
|
||||
unsafe Generic4LongData IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum(MemoryMarshal.Cast<byte, Generic4ByteData>(data));
|
||||
unsafe ISum IColorFormat.ToSum(Generic4LongData data) => Unsafe.BitCast<Generic4LongData, SumARGB>(data);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace HPPH;
|
||||
@ -8,7 +9,8 @@ public sealed partial class ColorFormatBGR
|
||||
{
|
||||
#region Methods
|
||||
|
||||
unsafe ISum IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum<ColorBGR, SumBGR>(MemoryMarshal.Cast<byte, ColorBGR>(data));
|
||||
unsafe Generic4LongData IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum(MemoryMarshal.Cast<byte, Generic3ByteData>(data));
|
||||
unsafe ISum IColorFormat.ToSum(Generic4LongData data) => Unsafe.BitCast<Generic4LongData, SumBGR>(data);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace HPPH;
|
||||
@ -8,7 +9,8 @@ public sealed partial class ColorFormatBGRA
|
||||
{
|
||||
#region Methods
|
||||
|
||||
unsafe ISum IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum<ColorBGRA, SumBGRA>(MemoryMarshal.Cast<byte, ColorBGRA>(data));
|
||||
unsafe Generic4LongData IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum(MemoryMarshal.Cast<byte, Generic4ByteData>(data));
|
||||
unsafe ISum IColorFormat.ToSum(Generic4LongData data) => Unsafe.BitCast<Generic4LongData, SumBGRA>(data);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace HPPH;
|
||||
@ -8,7 +9,8 @@ public sealed partial class ColorFormatRGB
|
||||
{
|
||||
#region Methods
|
||||
|
||||
unsafe ISum IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum<ColorRGB, SumRGB>(MemoryMarshal.Cast<byte, ColorRGB>(data));
|
||||
unsafe Generic4LongData IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum(MemoryMarshal.Cast<byte, Generic3ByteData>(data));
|
||||
unsafe ISum IColorFormat.ToSum(Generic4LongData data) => Unsafe.BitCast<Generic4LongData, SumRGB>(data);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace HPPH;
|
||||
@ -8,7 +9,8 @@ public sealed partial class ColorFormatRGBA
|
||||
{
|
||||
#region Methods
|
||||
|
||||
unsafe ISum IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum<ColorRGBA, SumRGBA>(MemoryMarshal.Cast<byte, ColorRGBA>(data));
|
||||
unsafe Generic4LongData IColorFormat.Sum(ReadOnlySpan<byte> data) => PixelHelper.Sum(MemoryMarshal.Cast<byte, Generic4ByteData>(data));
|
||||
unsafe ISum IColorFormat.ToSum(Generic4LongData data) => Unsafe.BitCast<Generic4LongData, SumRGBA>(data);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -4,5 +4,6 @@ namespace HPPH;
|
||||
|
||||
public partial interface IColorFormat
|
||||
{
|
||||
internal ISum Sum(ReadOnlySpan<byte> data);
|
||||
internal Generic4LongData Sum(ReadOnlySpan<byte> data);
|
||||
internal ISum ToSum(Generic4LongData data);
|
||||
}
|
||||
@ -156,6 +156,9 @@ internal sealed class IColorImageRow<T> : IImageRow
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public ReadOnlySpan<byte> AsByteSpan() => _buffer.AsSpan().Slice(_start, SizeInBytes);
|
||||
|
||||
/// <inheritdoc />
|
||||
public void CopyTo(Span<IColor> destination)
|
||||
{
|
||||
|
||||
@ -22,6 +22,8 @@ public interface IImageRow : IEnumerable<IColor>
|
||||
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
|
||||
IColor this[int x] { get; }
|
||||
|
||||
ReadOnlySpan<byte> AsByteSpan();
|
||||
|
||||
void CopyTo(Span<IColor> destination);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -245,8 +245,6 @@ public static partial class PixelHelper
|
||||
ColorCube<T>[] cubes = new ColorCube<T>[1 << splits];
|
||||
cubes[0] = new ColorCube<T>(0, colors.Length, SortTarget.None);
|
||||
|
||||
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = Environment.ProcessorCount };
|
||||
|
||||
int colorsLength = colors.Length;
|
||||
fixed (T* colorsPtr = colors)
|
||||
{
|
||||
@ -257,7 +255,7 @@ public static partial class PixelHelper
|
||||
{
|
||||
int currentCubeCount = 1 << i;
|
||||
|
||||
Parallel.For(0, currentCubeCount, parallelOptions, CreateCubes);
|
||||
Parallel.For(0, currentCubeCount, PARALLEL_OPTIONS, CreateCubes);
|
||||
|
||||
void CreateCubes(int index)
|
||||
{
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace HPPH;
|
||||
@ -14,29 +13,20 @@ public static unsafe partial class PixelHelper
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(image);
|
||||
|
||||
int dataLength = image.SizeInBytes;
|
||||
IColorFormat colorFormat = image.ColorFormat;
|
||||
|
||||
if (dataLength <= 1024)
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[dataLength];
|
||||
if (image.Height == 0) return colorFormat.ToSum(new Generic4LongData(0, 0, 0, 0));
|
||||
if (image.Height == 1) return colorFormat.ToSum(colorFormat.Sum(image.Rows[0].AsByteSpan()));
|
||||
|
||||
image.CopyTo(buffer);
|
||||
return image.ColorFormat.Sum(buffer);
|
||||
}
|
||||
else
|
||||
Vector256<long> result = Vector256<long>.Zero;
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
{
|
||||
byte[] array = ArrayPool<byte>.Shared.Rent(dataLength);
|
||||
Span<byte> buffer = array.AsSpan()[..dataLength];
|
||||
try
|
||||
{
|
||||
image.CopyTo(buffer);
|
||||
return image.ColorFormat.Sum(buffer);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(array);
|
||||
}
|
||||
Generic4LongData rowSum = colorFormat.Sum(image.Rows[y].AsByteSpan());
|
||||
Vector256<long> rowSumVector = Vector256.LoadUnsafe(ref Unsafe.As<Generic4LongData, long>(ref rowSum));
|
||||
result = Vector256.Add(result, rowSumVector);
|
||||
}
|
||||
|
||||
return colorFormat.ToSum(Unsafe.BitCast<Vector256<long>, Generic4LongData>(result));
|
||||
}
|
||||
|
||||
public static ISum Sum<T>(this IImage<T> image)
|
||||
@ -50,56 +40,37 @@ public static unsafe partial class PixelHelper
|
||||
public static ISum Sum<T>(this RefImage<T> image)
|
||||
where T : struct, IColor
|
||||
{
|
||||
int dataLength = image.Width * image.Height;
|
||||
int sizeInBytes = dataLength * T.ColorFormat.BytesPerPixel;
|
||||
IColorFormat colorFormat = T.ColorFormat;
|
||||
|
||||
if (sizeInBytes <= 1024)
|
||||
{
|
||||
Span<T> buffer = MemoryMarshal.Cast<byte, T>(stackalloc byte[sizeInBytes]);
|
||||
if (image.Height == 0) return colorFormat.ToSum(new Generic4LongData(0, 0, 0, 0));
|
||||
if (image.Height == 1) return colorFormat.ToSum(colorFormat.Sum(image.Rows[0].AsByteSpan()));
|
||||
|
||||
image.CopyTo(buffer);
|
||||
return Sum(buffer);
|
||||
}
|
||||
else
|
||||
Vector256<long> result = Vector256<long>.Zero;
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
{
|
||||
T[] array = ArrayPool<T>.Shared.Rent(dataLength);
|
||||
Span<T> buffer = array.AsSpan()[..(dataLength)];
|
||||
try
|
||||
{
|
||||
image.CopyTo(buffer);
|
||||
return Sum(buffer);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<T>.Shared.Return(array);
|
||||
}
|
||||
Generic4LongData rowSum = colorFormat.Sum(image.Rows[y].AsByteSpan());
|
||||
Vector256<long> rowSumVector = Vector256.LoadUnsafe(ref Unsafe.As<Generic4LongData, long>(ref rowSum));
|
||||
result = Vector256.Add(result, rowSumVector);
|
||||
}
|
||||
|
||||
return colorFormat.ToSum(Unsafe.BitCast<Vector256<long>, Generic4LongData>(result));
|
||||
}
|
||||
|
||||
public static ISum Sum<T>(this ReadOnlySpan<T> colors)
|
||||
where T : struct, IColor
|
||||
=> T.ColorFormat.Sum(MemoryMarshal.AsBytes(colors));
|
||||
{
|
||||
IColorFormat colorFormat = T.ColorFormat;
|
||||
return colorFormat.ToSum(colorFormat.Sum(MemoryMarshal.AsBytes(colors)));
|
||||
}
|
||||
|
||||
public static ISum Sum<T>(this Span<T> colors)
|
||||
where T : struct, IColor
|
||||
=> T.ColorFormat.Sum(MemoryMarshal.AsBytes(colors));
|
||||
|
||||
internal static ISum Sum<T, TSum>(ReadOnlySpan<T> colors)
|
||||
where T : struct, IColor
|
||||
where TSum : struct, ISum
|
||||
{
|
||||
if (colors == null) throw new ArgumentNullException(nameof(colors));
|
||||
|
||||
return T.ColorFormat.BytesPerPixel switch
|
||||
{
|
||||
// DarthAffe 05.07.2024: Important: The sum of 3-byte colors result in 4 byte data!
|
||||
3 => Unsafe.BitCast<Generic4LongData, TSum>(Sum(MemoryMarshal.Cast<T, Generic3ByteData>(colors))),
|
||||
4 => Unsafe.BitCast<Generic4LongData, TSum>(Sum(MemoryMarshal.Cast<T, Generic4ByteData>(colors))),
|
||||
_ => throw new NotSupportedException("Data is not of a supported valid color-type.")
|
||||
};
|
||||
IColorFormat colorFormat = T.ColorFormat;
|
||||
return colorFormat.ToSum(colorFormat.Sum(MemoryMarshal.AsBytes(colors)));
|
||||
}
|
||||
|
||||
private static Generic4LongData Sum(ReadOnlySpan<Generic3ByteData> data)
|
||||
internal static Generic4LongData Sum(ReadOnlySpan<Generic3ByteData> data)
|
||||
{
|
||||
long b1Sum = 0, b2Sum = 0, b3Sum = 0;
|
||||
|
||||
@ -215,7 +186,7 @@ public static unsafe partial class PixelHelper
|
||||
return new Generic4LongData(b1Sum, b2Sum, b3Sum, data.Length * 255);
|
||||
}
|
||||
|
||||
private static Generic4LongData Sum(ReadOnlySpan<Generic4ByteData> data)
|
||||
internal static Generic4LongData Sum(ReadOnlySpan<Generic4ByteData> data)
|
||||
{
|
||||
long b1Sum, b2Sum, b3Sum, b4Sum;
|
||||
int i = 0;
|
||||
|
||||
@ -3,4 +3,7 @@
|
||||
namespace HPPH;
|
||||
|
||||
[SkipLocalsInit]
|
||||
public static partial class PixelHelper;
|
||||
public static partial class PixelHelper
|
||||
{
|
||||
private static readonly ParallelOptions PARALLEL_OPTIONS = new() { MaxDegreeOfParallelism = Environment.ProcessorCount };
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user