2013-05-26 120 views
2

我想用MPI编写一个简单的程序,它找到所有小于514的数字,它们等于它们数字之和的指数(例如,512 =(5 + 1 + 2 )^ 3。我遇到的问题是主循环 - 它在几次迭代(c = 10)时工作得很好,但是当我尝试增加迭代次数(c = x)时,mpiexec.exe只是挂起 - 。看似MPI在执行期间挂起

的printf例行中旬,我敢肯定,死锁是罪魁祸首,但我找不到任何

的源代码:

#include <stdlib.h> 
#include <stdio.h> 
#include <iostream> 
#include "mpi.h" 

int main(int argc, char* argv[]) 
{ 
    //our number 
    int x=514; 
    //amount of iterations 
    int c = 10; 
    //tags for message identification 
    int tag = 42; 
    int tagnumber = 43; 
    int np, me, y1, y2; 
    MPI_Status status; 

    /* Initialize MPI */ 
    MPI_Init(&argc, &argv); 
    MPI_Comm_size(MPI_COMM_WORLD, &np); 
    MPI_Comm_rank(MPI_COMM_WORLD, &me); 
    /* Check that we run on more than two processors */ 
    if (np < 2) 
    { 
     printf("You have to use at least 2 processes to run this program\n"); 
     MPI_Finalize(); 
     exit(0); 
    } 
    //begin iterations 
    while(c>0) 
    { 
     //if main thread, then send messages to all created threads 
     if (me == 0) 
     { 
      printf("Amount of threads: %d\n", np); 
      int b = 1; 
      while(b<np) 
      { 
       int q = x-b; 
       //sends a number to a secondary thread 
       MPI_Send(&q, 1, MPI_INT, b, tagnumber, MPI_COMM_WORLD); 
       printf("Process %d sending to process %d, value: %d\n", me, b, q); 
       //get a number from secondary thread 
       MPI_Recv(&y2, 1, MPI_INT, b, tag, MPI_COMM_WORLD, &status); 
       printf ("Process %d received value %d\n", me, y2); 
       //compare it with the sent one 
       if (q==y2) 
       { 
        //if they're equal, then print the result 
        printf("\nValue found: %d\n", q); 
       } 
       b++; 
      } 
      x = x-b+1; 
      b = 1; 
     } 
     else 
     { 
      //if not a main thread, then process the message sent and send the result back. 
      MPI_Recv (&y1, 1, MPI_INT, 0, tagnumber, MPI_COMM_WORLD, &status); 
      int sum = 0; 
      int y2 = y1; 
      while (y1!=0) 
      { 
       //find the number's sum of digits 
       sum += y1%10; 
       y1 /= 10; 
      } 
      int sum2 = sum; 
      while(sum2<y2) 
      { 
       //calculate the exponentiation 
       sum2 = sum2*sum; 
      } 
      MPI_Send (&sum2, 1, MPI_INT, 0, tag, MPI_COMM_WORLD); 
     } 
     c--; 
    } 
    MPI_Finalize(); 
    exit(0); 
} 

然后我运行编译的exe文件为“mpiexec.exe -n 4 lab2.exe”。我使用HPC Pack 2008 SDK,如果这对您有任何用处。

有什么方法可以解决它吗?或者,也许某种方式来正确调试这种情况?

非常感谢!

回答

1

不知道,如果你已经发现了其中的问题,但你的无限运行在这个循环中发生的:

while(sum2<y2) 
{ 
    //calculate the exponentiation 
    sum2 = sum2*sum; 
} 

可以证实这一点通过设置c约300或以上,然后进行printf呼叫在此同时,循环。我还没有完全查明你的逻辑错误,但我打上你的代码的位置下面三点意见,我觉得很奇怪:

while(c>0) 
{ 
    if (me == 0) 
    { 
     ... 
     while(b<np) 
     { 
      int q = x-b; //<-- you subtract b from x here 
      ... 
      b++; 
     } 
     x = x-b+1; //<-- you subtract b again. sure this is what you want? 
     b = 1; //<-- this is useless 
    } 

希望这有助于。

+0

其实,是的,这是问题所在。程序试图找到一个数字的总和(比如说,“1000”),计算sum2 = 1 + 0 + 0 + 0 = 1,然后卡在1 = 1 * 1。非常感谢你指出这一点=)你是我的英雄。 – user2421911