J'essaie de faire une manipulation d'image en utilisant MPI (mpi4py) en Python. Ceci est mon code à ce jour:Manipulation d'image en Python en utilisant MPI
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")
Lorsque je tente de l'exécuter avec
mpirun -np 1 python parallel.py
cela fonctionne bien. Mais si je tente d'augmenter le nombre de processeurs, je reçois l'erreur suivante
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
Je l'exécution du code sur une machine virtuelle Ubuntu en utilisant Virtual Box. Appréciez votre aide!