Let say I have a class named Node which makes the Node with x and y coordinate. Then I make a vector of objects from set of Node. now I want to send this vector of Node with its all properties to another processor.
I tried MPI_Send and it doesn't work in this case. how can I send a vector of objects in MPI?
This is the vector of Node;
I would assume whatever you send can't have pointers, since the receiving end might not be on the same process or even the same computer. So you'd need to serialize your data structure. For example, this:
1 2 3 4 5 6 7 8 9 10
std::vector<int *> data_structure;
int *one = newint(42);
int *two = newint(37);
int *three = newint(1024);
data_structure.push_back(one);
data_structure.push_back(one);
data_structure.push_back(two);
data_structure.push_back(one);
data_structure.push_back(three);
send(&data_structure[0], data_structure.size() * sizeof(int *)); //wrong
would have to be turned into this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
std::vector<int> ints;
ints.push_back(42);
ints.push_back(37);
ints.push_back(1024);
std::vector<size_t> offsets;
offsets.push_back(0);
offsets.push_back(0);
offsets.push_back(1);
offsets.push_back(0);
offsets.push_back(2);
std::vector<byte> buffer;
auto a = ints.size() * sizeof(int);
auto b = offsets.size() * sizeof(size_t);
buffer.resize(a + b);
memcpy(&buffer[0], &ints[0], a);
memcpy(&buffer[a], &offsetse[0], b);
send(&buffer[0], buffer.size());
You could also use a data exchange format such as JSON:
Plain Old Data (POD) means a simple struct or class with no embedded pointers or complex types. i.e. It may not contain for example a std::string, std::vector or other type which manages it's own memory.