Concatenate numpy arrays with Allgather?

443 views
Skip to first unread message

ar...@nervanasys.com

unread,
Oct 24, 2014, 4:11:25 AM10/24/14
to mpi...@googlegroups.com
I have the following code that I am running on 4 processes:


import numpy as np
from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
data = np.hstack((rank*np.ones((1,2)), (rank+.5)*np.ones((1,2))))
data_ = np.zeros((2,8))

comm.Allgather(data, data_)
print rank, data_


I would like to see the output:
0   0 1    1    2    2    3    3
.5 .5 1.5 1.5 2.5 2.5 3.5 3.5

Instead, I see:

0 [[ 0.   0.   0.5  0.5  1.   1.   1.5  1.5]

 [ 2.   2.   2.5  2.5  3.   3.   3.5  3.5]]

Is there a way to change the code to see the desired output?

Thanks!



Yury V. Zaytsev

unread,
Oct 24, 2014, 6:44:21 AM10/24/14
to mpi...@googlegroups.com
On Fri, 2014-10-24 at 01:11 -0700, ar...@nervanasys.com wrote:
>
> Is there a way to change the code to see the desired output?

Maybe you should then change hstack to vstack?

--
Sincerely yours,
Yury V. Zaytsev


ar...@nervanasys.com

unread,
Oct 24, 2014, 11:49:30 AM10/24/14
to mpi...@googlegroups.com
You are right about using vstack instead of hstack, but the output of the Allgather is still the same in both cases.

Yury V. Zaytsev

unread,
Oct 24, 2014, 3:54:15 PM10/24/14
to mpi...@googlegroups.com
On Fri, 2014-10-24 at 08:49 -0700, ar...@nervanasys.com wrote:
> You are right about using vstack instead of hstack, but the output of
> the Allgather is still the same in both cases.

Oh, I see; I'm not sure if there is a good solution to this problem. The
easiest fix that I can think of is to simply transpose the blocks, but
maybe there is a better way to do it by playing more with row/column
order and transpositions.
test_gather.py

ar...@nervanasys.com

unread,
Oct 24, 2014, 4:13:04 PM10/24/14
to mpi...@googlegroups.com
Thanks. For now, I am using a combination of resizing the data_ buffer and np.hstack and np.split, but it would be nice if MPI had something more direct.

import numpy as np
from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
data = np.vstack((rank*np.ones((1,2)), (rank+.5)*np.ones((1,2))))
data_ = np.zeros((8,2))

print rank, data

comm.Allgather(data, data_)
if rank==0:
    print rank, data_
    print np.hstack(np.split(data_, 4))
Reply all
Reply to author
Forward
0 new messages