【问题标题】:Didn't understand the output from parallel reduction using max operation OpenMP program?不了解使用最大运算 OpenMP 程序并行减少的输出?
【发布时间】:2021-05-02 14:16:45
【问题描述】:

我在阅读有关并行编程的内容时遇到了这个基本代码。 这是使用最大操作的并行缩减程序。我们对这次行动的期望究竟是什么?

 
 #include <stdio.h>

 #include <omp.h>

 int main() {
   double arr[10];
   omp_set_num_threads(4);
   double max_val = 0.0;
   int i;
   for (i = 0; i < 10; i++)
     arr[i] = 2.0 + i;
   #pragma omp parallel
   for reduction(max: max_val)
   for (i = 0; i < 10; i++) {
     printf("thread id = %d  and i = %d \n", omp_get_thread_num(), i);
     if (arr[i] > max_val) {
       max_val = arr[i];
     }
   }
   printf("\nmax_val = %f", max_val);
 }
  

这是输出

thread id = 2  and i = 6
thread id = 2  and i = 7
thread id = 1  and i = 3
thread id = 1  and i = 4
thread id = 1  and i = 5
thread id = 3  and i = 8
thread id = 3  and i = 9
thread id = 0  and i = 0
thread id = 0  and i = 1
thread id = 0  and i = 2

max_val = 11.000000

我是 openmp 新手。请帮助我理解这段代码。我没有得到这个输出。这个结果是怎么来的?

【问题讨论】:

    标签: c multithreading parallel-processing openmp


    【解决方案1】:

    您的代码填满了arr

       int i;
       for (i = 0; i < 10; i++)
         arr[i] = 2.0 + i;
    

    使用来自2 to 11的值:

    然后在并行循环中:

       #pragma omp parallel for reduction(max: max_val)
       for (i = 0; i < 10; i++) {
         printf("thread id = %d  and i = %d \n", omp_get_thread_num(), i);
         if (arr[i] > max_val) {
           max_val = arr[i];
         }
       }
    

    当 OpenMP 读取 #pragma omp parallel for OpenMP 会将循环的迭代在团队之间划分为线程,在您的情况下为 4 个线程:

     omp_set_num_threads(4);
    

    关于#pragma omp parallel#pragma omp parallel for的更详细解答可以在here找到。

    使用reduction(max: max_val),OpenMP 将创建变量max_val 的副本每个 线程,在并行区域之后,原始变量max_val 将等于所有线程中最大的max_val .在你的情况下是11.0

    关于reduction 子句的更详细解答可以在here找到。

    函数omp_get_thread_num()返回调用该方法的线程的ID。

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 2014-10-28
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多