OpenMP和MPI混合动态调度

时间:2015-09-16 13:23:13

标签: multithreading mpi openmp

随着线程数量的增加,“temp”的计数减少了。 当我发送线程数为“1”时,它给出了正确答案,但随着线程数量的增加,运行时间缩短但给出了错误的答案

#include <stdio.h>
#include <mpi.h>
#include <complex.h>
#include <time.h>
#include <omp.h>

#define MAXITERS 1000

// globals
int count = 0;
int nptsside;
float side2;
float side4;
int temp = 0;

int inset(double complex c) {
   int iters;
   float rl,im;
   double complex z = c;
   for (iters = 0; iters < MAXITERS; iters++) { 
      z = z*z + c;
      rl = creal(z);
      im = cimag(z);
      if (rl*rl + im*im > 4) return 0;
   }
   return 1;
}

int main(int argc, char **argv)
{
   nptsside = atoi(argv[1]);
   side2 = nptsside / 2.0;
   side4 = nptsside / 4.0;

   //struct timespec bgn,nd;
   //clock_gettime(CLOCK_REALTIME, &bgn);

   int x,y; float xv,yv;
  double complex z;
  int i;
  int mystart, myend;
  int nrows;
  int nprocs, mype;
  int data;


  MPI_Status status;
  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
  MPI_Comm_rank(MPI_COMM_WORLD, &mype);
  nrows = nptsside/nprocs;
  printf("%d\n", nprocs);

  mystart = mype*nrows;
  myend = mystart + nrows - 1;


  #pragma omp parallel shared(mystart, myend, temp)
  {
  int nth = omp_get_num_threads();
  printf("%d\n", nth);
  #ifdef STATIC
  #pragma omp for reduction(+:temp) schedule(static)
  #elif defined DYNAMIC
  #pragma omp for reduction(+:temp) schedule(dynamic)
  #elif defined GUIDED
  #pragma omp for reduction(+:temp) schedule(guided)
  #endif
  for (x=mystart; x<=myend; x++) {  

     for ( y=0; y<nptsside; y++)  {
        xv = (x - side2) / side4;
        yv = (y - side2) / side4;
        z = xv + yv*I;
        if (inset(z)) {
           temp++;
        }
     }
  }
  }


  if(mype==0) {
     count += temp;
     printf("%d\n", temp);

     for (i = 1; i < nprocs; i++) {
        MPI_Recv(&temp, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &status);
        count += temp;
        printf("%d\n", temp);
        }
        }
        else{
        MPI_Send(&temp, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
        }



  MPI_Finalize();

  if(mype==0) {
  printf("%d\n", count);
  }

   //clock_gettime(CLOCK_REALTIME, &nd);
   //printf("%f\n",timediff(bgn,nd));
}

1 个答案:

答案 0 :(得分:0)

当您进入OpenMP循环时,您没有定义任何私有变量。

首先,您必须始终声明OpenMP循环的循环计数器(以及OpenMP循环内嵌套循环的任何循环计数器)私有。

其次,您有三个变量(xvyvz),每个变量都依赖于这些循环中的迭代。因此,每个线程也需要拥有自己的这些变量的私有副本。将并行语句更改为

#pragma omp parallel shared(mystart, myend, temp) private(x, y, xv, yv, z)

应修复您的OpenMP问题。

看到你说将线程数设置为1会产生正确的答案,我没有查看你的MPI代码。

编辑:好的,我说谎,我现在简要地查看了你的MPI代码。而不是所有的发送和接收,你应该写一个单一的减少。这个集体将比您当前设置的阻止通信快得多。

MPI_Reduce(&temp, &count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);