I'm looking for a way to execute sections of code in parallel using multiple threads for each section. For example, if I have 16 threads and two tasks, I want 8 threads each to simultaneously execute those two tasks. OpenMP has several constructs (section
, task
) that execute general code in parallel, but they are single-threaded. In my scenario, using section
or task
would result in one thread executing each of the two tasks, while 14 threads sit idly by.
Is something like that even possible with OpenMP? If so, how do I do it, and if not, what can I use for that purpose?
Thanks for your time!
edit 2:
Let me expand on this question with an example code:
class some_class{
void task(){
cout<<"Entering the task method"<<endl;
#pragma openmp parallel for
for(int i=0; i < large_matrix.rows(); i++){
perform_thread_safe_operation(large_matrix.getRow(i));
}
}
matrix large_matrix;
};
void main(){
//I have 16 cores, so I want to spawn 16 threads
some_class o1;
some_class o2;
// I want 8 of the 16 threads to execute this line:
o1.task();
// and 8 remaining threads to execute this line:
o2.task();
}