2

私は 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 を使用しています。

それを修正する方法はありますか?または、その状況を適切にデバッグする方法はありますか?

よろしくお願いします!

4

1 に答える 1

1

どこに問題があるのか​​ がすでにわかっているかどうかはわかりませんが、無限実行は次のループで発生します。

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

cこれを確認するには、約 300 以上に設定してからprintf、この while ループで呼び出します。ロジックのエラーを完全に特定したわけではありませんが、奇妙なと感じるコードの場所に以下の 3 つのコメントをマークしました。

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
    }

お役に立てれば。

于 2013-05-26T15:00:24.540 に答える