Sending vector of vector with mpi

I want to send the vector<vector<int>> vec from processor 1 to processor 2.
Let say the vec {{1,2,4},{2,3,5},{3,4,6},{4,5,2},{3,4,6}}.

I send the vec like this

MPI_Send(&vec[0], Vector_size, MPI_INT, i_proc, 0, MPI_COMM_WORLD);

and receive in processor 2 like this

1
2
vector<vector<int>> vec;
MPI_Recv(&vec[0], Vector_size, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);


everytime I run this code I get weird error. "microsoft visual studion run time library, assertation failed and vector subscript out of range "

also I tried this one but no difference.

MPI_Send(&vec[0][0], Vector_size, MPI_INT, i_proc, 0, MPI_COMM_WORLD);

and receive in processor 2 like this

1
2
vector<vector<int>> vec;
MPI_Recv(&vec[0][0], Vector_size, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
Last edited on
A vector stores its elements indirectly. Therefore this approach is doomed to fail.
So how can I send a vector of vector using mpi send?
You can't. You need to flatten it to a 1-d vector, from whence you can send it via the vector's .data() buffer (as shown in your previous thread).

MPI sends 1-d contiguous data.
Thank you all
@lastchance after flatten a 2d vector the vector become 1d vector?I mean if before flattening we use i,j to access data inside the vector of vector,now we can access the data by just one iterator k? I want to send a vector of vector,let say VEC which has been flattened and I'm using VEC.data() in sending ind receiving buffer,but still I have problem
resabzr wrote:
I mean if before flattening we use i,j to access data inside the vector of vector,now we can access the data by just one iterator k?


Yes, your 1-d flattened array has single dimension ni x nj and you would get at the (i,j) element as
k = i * nj + j
The elements just come as a sequence of row after row.
Topic archived. No new replies allowed.