0

我正在尝试在超立方体拓扑上使用 mpi c 库计算 pi。但执行不会继续 MPI_Send 和 MPI_Recv 部分。

我正在使用4个处理器!

似乎没有一个处理器正在接收任何数据。

这是我得到的代码、输出和错误。

任何帮助,将不胜感激!谢谢!

代码:在每个处理器初始化和计算本地 mypi 之后。

  mypi = h * sum;
    printf("Processor %d has local pi = %f", myid, mypi);
    //Logic for send and receive!                                                                                                                                                   
    int k;
    for(k = 0; k < log10(numprocs) / log10(2.0); k++){
      printf("entering dimension %d \n", dimension);
      dimension = k;
      if(decimalRank[k] == 1 && k < e){
        //if it is a processor that need to send then                                                                                                                               
        int destination = 0;
        //find destination processor and send                                                                                                                                       
        destination = myid ^ (int)pow(2,dimension);
        printf("Processor %d sending to %d in dimension %d the value %f\n", myid, destination, dimension,  mypi);

        MPI_SEND(&mypi, 1, MPI_DOUBLE, destination, MPI_ANY_TAG, MPI_COMM_WORLD);
        printf("Processor %d done sending to %d in dimension %d the value %f\n", myid, destination, dimension, mypi);
      }
      else{
        //Else this processor is supposed to be receiving                                                                                                                           
        pi += mypi;
        printf("Processor %d ready to receive in dimension %d\n", myid, dimension);
        MPI_RECV(&mypi, 1, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD);
        printf("Processor %d received value %d in dimension %d\n", myid, pi, dimension);
        pi += mypi;
      }
    }

    done = 1;
  }

错误:

mpiexec: Warning: tasks 0-3 died with signal 11 (Segmentation fault).

输出:

bcast complete
Processor 0 has local pi = 0.785473
Processor 0 ready to receive in dimension 0
Processor 1 has local pi = 0.785423
Processor 1 sending to 0 in dimension 0 the value 0.785423
Processor 3 has local pi = 0.785323
Processor 3 sending to 2 in dimension 0 the value 0.785323
Processor 2 has local pi = 0.785373
Processor 2 ready to receive in dimension 0
4

2 回答 2

2

MPI_ANY_TAG不是发送操作中的有效标签值。它只能在接收操作中用作通配符标记值,以便接收消息,无论它们的标记值是什么。发件人必须指定一个有效的标签值——0在大多数情况下就足够了。

于 2014-03-19T09:15:30.903 回答
0

这个:

for(k = 0; k < log10(numprocs) / log10(2.0); k++) ...

和这个:

... pow(2,dimension);

不好:您必须只使用整数逻辑。确保在某个时候某些东西会被赋值为“2.999999”并四舍五入为“2”,从而破坏您的算法。

我会尝试类似的东西:

for(k = 0, k2 = 1; k2 < numprocs; k++, k2 <<= 1) ...
于 2014-03-19T11:04:39.677 回答