-
Notifications
You must be signed in to change notification settings - Fork 486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ParallelUnbalancedWork for efficient unbalanced parallel loops #7787
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide some kind of benchmark?
src/Nethermind/Nethermind.Consensus/Processing/BlockCachePreWarmer.cs
Outdated
Show resolved
Hide resolved
- Introduced the `ParallelUnbalancedWork` class to efficiently execute parallel loops over a range of integers, handling unbalanced workloads. - Added static `For` methods to support parallel execution with and without thread-local data, including initialization and finalization functions. - Utilized thread pooling and a shared counter (`SharedCounter`) to distribute iterations among threads dynamically. - Implemented internal classes (`BaseData`, `Data`, and `InitProcessor<TLocal>`) to manage shared state and thread synchronization. - Aimed to optimize performance in scenarios where the workload per iteration is uneven, ensuring better resource utilization and reduced execution time.
0072faa
to
061956a
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a feeling ParallelUnbalancedWork
deserves either to be a PR to dotnet or at least its own nuget package.
{ | ||
IReadOnlyTxProcessorSource env = _envPool.Get(); | ||
int i = 0; | ||
IReadOnlyTxProcessorSource env = state.preWarmer._envPool.Get(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
super minor: maybe better to pass the pool directly rather than whole prewarmer as it is not needed?
And maybe build custom struct for it as those are used in 2 places?
public int ActiveThreads => Volatile.Read(ref _activeThreads); | ||
|
||
/// <summary> | ||
/// Marks a thread as completed. | ||
/// </summary> | ||
/// <returns>The number of remaining active threads.</returns> | ||
public int MarkThreadCompleted() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: in theory this could be misused by calling MarkThreadCompleted
first, but as this is private class we can ignore it.
I agree this should be in dotnet itself |
Changes
ParallelUnbalancedWork
class to efficiently execute parallel loops, handling unbalanced workloads.SharedCounter
to distribute iterations among threads dynamically..Wait
ing for background threads to completeParallel.For
Types of changes
What types of changes does your code introduce?
Testing
Requires testing