簡體   English   中英

C,MPI:程序不終止也不打印號碼

[英]C, MPI: Program not terminating and not printing numbers

#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的值以及接收到的字符和源進程。
但是,當我運行程序時,它不會終止。
輸出看起來像這樣:

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?

您正在使用主從架構進行並行處理,您的進程0是主進程,正在等待其他9個進程的輸入,但是在您的代碼中,只有具有偶數id的進程才會觸發輸出,即進程2、4 ,6、8。

您沒有為進程1、3、5、7和9設置行為,因此主服務器仍在等待它們,因此程序在等待並行進程完成:

您需要在此處完成源代碼

    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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM