<分区>
我写了一些这样的代码:
void main(int argc, char **argv ) {
char message[20];
int i, rank, size, type=99;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0) {
strcpy_s(message, "Hello, world");
for (i=1; i<size; i++)
MPI_Send(message, 13, MPI_CHAR, i, type, MPI_COMM_WORLD);
} else {
MPI_Recv(message, 20, MPI_CHAR, 0, type, MPI_COMM_WORLD, &status);
}
printf( "Message from process =%d : %.13s\n", rank, message);
MPI_Finalize();
}
我的输出是随机排序的。例如:
Message from process = 4 : Hello, world
Message from process = 2 : Hello, world
Message from process = 0 : Hello, world
Message from process = 1 : Hello, world
Message from process = 3 : Hello, world
但是我想这样做:
Message from process = 0 : Hello, world
Message from process = 1 : Hello, world
Message from process = 2 : Hello, world
Message from process = 3 : Hello, world
Message from process = 4 : Hello, world
我尝试了一些代码,但仍然处理随机顺序。
我更改我的代码。现在,每个进程都等待来自前任的消息。但仍然处理随机排序的输出。我写了 MPI_Wait,但它不起作用。
if(rank == 0) {
strcpy_s(message, "Hello, world");
printf( "Message from process =%d : %.13s\n", rank, message);
MPI_Isend(message, 13, MPI_CHAR, rank + 1, type, MPI_COMM_WORLD, &request);
MPI_Wait(&request, &status);
} else {
MPI_Recv(message, 20, MPI_CHAR, (rank - 1) % size, type, MPI_COMM_WORLD, &status);
printf( "Message from process =%d : %.13s\n", rank, message);
if(rank < (size - 1)){
MPI_Isend(message, 13, MPI_CHAR, (rank + 1) % size, type, MPI_COMM_WORLD, &request);
MPI_Wait(&request, &status);
}
}
我希望有人能帮助我。谢谢。