Reputation: 3071
Is there a way for OpenMP to remove the barrier on a parallel
block?
I know that nowait
can be used in for
or sections
blocks within a parallel
in order to allow threads to move ahead without having to wait for all of them to finish the relevant block. However, #pragma omp parallel nowait
generates a compiler error.
I am developing a program with a UI. I call a function to load up the UI and, while that happens, I also want to get in touch with a server to send some usage data.
Each of these functions works fine, but if I don't do them in parallel it takes a while. If the server is down, then the connection attempt can take a few seconds to abort, and then simply loading the UI takes a second as well, at which point things are just taking too long. Loading the UI first and then making the connection makes the UI appear to freeze.
So I would like to do something like
// this generates a compiler error
#pragma omp parallel num_threads(2) nowait
{
if(omp_get_thread_num() == 0)
LoadUI();
else
Connect();
}
This way, the master thread (which must be the one to load the UI) will load the UI and then move on regardless of any connection problems. However, since parallel nowait
is forbidden, it is my understanding that the program will still momentarily hang if there's a connection problem (which takes longer to abort than the UI takes to load) due to the implicit barrier at the end of parallel
.
Is there a way around this? What I really want to do is request a new thread to do the connection while the program goes on regardless of what the thread does or how long it takes to finish (obviously, the thread doesn't touch any of the data used by the master thread).
Upvotes: 1
Views: 23664
Reputation: 5162
I don't think what you are trying to do is compatible with the OpenMP fork-join model. The idea is that in each parallel region you spawn threads which are joined at the end of the parallel region. OK, the threads are not actually spawned because there is typically a thread pool, but this is the conceptual model.
Note that even the following generates a compiler error:
#pragma omp parallel for nowait
for (i=0; i<10; i++) {
...
}
What you can do is to avoid the barrier after the for
, inside a parallel region. For example:
#pragma omp parallel
{
#pragma omp parallel for
for (int i=0; i<2; i++) {
}
/* no wait here */
#pragma omp for
for (int i=0; i<2; i++) {
}
}
To do what you need you will probably have to use pthreads or thread library.
PS: You can use OpenMP sections instead of checking the thread id.
Upvotes: 3