#include #include #include #include #include #include "scatterv.hpp" #include "gatherv.hpp" using std::vector; using std::ifstream; using std::cout; using std::cin; using std::endl; void print( vector* M, int m, int n ) { for ( int i = 0; i < m; ++ i ) { for ( int j = 0; j < n; ++j ) { cout << M[i][j] << " "; } cout << endl; } } // ---------- end of function print ---------- int main ( int argc, char *argv[] ) { boost::mpi::environment env( argc, argv ); boost::mpi::communicator world; const int nprocs = world.size(); const int myRank = world.rank(); const int root = 0; const int dim = 5; int ninputs; ifstream file; if ( myRank == root ) { file.open( "input.txt", ifstream::in ); file >> ninputs; file.close(); } boost::mpi::broadcast( world, ninputs, root ); int remainder = ninputs % nprocs; vector pieceFor( nprocs, ninputs/nprocs ); for ( int rank = 0; rank < remainder; rank++ ) ++pieceFor[rank]; int myPiece = pieceFor[myRank]; vector displs(nprocs); for ( int i = 0, aux = 0; i < nprocs; i++ ) { displs[i] = aux; aux += pieceFor[i]; } //---------------------------------------------------------------------- // C-like data to be scattered //---------------------------------------------------------------------- vector* PTR = new vector[ninputs]; for ( int i = 0; i < ninputs; ++i ) PTR[i].resize(dim); for ( int i = 0; i < ninputs; ++i ) { for ( int j = 0; j < dim; ++j ) { PTR[i][j] = i+1; } } //---------------------------------------------------------------------- // C++-like data to be scattered //---------------------------------------------------------------------- vector< vector > V( ninputs, vector(dim) ); for ( int i = 0; i < ninputs; ++i ) { for ( int j = 0; j < dim; ++j ) { V[i][j] = i+1; } } //---------------------------------------------------------------------- // BOOST::MPI::SCATTERV //---------------------------------------------------------------------- vector* ptr = new vector[myPiece]; cout << "***** Scatterv - Prototype 1 *****" << endl; boost::mpi::scatterv( world, PTR, pieceFor, displs, ptr, myPiece, root ); print( ptr, myPiece, dim ); // cout << "***** Scatterv - Prototype 2 *****" << endl; // boost::mpi::scatterv( world, V, pieceFor, displs, ptr, myPiece, root ); // print( ptr, myPiece, dim ); // cout << "***** Scatterv - Prototype 3 *****" << endl; // if ( myRank == root ) // boost::mpi::scatterv( world, PTR, pieceFor, displs, ptr, myPiece, root ); // else // boost::mpi::scatterv( world, ptr, myPiece, root ); // print( ptr, myPiece, dim ); // cout << "***** Scatterv - Prototype 4 *****" << endl; // boost::mpi::scatterv( world, PTR, pieceFor, ptr, root ); // print( ptr, myPiece, dim ); // cout << "***** Scatterv - Prototype 5 *****" << endl; // boost::mpi::scatterv( world, V, pieceFor, ptr, root ); // print( ptr, myPiece, dim ); //---------------------------------------------------------------------- // C-like data to be gathered //---------------------------------------------------------------------- // ptr will be gathered //---------------------------------------------------------------------- // C++-like data to be gathered //---------------------------------------------------------------------- vector< vector > v( myPiece ); for ( int i = 0; i < myPiece; ++i ) v[i] = ptr[i]; //---------------------------------------------------------------------- // BOOST::MPI::GATHERV //---------------------------------------------------------------------- for ( int i = 0; i < ninputs; ++i ) { for ( int j = 0; j < dim; ++j ) { PTR[i][j] = 0; } } if ( myRank == root ) cout << "***** Gatherv - Prototype 1 *****" << endl; boost::mpi::gatherv( world, ptr, myPiece, PTR, pieceFor, displs, root ); if ( myRank == root ) print( PTR, ninputs, dim ); // if ( myRank == root ) cout << "***** Gatherv - Prototype 2 *****" << endl; // boost::mpi::gatherv( world, v, PTR, pieceFor, displs, root ); // if ( myRank == root ) print( PTR, ninputs, dim ); // if ( myRank == root ) cout << "***** Gatherv - Prototype 3 *****" << endl; // if ( myRank == root ) // boost::mpi::gatherv( world, ptr, myPiece, PTR, pieceFor, displs, root ); // else // boost::mpi::gatherv( world, ptr, myPiece, root ); // if ( myRank == root ) print( PTR, ninputs, dim ); // if ( myRank == root ) cout << "***** Gatherv - Prototype 4 *****" << endl; // if ( myRank == root ) // boost::mpi::gatherv( world, v, PTR, pieceFor, displs, root ); // else // boost::mpi::gatherv( world, v, root ); // if ( myRank == root ) print( PTR, ninputs, dim ); // if ( myRank == root ) cout << "***** Gatherv - Prototype 5 *****" << endl; // boost::mpi::gatherv( world, ptr, myPiece, PTR, pieceFor, root ); // if ( myRank == root ) print( PTR, ninputs, dim ); // if ( myRank == root ) cout << "***** Gatherv - Prototype 6 *****" << endl; // boost::mpi::gatherv( world, v, PTR, pieceFor, root ); // if ( myRank == root ) print( PTR, ninputs, dim ); // --------------------------------------------------------------------- delete[] PTR; delete[] ptr; V.clear(); v.clear(); cout << "----- PROCESS " << myRank << " -----" << endl; cout << myPiece << " of " << ninputs << endl; cin.get(); return 0; } // ---------- end of function main ----------