explict flush directive with OpenMP: when is it necessary and when is it helpful

后端 未结 2 1362
青春惊慌失措
青春惊慌失措 2020-12-15 11:36

One OpenMP directive I have never used and don\'t know when to use is flush(with and without a list).

I have two questions:

1.) When i         


        
2条回答
  •  自闭症患者
    2020-12-15 11:55

    Not exactly an answer, but Michael Klemm's question is closed for comments. I think an excellent example of why flushes are so hard to understand and use properly is the following one copied (and shortened a bit) from the OpenMP Examples:

    //http://www.openmp.org/wp-content/uploads/openmp-examples-4.0.2.pdf
    //Example mem_model.2c, from Chapter 2 (The OpenMP Memory Model)
    int main() {
       int data, flag = 0;
       #pragma omp parallel num_threads(2)
       {
          if (omp_get_thread_num()==0) {
             /* Write to the data buffer that will be read by thread */
             data = 42;
             /* Flush data to thread 1 and strictly order the write to data
                relative to the write to the flag */
             #pragma omp flush(flag, data)
             /* Set flag to release thread 1 */
             flag = 1;
             /* Flush flag to ensure that thread 1 sees S-21 the change */
             #pragma omp flush(flag)
          }
          else if (omp_get_thread_num()==1) {
             /* Loop until we see the update to the flag */
             #pragma omp flush(flag, data)
             while (flag < 1) {
                #pragma omp flush(flag, data)
             }
             /* Values of flag and data are undefined */
             printf("flag=%d data=%d\n", flag, data);
             #pragma omp flush(flag, data)
             /* Values data will be 42, value of flag still undefined */
             printf("flag=%d data=%d\n", flag, data);
          }
       }
       return 0;
    }
    

    Read the comments and try to understand.

提交回复
热议问题