From 11a235027e71fea790483c99a23130ddf62e50cb Mon Sep 17 00:00:00 2001 From: 13511001 <13511001@ld5-01.if.itb.ac.id> Date: Fri, 5 Feb 2016 11:07:25 +0700 Subject: [PATCH] Send and Receive added --- sendreceive.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 sendreceive.c diff --git a/sendreceive.c b/sendreceive.c new file mode 100644 index 0000000..4a5b6d8 --- /dev/null +++ b/sendreceive.c @@ -0,0 +1,32 @@ +#include "mpi.h" +#include <stdio.h> + +int main(int argc, char *argv[]) { +int numtasks, rank, dest, source, rc, count, tag=1; +char inmsg, outmsg='x'; +MPI_Status Stat; + +MPI_Init(&argc,&argv); +MPI_Comm_size(MPI_COMM_WORLD, &numtasks); +MPI_Comm_rank(MPI_COMM_WORLD, &rank); + +if (rank == 0) { +dest = 1; +source = 1; +rc = MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD); +rc = MPI_Recv(&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat); +} +else if (rank == 1) { +dest = 0; +source = 0; +rc = MPI_Recv(&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat); +rc = MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD); +} + +rc = MPI_Get_count(&Stat, MPI_CHAR, &count); +printf("Task %d: Received %d char(s) from task %d with tag %d \n", +rank, count, Stat.MPI_SOURCE, Stat.MPI_TAG); + +MPI_Finalize(); +} + -- GitLab