inverse matrix Python3 mpi4py EOFError: EOF when reading a line

inverse matrix Python3 mpi4py EOFError: EOF when reading a line

我正在编写一段代码,使用 mpi4py 对矩阵求逆。我收到一个给我带来问题的错误。

我放置了 2 个默认变量 m, n,用户将数字插入到 rank 0 过程中以填充矩阵。进程 rank 0 将矩阵发送到 process 1process 2。在 process 1 中,我初始化矩阵和行列式,计算行列式,并将其发送回 rank 0。在process 2中,我初始化矩阵,求逆矩阵,将逆矩阵传回进程0.

process 0中,我收到了行列式和逆矩阵。如果行列式为0,则发送消息并退出程序(即使行列式为0也可以计算逆矩阵,但这是不正确的)。如果它不是 0,程序将打印逆矩阵。

请注意,我知道当前的实现并不是并行处理此问题的最佳方式,但我需要从某个地方开始。

行列式给了我一些错误,所以我将它初始化为一个包含 2 个元素的空 numpy 数组,其中第一个元素是行列式,第二个元素是 0。我稍微编辑了我的代码母语为英语,因此更容易阅读和理解,因此可能包含一些错误。

问题是当我想给用户写入矩阵大小的权限时。我查看了一些相关答案,并尝试使用 map()raw_input() 和其他选项,但 none 有效。

代码:

import numpy as np
from numpy.linalg import inv
from scipy.linalg import det
from mpi4py import MPI



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

m = int(input())
n = int(input())

if rank==0:
    matrix = np.zeros((m,n))
    determinant = np.zeros((2))
    for i in range (0,m):
        for j in range(0,n):
            print("Enter the value for the field: m = ", i+1, ", n = ", j+1,"\n")
            matrix[i][j] = float(input())

    comm.Send(matrix, dest=1, tag=0)
    comm.Send(matrix, dest=2, tag=0)
    comm.Recv(determinant, source=1, tag=0)
    comm.Recv(matrix, source=2, tag=0)
    if(determinant[0]==0):
        print("There is no inverse matrix since the determinant is 0!")
    else:
        print("Inverse matrix:\n",matrica)
elif rank==1:
    matrix = np.zeros((m,n))
    determinant = np.zeros((2))
    comm.Recv(matrix, source=0, tag=0)
    determinant = np.array([[det(matrix)],[0]])
    comm.Send(determinant, dest=0)
elif rank==2:
    matrix = np.zeros((m,n))
    comm.Recv(matrix, source=0, tag=0)
    matrix = inv(matrix)
    comm.Send(matrix, dest=0)
else:
    exit()

错误:

Traceback (most recent call last):
  File "lambda.py", line 13, in <module>
    m = int(input())
EOFError: EOF when reading a line
Traceback (most recent call last):
  File "lambda.py", line 13, in <module>
    m = int(input())
EOFError: EOF when reading a line

编辑: 我正在使用 PuTTY 从大学连接到 Debian OS。

解释器版本:sys.version_info(major=3, minor=5, micro=1, releaselevel='final', serial=0)

我输入命令行:mpi运行 -np 3 python3 lambda.py,我得到一个 EoFError 并插入数字直到它应该做某事的点,而不是这样做之后,它会继续 运行 作为一个无限循环。(在图片中看得更清楚) Picture

添加了跟踪输入的代码:

m = int(input("Enter number of rows, m = \n"))
n = int(input("Enter number of columns, n = \n"))

Picture 2

你的问题与矩阵无关:是你的线

m = int(input())

正在所有节点上发生。您需要在根节点上执行此操作,然后根据需要共享结果,比如通过广播。例如,如果我们从

开始
from mpi4py import MPI

x = input("enter x: ")

我们得到

(3.5) dsm@winter:~/coding/mpipar$ mpirun -np 3 python m1.py
enter x: enter x: Traceback (most recent call last):
  File "m1.py", line 3, in <module>
    x = input("enter x: ")
EOFError: EOF when reading a line
enter x: Traceback (most recent call last):
  File "m1.py", line 3, in <module>
    x = input("enter x: ")
EOFError: EOF when reading a line

注意有两个错误。但是如果我们保护它:

from mpi4py import MPI

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

x = None
if rank == 0:
    x = input("enter x: ")

x = comm.bcast(x, root=0)

print("rank", rank, "thinks x is", x)

然后我们得到

(3.5) dsm@winter:~/coding/mpipar$ mpirun -np 3 python m2.py
enter x: abc
rank 0 thinks x is abc
rank 1 thinks x is abc
rank 2 thinks x is abc