說,我有8個進程。當我做以下事情時,MPU_COMM_WORLD通信器將被分解爲兩個通信器。帶有偶數ID的進程將屬於一個通信者,而具有奇數ID的進程將屬於另一個通信者。在MPI_Comm_split之後如何分發句柄?
color=myid % 2;
MPI_Comm_split(MPI_COMM_WORLD,color,myid,&NEW_COMM);
MPI_Comm_rank(NEW_COMM, &new_id);
我的問題是這兩個傳播者的句柄在哪裏。分割之後,處理器的id將會變爲0 2 4 6 | 1 3 5 7.
現在,我的問題是:假設我想在一個特定的通信器中發送和接收,比如說那個託管偶數ids的那個,然後當我使用錯誤的通信器從0發送到2的消息時消息可能會在第二個通信器中結束,這是正確的嗎?提前謝謝澄清!
if(new_id < 2){
MPI_Send(&my_num, 1, MPI_INT, 2 + new_id, 0, NEW_COMM);
MPI_Recv(&my_received, 1, MPI_INT, 2 + new_id, 0, NEW_COMM, MPI_STATUS_IGNORE);
}
else
{
MPI_Recv(&my_received, 1, MPI_INT, new_id - 2, 0, NEW_COMM, MPI_STATUS_IGNORE);
MPI_Send(&my_num, 1, MPI_INT, new_id - 2 , 0, NEW_COMM);
}
的完整代碼
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
int main(argc,argv)
int argc;
char *argv[];
{
int myid, numprocs;
int color,Zero_one,new_id,new_nodes;
MPI_Comm NEW_COMM;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
int my_num, my_received;
int old_id;
switch(myid){
case 0:
my_num = 0;
old_id = 0;
break;
case 1:
my_num = 1;
old_id = 1;
break;
case 2:
my_num = 2;
old_id = 2;
break;
case 3:
my_num = 3;
old_id = 3;
break;
case 4:
my_num = 4;
old_id = 4;
break;
case 5:
my_num = 5;
old_id = 5;
break;
case 6:
my_num = 6;
old_id = 6;
break;
case 7:
my_num = 7;
old_id = 7;
break;
}
color=myid % 2;
MPI_Comm_split(MPI_COMM_WORLD,color,myid,&NEW_COMM);
MPI_Comm_rank(NEW_COMM, &new_id);
MPI_Comm_rank(NEW_COMM, &new_nodes);
// 0 1 2 3 4 5 6 7 //After splits we have these nums for 8 processors
// 2 3 0 1 6 7 4 5 //After the below exchange we should have this...each two elements in each communicator will exchange to next two elements in that same communicator
if(new_id < 2){
MPI_Send(&my_num, 1, MPI_INT, 2 + new_id, 0, NEW_COMM);
MPI_Recv(&my_received, 1, MPI_INT, 2 + new_id, 0, NEW_COMM, MPI_STATUS_IGNORE);
}
else
{
MPI_Recv(&my_received, 1, MPI_INT, new_id - 2, 0, NEW_COMM, MPI_STATUS_IGNORE);
MPI_Send(&my_num, 1, MPI_INT, new_id - 2 , 0, NEW_COMM);
}
printf("old_id= %d received num= %d\n", old_id, my_received);
MPI_Finalize();
}
謝謝!當我打印舊ID和新ID時,其打印old_id = 0新ID = 0 old_id = 1新ID = 0 old_id = 2新ID = 1 old_id = 3新ID = 1 old_id = 4新ID = 2 old_id = 5 new id = 2 old_id = 6 new id = 3 old_id = 7 new id = 3 而不是您編輯的那個。所以,這裏有兩個進程具有相同的id,例如id 0。在這種情況下,我如何區分進程,當我做mpi_send時,我需要接收者的id。在這種情況下,mpi_comm_split中的關鍵參數是否必須對區分進程做任何事情? – user3256520
一如既往,您可以根據自己想要使用的溝通器中的排名區分流程。關鍵參數允許您向MPI表明您不關心排序重新排序,允許MPI自由選擇新的排序(通過在關鍵參數的所有進程中傳遞相同的值完成),或者明確地通過一個不同的排序根據您自己的排名順序爲關鍵參數賦值。因此,如果你正確調用MPI_Comm_split,你會得到兩個新的通信器,但有兩種可能性。 –
1)第一次通過等級0,2,4,6第二通信等級1,3,5,7和2)第一通信等級0,1,2,3第二通信等級0,1,2,3 。在第二種情況下,您必須簡單地在發送/接收數據時採取相應措施。例如,在第一種情況下,排名爲0和2的進程之間的交換成爲第二種情況,而在排名爲0和1的進程之間進行交換。爲了確保您使用的是正確的過程,只需檢查如下:if(!old_id &&!new_id)for proc在舊的通訊服務中的排名爲0 –