简体   繁体   English

MPI4PY:散布矩阵

[英]MPI4PY: Scatter a matrix

I am using MPI4PY to scatter n/p columns to two input data processes.我正在使用 MPI4PY 将 n/p 列分散到两个输入数据进程。 However, I am unable to send the columns as I would like.但是,我无法按照我的意愿发送列。 What changes do I have to make to the code in order to get the result reported in the final comment?为了在最终评论中报告结果,我必须对代码进行哪些更改?

The matrix is:矩阵是:

[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
[13, 14, 15, 16]

Then, n=4 and p=2.然后,n=4 和 p=2。 Each process will have 2 columns respectively.每个进程将分别有 2 列。

This is my code:这是我的代码:

# Imports
from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
size = comm.Get_size() 
rank = comm.Get_rank()

rows = 4
num_columns = rows/size

data=None

if rank == 0:
  data = np.matrix([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])

recvbuf = np.empty((rows, int(num_columns)), dtype='int')
comm.Scatterv(data, recvbuf, root=0)
print('Rank: ',rank, ', recvbuf received:\n ',recvbuf)

I get the following output:我得到以下 output:

Rank:  0 , recvbuf received:
[[1 2]
[3 4]
[5 6]
[7 8]]
Rank:  1 , recvbuf received:
[[ 9 10]
[11 12]
[13 14]
[15 16]]

I want to get the following output instead:我想得到以下 output 代替:

Rank:  0 , recvbuf received:
[[1 2]
[5 6]
[9 10]
[13 14]]
Rank:  1 , recvbuf received:
[[ 3 4]
[7 8]
[11 12]
[15 16]]

I think this code does what you are looking for.我认为这段代码可以满足您的需求。 The issue here is that Scatterv doesn't care about numpy array shape at all, it just considers a linear block of memory containing your values.这里的问题是 Scatterv 根本不关心 numpy 数组形状,它只考虑包含您的值的 memory 线性块。 Therefore, the simplest approach is to manipulate your data into the correct order beforehand.因此,最简单的方法是事先将数据处理成正确的顺序。 Note that send_data is a 1D array, but this doesn't matter because Scatterv doesn't care.请注意, send_data是一维数组,但这并不重要,因为 Scatterv 不在乎。 At the other end, the shape of recvbuf is already defined, and Scatterv just fills it up from the 1D input received.在另一端,recvbuf 的形状已经定义,Scatterv 只是从接收到的一维输入中填充它。

# Imports
from mpi4py import MPI
import numpy as np

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

rows = 4
num_cols = rows/size

send_data=None

if rank == 0:
  data = np.matrix([[1, 2, 3, 4],
                    [5, 6, 7, 8],
                    [9, 10, 11, 12],
                    [13, 14, 15, 16]])

  # Split into sub-arrays along required axis
  arrs = np.split(data, size, axis=1)

  # Flatten the sub-arrays
  raveled = [np.ravel(arr) for arr in arrs]

  # Join them back up into a 1D array
  send_data = np.concatenate(raveled)


recvbuf = np.empty((rows, int(num_cols)), dtype='int')
comm.Scatterv(send_data, recvbuf, root=0)

print('Rank: ',rank, ', recvbuf received:\n ',recvbuf)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM