企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
https://www.sharcnet.ca/help/index.php/Getting_Started_with_MPI ~~~ #include <stdio.h> #include <mpi.h> #define BUFMAX 81 int main(int argc, char *argv[]) { char outbuf[BUFMAX], inbuf[BUFMAX]; int rank, size; int sendto, recvfrom; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); sprintf(outbuf, "Hello, world! from process %d of %d", rank, size); sendto = (rank + 1) % size; recvfrom = ((rank + size) - 1) % size; MPI_Send(outbuf, BUFMAX, MPI_CHAR, sendto, 0, MPI_COMM_WORLD); MPI_Recv(inbuf, BUFMAX, MPI_CHAR, recvfrom, 0, MPI_COMM_WORLD, &status); printf("[P_%d] process %d said: \"%s\"]\n", rank, recvfrom, inbuf); MPI_Finalize(); return(0); } ~~~ 上面的实现之所有没有死锁是依赖于数据传输的中间buffer,也就是写的时候直接写到buffer,不需要等到接收端的回应,代码是可以正确运行的,但是他的实现是并不是完全依赖MPI的标准,下面给出一个MPI 安全的例子 ~~~ #include <stdio.h> #include <mpi.h> #define BUFMAX 81 int main(int argc, char *argv[]) { char outbuf[BUFMAX], inbuf[BUFMAX]; int rank, size; int sendto, recvfrom; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); sprintf(outbuf, "Hello, world! from process %d of %d", rank, size); sendto = (rank + 1) % size; recvfrom = ((rank + size) - 1) % size; if (!(rank % 2)) { MPI_Send(outbuf, BUFMAX, MPI_CHAR, sendto, 0, MPI_COMM_WORLD); MPI_Recv(inbuf, BUFMAX, MPI_CHAR, recvfrom, 0, MPI_COMM_WORLD, &status); } else { MPI_Recv(inbuf, BUFMAX, MPI_CHAR, recvfrom, 0, MPI_COMM_WORLD, &status); MPI_Send(outbuf, BUFMAX, MPI_CHAR, sendto, 0, MPI_COMM_WORLD); } printf("[P_%d] process %d said: \"%s\"]\n", rank, recvfrom, inbuf); MPI_Finalize(); return(0); } ~~~ 这个例子和上面的最大的不同就是根据rank分成两组,这两组的接收和发送的顺序是刚好相反的,这样如果没有buffer,一样可以正确的得到结果,因此是安全的。