Sending dynamic arrays in MPI, C++

My task is to measure time of communication betweeen two processes. I want to send 4,8,...,1000,...., 10000 bytes of data and measure time it takes to send and receive back the message. So i figured out that i will send an array of shorts.

When i send array initialised like that:

mpi::communicator world;
short message[100000];
....
world.send(1,0, message);

time seems to be ok, and I can see a time difference between message[100000] and [1000]

But I want to allocate array dynamically like that:

short *message = new short[100000];
...
world.send(1,0, *message);

It seems like the second send is always sending the same amount of data no matter what size the array will be.

So my question is, how to send a dynamically allocated array?

Answers


In the second case message is of type short * and *message dereferences to a scalar short, i.e. to the first element of the array only. Use

world.send(1, 0, message, n);

instead and vary the value of n. It should also (probably) work if you cast the pointer to a pointer to an array and then dereference it:

world.send(1, 0, *reinterpret_cast<int(*)[100]>(message));

The int(*)[100] type is a pointer to an integer array with 100 elements.


Need Your Help

Why is ContentDialog and Prism Navigation blowing up my UWP app?

c# windows-runtime win-universal-app prism

I am showing a content dialog on one of my sessionstateaware pages, I close the dialog fine and can reopen another on the same page as many times as I want. When I navigate away and come back and t...

Webcam streaming in Django project

python django webcam

I am currently working on a Django project and would like to add the ability for uses to enter a video conference with each other using their webcams. I understand html5 has capabilities for this, ...