Skip to content
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

Add a new thread pool that switches between single- and multi- threaded execution #73

Merged
merged 5 commits into from
Jan 24, 2025

Conversation

dian-lun-lin
Copy link
Contributor

A thread pool that dynamically switches between single-threaded and multi-threaded execution.

  • If n == 1, the task will be executed on the main thread without any locking mechanism that exists in NativeThreadPool.
  • For n > 1, the tasks will be delegated to the internal NativeThreadPool for parallel execution.

@dian-lun-lin dian-lun-lin requested a review from ibhati January 22, 2025 20:42
///// - If `n == 1`, the task will be executed on the main thread without any locking mechanism.
///// - For `n > 1`, the tasks will be delegated to the internal `NativeThreadPool` for parallel execution.
/////
class SwitchNativeThreadPool {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the NativeThreadPoolBase ::parallel_for() implementation, a task is executed in the current thread in case of n == 1.
Why we cannot just fix existing code rather than making one more class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default NativeThreadPool is designed to be thread-safe. We want to ensure that no external threads call parallel_for in parallel, even with n==1. That is, the lock in NativeThreadPool will be always at the beginning of parallel_for.

It would be better to have a separate implementation that focuses on this dynamic switching capability.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks. Looking to NativeThreadPool::size() code I did not realize that NativeThreadPool is thread-safe.

@dian-lun-lin dian-lun-lin merged commit fc44f44 into intel:main Jan 24, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants