ex7
This commit is contained in:
parent
89326dd329
commit
2467b9ae03
44 changed files with 22631 additions and 0 deletions
2877
ex7/code/task2/first.template/Doxyfile
Normal file
2877
ex7/code/task2/first.template/Doxyfile
Normal file
File diff suppressed because it is too large
Load diff
23
ex7/code/task2/first.template/Makefile
Normal file
23
ex7/code/task2/first.template/Makefile
Normal 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
|
||||
85
ex7/code/task2/first.template/greetings.cpp
Normal file
85
ex7/code/task2/first.template/greetings.cpp
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
#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, i, MPI_ANY_TAG, 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;
|
||||
}
|
||||
16
ex7/code/task2/first.template/greetings.h
Normal file
16
ex7/code/task2/first.template/greetings.h
Normal 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
|
||||
33
ex7/code/task2/first.template/main.cpp
Normal file
33
ex7/code/task2/first.template/main.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// 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);
|
||||
int myrank, numprocs;
|
||||
// numprocs = 1; // delete this line when uncommenting the next line
|
||||
MPI_Comm_rank(icomm, &myrank); // my MPI-rank
|
||||
MPI_Comm_size(icomm, &numprocs);
|
||||
|
||||
if (0==myrank) {
|
||||
cout << "\n There are " << numprocs << " processes running.\n \n";
|
||||
}
|
||||
|
||||
// greetings(icomm);
|
||||
greetings_cpp(icomm);
|
||||
|
||||
if (0==myrank) cout << endl;
|
||||
|
||||
MPI_Finalize();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue