#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 //---------------------------------------------------------------------- vector* bigPtr = new vector[ninputs]; for ( int i = 0; i < ninputs; ++i ) bigPtr[i].resize(dim); for ( int i = 0; i < ninputs; ++i ) { for ( int j = 0; j < dim; ++j ) { bigPtr[i][j] = i+1; } } //---------------------------------------------------------------------- // C++-like DATA //---------------------------------------------------------------------- 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* v = new vector[myPiece]; cout << "***** Scatterv - Prototype 1 *****" << endl; boost::mpi::scatterv( world, bigPtr, pieceFor, displs, v, myPiece, root ); print( v, myPiece, dim ); // cout << "***** Scatterv - Prototype 2 *****" << endl; // boost::mpi::scatterv( world, V, pieceFor, displs, v, myPiece, root ); // print( v, myPiece, dim ); // cout << "***** Scatterv - Prototype 3 *****" << endl; // boost::mpi::scatterv( world, bigPtr, ninputs, v, root ); // print( v, myPiece, dim ); // cout << "***** Scatterv - Prototype 4 *****" << endl; // boost::mpi::scatterv( world, V, v, root ); // print( v, myPiece, dim ); // cout << "***** Scatterv - Prototype 5 *****" << endl; // if ( myRank == root ) // boost::mpi::scatterv( world, bigPtr, pieceFor, displs, v, myPiece, root ); // else // boost::mpi::scatterv( world, v, myPiece, root ); // print( v, myPiece, dim ); //---------------------------------------------------------------------- // BOOST::MPI::GATHERV //---------------------------------------------------------------------- for ( int i = 0; i < ninputs; ++i ) { for ( int j = 0; j < dim; ++j ) { V[i][j] = 0; bigPtr[i][j] = 0; } } if ( myRank == root ) cout << "***** Gatherv - Prototype 1 *****" << endl; boost::mpi::gatherv( world, v, myPiece, bigPtr, pieceFor, displs, root ); if ( myRank == root ) print( bigPtr, ninputs, dim ); // if ( myRank == root ) cout << "***** Gatherv - Prototype 2 *****" << endl; // boost::mpi::gatherv( world, v, myPiece, V, pieceFor, displs, root ); // if ( myRank == root ) print( &V[0], ninputs, dim ); // if ( myRank == root ) cout << "***** Gatherv - Prototype 3 *****" << endl; // boost::mpi::gatherv( world, v, myPiece, bigPtr, root ); // if ( myRank == root ) print( bigPtr, ninputs, dim ); // if ( myRank == root ) cout << "***** Gatherv - Prototype 4 *****" << endl; // boost::mpi::gatherv( world, v, myPiece, bigPtr, root ); // if ( myRank == root ) print( bigPtr, ninputs, dim ); // if ( myRank == root ) cout << "***** Gatherv - Prototype 5 *****" << endl; // boost::mpi::gatherv( world, v, myPiece, V, root ); // if ( myRank == root ) print( &V[0], ninputs, dim ); // if ( myRank == root ) cout << "***** Gatherv - Prototype 6 *****" << endl; // if ( myRank == root ) // boost::mpi::gatherv( world, v, myPiece, V, pieceFor, displs, root ); // else // boost::mpi::gatherv( world, v, myPiece, root ); // if ( myRank == root ) print( &V[0], ninputs, dim ); // --------------------------------------------------------------------- delete[] bigPtr; delete[] v; V.clear(); cout << "----- PROCESS " << myRank << " -----" << endl; cout << myPiece << " of " << ninputs << endl; cin.get(); return 0; } // ---------- end of function main ----------