MPI 向工作人员发送信息时出现问题

发布于 2024-10-15 18:31:45 字数 2880 浏览 2 评论 0原文

在 MPI 程序中,我想将信息传输给工作人员,但遇到一个问题:

    Number of worker tasks = 4
   sending 1-th element q=0.011000 to task 1
   received 1-th element q=0.000000 in task 108827872
   sending 2-th element q=0.012000 to task 2
   sending 3-th element q=0.013000 to task 3
   received 2-th element q=0.000000 in task 1353735488
   sending 4-th element q=0.014000 to task 4
   received 3-th element q=0.000000 in task -1900924208
   received 4-th element q=0.000000 in task -1215863168

我很可能没有进行正确的消息传递,你知道错误可能出在哪里吗?

#include "mpi.h"               /* required MPI library */
#include <stdio.h>
#include <math.h>

#define NRRR 16               /* number of rows in matrix A */
#define NLLL 16                 /* number of columns in matrix A */
#define MASTER 0               /* taskid of first task */
#define FROM_MASTER 1          /* setting a message type */
#define FROM_WORKER 2          /* setting a message type */

int main(argc,argv)
int argc;
char *argv[];
{
    int numtasks,              /* number of tasks in partition */
    taskid,                /* a task identifier */
    numworkers,            /* number of worker tasks */
    source,                /* task id of message source */
    dest,                  /* task id of message destination */
    mtype,
    i,j,
    rc;                 /* message type */
    double  qr[NRRR],
    ql[NLLL],
    element_r[NRRR][3],
    element_l[NLLL][3];           


    MPI_Status status;  
    rc = MPI_Init(&argc,&argv);
    rc|= MPI_Comm_size(MPI_COMM_WORLD,&numtasks);   
    rc|= MPI_Comm_rank(MPI_COMM_WORLD,&taskid); 

    if (rc != 0)
        printf ("error initializing MPI and obtaining task ID info\n");

    numworkers = numtasks-1;

    // MASTER
    if (taskid == MASTER)
    {
        printf("\n\n\n\nNumber of worker tasks = %d\n",numworkers);

        // init element_r and element_l
        for(j=0;j<NRRR;j++){
            element_r[j][0]=j;
            element_r[j][1]=j+1;
            element_r[j][2]=j+2;
            qr[j] = j*1e-4+1e-3;
        }

        for(i=0;i<NLLL;i++){
            element_l[i][0]=12000+i;
            element_l[i][1]=12000+i+1;
            element_l[i][2]=12000+i+2;
            ql[i] = i*1e-3 +1e-2 ;
        }

        mtype = FROM_MASTER;
        for (dest=1; dest<=numworkers; dest++)
        {
            printf("   sending %d-th element q=%f to task %d\n",dest,ql[dest],dest);
            MPI_Send(&ql[dest], 1, MPI_DOUBLE, dest, mtype, MPI_COMM_WORLD);                        
        }

    }

    // WORKER
    if (taskid > MASTER)
    {
        mtype = FROM_MASTER;        
        MPI_Recv(&ql, 1, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status);
        printf("   received %d-th element q=%f in task %d\n",taskid,ql,taskid);     

    }

    MPI_Finalize();
}

In a MPI program I want to transfer info to the workers but I get a problem:

    Number of worker tasks = 4
   sending 1-th element q=0.011000 to task 1
   received 1-th element q=0.000000 in task 108827872
   sending 2-th element q=0.012000 to task 2
   sending 3-th element q=0.013000 to task 3
   received 2-th element q=0.000000 in task 1353735488
   sending 4-th element q=0.014000 to task 4
   received 3-th element q=0.000000 in task -1900924208
   received 4-th element q=0.000000 in task -1215863168

It is very likely I am not doing the right message passing, do you know where the error could be?

#include "mpi.h"               /* required MPI library */
#include <stdio.h>
#include <math.h>

#define NRRR 16               /* number of rows in matrix A */
#define NLLL 16                 /* number of columns in matrix A */
#define MASTER 0               /* taskid of first task */
#define FROM_MASTER 1          /* setting a message type */
#define FROM_WORKER 2          /* setting a message type */

int main(argc,argv)
int argc;
char *argv[];
{
    int numtasks,              /* number of tasks in partition */
    taskid,                /* a task identifier */
    numworkers,            /* number of worker tasks */
    source,                /* task id of message source */
    dest,                  /* task id of message destination */
    mtype,
    i,j,
    rc;                 /* message type */
    double  qr[NRRR],
    ql[NLLL],
    element_r[NRRR][3],
    element_l[NLLL][3];           


    MPI_Status status;  
    rc = MPI_Init(&argc,&argv);
    rc|= MPI_Comm_size(MPI_COMM_WORLD,&numtasks);   
    rc|= MPI_Comm_rank(MPI_COMM_WORLD,&taskid); 

    if (rc != 0)
        printf ("error initializing MPI and obtaining task ID info\n");

    numworkers = numtasks-1;

    // MASTER
    if (taskid == MASTER)
    {
        printf("\n\n\n\nNumber of worker tasks = %d\n",numworkers);

        // init element_r and element_l
        for(j=0;j<NRRR;j++){
            element_r[j][0]=j;
            element_r[j][1]=j+1;
            element_r[j][2]=j+2;
            qr[j] = j*1e-4+1e-3;
        }

        for(i=0;i<NLLL;i++){
            element_l[i][0]=12000+i;
            element_l[i][1]=12000+i+1;
            element_l[i][2]=12000+i+2;
            ql[i] = i*1e-3 +1e-2 ;
        }

        mtype = FROM_MASTER;
        for (dest=1; dest<=numworkers; dest++)
        {
            printf("   sending %d-th element q=%f to task %d\n",dest,ql[dest],dest);
            MPI_Send(&ql[dest], 1, MPI_DOUBLE, dest, mtype, MPI_COMM_WORLD);                        
        }

    }

    // WORKER
    if (taskid > MASTER)
    {
        mtype = FROM_MASTER;        
        MPI_Recv(&ql, 1, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status);
        printf("   received %d-th element q=%f in task %d\n",taskid,ql,taskid);     

    }

    MPI_Finalize();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下壹個目標 2024-10-22 18:31:45

看看你的发送和接收,我没有发现任何问题。 Master 正在向每个工作器 N 发送 ql[N],该消息被接收并存储在 ql[0] 中。

看来您只是没有正确打印出来。

当我将工作循环替换为以下内容时,我得到了合理的输出:

// WORKER
if (taskid != MASTER)
{
    mtype = FROM_MASTER;
    MPI_Recv(&ql, 1, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status);
    printf("   received %d-th element q=%f in task %d\n", taskid, ql[0], taskid);

}

示例运行:

$ mpiexec.exe -np 5 ./a.exe

Number of worker tasks = 4
   sending 1-th element q=0.011000 to task 1
   sending 2-th element q=0.012000 to task 2
   received 1-th element q=0.011000 in task 1
   sending 3-th element q=0.013000 to task 3
   received 2-th element q=0.012000 in task 2
   sending 4-th element q=0.014000 to task 4
   received 3-th element q=0.013000 in task 3
   received 4-th element q=0.014000 in task 4

顺便说一句,您当前正在做的事情是 MPI_Scatter 的设计目的。 示例

Looking at your sends and receives, I don't see anything wrong. Master is sending ql[N], to each worker N, and this is received and stored in ql[0].

It looks like you're just not printing it out correctly.

I get a sensible output when I replace the worker loop with:

// WORKER
if (taskid != MASTER)
{
    mtype = FROM_MASTER;
    MPI_Recv(&ql, 1, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status);
    printf("   received %d-th element q=%f in task %d\n", taskid, ql[0], taskid);

}

Example run:

$ mpiexec.exe -np 5 ./a.exe

Number of worker tasks = 4
   sending 1-th element q=0.011000 to task 1
   sending 2-th element q=0.012000 to task 2
   received 1-th element q=0.011000 in task 1
   sending 3-th element q=0.013000 to task 3
   received 2-th element q=0.012000 in task 2
   sending 4-th element q=0.014000 to task 4
   received 3-th element q=0.013000 in task 3
   received 4-th element q=0.014000 in task 4

As an aside, what you're currently doing is what MPI_Scatter was designed for. Examples here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文