Reputation: 23052
Suppose I have two functions:
Fun1();
Fun2();
These functions are independent and the task would be improved if I could run them in parallel (that is, run Fun1
on one thread and Fun2
on another. I am using Visual Studio 2012 - so Open MP 2.0.
Is there a straight-forward way (without any parallel region thread number testing or for loop dodginess) to achieve this? Does OpenMP provide this kind of functionality?
I tried looking at the parallel
and task
directives as a place to start but I find most of the literature thoroughly incomprehensible and couldn't find any examples...
Upvotes: 0
Views: 98
Reputation: 8032
You may use a sections
work-sharing construct:
#pragma omp parallel sections
{
#pragma omp section
Fun1();
#pragma omp section
Fun2();
}
Quoting from OpenMP 2.0 specifications (sec. 2.4.2):
The sections directive identifies a noniterative work-sharing construct that specifies a set of constructs that are to be divided among threads in a team. Each section is executed once by a thread in the team.
Upvotes: 4
Reputation: 1653
Yeah, why not using native threads or some wrappers around them? Open MP, as far as i know is much more powerful, than a simple thread runner tool. Of course, you can mark some flag variable as shared or private and check it inside parallelling pragma or perhaps obtain appropriate results with using schedule directive (don't remember exactly all OMP features), but...
Looks like u don't really need Open MP.
Upvotes: 0
Reputation: 249153
C++11:
std::thread thread1(Fun1);
Fun2(); // runs on main thread
thread1.join(); // wait for completion
If you don't have C++11, you can replace std::thread
with boost::thread
and do the same.
Upvotes: 5