MPI_Send或MPI_Recv正在给出分段错误

时间:2014-03-19 08:29:50

标签: c mpi hypercube

我正在尝试使用超立方体拓扑上的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

2 个答案:

答案 0 :(得分:1)

发送操作中

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

答案 1 :(得分:0)

此:

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

和此:

... pow(2,dimension);

bad :您必须仅使用整数逻辑。确保在某个时候会发生某些事情的重要性,因为&#34; 2.999999&#34;并四舍五入到#34; 2&#34;,打破你的算法。

我尝试过类似的事情:

for(k = 0, k2 = 1; k2 < numprocs; k++, k2 <<= 1) ...
相关问题