为什么我的MPI程序错误输出

为什么我的MPI程序错误输出

本文介绍了为什么我的MPI程序错误输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MPI一个新手,我有一门功课,我不要求你去解决它,我只需要在为什么我的计划是故障的提示。

I am a newbie in MPI and I have a homework, I am not asking for you to solve it, I only need a hint on why my program is malfunctioning.

这是问题所在。

写MPI C程序模拟了一个乒乓球桌的游戏。 2个进程,才应使用。
工艺使用MPI_SEND和MPI_RECV持续反弹的消息了对方,一个特定的
次数。该消息由每个递增的整数计数变量的形成
在发送之前的过程。计数变量被初始化为零开始游戏之前

Write a MPI C program that simulates a ping pong table game. 2 processes should only be used.Processes use MPI_Send and MPI_Recv to continually bounce messages off to each other, a specificnumber of times. The message is formed of an integer count variable that is incremented by eachprocess before being sent. The count variable is initialized to zero prior to starting the game.

输出

称为一个文本文件ping_pong_output.txt形成为下面的例子:如果计数= 5

A text file called ping_pong_output.txt formed as the following example: if count = 5

进程0开始游戏并初始化计数进程0
  递增计数(1),把它处理1

进程接收到的计数处理0递增计数(2)及
  把它送回处理0

Process received the count Process 0 incremented the count ( 2 ) & sent it back to process 0

进程接收到的计数处理0递增计数(3)及
  把它送回处理1

Process received the count Process 0 incremented the count ( 3 ) & sent it back to process 1

进程接收到的计数处理0递增计数(4)及
  把它送回处理0

Process received the count Process 0 incremented the count ( 4 ) & sent it back to process 0

进程接收到的计数处理0递增计数(5)及
  把它送回处理1

Process received the count Process 0 incremented the count ( 5 ) & sent it back to process 1

请注意以下内容:


  • 的过程中轮流作为发送者和接收者(需要用%)

  • 程序应该只允许2个进程玩游戏

在code我写了

#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>
#include <time.h>

int main(int argc, char * argv[]){

        int size=0,my_rank=0,tag=1,count;
        MPI_Status status;
        MPI_Init(&argc,&argv);
        MPI_Comm_size(MPI_COMM_WORLD,&size);
        MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
        MPI_Barrier(MPI_COMM_WORLD);

        if (my_rank==0){
                count=0;
                count++;
         printf("Process 0 started the game and initialized the count\nProcess 0 incremented the count and sent it to process 1\n");

         MPI_Send(&count,1,MPI_INT,1,tag,MPI_COMM_WORLD);


                }
        MPI_Barrier(MPI_COMM_WORLD);
        while (count<=5){
                MPI_Barrier(MPI_COMM_WORLD);

                MPI_Recv(&count,1,MPI_INT,(my_rank+1)%2,tag,MPI_COMM_WORLD,&status);
                printf("Process %d received the count\n",my_rank);

                count++;
                MPI_Send(&count,1,MPI_INT,(my_rank+1)%2,tag,MPI_COMM_WORLD);
                printf("process %d incremented the count (%d) & sent it back to process %d\n",my_rank,count,(my_rank+1)%2);
        }

                MPI_Finalize();
                return 0;
}

我得到的输出是:

The output I got is :

1处理收到的计数

进程递增1计数(2)及把它送回处理0

process 1 incremented the count (2) & sent it back to process 0

1处理收到的计数

进程递增1计数(4)及把它送回处理0

process 1 incremented the count (4) & sent it back to process 0

1处理收到的计数

进程递增1计数(6)及把它送回处理0

process 1 incremented the count (6) & sent it back to process 0

进程0开始游戏并初始化计数

Process 0 started the game and initialized the count

进程0递增计数,把它处理1

Process 0 incremented the count and sent it to process 1

进程0收到的计数

进程0递增计数(3)及把它送回处理1

process 0 incremented the count (3) & sent it back to process 1

进程0收到的计数

进程0递增计数(5)及把它送回处理1

process 0 incremented the count (5) & sent it back to process 1

进程0收到的计数

进程0递增计数(7)及把它送回处理1

process 0 incremented the count (7) & sent it back to process 1

我不明白,为什么进程1先运行,循环甚至if语句之前运行。所有这些MPI_Barrier可能是无用的,但他们出来的绝望。
任何帮助是AP preciated。

I don't understand why process 1 is running first, the loop is even running before the if statement. All those MPI_Barrier are probably useless but they came out of desperation.Any help is appreciated.

推荐答案

对于任何人谁可能会寻找答案,我设法找到了,你应该让一个进程处理所有的打印。

For anyone who might be looking for the answer, i managed to find out that you should let one process handle all the printing.

这篇关于为什么我的MPI程序错误输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:43