I want to send a struct that has a vector property.
typedef struct {
int id;
vector neighbors;
} Node;
I know i have to
Note that internally a vector
looks something like this:
struct vector {
size_t size;
size_t alloc_size;
int* data;
};
Thus, if you try to send the struct as puelo suggested, it won't access the actual data underlying the vector, but instead send the size
fields, the data
pointer and any data the follows these items in memory, which most likely will result in invalid memory access. The actual data in the vector won't be send like this.
Generally, MPI does not work well for sending structures that contain pointers to more data. Instead you should try to think about how to send the actual underlying data itself.
MPI communication will be easier and more efficient, if you can represent your data in a contiguous fashion.
Your struct Node
looks like you are trying to represent a node in a graph. You could for instance represent your graph data in a adjacency array format, where all neighbor ids are represented in one single large vector. Think of it like a concatenation of all neighbors
vectors from your previous struct Node
. For each node, you will then save the offset into the new neighbors
vector.
std::vector node_ids(num_nodes);
std::vector nodes_offsets(num_nodes);
std::vector neighbors(num_edges);
// neighbors for node i are accessible via:
for (int j = node_offsets[i]; j <= node_offsets[i+1]-1; ++j) {
int neighbor = neighbors[j];
// ...
}
You can then send/receive this information with MPI easily:
MPI_Send(&neighbors[0], MPI_INT, neighbors.size(), ...);
When working with MPI, finding a good data layout for your data is among the most important steps while implementing your algorithms.