Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/ImageSharp/Advanced/ParallelUtils/ParallelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,23 @@ public static void IterateRows(

var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps };

int top = rectangle.Top;
int bottom = rectangle.Bottom;

Parallel.For(
0,
numOfSteps,
parallelOptions,
i =>
{
int yMin = rectangle.Top + (i * verticalStep);
int yMax = Math.Min(yMin + verticalStep, rectangle.Bottom);
int yMin = top + (i * verticalStep);

if (yMin >= bottom)
{
return;
}

int yMax = Math.Min(yMin + verticalStep, bottom);

var rows = new RowInterval(yMin, yMax);
body(rows);
Expand Down Expand Up @@ -110,13 +119,22 @@ internal static void IterateRowsWithTempBuffer<T>(

var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps };

int top = rectangle.Top;
int bottom = rectangle.Bottom;

Parallel.For(
0,
numOfSteps,
parallelOptions,
i =>
{
int yMin = rectangle.Top + (i * verticalStep);
int yMin = top + (i * verticalStep);

if (yMin >= bottom)
{
return;
}

int yMax = Math.Min(yMin + verticalStep, rectangle.Bottom);

var rows = new RowInterval(yMin, yMax);
Expand Down
40 changes: 24 additions & 16 deletions tests/ImageSharp.Tests/Helpers/ParallelHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ public ParallelHelperTests(ITestOutputHelper output)
/// <summary>
/// maxDegreeOfParallelism, minY, maxY, expectedStepLength, expectedLastStepLength
/// </summary>
public static TheoryData<int, int, int, int, int> IterateRows_OverMinimumPixelsLimit_Data =
new TheoryData<int, int, int, int, int>
public static TheoryData<int, int, int, int, int, int> IterateRows_OverMinimumPixelsLimit_Data =
new TheoryData<int, int, int, int, int, int>
{
{ 1, 0, 100, -1, 100 },
{ 2, 0, 9, 5, 4 },
{ 4, 0, 19, 5, 4 },
{ 2, 10, 19, 5, 4 },
{ 4, 0, 200, 50, 50 },
{ 4, 123, 323, 50, 50 },
{ 4, 0, 1201, 301, 298 },
{ 8, 10, 236, 29, 23 }
{ 1, 0, 100, -1, 100, 1 },
{ 2, 0, 9, 5, 4, 2 },
{ 4, 0, 19, 5, 4, 4 },
{ 2, 10, 19, 5, 4, 2 },
{ 4, 0, 200, 50, 50, 4 },
{ 4, 123, 323, 50, 50, 4 },
{ 4, 0, 1201, 301, 298, 4 },
{ 8, 10, 236, 29, 23, 8 },
{ 16, 0, 209, 14, 13, 15 },
{ 24, 0, 209, 9, 2, 24 },
{ 32, 0, 209, 7, 6, 30 },
{ 64, 0, 209, 4, 1, 53 },
};

[Theory]
Expand All @@ -48,7 +52,8 @@ public void IterateRows_OverMinimumPixelsLimit_IntervalsAreCorrect(
int minY,
int maxY,
int expectedStepLength,
int expectedLastStepLength)
int expectedLastStepLength,
int expectedNumberOfSteps)
{
var parallelSettings = new ParallelExecutionSettings(
maxDegreeOfParallelism,
Expand All @@ -74,7 +79,7 @@ public void IterateRows_OverMinimumPixelsLimit_IntervalsAreCorrect(
Assert.Equal(expected, step);
});

Assert.Equal(maxDegreeOfParallelism, actualNumberOfSteps);
Assert.Equal(expectedNumberOfSteps, actualNumberOfSteps);
}

[Theory]
Expand All @@ -84,7 +89,8 @@ public void IterateRows_OverMinimumPixelsLimit_ShouldVisitAllRows(
int minY,
int maxY,
int expectedStepLength,
int expectedLastStepLength)
int expectedLastStepLength,
int expectedNumberOfSteps)
{
var parallelSettings = new ParallelExecutionSettings(
maxDegreeOfParallelism,
Expand Down Expand Up @@ -117,7 +123,8 @@ public void IterateRowsWithTempBuffer_OverMinimumPixelsLimit(
int minY,
int maxY,
int expectedStepLength,
int expectedLastStepLength)
int expectedLastStepLength,
int expectedNumberOfSteps)
{
var parallelSettings = new ParallelExecutionSettings(
maxDegreeOfParallelism,
Expand Down Expand Up @@ -146,7 +153,7 @@ public void IterateRowsWithTempBuffer_OverMinimumPixelsLimit(
Assert.Equal(expected, step);
});

Assert.Equal(maxDegreeOfParallelism, actualNumberOfSteps);
Assert.Equal(expectedNumberOfSteps, actualNumberOfSteps);

int numberOfDifferentBuffers = bufferHashes.Distinct().Count();
Assert.Equal(actualNumberOfSteps, numberOfDifferentBuffers);
Expand All @@ -159,7 +166,8 @@ public void IterateRowsWithTempBuffer_OverMinimumPixelsLimit_ShouldVisitAllRows(
int minY,
int maxY,
int expectedStepLength,
int expectedLastStepLength)
int expectedLastStepLength,
int expectedNumberOfSteps)
{
var parallelSettings = new ParallelExecutionSettings(
maxDegreeOfParallelism,
Expand Down