diff --git a/Sheet_7/bsp_7_2/Makefile b/Sheet_7/bsp_7_2/Makefile new file mode 100644 index 0000000..9cdabc2 --- /dev/null +++ b/Sheet_7/bsp_7_2/Makefile @@ -0,0 +1,23 @@ +# +# Compile with +# make 2>&1 | grep -v openmpi +# to avoid warnings caused by OpenMPI + +# use GNU-Compiler tools +COMPILER=GCC_ +# alternatively from the shell +# export COMPILER=GCC_ +# or, alternatively from the shell +# make COMPILER=GCC_ + +MAIN = main +SOURCES = ${MAIN}.cpp greetings.cpp +OBJECTS = $(SOURCES:.cpp=.o) + +PROGRAM = ${MAIN}.${COMPILER} + +# uncomment the next to lines for debugging and detailed performance analysis +CXXFLAGS += -g +LINKFLAGS += + +include ../${COMPILER}default.mk diff --git a/Sheet_7/bsp_7_2/greetings.cpp b/Sheet_7/bsp_7_2/greetings.cpp new file mode 100644 index 0000000..bc62d5b --- /dev/null +++ b/Sheet_7/bsp_7_2/greetings.cpp @@ -0,0 +1,86 @@ +#include "greetings.h" +#include +#include +#include +#include // MPI +#include +using namespace std; + +// see http://www.open-mpi.org/doc/current +// for details on MPI functions + +void greetings(MPI_Comm const &icomm) +{ + int myrank, numprocs; + MPI_Comm_rank(icomm, &myrank); // my MPI-rank + MPI_Comm_size(icomm, &numprocs); // #MPI processes + char *name = new char [MPI_MAX_PROCESSOR_NAME], + *chbuf = new char [MPI_MAX_PROCESSOR_NAME]; + + int reslen, ierr; + MPI_Get_processor_name( name, &reslen); + + if (0==myrank) { + cout << " " << myrank << " runs on " << name << endl; + for (int i = 1; i < numprocs; ++i) { + MPI_Status stat; + stat.MPI_ERROR = 0; // M U S T be initialized!! + + ierr = MPI_Recv(chbuf, MPI_MAX_PROCESSOR_NAME, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, icomm, &stat); + assert(0==ierr); + + cout << " " << stat.MPI_SOURCE << " runs on " << chbuf; + int count; + MPI_Get_count(&stat, MPI_CHAR, &count); // size of received data + cout << " (length: " << count << " )" << endl; + // stat.Get_error() // Error code + } + } + else { + int dest = 0; + ierr = MPI_Send(name, strlen(name) + 1, MPI_CHAR, dest, myrank, icomm); + assert(0==ierr); + } + delete [] chbuf; + delete [] name; + return; +} + + +void greetings_cpp(MPI_Comm const &icomm) +{ + int myrank, numprocs; + MPI_Comm_rank(icomm, &myrank); // my MPI-rank + MPI_Comm_size(icomm, &numprocs); // #MPI processes + string name(MPI_MAX_PROCESSOR_NAME,'#'), // C++ + recvbuf(MPI_MAX_PROCESSOR_NAME,'#'); // C++: receive buffer, don't change size + + int reslen, ierr; + MPI_Get_processor_name(name.data(), &reslen); + name.resize(reslen); // C++ + + if (0==myrank) { + cout << " " << myrank << " runs on " << name << endl; + for (int i = 1; i < numprocs; ++i) { + MPI_Status stat; + stat.MPI_ERROR = 0; // M U S T be initialized!! + + //ierr = MPI_Recv(recvbuf.data(), MPI_MAX_PROCESSOR_NAME, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, icomm, &stat); + ierr = MPI_Recv(recvbuf.data(), MPI_MAX_PROCESSOR_NAME, MPI_CHAR, i, i, icomm, &stat); + assert(0==ierr); + + int count; + MPI_Get_count(&stat, MPI_CHAR, &count); // size of received data + string const chbuf(recvbuf,0,count); // C++ + cout << " " << stat.MPI_SOURCE << " runs on " << chbuf; + cout << " (length: " << count << " )" << endl; + // stat.Get_error() // Error code + } + } + else { + int dest = 0; + ierr = MPI_Send(name.data(), name.size(), MPI_CHAR, dest, myrank, icomm); + assert(0==ierr); + } + return; +} diff --git a/Sheet_7/bsp_7_2/greetings.h b/Sheet_7/bsp_7_2/greetings.h new file mode 100644 index 0000000..d841300 --- /dev/null +++ b/Sheet_7/bsp_7_2/greetings.h @@ -0,0 +1,16 @@ +// general header for all functions in directory + +#ifndef GREETINGS_FILE +#define GREETINGS_FILE + +#include + +/** Each process finds out its host, sends this information + to root process 0 which prints this information for each process. + @param[in] icomm the MPI process group that is used. +*/ + +void greetings(MPI_Comm const &icomm); +void greetings_cpp(MPI_Comm const &icomm); + +#endif diff --git a/Sheet_7/bsp_7_2/main.cpp b/Sheet_7/bsp_7_2/main.cpp new file mode 100644 index 0000000..277eb9f --- /dev/null +++ b/Sheet_7/bsp_7_2/main.cpp @@ -0,0 +1,35 @@ +// MPI code in C++. +// See [Gropp/Lusk/Skjellum, "Using MPI", p.33/41 etc.] +// and /opt/mpich/include/mpi2c++/comm.h for details + +#include "greetings.h" +#include // MPI +#include +using namespace std; + +int main(int argc, char *argv[]) +{ + MPI_Comm icomm = MPI_COMM_WORLD; + MPI_Init(&argc, &argv); // E2 + + int myrank, numprocs; + MPI_Comm_rank(icomm, &myrank); // my MPI-rank, process-ID + MPI_Comm_size(icomm, &numprocs); // number of all processes + + // cout << "\n Process nr. " << myrank << " says, there are " << numprocs << " processes running.\n \n"; // + + // E3 + if (0==myrank) { + cout << "\n Process nr. " << myrank << " says, there are " << numprocs << " processes running.\n \n"; + } + + //greetings(icomm); + greetings_cpp(icomm); // E4 + + + MPI_Finalize(); // E2 + + return 0; +} + + diff --git a/Sheet_7/bsp_7_2/results_7_2.txt b/Sheet_7/bsp_7_2/results_7_2.txt new file mode 100644 index 0000000..ddfd997 --- /dev/null +++ b/Sheet_7/bsp_7_2/results_7_2.txt @@ -0,0 +1,21 @@ +Start with : /usr/bin/mpirun --oversubscribe -display-map -mca btl ^openib -np num_proc main.GCC_ + +/usr/bin/mpirun --oversubscribe -display-map -mca btl ^openib -np 4 ./main.GCC_ + Data for JOB [34080,1] offset 0 Total slots allocated 4 + + ======================== JOB MAP ======================== + + Data for node: LAPTOP-LTDL04HH Num slots: 4 Max slots: 0 Num procs: 4 + Process OMPI jobid: [34080,1] App: 0 Process rank: 0 Bound: UNBOUND + Process OMPI jobid: [34080,1] App: 0 Process rank: 1 Bound: UNBOUND + Process OMPI jobid: [34080,1] App: 0 Process rank: 2 Bound: UNBOUND + Process OMPI jobid: [34080,1] App: 0 Process rank: 3 Bound: UNBOUND + + ============================================================= + + Process nr. 0 says, there are 4 processes running. + + 0 runs on LAPTOP-LTDL04HH + 1 runs on LAPTOP-LTDL04HH (length: 15 ) + 2 runs on LAPTOP-LTDL04HH (length: 15 ) + 3 runs on LAPTOP-LTDL04HH (length: 15 ) \ No newline at end of file