-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ParallelUnbalancedWork for efficient unbalanced parallel loops (#7787)
- Loading branch information
Showing
12 changed files
with
546 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/Nethermind/Nethermind.Benchmark/Core/ParallelBenchmark.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using BenchmarkDotNet.Attributes; | ||
using Nethermind.Core.Threading; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Nethermind.Benchmarks.Core; | ||
|
||
[HideColumns("Job", "RatioSD")] | ||
public class ParallelBenchmark | ||
{ | ||
private int[] _times; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_times = new int[200]; | ||
|
||
for (int i = 0; i < _times.Length; i++) | ||
{ | ||
_times[i] = i % 100; | ||
} | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void ParallelFor() | ||
{ | ||
Parallel.For( | ||
0, | ||
_times.Length, | ||
(i) => Thread.Sleep(_times[i])); | ||
} | ||
|
||
[Benchmark] | ||
public void ParallelForEach() | ||
{ | ||
Parallel.ForEach( | ||
_times, | ||
(time) => Thread.Sleep(time)); | ||
} | ||
|
||
[Benchmark] | ||
public void UnbalancedParallel() | ||
{ | ||
ParallelUnbalancedWork.For<int[]>( | ||
0, | ||
_times.Length, | ||
ParallelUnbalancedWork.DefaultOptions, | ||
_times, | ||
(i, value) => | ||
{ | ||
Thread.Sleep(value[i]); | ||
return value; | ||
}, | ||
(value) => { }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.