1,小ring 程序
数据从rank 0开始向外传递,rank1收到后再传递给 rank2,以此类推
除了rank0,程序一开始时,每个rank先进入接收等待状态,像是多米诺骨牌,都立起来。
rank 0开始倒下,依次推倒 rank1,rank2,。。。。
源码:
#include <stdio.h>
#include <mpi.h>
int main(){
	// Initialize the MPI environment
	MPI_Init(NULL, NULL);
	// Get the rank and size in the original communicator
	int world_rank, world_size;
	
	MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
	MPI_Comm_size(MPI_COMM_WORLD, &world_size);
	int token;
	if (world_rank != 0) {
	    MPI_Recv(&token, 1, MPI_INT, world_rank - 1, 0,
        	     MPI_COMM_WORLD, MPI_STATUS_IGNORE);
	    printf("Process %d received token %d from process %d\n",
        	   world_rank, token, world_rank - 1);
	} else {
	    // Set the token's value if you are process 0
	    token = 777;
	}
	MPI_Send(&token, 1, MPI_INT, (world_rank + 1) % world_size,
        	 0, MPI_COMM_WORLD);
	// Now process 0 can receive from the last process.
	if (world_rank == 0) {
	    MPI_Recv(&token, 1, MPI_INT, world_size - 1, 0,
        	     MPI_COMM_WORLD, MPI_STATUS_IGNORE);
	    printf("Process %d received token %d from process %d\n",
        	   world_rank, token, world_size - 1);
	}
	return 0;
}编译:
openmpi/bin/mpicxx ./hello_little_ring.cpp -o ring
运行:

可见,每个rank都拿到了777这个数值。










![[内容转载] 微信全平台开源终端数据库WCDB迎来重大升级!](https://i-blog.csdnimg.cn/direct/26b99df7ec814e72a6f8afc87d247e74.png)








