将 1 到 100 的数字相加 OpenMP

Adding numbers from 1 to 100 OpenMP

我试图只使用 5 个线程来计算 1 到 100 之间的数字总和,尽管我有 12 个线程可用。

这是我的方法。 请告诉我哪里出错了。

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
int nthreads = 5, tid;
int sum = 0;
int a[100];

for(int i = 1; i < 101; i++){
    a[i] = i;
}

  /* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
nthreads = omp_get_num_threads();

#pragma omp parallel for reduction (+:sum)

    for(int i = 0; i < 100; i++){
        sum = sum + a[i + 1];
    }

    tid = omp_get_thread_num();
    printf("Current thread is %d\n", tid);
    printf("Number of threads = %d\n", nthreads);
    printf("The total sum is %d\n\n\n", sum);

  }

编辑:这是我得到的输出: Code Output

我想要的输出如下:

您的代码存在一些问题,即:

第一个:

for(int i = 1; i < 101; i++){
    a[i] = i;
}

您已将数组 a 分配为 int a[100];,因此在您的循环中您获取数组边界,将其更改为:

for(int i = 0; i < 100; i++){
    a[i] = i + 1;
}

第二个:

int nthreads = 5
...
/* Fork a team of threads giving them their own copies of variables */
#pragma omp parallel private(nthreads, tid)
nthreads = omp_get_num_threads();

这毫无意义。您创建一个并行区域,所有线程都有 nthreads 的副本,它们将该变量设置为线程数,但在并行区域之后该值消失了。之后打印 5 in:

的唯一原因
printf("Number of threads = %d\n", nthreads);

是因为nthreads最初设置为5.

第三名:

 for(int i = 0; i < 100; i++){
        sum = sum + a[i + 1];
    }

您再次访问数组 a 之外的位置,将其更改为:

 for(int i = 0; i < 100; i++){
        sum = sum + a[i];
    }

第四名:

I'm trying to get the sum of numbers from 1 to 100 using only 5 threads even though I have 12 available.

因为你没有指定并行区域的线程数:

#pragma omp parallel for reduction (+:sum)

你的代码是 运行 12 个线程。

The Output that I want is the following:

The total sum is 5050 instead of 4950 is there a way to output the each thread's local sum?

您要做的是:

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
   int sum = 0;
   int a[100];

   for(int i = 0; i < 100; i++){
       a[i] = i + 1;
   }
   int total_threads_used;
   // Create a parallel region with 5 threads and reduce the partial sum's values
   #pragma omp parallel num_threads(5) reduction (+:sum)
   {
        total_threads_used = omp_get_num_threads(); // Get the total threads used
        #pragma omp for
        for(int i = 0; i < 100; i++){
           sum = sum + a[i];
        }
        printf("Current thread is %d and SUM %d\n", omp_get_thread_num(), sum);  
    }
    printf("Number of threads = %d\n", total_threads_used);
    printf("The total sum is %d\n\n\n", sum);
}

输出:

Current thread is 0 and SUM 210
Current thread is 2 and SUM 1010
Current thread is 1 and SUM 610
Current thread is 3 and SUM 1410
Current thread is 4 and SUM 1810
Number of threads = 5
The total sum is 5050

这些行将以与 运行 运行 不同的顺序输出,因为这些行是并行打印的,但是不管 运行 你应该有 5 "Current thread is" 从 0 到 4、1 "Number of threads = 5" 和 1 "The total sum is 4950".