【问题标题】:Python: Sending discontinuous data with mpi4pyPython:使用 mpi4py 发送不连续的数据
【发布时间】:2012-09-12 18:15:26
【问题描述】:

我有一个维度为 (N,M) 的 C 阶矩阵

mat = np.random.randn(N, M)

我想通过持久的 MPI 请求将 发送到另一个节点。但是,使用mpi4py

sreq = MPI.COMM_WORLD.Send_Init((mat[:,idx], MPI.DOUBLE), send_id, tag)

由于切片不连续而失败。有人可以建议一种解决方法吗?我相信 C 语言中 MPI_Type_vector 允许在创建类型时指定 stride。我如何使用mpi4py 完成此操作?

【问题讨论】:

  • 如果numpy 矩阵的存储方式与 C 中的 2D 数组相同(行在内存中连续分配),您可以像在 C 中那样构造一个跨步向量类型。

标签: python mpi mpi4py memory-layout


【解决方案1】:

创建一个发送缓冲区! 看看这个例子:

  1 #!/usr/bin/python2
  2 # -*- coding: utf-8 -*-
  3
  4 from mpi4py import MPI
  5 import numpy as np
  6
  7 comm = MPI.COMM_WORLD
  8 rank = comm.Get_rank()
  9
 10 matrix = np.empty((5, 10), dtype='f')
 11 for y in xrange(len(matrix)):
 12     for x in xrange(len(matrix[0])):
 13         matrix[y,x] = rank * 10 + x * y
 14
 15 sendbuf = np.empty(5, dtype='f')
 16
 17 #column 1
 18 sendbuf[:] = matrix[:,1]
 19
 20 result = comm.gather(sendbuf, root=0)
 21
 22 if rank == 0:
 23     for res in result:
 24         print res

这会给你:

$ mpirun -np 4 column.py
[ 0.  1.  2.  3.  4.]
[ 10.  11.  12.  13.  14.]
[ 20.  21.  22.  23.  24.]
[ 30.  31.  32.  33.  34.]

【讨论】:

    猜你喜欢
    • 2016-03-22
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多