Reputation: 2055
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;
}
Upvotes: 4
Views: 1876
Reputation: 4358
BOOST_IS_MPI_DATATYPE is only for fixed length data. Your array is not fixed length and this is why it fails.
Upvotes: 0
Reputation: 8273
You shouldn't send the vector
object itself in the message, as the receiver only needs its contents, and the internal state of the vector
would probably be all messed up after receiving anyway (memory addresses that were valid on the sending end probably won't be valid on the receiving end).
Instead, here's what you need to do:
vector
you want to send.At least, that's what you need to do with vanilla C++ and MPI. I'm not familiar with boost, so I don't know if it makes any of these steps easier.
Upvotes: 3