From 51b1f5f36bf2015e7fd8f1926e0a07d0f03ad917 Mon Sep 17 00:00:00 2001 From: Fabio Durastante Date: Wed, 24 Apr 2024 16:49:35 +0200 Subject: [PATCH] Added easy send receive example --- Makefile | 5 +++-- easysendrcv.c | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 easysendrcv.c diff --git a/Makefile b/Makefile index eccd070..078d9db 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,11 @@ MPICC = mpicc #The wrapper for the compiler CFLAGS += -g #Useful for debug symbols -all: helloworld hamlet +all: helloworld hamlet easysendrec helloworld: helloworld.c $(MPICC) $(CFLAGS) $(LDFLAGS) $? $(LDLIBS) -o $@ hamlet: hamlet.c $(MPICC) $(CFLAGS) $(LDFLAGS) $? $(LDLIBS) -o $@ - +easysendrec: easysendrcv.c + $(MPICC) $(CFLAGS) $(LDFLAGS) $? $(LDLIBS) -o $@ clean: rm -f helloworld diff --git a/easysendrcv.c b/easysendrcv.c new file mode 100644 index 0000000..fa09533 --- /dev/null +++ b/easysendrcv.c @@ -0,0 +1,18 @@ +#include "mpi.h" +#include +#include +int main( int argc, char **argv){ +char message[20]; int myrank; MPI_Status status; +MPI_Init(&argc, &argv); +MPI_Comm_rank(MPI_COMM_WORLD, &myrank); +if (myrank == 0) { +strcpy(message,"Hello, there"); +MPI_Send(message, strlen(message)+1, MPI_CHAR, 1, 99, MPI_COMM_WORLD); +} else if (myrank == 1) { +printf("Message contains: %s\n",message); +MPI_Recv(message, 20, MPI_CHAR, 0, 99, MPI_COMM_WORLD,&status); +printf("Received :%s:\n",message); +} +MPI_Finalize(); +return 0; +}