MPI - 广播到所有进程以打印某些内容
我在 main 中声明了 int 值,并且所有进程都已初始化该值。它们都是存储值,我想在计算完成后将其写在屏幕上。广播是一个解决方案吗?例如如何实施?
int i;
int value;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
MPI_Comm_rank(MPI_COMM_WORLD;&myrank);
left = (myrank - 1); if (left < 0) left = numtasks-1;
right = (myrank + 1); if (right >= numtasks) right = 0;
if(myrank==0){
value=value+myrank;
MPI_Send(value,MPI_INT,right,99,MPI_COMM_WORLD);
MPI_Recv(value,MPI_INT,left,99,MPI_COMM_WORLD,&status);
}
else if(myrank==(numtasks-1)){
MPI_Recv(value,MPI_INT,left,99,MPI_COMM_WORLD,&status);
value=value+myrank;
MPI_Send(value,MPI_INT,right,99,MPI_COMM_WORLD);
}
else{
MPI_Recv(value,MPI_INT,left,99,MPI_COMM_WORLD,&status);
value=value+myrank;
MPI_Send(value,MPI_INT,right,99,MPI_COMM_WORLD);
}
这些应该形成逻辑循环。我做了一次计算(所有等级的总和),在过程 0 中我得到结果。这个结果(对于 4 个进程来说是 6)我想在计算后由每个进程打印出来。但我不知道如何准确使用屏障以及在哪里使用。
还有一件事,在所有 N-1 次发送之后(其中 N 是进程数),我应该得到每个进程中所有等级的总和。在我的代码中,我只将这个总和放入进程 0...这可能是一个不好的方法:-(
I have declared int value in my main, and all of the processes has inicialized this value. All of them are storing value, which I want to write on the screen after computing is finished. Is Broadcast a solution? E.g. how to implement it?
int i;
int value;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
MPI_Comm_rank(MPI_COMM_WORLD;&myrank);
left = (myrank - 1); if (left < 0) left = numtasks-1;
right = (myrank + 1); if (right >= numtasks) right = 0;
if(myrank==0){
value=value+myrank;
MPI_Send(value,MPI_INT,right,99,MPI_COMM_WORLD);
MPI_Recv(value,MPI_INT,left,99,MPI_COMM_WORLD,&status);
}
else if(myrank==(numtasks-1)){
MPI_Recv(value,MPI_INT,left,99,MPI_COMM_WORLD,&status);
value=value+myrank;
MPI_Send(value,MPI_INT,right,99,MPI_COMM_WORLD);
}
else{
MPI_Recv(value,MPI_INT,left,99,MPI_COMM_WORLD,&status);
value=value+myrank;
MPI_Send(value,MPI_INT,right,99,MPI_COMM_WORLD);
}
Theese should make logical circle. I do one computing (sum of all ranks), and in process 0 I get result. This result (for 4 processes it will be 6) I want to be printed by each of the processes after this computing. But I don't see how to use barrier exactly and where.
There is also one thing, after all N-1 sendings (where N is number of processes) I should have sum of all ranks in each of processes. In my code I get this sum only into process 0... It might be a bad approach :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有关代码结构的更多详细信息会有所帮助,但听起来您可以使用 MPI_Barrier。您的进程不需要交换任何数据,它们只需要等待,直到每个人都到达代码中您希望进行打印的位置,这正是 Barrier 所做的。
编辑:在您发布的代码中,屏障将位于最后(在
if
语句之后),后面是printf(value)
。但是,您的代码不会计算所有节点中所有排名的总和,因为进程
i
仅接收前i-1
进程的排名总和。如果你希望每个进程最后都有总和,那么用Broadcast代替Barrier确实是最好的选择。 (事实上,整个if
语句和广播可以由单个MPI_Reduce()
调用替换,但这并不能真正帮助您学习 MPI。:))Some more detail about the structure of your code would help, but it sounds like you can just use MPI_Barrier. Your processes don't need to exchange any data, they just have to wait until everyone reached the point in your code where you want the printing to happen, which is exactly what Barrier does.
EDIT: In the code you posted, the Barrier would go at the very end (after the
if
statement), followed byprintf(value)
.However, your code will not compute the total sum of all ranks in all nodes, since process
i
only receives the summed ranks of the firsti-1
processes. If you want each process to have the total sum at the end, then replacing Barrier with Broadcast is indeed the best option. (In fact, the entireif
statement AND the Broadcast could be replaced by a singleMPI_Reduce()
call, but that wouldn't really help you learn MPI. :) )