// BasicMPI.cpp // NKU CSC 333/601 - Fall 2002 - Kirby // ------------------------------------------------------------ // A little program to demonstrate basic MPI in C++. // Sends random vectors around the network, circularly. // /************************************************************** Make sure your .rhosts file includes the lines: head.bobb.hpc.nku.edu yourusername b1.bobb.hpc.nku.edu yourusername b2.bobb.hpc.nku.edu yourusername b3.bobb.hpc.nku.edu yourusername b4.bobb.hpc.nku.edu yourusername b5.bobb.hpc.nku.edu yourusername b6.bobb.hpc.nku.edu yourusername b7.bobb.hpc.nku.edu yourusername b8.bobb.hpc.nku.edu yourusername b9.bobb.hpc.nku.edu yourusername b10.bobb.hpc.nku.edu yourusername b11.bobb.hpc.nku.edu yourusername NOTE: Contrary to earlier reports, there are only nodes 0-11, no node 12! Before running, turn on MPI (the -v option confirms which nodes are up) lamboot -v lamhosts To compile: mpiCC BasicMPI.cpp To run on 12 nodes: mpirun -np 12 a.out /***************************************************************/ #include #include #include #include #include #include using namespace std ; #include "mpi.h" // a dummy version provided on CSC 333 web page int main( int argc, char** argv ) { // In this example, all messages bear the same tag. const int TAG= 99 ; // Turn on MPI. MPI_Init( &argc, &argv ) ; // How many processors are out there? int size ; MPI_Comm_size( MPI_COMM_WORLD, &size ) ; // Which processor am I? (0...size-1) int rank ; MPI_Comm_rank( MPI_COMM_WORLD, &rank ) ; // Set the random number seed differently on each processor. srand( time(0) * (1+rank) ) ; // Create a vector. vector v(8) ; // Fill the vector with some (partially random) integers. for ( int i=0 ; i < v.size() ; ++i ) v[i]= 1000*rank + rand()%100 ; // Sort the vector. std::sort( v.begin(), v.end() ) ; // Send my v to my "right" neighbor. int iDest= (rank+1) % size ; MPI_Send( &(v[0]), v.size(), MPI_INT, iDest, TAG, MPI_COMM_WORLD ) ; // Receive my "left" neighbor's message data into my v. int iSource= (rank-1+size) % size ; // = (rank-1) mod size MPI_Status status ; MPI_Recv( &(v[0]), v.size(), MPI_INT, iSource, TAG, MPI_COMM_WORLD, &status ) ; // Have the head node write v to a file. if ( rank == 0 ) { ofstream summary( "summary.txt" ) ; for ( int i=0 ; i < v.size() ; ++i ) summary << v[i] << " " ; summary << endl << "That's all folks." << endl ; cout << "Wrote file summary.txt" << endl ; } // All done. MPI_Finalize() ; return 0 ; }