I searched to send a set object and the closest I found was with vector (it's different and don't work with set).
How can I send a set object in MPI_Send? (without using boost library) Anyone can put a simple example?
I searched to send a set object and the closest I found was with vector (it's different and don't work with set).
How can I send a set object in MPI_Send? (without using boost library) Anyone can put a simple example?
Whether you have to write a complex data structure to a file or over the network in MPI, the issues are the same; you have to extract the data into "Plain Old Data" (POD), save it, and then output it, and likewise be able to unpack the saved data into the same sort of structure. In general, this is called serialization.
For any given structure you can always write your own routines for doing this, but in C++, there's a framework in Boost called the Boost Serialization Library for doing this; it's a bit heavyweight for this example but it will work for most (all?) STL containers and there are hooks for adding support for your own classes.
The main trick with using Boost for this is that the Boost libraries (and all the examples) make it very easy to write the data to a file, but here you want to keep it in memory and send/receive it over the network; that means jumping through a couple more hoops to make sure the serialization is into an array you can access. This SO answer is very helpful in this regard.
So a complete working example looks like this:
Running gives:
If you really don't want to use Boost, since you can't actually see directly into the set data structure, there's not much alternative but to extract the data into an array or vector, and send the data that way:
And running gives:
But really, if you're going to be doing this with other types of object as well, Boost is the way to go.