0

I have experience with parallel loops and sections with OpenMP in C++, but now I need to make the threads go ahead and start a function while one thread keep running a forner function

The function table_builder_1 create a matrix and after all threads finish running, only one thread start to write the matrix in a file (currently all others threads wait for him) .

1. How can I do the threads go ahead and start the function table_builder_2?
2. After the writer thread finish he will join with others?

int main{

Class foo

foo.table_builder_1;
foo.table_builder_2;


return 0;
}



void foo:table_builder_1{

#pragma omp parallel for schedule(dynamic)
    for (int i = 0; i < N_size; i++){
      for (int j = 0; j < N_size; j++) {
         create table
    }
}

\\write the table in file: 
file.write (Table) 


return;
}

Remark: 1. I can’t parallelize the write because the data need to be ordered. 2. The function table_builder_2 do the same as table_builder_1

Guus
  • 15
  • 3
  • 1
    Could you add valid c++ code, please? Also if both functions do the same why do you have two functions in the first place instead of one? Regarding remark 1: doing parallel writes to io hardware is never a good idea. – Timo Sep 29 '19 at 14:41
  • I will edit a simplified code and post it as soon as it's over. The functions do almost the same thing, the idea is the same. Between the functions I check if the file exists, if not the creation function is executed. – Guus Sep 29 '19 at 14:56

1 Answers1

0

This sounds like a good case for #pragma omp single nowait.

#pragma omp single specifies that a section of code should be executed by single thread (not necessarily the master thread) (from this answer.)

The nowait clause prevents the other threads from waiting for the thread that writes.

stephen
  • 85
  • 6