Source code
/* Program:
* 每個 node 將自己的 id 資訊送給下一個 node。
* History:
* 2008-06-05 BETA
* 2008-06-06 改變 #30 printf 位置,以了解 MPI 函式執行順序關係
* 2008-06-12 更改顯示方法。
* 2008-06-17 變更變數名 myid -> myrank,增加註解
*/
#include <mpi.h>
#include <stdio.h>
main(int argc,char **argv)
{
int n, myrank, numprocs;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
/* node 0 will send the first message */
if(myrank == 0)
{
n = myrank;
MPI_Send(&n, 1, MPI_INT, 1, 99, MPI_COMM_WORLD);
printf("[Ndde %d]「%d」 >> [Node %d]\n\n", myrank, n, myrank+1);
}
/* node 1 to node n-2 will send message to the next node */
if(myrank>0 && myrank<numprocs-1)
{
MPI_Recv(&n, 1, MPI_INT, myrank-1, 99, MPI_COMM_WORLD, &status);
printf("[Node %d] << 「%d」[Node %d]\n", myrank, n, status.MPI_SOURCE);
n = myrank;
MPI_Send(&n, 1, MPI_INT, myrank+1, 99, MPI_COMM_WORLD);
printf("[Ndde %d]「%d」 >> [Node %d]\n\n", myrank, n, myrank+1);
}
/* the final node n-1 will not send any message but receive*/
if(myrank==numprocs-1)
{
MPI_Recv(&n, 1, MPI_INT, myrank-1, 99, MPI_COMM_WORLD, &status);
printf("[Node %d] << 「%d」[Node %d]\n", myrank, n, status.MPI_SOURCE);
}
MPI_Finalize();
}
Result
Last modified 17 years ago
Last modified on Jun 25, 2008, 5:15:48 PM
Attachments (1)
- demo2-01.png (64.3 KB) - added by wade 17 years ago.
Download all attachments as: .zip

