diff --git a/MPI-Codes/IntroMPI/Makefile b/MPI-Codes/IntroMPI/Makefile new file mode 100755 index 0000000..f7ed9e8 --- /dev/null +++ b/MPI-Codes/IntroMPI/Makefile @@ -0,0 +1,19 @@ +MPICC = mpicc #The wrapper for the compiler +CFLAGS += -g #Useful for debug symbols + +all: helloworld hamlet easysendrecv nonblockingsendrecv + +helloworld: helloworld.c + $(MPICC) $(CFLAGS) $(LDFLAGS) $? $(LDLIBS) -o $@ + +hamlet: hamlet.c + $(MPICC) $(CFLAGS) $(LDFLAGS) $? $(LDLIBS) -o $@ + +easysendrecv: easysendrecv.c + $(MPICC) $(CFLAGS) $(LDFLAGS) $? $(LDLIBS) -o $@ + +nonblockingsendrecv: nonblockingsendrecv.c + $(MPICC) $(CFLAGS) $(LDFLAGS) $? $(LDLIBS) -o $@ + +clean: + rm -f helloworld hamlet easysendrecv nonblockingsendrecv diff --git a/MPI-Codes/IntroMPI/easysendrecv.c b/MPI-Codes/IntroMPI/easysendrecv.c new file mode 100755 index 0000000..e98a617 --- /dev/null +++ b/MPI-Codes/IntroMPI/easysendrecv.c @@ -0,0 +1,20 @@ +#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){ /* code for process zero */ + strcpy(message,"Hello, there"); + MPI_Send(message, strlen(message)+1, MPI_CHAR, 1, 99, MPI_COMM_WORLD); + } + else if (myrank == 1){ /* code for process one */ + MPI_Recv(message, 20, MPI_CHAR, 0, 99, MPI_COMM_WORLD, &status); + printf("received :%s:\n", message); + } + MPI_Finalize(); + return 0; +} diff --git a/MPI-Codes/IntroMPI/hamlet.c b/MPI-Codes/IntroMPI/hamlet.c new file mode 100755 index 0000000..64b3ef2 --- /dev/null +++ b/MPI-Codes/IntroMPI/hamlet.c @@ -0,0 +1,11 @@ +#include "mpi.h" +#include +int main( int argc, char **argv ){ + int rank, size; + MPI_Init( &argc, &argv ); + MPI_Comm_rank( MPI_COMM_WORLD, &rank ); + MPI_Comm_size( MPI_COMM_WORLD, &size ); + printf( "Hello world! I'm process %d of %d\n",rank, size ); + MPI_Finalize(); + return 0; +} diff --git a/MPI-Codes/IntroMPI/helloworld.c b/MPI-Codes/IntroMPI/helloworld.c new file mode 100755 index 0000000..a6329d0 --- /dev/null +++ b/MPI-Codes/IntroMPI/helloworld.c @@ -0,0 +1,9 @@ +#include "mpi.h" +#include + +int main(int argc, char **argv){ + MPI_Init( &argc, &argv); + printf("Hello, world!\n"); + MPI_Finalize(); + return 0; +} diff --git a/MPI-Codes/IntroMPI/launcher.sh b/MPI-Codes/IntroMPI/launcher.sh new file mode 100644 index 0000000..d3ea24c --- /dev/null +++ b/MPI-Codes/IntroMPI/launcher.sh @@ -0,0 +1,8 @@ +#!/bin/bash +#SBATCH --job-name=helloworld +#SBATCH --nodes=1 +#SBATCH --ntasks=6 +#SBATCH --time=00:05:00 +#SBATCH -o ./%x.%j.out +#SBATCH -e ./%x.%j.err +mpirun helloworld diff --git a/MPI-Codes/IntroMPI/nonblockingsendrecv.c b/MPI-Codes/IntroMPI/nonblockingsendrecv.c new file mode 100755 index 0000000..2a3fee0 --- /dev/null +++ b/MPI-Codes/IntroMPI/nonblockingsendrecv.c @@ -0,0 +1,30 @@ +#include +#include + +int main(int argc, char **argv) { + int a, b; + int size, rank; + int tag = 0; + MPI_Status status; + MPI_Request send_request, recv_request; + MPI_Init(&argc, &argv); + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + if (rank == 0) { + a = 314159; + MPI_Isend(&a, 1, MPI_INT, 1, tag, MPI_COMM_WORLD, &send_request); + MPI_Irecv (&b, 1, MPI_INT, 1, tag, MPI_COMM_WORLD, &recv_request); + MPI_Wait(&send_request, &status); + MPI_Wait(&recv_request, &status); + printf ("Process %d received value %d\n", rank, b); + } else { + a = 667; + MPI_Isend (&a, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &send_request); + MPI_Irecv (&b, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &recv_request); + MPI_Wait(&send_request, &status); + MPI_Wait(&recv_request, &status); + printf ("Process %d received value %d\n", rank, b); + } + MPI_Finalize(); + return 0; +}