gpt4 book ai didi

c - MPI-- 无限发送和接收

转载 作者:太空宇宙 更新时间:2023-11-04 02:17:43 26 4
gpt4 key购买 nike

如果我使用阻塞发送和接收(MPI_send(), MPI_recv()),如何使这两个操作不确定?就像他们一遍又一遍地重复?

示例代码:

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
MPI_Comm_rank (MPI_COMM_WORLD,&rank);
if(rank==0){
rc=MPI_Send(msg,1,MPI_CHAR,1,1,MPI_COMM_WORLD);
rc=MPI_Recv(msg,1,MPI_CHAR,1,1,MPI_COMM_WORLD,&status);
}else{
rc=MPI_Recv(msg,1,MPI_CHAR,0,0,MPI_COMM_WORLD,&status);
rc=MPI_Send(msg,1,MPI_CHAR,0,0,MPI_COMM_WORLD);

}

我尝试在 if(rank==0) -> while(1) 之前添加,它完成了工作,但我看到有几个发送,然后有几个接收,我想要它喜欢 - 发送(0),接收(1),发送(1),接收(0)。

最佳答案

您可以使用 MPI_Sendrecv 轻松编写一系列发送-接收操作:

int MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
int dest, int sendtag, void *recvbuf, int recvcount,
MPI_Datatype recvtype, int source, int recvtag,
MPI_Comm comm, MPI_Status *status)

如您所见,它只是 MPI_Send 和 MPI_Recv 的压缩版本,但当所有进程都需要发送 接收时,它会派上用场 某事。

以下代码适用于任何个进程(您可以根据需要调整它):

代码更新 #1(使用 MPI_Sendrecv)

#include <stdio.h>
#include <unistd.h>
#include <mpi.h>

int main (int argc, char *argv[])
{
int size, rank, value, next, prev, sendval, recval;
double t0, t;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

value = 5;

if (size > 1)
{
next = (rank + 1)% size;
prev = (size+rank - 1)% size;

sendval = value + rank;

for (;;)
{
t0 = MPI_Wtime();
MPI_Sendrecv(&sendval, 1, MPI_INT, next, 1, &recval, 1, MPI_INT, prev, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
t = MPI_Wtime();

fprintf(stdout, "[%d of %d]: Sended %d to process %d, Received %d from process %d (MPI_SendRecv Time: %f)\n",rank, size-1, sendval, next, recval, prev, (t - t0));
}

}

MPI_Finalize();
return 0;
}

代码更新 #2(使用单独的 MPI_Send/MPI_Recv)

#include <stdio.h>
#include <unistd.h>
#include <mpi.h>

int main (int argc, char *argv[])
{
int size, rank, value, next, prev, sendval, recval;
double s0, s, r, r0;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

value = 5;

if (size > 1)
{
next = (rank + 1)% size;
prev = (size+rank - 1)% size;

sendval = value + rank;

for (;;)
{
s0 = MPI_Wtime();
MPI_Send(&sendval, 1, MPI_INT, next, 1, MPI_COMM_WORLD);
s = MPI_Wtime();
fprintf(stdout, "[%d of %d]: Sended %d to process %d (MPI_Send Time: %f)\n", rank, size-1, sendval, next, s-s0);

r0 = MPI_Wtime();
MPI_Recv(&recval, 1, MPI_INT, prev, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
r = MPI_Wtime();
fprintf(stdout, "[%d of %d]: Received %d from process %d (MPI_Recv Time: %f)\n", rank, size-1, recval, prev, r-r0);

}

}
MPI_Finalize();
return 0;
}

运行示例

mpicc -o sendrecv sendrecv.c
mpirun -n 2 sendrecv

[0 of 1]: Sended 5 to process 1, Received 6 from process 1 (MPI_SendRecv Time: 0.000121)
[1 of 1]: Sended 6 to process 0, Received 5 from process 0 (MPI_SendRecv Time: 0.000068)
...

关于c - MPI-- 无限发送和接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5052094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com