我试图在Python中使用MPI(mpi4py)进行一些图像处理。到目前为止,这是我的代码:
from mpi4py import MPI
from PIL import Image
comm = MPI.COMM_WORLD
size = comm.size #number of processors
rank = comm.rank #calling process rank
root = 0
if rank == 0:
print "rank = 0 \n"
img = Image.open('dog.jpg')
width, height = img.size
pix = list(img.getdata())
for i in range(len(pix)):
pix[i] = list(pix[i])
chunks = [[] for _ in range(size)]
for i, chunk in enumerate(pix):
chunks[i % size].append(chunk)
else:
print "rank != 0"
data = comm.scatter(chunks, root)
#do some pixel manipulations
data = comm.gather(data, root)
for i in range(len(pix)):
pix[i] = tuple(data[0][i])
image = Image.new("RGB", (width, height))
image.putdata(pix)
image.save("test.jpg")
当我尝试使用
运行它时mpirun -np 1 python parallel.py
它运作正常。但如果我尝试增加处理器数量,我会收到以下错误
mpirun -np 3 python parallel.py
rank = 1
rank = 2
rank = 0
Traceback (most recent call last):
File "parallel.py", line 21, in <module>
data = comm.scatter(chunks, root)
NameError: name 'chunks' is not defined
Traceback (most recent call last):
File "parallel.py", line 21, in <module>
data = comm.scatter(chunks, root)
NameError: name 'chunks' is not defined
我正在使用Virtual Box在Ubuntu虚拟机上运行代码。 感谢您的帮助!
答案 0 :(得分:1)
这不是MPI问题。 chunks
在条件的else:
分支的过程中未定义,即在除了等级0之外的所有过程中。只需在那里添加chunks = None
:
...
if rank == 0:
...
else:
print "rank != 0"
chunks = None
...
它适用于一个进程,因为在这种情况下只有一个等级(0),而且从不采用else分支。