Sheet 7 - 1

This commit is contained in:
Georg Thomas Mandl 2026-01-07 00:58:59 +01:00
commit 4c107a5c28
5 changed files with 181 additions and 0 deletions

23
Sheet_7/bsp_7_2/Makefile Normal file
View file

@ -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

View file

@ -0,0 +1,86 @@
#include "greetings.h"
#include <cassert>
#include <cstring>
#include <iostream>
#include <mpi.h> // MPI
#include <string>
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;
}

View file

@ -0,0 +1,16 @@
// general header for all functions in directory
#ifndef GREETINGS_FILE
#define GREETINGS_FILE
#include <mpi.h>
/** 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

35
Sheet_7/bsp_7_2/main.cpp Normal file
View file

@ -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 <iostream> // MPI
#include <mpi.h>
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;
}

View file

@ -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 )