What I'm trying to do is that the first part of the function runs in a single thread and other thread start the second function (this function has three loops) and after the single thread ends the first function he join with others to help with loops.
I made the following code, which is wrong because all threads run the second function, and is only needed one to run but all help with loops.
void main(){
/* code */
#pragma omp parallel
{
#pragma omp single nowait
{
std::cout << "Running func 1:" << omp_get_thread_num() << std::endl;
func1();
std::cout << "Finish func 1" <<std::endl;
}
#pragma omp nowait
std::cout << "Running func 2:" << omp_get_thread_num() << std::endl;
func2();
#pragma omp barrier
}//close parallel
/* more code */
}//close main
void func2(void){
/* code to read file */
#pragma omp parallel
{
for (int i=40;i<60;i++){
#pragma omp for nowait
for (int j=0;j<100;j++){
/* code */
}
}
#pragma omp for schedule(dynamic,1) nowait
for (int i=0;i<40;i++){
for (int j=0;j<100;j++){
/* code */
}
}
#pragma omp for schedule(dynamic)
for (int i=60;i<100;i++){
for (int j=0;j<100;j++){
/* code */
}
}
/* code to write file */
}//close parallel
#pragma omp barrier
} //close func2
My terminal shows:
Running func 1: 0
Running func 2: 1
Running func 2: 2
Running func 2: 3
Finish func 1
Running func 2: 0
Edit
Obs.: Func1 should only be executed on one thread
func2 has been split into three for loops because the first for consumes more time than all the others together. If I only use one for loop, all other threads will end and some will continue to run. This way, the hardes ones are calculated first.
Using the code suggested by Jim Cownie, func1 and func2 run in parallel, however either the threads run func2 twice or only one thread runs alone without the help of the others.
Any ideas how I can do even if it's necessary using task or sections?