Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to limit threads count for C++ 17 parallel `for_each`?

I use std::for_each with std::execution::par to perform complex computation on huge input represented as vector of structures. The computation doesn't need any delays related to hardware (network or disk IO for example), it is "just CPU" computation. For me it looks logical that there are no sense to create more OS threads that we have hardware ones; however, Visual C++ 2019 creates in average 50 threads, and sometimes up to 500 ones even there are only 12 hardware threads.

Is there a way to limit parallel threads count to hardware_concurrency with std::for_each and std::execution::par, or the only way to create reasonable threads count is to use custom code with std::thread?

like image 612
Vitalii Avatar asked Nov 18 '19 13:11

Vitalii


People also ask

Will parallel extensions start more than 1000 threads?

Show activity on this post. No, it won't start 1000 threads - yes, it will limit how many threads are used. Parallel Extensions uses an appropriate number of cores, based on how many you physically have and how many are already busy.

How to limit the number of instances of a parallel option?

Use another overload of Parallel.Foreach that takes a ParallelOptions instance, and set MaxDegreeOfParallelism to limit how many instances execute in parallel. Show activity on this post.

What determines the number of threads a thread can spawn?

Basically the thread pool behind all the various Parallel library functions, will work out an optimum number of threads to use. The number of physical processor cores forms only part of the equation. There is NOT a simple one to one relationship between the number of cores and the number of threads spawned.

Is it possible to start 1000 threads at a time?

No, it won't start 1000 threads - yes, it will limit how many threads are used. Parallel Extensions uses an appropriate number of cores, based on how many you physically have and how many are already busy.


1 Answers

Is it possible to limit threads count for C++ 17 parallel for_each?

No, at least not in C++17. However, there is a proposal for executors in a standard to come, which basically gives you the ability to influence the execution context (in terms of location and time) for the high-level STL algorithm interface:

thread_pool pool{ std::thread::hardware_concurrency() };
auto exec = pool.executor();
std::for_each(std::execution::par.on(exec), begin(data), end(data), some_operation);

Up to then, you have to either trust your compiler vendor that he knows what is best for the overall performance, as e.g. the developers of Visual Studio state:

Scheduling in our implementation is handled by the Windows system thread pool. The thread pool takes advantage of information not available to the standard library, such as what other threads on the system are doing, what kernel resources threads are waiting for, and similar. It chooses when to create more threads, and when to terminate them. It’s also shared with other system components, including those not using C++.

The other option would be to give up on solely relying on the standard library and use STL implementations which already feature the new proposal.

like image 59
Jodocus Avatar answered Sep 21 '22 22:09

Jodocus