输出无序,使用 MPI 进行并行编程
Output isn't ordered, Parallel Programing with MPI
我写了一些这样的代码:
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);
}
}
希望有人能帮助我。谢谢
默认情况下,printf
语句将由首先碰巧 运行 的处理器进行评估,而不是按排名顺序。如果你绝对必须按排名顺序进行,那么你可以尝试这样的事情:
for(i = 0; i < size; i++) {
if(i == rank) {
printf("Message from process =%d : %.13s\n", rank, message);
}
MPI_Barrier(MPI_COMM_WORLD);
}
这遍历每个等级,只允许打印当前等级,而其他人都点击 MPI_Barrier
并被迫等待。不过请记住,这不是一种可扩展的方法。
我非常喜欢 MPI_Barrier 循环方法!另一种方法是传递令牌:
source = myrank -1;
dest = myrank + 1
if (source < 0) source = MPI_PROC_NULL;
if (dest >= nprocs) dest = MPI_PROC_NULL;
MPI_Recv(NULL, 0, MPI_BYTE, source, 0, comm, MPI_STATUS_IGNORE);
printf("Message from process =%d : %.13s\n", rank, message);
MPI_Send(NULL, 0, MPI_BYTE, dest, 0, comm);
另一种方法(这应该是一个单独的答案吗?)是在行前加上排名,然后排序。在 MPICH 中,这将是 mpiexec -np 4 -prepend-rank ...
我写了一些这样的代码:
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);
}
}
希望有人能帮助我。谢谢
默认情况下,printf
语句将由首先碰巧 运行 的处理器进行评估,而不是按排名顺序。如果你绝对必须按排名顺序进行,那么你可以尝试这样的事情:
for(i = 0; i < size; i++) {
if(i == rank) {
printf("Message from process =%d : %.13s\n", rank, message);
}
MPI_Barrier(MPI_COMM_WORLD);
}
这遍历每个等级,只允许打印当前等级,而其他人都点击 MPI_Barrier
并被迫等待。不过请记住,这不是一种可扩展的方法。
我非常喜欢 MPI_Barrier 循环方法!另一种方法是传递令牌:
source = myrank -1;
dest = myrank + 1
if (source < 0) source = MPI_PROC_NULL;
if (dest >= nprocs) dest = MPI_PROC_NULL;
MPI_Recv(NULL, 0, MPI_BYTE, source, 0, comm, MPI_STATUS_IGNORE);
printf("Message from process =%d : %.13s\n", rank, message);
MPI_Send(NULL, 0, MPI_BYTE, dest, 0, comm);
另一种方法(这应该是一个单独的答案吗?)是在行前加上排名,然后排序。在 MPICH 中,这将是 mpiexec -np 4 -prepend-rank ...