How to pass user-defined structs using boost mpi
Posted
by lava
on Stack Overflow
See other posts from Stack Overflow
or by lava
Published on 2010-04-21T17:35:48Z
Indexed on
2010/04/21
18:23 UTC
Read the original article
Hit count: 555
I am trying to send a user-defined structure named ABC using boost::mpi::send () call.
The given struct contains a vector "data" whose size is determined at runtime. Objects of struct ABC are sent by master to slaves. But the slaves need to know the size of vector "data" so that the sufficient buffer is available on the slave to receive this data. I can work around it by sending the size first and initialize sufficient buffer on the slave before receiving the objects of struct ABC. But that defeats the whole purpose of using STL containers.
Does anyone know of a better way to do handle this ? Any suggestions are greatly appreciated.
Here is a sample code that describes the intent of my program. This code fails at runtime due to above mentioned reason.
struct ABC
{
double cur_stock_price;
double strike_price;
double risk_free_rate;
double option_price;
std::vector <char> data;
};
namespace boost
{
namespace serialization
{
template<class Archive>
void serialize (Archive &ar,
struct ABC &abc,
unsigned int version)
{
ar & abc.cur_stock_price;
ar & abc.strike_price;
ar & abc.risk_free_rate;
ar & abc.option_price;
ar & bopr.data;
}
}
}
BOOST_IS_MPI_DATATYPE (ABC);
int main(int argc, char* argv[])
{
mpi::environment env (argc, argv);
mpi::communicator world;
if (world.rank () == 0)
{
ABC abc_obj;
abc.cur_stock_price = 1.0;
abc.strike_price = 5.0;
abc.risk_free_rate = 2.5;
abc.option_price = 3.0;
abc_obj.data.push_back ('a');
abc_obj.data.push_back ('b');
world.send ( 1, ANY_TAG, abc_obj;);
std::cout << "Rank 0 OK!" << std::endl;
}
else if (world.rank () == 1)
{
ABC abc_obj;
// Fails here because abc_obj is not big enough
world.recv (0,ANY_TAG, abc_obj;);
std::cout << "Rank 1 OK!" << std::endl;
for (int i = 0; i < abc_obj;.data.size(); i++)
std::cout << i << "=" << abc_obj.data[i] << std::endl;
}
MPI_Finalize();
return 0;
}
© Stack Overflow or respective owner