We have to ensure that the runtime is initialized _before_ waiting
for the two started threads to guarantee that the master threads
post their ompt_event_thread_begin before the worker threads. This
is not guaranteed in the parallel region where one worker thread
could start before the other master thread has invoked the callback.
The problem did not happen with Clang becauses the generated code
calls __kmpc_global_thread_num() and cashes its result for functions
that contain OpenMP pragmas.
Differential Revision: https://reviews.llvm.org/D43882
llvm-svn: 326435
#include <iostream>
#include <thread>
+
#include "callback.h"
+#include "omp.h"
+
int condition = 0;
+
void f() {
+ // Call OpenMP API function to force initialization of OMPT.
+ // (omp_get_thread_num() does not work because it just returns 0 if the
+ // runtime isn't initialized yet...)
+ omp_get_num_threads();
+
OMPT_SIGNAL(condition);
- // wait for both pthreads to arrive
+ // Wait for both initial threads to arrive that will eventually become the
+ // master threads in the following parallel region.
OMPT_WAIT(condition, 2);
- int i = 0;
+
#pragma omp parallel num_threads(2)
{
+ // Wait for all threads to arrive so that no worker thread can be reused...
OMPT_SIGNAL(condition);
OMPT_WAIT(condition, 6);
}
}
+
int main() {
std::thread t1(f);
std::thread t2(f);