【问题标题】:inverse matrix Python3 mpi4py EOFError: EOF when reading a line逆矩阵Python3 mpi4py EOFError:读取一行时的EOF
【发布时间】:2016-02-21 17:38:51
【问题描述】:

我正在编写一段代码,它将使用mpi4py 对矩阵进行逆运算。我遇到了一个给我带来问题的错误。

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

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

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

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

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

代码:

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 操作系统。

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

我输入命令行:mpirun -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

【问题讨论】:

  • 您提到您已将代码从您的母语编辑为英语。您能否测试修改后的代码并确认它的行为与您的本机代码相同?否则我们排错也没有意义。
  • 你是如何实际执行代码的?你确定是Python3吗?
  • @MarcoBubi,您能否在您的解释器中运行以下命令,或者将其保存为 python 文件,运行它并告诉我们结果?它应该告诉我们您的翻译版本:import sys;print(sys.version_info)
  • sys.version_info(major=3, minor=5, micro=1, releaselevel='final', serial=0) 我正在使用腻子连接到大学的终端,我是确定是python3,我执行的coce是:mpirun -np 3 python3 lambda.py,因为lambda.py是文件名。 @ali_m 由于出现错误,因此没有输入,如果我设置 m = 3 和 n = 3,则它可以正常工作。
  • 执行应该在该行暂停,直到您输入输入并按回车键。你的情况发生了什么?您可以通过添加提示字符串来使这种行为更加明显,例如s = input("Enter the value for m: ").

标签: python python-3.x numpy matrix mpi4py


【解决方案1】:

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

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

【讨论】:

  • 是的,它有效!谢谢!我知道必须这样做,只是不知道怎么做,对 python 来说很新。我想我需要多检查一下文档。 :P
猜你喜欢
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多