1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
#include <vector>
#include <mpi.h>
int main(int argc, char *argv[])
{
MPI_Init( &argc, &argv );
MPI_Comm comm = MPI_COMM_WORLD;
std::vector < std::vector <double> > some_2d_vec {{1,2,3}, {10,20,30}};
std::vector < std::vector <double> > reduced_vec {2}; // vector of two empty vectors
int no_of_elems = some_2d_vec[0].size(); // number of elements to send via mpi
reduced_vec[0].resize( no_of_elems ); // make enough space in the receiving vector
// MPI_Reduce( &some_2d_vec[0][0], &reduced_vec[0][0], no_of_elems, MPI_DOUBLE, MPI_SUM, 0, comm);
MPI_Reduce( some_2d_vec[0].data(), reduced_vec[0].data(), no_of_elems, MPI_DOUBLE, MPI_SUM, 0, comm);
// Let's at least see the output ...
int rank;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
if ( rank == 0 )
{
for ( double e : reduced_vec[0] ) std::cout << e <<'\n';
}
MPI_Finalize();
}
|