![](http://static.javashuo.com/static/loading.gif)
#include<stdio.h>
![](http://static.javashuo.com/static/loading.gif)
#include<mpi.h>
/***********************************************
mpicc -o block block.c
mpirun -np 2 block
***********************************************/
int main(
int argc,
char *argv[]){
int numTasks, rank, rc;
![](http://static.javashuo.com/static/loading.gif)
MPI_Init(&argc, &argv);
![](http://static.javashuo.com/static/loading.gif)
MPI_Comm_size(MPI_COMM_WORLD, &numTasks);
//get from -np
![](http://static.javashuo.com/static/loading.gif)
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//each process's ID
if(rank == 0){
char sendChar = 'A';
char receiveChar;
int count = 1;
int destRank = 1;
int sourceRank = 1;
//MPI_ANY_SOURCE
int tagSendMSGID = 6;
int tagRecvMSGID = 9;
![](http://static.javashuo.com/static/loading.gif)
MPI_Status status;
![](http://static.javashuo.com/static/loading.gif)
rc = MPI_Send(&sendChar, count, MPI_CHAR, destRank, tagSendMSGID, MPI_COMM_WORLD);
![](http://static.javashuo.com/static/loading.gif)
rc = MPI_Recv(&receiveChar, count, MPI_CHAR, sourceRank, tagRecvMSGID, MPI_COMM_WORLD, &status);
![](http://static.javashuo.com/static/loading.gif)
printf(
"Rank = %d, sendChar = %c, received char = %c\n", rank, sendChar, receiveChar);
![](http://static.javashuo.com/static/loading.gif)
printf(
"Received from task %d with tag %d\n", status.MPI_SOURCE, status.MPI_TAG);
![](http://static.javashuo.com/static/loading.gif)
}
else
if(rank == 1){
char sendChar = 'Z';
char receiveChar;
int count = 1;
int destRank = 0;
int sourceRank = 0;
int tagRecvMSGID = 6;
int tagSendMSGID = 9;
![](http://static.javashuo.com/static/loading.gif)
MPI_Status status;
![](http://static.javashuo.com/static/loading.gif)
rc = MPI_Recv(&receiveChar, count, MPI_CHAR, sourceRank, tagRecvMSGID, MPI_COMM_WORLD, &status);
if(receiveChar == 'A'){
![](http://static.javashuo.com/static/loading.gif)
rc = MPI_Send(&sendChar, count, MPI_CHAR, destRank, tagSendMSGID, MPI_COMM_WORLD);
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
printf(
"Rank = %d, received char = %c, then send char = %c\n", rank, receiveChar, sendChar);
![](http://static.javashuo.com/static/loading.gif)
printf(
"Received from task %d with tag %d\n", status.MPI_SOURCE, status.MPI_TAG);
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
MPI_Finalize();
return 0;
![](http://static.javashuo.com/static/loading.gif)
}