簡體   English   中英

為什么我的MPI程序無法正常打印

[英]Why is my MPI program not printing as it is supposed to be

我試圖讓一個進程處理所有的printtf操作,以便按我想要的順序進行打印。 我正在嘗試存儲進程1生成的數據,並讓進程0打印進程0和進程1都生成的數據,但我只知道進程0正在生成的數據。

這是代碼的相關部分

 char str3[33];
        char str4[66];
        MPI_Barrier(MPI_COMM_WORLD);
        while (count<5){


                MPI_Recv(&count,1,MPI_INT,(my_rank+1)%2,tag,MPI_COMM_WORLD,&status);
                char str[30];
                if(my_rank==0)
                        sprintf(str,"Process %d received the count\n",my_rank);
                if(my_rank==1)
                        sprintf(str3,"Process %d received the count\n",my_rank);
                count++;



                char str2[66];
                if (my_rank==0)
                        sprintf(str2,"Process %d incremented the count(%d) and sent it back to process %d\n",my_rank,count,(my_rank+1)%2);
                if (my_rank==1)
                        sprintf(str4,"Process %d incremented the count(%d) and sent it back to process %d\n",my_rank,count,(my_rank+1)%2);

                if(my_rank==0){

                        printf(str3);
                        printf (str4);

                        printf(str);
                        printf(str2);

                        memset(str3,'\0',sizeof(str3));
                        memset(str4,'\0',sizeof(str4));
                        memset(str,'\0',sizeof(str));
                        memset(str2,'\0',sizeof(str2));
        }

                 MPI_Send(&count,1,MPI_INT,(my_rank+1)%2,tag,MPI_COMM_WORLD);
        }

首先,請注意,正如@grancis所指出的,由於死鎖,您的代碼似乎有缺陷,因為在發送之前,MPI_recv中的0和1塊都被阻塞了。 可能是您在輸入數據之前輸入了問題中顯示的代碼片段,從而允許代碼繼續。

無論如何,問題在於緩沖區str3和str4已由進程1修改,因此當進程0嘗試打印它們時,顯然不能打印出與這些緩沖區最初包含的內容不同的內容(這是第一個緩沖區中未初始化的內存)迭代,由於您使用了內存集,因此在后續迭代中為零)。 請記住,在MPI進程中不要共享內存。

如果您不希望進程1打印信息,則進程1必須將其信息發送到進程0(通過MPI1中的普通MPI_Send / MPI_Recv或MPI2或MPI3中的單面通信),只有進程0才能打印該信息。

暫無
暫無

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

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