|
Boost Users : |
Subject: [Boost-users] Using Boost.MPI with gather , data becomes overwritten after gather operation
From: hgc-01134_at_[hidden]
Date: 2010-08-31 12:26:54
Hi,
I am trying to use Boost.MPI to do a very simple program that
1. Processes from different ranks do a gather operation
2. The gather operation will gather the CoProcessInfo ( which is just
a data structure of rank# and hostname) into a vector
3. After the gather operation, Rank 0 will print the CoProcessInfo
one by one in the vector.
------------------------------------------------------------------------------------------------------------------------------------------------------
My testing environment consists of TWO virtual machine ( each of the
machine has two cores ):
Centos 5.5
mpich2
Boost.mpich2
Boost 1.41
mpiexec
------------------------------------------------------------------------------------------------------------------------------------------------------
The expected output of the program should be something like ( just an
example order of ranks ):
Host VM1, Rank 0
Host VM1, Rank 1
Host VM2, Rank 2
Host VM2, Rank 3
----------------------------------------------------------------------------------------------------------------------------------------------------------
However, I got something like: ( just an example order of ranks ), the
hostname is incorrect after gather operation ( three VM2 appears!)
Host VM1, Rank 0
Host VM2, Rank 1
Host VM2, Rank 2
Host VM2, Rank 3
I found that each process CAN correctly found out the hostname and
rank#, however, the data will become a bit like overwritten, after the
gather operation. Thank you very much.
------------------------------------------- Source Code
-------------------------------------------------------------------
#include <boost/asio/ip/host_name.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/mpi.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <sstream>
#include <fstream>
using namespace std;
namespace mpi = boost::mpi;
class CoProcessInfo
{
private:
string hostname;
int rank;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /* version */)
{
ar & hostname & rank;
}
public:
CoProcessInfo()
{
hostname = boost::asio::ip::host_name();
rank = 0;
}
void setRank(int _rank)
{
rank = _rank;
}
string getHostname() const
{
return hostname;
}
int getRank() const
{
return rank;
}
};
int main(int argc, char* argv[])
{
int myrank;
mpi::environment env(argc, argv);
mpi::communicator world;
myrank = world.rank();
CoProcessInfo localInfo;
localInfo.setRank( myrank );
std::vector<CoProcessInfo> coProcInfo;
mpi::gather(world, localInfo, coProcInfo, 0);
if( myrank == 0 ){
for (std::vector<CoProcessInfo>::const_iterator it = coProcInfo.begin();
it !=coProcInfo.end(); ++it) {
cout << endl << " Host : " << it->getHostname() << " , Rank : "
<< it->getRank() << endl;
}
}
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Regards,
Joseph
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net