C,MPI:程序不终止也不打印号码 [英] C, MPI: Program not terminating and not printing numbers

查看:128
本文介绍了C,MPI:程序不终止也不打印号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "mpi.h"
#include <stdio.h>
int main(int argc,char *argv[]){ 
    int numtasks, rank, rc, count, tag=1, i =0;
    MPI_Status Stat;
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    if (rank == 0)  //for process 0 we print received messages
    { 

        for(i=0; i< 9; i ++){   
            printf("value of i is: %d\n",i );
            rc = MPI_Recv(&inmsg, 1, MPI_CHAR, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &Stat);
            printf("Task %d: Received %d char(s) from task %d with tag %d \n", rank, count, Stat.MPI_SOURCE, Stat.MPI_TAG);
        }
    }
    else //for the other 9 processes
    { 
        if(rank % 2 == 0){  //if rank is an even number
            rc = MPI_Send(&outmsg, 1, MPI_CHAR, 0, tag, MPI_COMM_WORLD);    //send message to process with rank 0
        }
    }
    MPI_Finalize();
} 
//

该程序运行10个进程.等级为0的进程接收消息,如果源进程的编号为偶数,则将其打印出来.等级为0以外的进程向等级为0的进程发送包含字符'x'的消息
现在,关于等级0,它具有一个for循环,该循环基本上循环9次.在循环中,它打印出迭代变量i的值以及接收到的字符和源进程.
但是,当我运行程序时,它不会终止.
输出看起来像这样:

This program is run with 10 processes. Process with rank 0 receives messages and prints them out if the source process has an even numbered rank. Processes with rank other than 0 send process with rank 0 a message containing a character 'x'
Now, in regards to rank 0, it has a for loop that basically loops 9 times. In the loop it prints out the value of the iterating variable i, and the received character and source process.
However, when I run my program it does not terminate.
The output looks like this:

Task 0: Received 0 char(s) from task 2 with tag 1 
value of i is: 1
Task 0: Received 0 char(s) from task 6 with tag 1 
value of i is: 2
Task 0: Received 0 char(s) from task 4 with tag 1 
value of i is: 3
Task 0: Received 0 char(s) from task 8 with tag 1 
value of i is: 4

如何获取它来打印i的其他值,例如5,6,7,8,9?

How do I get it to print the other values of i such as 5,6,7,8,9?

推荐答案

您正在使用主从架构进行并行处理,您的进程0是主服务器,正在等待其他9个进程的输入,但是在您的具有偶数ID号的仅代码处理将触发输出,即处理2、4、6、8.

You're using a master-slave architecture for parallel processing, your process 0 is the master and is waiting for the input of 9 other process, but in your code only process with even id number will fire an output, namely process 2, 4, 6, 8.

您没有为进程1、3、5、7和9设置行为,因此主服务器仍在等待它们,因此程序在等待并行进程完成:

you didn't put behavior for process 1,3,5,7 and 9 so the master is still waiting for them hence, the program waiting for parallel process to finish:

您需要在此处完成源代码

you need to complete your source code here

    if(rank % 2 == 0){  //if rank is an even number
            rc = MPI_Send(&outmsg, 1, MPI_CHAR, 0, tag, MPI_COMM_WORLD);    //send message to process with rank 0
        }else{
           //logic for process 1,3,5,7,9
}

这篇关于C,MPI:程序不终止也不打印号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆