Is there a way to explicitly set / limit the degree of parallelism (= number of separate threads) used by std::async and related classes?
Listening to the thread support library has nothing to do with it.
As I understand it, the std::async implementation (usually?) Uses the thread pool inside. Is there a standardized API to manage this?
For the background: Im in the setup (shared cluster), where I need to manually limit the number of cores used. If I canβt do this, the load balancer will push and I will be fined. In particular, std::thread::hardware_concurrency() does not contain any useful information, since the number of physical cores does not matter for Im restrictions.
Here is a piece of code (which in C ++ 17 with parallelism TS is likely to be written using parallel std::transform ):
auto read_data(std::string const&) -> std::string; auto multi_read_data(std::vector<std::string> const& filenames, int ncores = 2) -> std::vector<std::string> { auto futures = std::vector<std::future<std::string>>{}; // Haha, I wish. std::thread_pool::set_max_parallelism(ncores); for (auto const& filename : filenames) { futures.push_back(std::async(std::launch::async, read_data, filename)); } auto ret = std::vector<std::string>(filenames.size()); std::transform(futures.begin(), futures.end(), ret.begin(), [](std::future<std::string>& f) {return f.get();}); return ret; }
From a design point of view, Id expected the class std::execution::parallel_policy (from parallelism TS) to indicate that (in fact, this is what I did in the framework developed for my master's thesis). But this does not seem to be the case.
Ideal Id as a solution for C ++ 11, but if it is for later versions, I would still like to know about it (although I can not use it).
c ++ multithreading asynchronous parallel-processing c ++ 11
Konrad Rudolph
source share