/**
\file mpiExample1.cpp
\brief MPI programming example #1.
\author george j. grevera, ph.d.
This program is a skeleton of a parallel MPI application using the one
manager/many workers framework.
compile: mpic++ -g -o mpiExample1.exe mpiExample1.cpp # debug version
mpic++ -O3 -o mpiExample1.exe mpiExample1.cpp # optimized version
g++ -g -pthread -o mpiExample1.exe mpiExample1.cpp -llammpio \
-llammpi++ -llamf77mpi -lmpi -llam -lutil
run : lamboot -v # to start lam mpi
mpirun -np 4 ./mpiExample1.exe # run in parallel w/ 4 processes
lamhalt @ to stop lam mpi
*/
#include
#include
#include
#include
static char mpiName[ 1024 ]; ///< host computer name
static int mpiRank; ///< number of this process (0..n-1)
static int mpiSize; ///< total number of processes (n)
static int myPID; ///< process id
//----------------------------------------------------------------------
/** \brief manager code for example 1
*/
static void manager ( void ) {
printf( "manager: my rank=%d, size=%d, pid=%d. \n",
mpiRank, mpiSize, myPID );
/** \todo insert manager code here. */
}
//----------------------------------------------------------------------
/** \brief worker code for example 1
*/
static void worker ( void ) {
printf( "worker: my rank=%d, size=%d, pid=%d. \n",
mpiRank, mpiSize, myPID );
/** \todo insert worker code here. */
}
//----------------------------------------------------------------------
/** \brief main program entry point for example 1. execution begins here.
\param argc count of command line arguments.
\param argv array of command line arguments.
\returns 0 is always returned.
*/
int main ( int argc, char* argv[] ) { //not const because MPI_Init may change
if (MPI_Init( &argc, &argv ) != MPI_SUCCESS) {
//actually, we'll never get here but it is a good idea to check.
// if MPI_Init fails, mpi will exit with an error message.
puts( "mpi init failed." );
return 0;
}
//get the name of this computer
gethostname( mpiName, sizeof( mpiName ) );
//determine rank
MPI_Comm_rank( MPI_COMM_WORLD, &mpiRank );
//determine the total number of processes
MPI_Comm_size( MPI_COMM_WORLD, &mpiSize );
//get the process id
myPID = getpid();
printf( "mpi initialized. my rank=%d, size=%d, pid=%d. \n",
mpiRank, mpiSize, myPID );
if (mpiSize<2) {
puts("this example requires at least 1 manager and 1 worker process.");
MPI_Finalize();
return 0;
}
if (mpiRank==0) manager();
else worker();
MPI_Finalize();
return 0;
}
//----------------------------------------------------------------------