【问题标题】:MPI hangs during executionMPI 在执行期间挂起
【发布时间】:2013-05-26 23:00:24
【问题描述】:

我正在尝试使用 MPI 编写一个简单的程序,该程序可以找到小于 514 的所有数字,这些数字等于它们的数字之和的指数(例如,512 = (5+1+2)^3。我遇到的问题是主循环 - 它在几次迭代(c = 10)上工作得很好,但是当我尝试增加迭代次数(c = x)时,mpiexec.exe 只是挂起 - 似乎在中间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,如果这对你们有用的话。

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

提前非常感谢!

【问题讨论】:

    标签: debugging mpi freeze


    【解决方案1】:

    不确定您是否已经找到问题所在,但您的无限运行发生在此循环中:

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

    您可以通过将c 设置为大约300 或更高来确认这一点,然后在此while 循环中调用printf。我还没有完全指出你的逻辑错误,但是我在你觉得奇怪的代码位置标记了下面三个 cmets:

    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
        }
    

    希望这会有所帮助。

    【讨论】:

    • 实际上,是的,这就是问题所在。该程序试图找到一个数字的数字之和(例如,“1000”),计算 sum2=1+0+0+0 = 1,然后陷入 1=1* 的无限循环1.非常感谢您指出这一点 =) 你就像我的英雄。
    猜你喜欢
    • 2015-12-30
    • 1970-01-01
    • 2016-01-23
    • 2015-05-18
    • 2014-01-14
    • 1970-01-01
    • 2015-12-01
    • 2019-01-03
    • 1970-01-01
    相关资源
    最近更新 更多