Fakultas Ilmu Komputer UI

Commit 17d1e9c6 authored by Ardhi Putra Pratama's avatar Ardhi Putra Pratama
Browse files

MPI send-receive-exchange example

parent 153308bd
#include "mpi.h"
#include <stdio.h>
int main(int argc, char** argv) {
int size, rank, dest, source, rc, count;
char inmsg, outmsg='x';
MPI_Status Stat;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
dest = source = 1;
outmsg = 'x';
rc = MPI_Send(&outmsg, 1, MPI_CHAR, dest, 1, MPI_COMM_WORLD);
rc = MPI_Recv(&inmsg, 1, MPI_CHAR, source, 1, MPI_COMM_WORLD, &Stat);
}
else if (rank == 1) {
dest = source = 0;
outmsg = 'y';
rc = MPI_Recv(&inmsg, 1, MPI_CHAR, source, 1, MPI_COMM_WORLD, &Stat);
rc = MPI_Send(&outmsg, 1, MPI_CHAR, dest, 1, MPI_COMM_WORLD);
}
printf("Rank %d: Received char %c from task %d \n", rank, inmsg, Stat.MPI_SOURCE);
MPI_Finalize();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment