C ++ async vs OpenMP tasks

In OpenMP, I can create a set of tasks as follows and execute them asynchronously using some fixed number of threads:

#pragma omp parallel
{
   #pragma omp single 
   {
      for (int i = 0; i < 1000; i++) {
         #pragma omp task
         f(i);
}  }  }

In C ++ 11, I can do something not quite the same std::async:

std::vector<std::future> futures;
for (int i = 0; i < 1000; i++) {
   auto fut = std::async(f, i);
   futures.push_back(std::move(fut));
}
...
for (auto & fut : futures) {
  auto res = fut.get();
  // do something with res
}

I am worried about efficiency. If I am right, in OpenMP , tasks are stored in the pool of some tasks, and then are distributed among threads (automatically using the OpenMP environment).

In C ++ , at the time of a call, the std::asyncruntime decides whether to start asynchronously f(i)in a new thread or delay its execution to the point of call std::future::get.

Therefore, either the runtime

  • creates 1000 threads and starts them at the same time,
  • , f(i) ( ).

, , , , OpenMP ( ).

, OpenMP ++?

UPDATE

: https://wandbox.org/permlink/gLCFPr1IjTofxwQh 12C Xeon E5, GCC 7.2 -O2:

  • OpenMP 12 : 12.2 [s]
  • ++: 12.4 [s]

( ). .

500 000 (n) 1000 (m), :

  • OpenMP 12 : 15.1 [s]
  • ++ threding runtime: 175.6 [s]

2

( pthread_create : qaru.site/questions/873190/...):

(20 000 , 20 000 ):

  • OpenMP 12 : 11
  • ++ threding runtime: 20 000

(500 000 , 1000 ):

  • OpenMP 12 : 11
  • ++ threding runtime: 32 744
+4
2

, , std::async.

OpenMP , , . untied , -, .

, ++ 11, std::launch::async, std::launch::deferred. std::thread, , wait. , ( ):

, policy launch::async | launch::deferred, , concurrency .

, , , , , , -, ! launch:async , , std::thread - , , .

, std::thread , . , , [ <thread>] " " .

, . OpenMP .

, std::async, .

+2

, , , , OpenMP ( ).

, , OpenMP. OpenMP - API, " " , . , . , . , , parallelism.

, OpenMP: , , , , , , , std::async : ( ) , - , , . std::async ( ) . .

, , , std::thread. , , ( ), std::async OpenMP, , .

, , : , OpenMP , . . , , - , .

0

Source: https://habr.com/ru/post/1691070/


All Articles