Send/receive C++ vector using MPI

I am using IT++ for some of my code and I tried to parallelize it. It came to the point where I need to sum up all the vectors from all the process. So in MPI, I will need to use MPI_Reduce. My question is since IT++ vector is not an array, can we then use MPI_Reduce with “count” parameter or do we need to define a new MPI data type to handle it?

It turns out that C++ std::vector stores the data contiguously similar to Array [1]. And IT++ vector uses std::vector of C++ standard. Hence, we can use a similar technique as Array.

this is a simple test code:

#include "mpi.h"

using namespace itpp;
int main(int argc, char** argv)
int my_rank;
int p;


vec test(3),result(3);
if (my_rank==0)
test(0)=1; test(1)=2; test(2)=3;
test(0)=4; test(1)=5; test(2)=6;
MPI_Reduce(&test(0),&result(0), test.length(), MPI_DOUBLE,
std::cout<<"test from "<<my_rank<<" :"<<test<<"\n";
std::cout<<"result from "<<my_rank<<" :"<<result<<"\n";

The code basically creates vector “test” and store {1,2,3} in root process and {4,5,6} in other process. After that the vector is summed up in root process and displayed. To compile this, I need to include the MPI header file and IT++ header files and libs (I also used Boost library to modify my IT++ source code)

mpicxx -I/usr/lib/openmpi/include -I/home/kurniawano/local/include -I/home/kurniawano/Download/NumericalComputation/boost_1_36_0 -L/home/kurniawano/local/lib -litpp testmpivec.cpp

Then I ran the code in two processes using

mpirun -np 2 ./a.out

The output is:
test from 1 :[4 5 6]
result from 1 :[0 8.44682e+252 5.31157e+222]
test from 0 :[1 2 3]
result from 0 :[5 7 9]

So you can see that the result in root (0) is a summation of the vector from the two processes.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: