openMP exercise omp_bug2.c

时间:2014-02-08 17:18:30

标签: c++ multithreading multiprocessing openmp

这是来自OpenMP网站的练习: https://computing.llnl.gov/tutorials/openMP/exercise.html


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

int _tmain(int argc, _TCHAR* argv[])
{
int nthreads, i, tid;
float total;

/*** Spawn parallel region ***/
#pragma omp parallel private(i, tid) // i changed this line 
  {
  /* Obtain thread number */
  tid = omp_get_thread_num();
  /* Only master thread does this */
  if (tid == 0) {
    nthreads = omp_get_num_threads();
    printf("Number of threads = %d\n", nthreads);
    }
  printf("Thread %d is starting...\n",tid);

  #pragma omp barrier

  /* do some work */
  total = 0.0;
  #pragma omp for schedule(dynamic,10) 
  for (i=0; i<1000000; i++)
     total = total + i*1.0;

  printf ("Thread %d is done! Total= %e\n",tid,total);

  } 
}

输出为

Number of threads = 4
Thread 0 is starting...
Thread 3 is starting...
Thread 2 is starting...
Thread 1 is starting...
Thread 0 is done! Total= 0.000000e+000
Thread 3 is done! Total= 0.000000e+000
Thread 2 is done! Total= 0.000000e+000
Thread 1 is done! Total= 0.000000e+000

这意味着我们对变量“total”

存在问题

这是网站enter image description here

上的帮助

这是我的解决方案:您认为这是正确的方法吗?

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

int _tmain(int argc, _TCHAR* argv[])
{
int nthreads, i, tid;
float total;

/*** Spawn parallel region ***/
#pragma omp parallel private(total,tid)
  {
  /* Obtain thread number */
  tid = omp_get_thread_num();
  total= 0.0;
  /* Only master thread does this */
  if (tid == 0) {
    nthreads = omp_get_num_threads();
    printf("Number of threads = %d\n", nthreads);
    }
  printf("Thread %d is starting...\n",tid);



  #pragma omp parallel for schedule(static,10)\
  private(i)\
  reduction(+:total)
  for (i=0; i<1000000; i++) 
     total = total + i*1.0;

  printf ("Thread %d is done! Total= %e\n",tid,total);

  } /*** End of parallel region ***/
}

这是我的新输出:

Number of threads = 4
Thread 0 is starting...
Thread 1 is starting...
Thread 0 is done! Total= 4.999404e+011
Thread 2 is starting...
Thread 1 is done! Total= 4.999404e+011
Thread 2 is done! Total= 4.999404e+011
Thread 3 is starting...
Thread 3 is done! Total= 4.999404e+011

1 个答案:

答案 0 :(得分:1)

是的,你当然希望total成为一个线程私有变量。你可能会在一个真实的例子中做的一件事是在最后将线程私有totals减少到一个全局总数(然后只让一个线程打印结果)。一种方法是简单的

#pragma omp atomic
global_total += total

最后(虽然使用缩减有更好的方法)。

PS:omp for的循环计数器默认是私有的,因此您实际上不必明确指定它。