我有一个维度为 (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 完成此操作?
请您参考如下方法:
创建一个发送缓冲区! 看这个例子:
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.]
