How can I send part of an array with scatter?

后端 未结 1 936
一个人的身影
一个人的身影 2021-01-03 09:47

I am teaching myself the Python mpi4py module for programming in multiple processes. I have written the following piece of code to practice scatter.

from mpi         


        
相关标签:
1条回答
  • 2021-01-03 10:20

    It seems that comm.scatter can not take count as an argument and expects a list of exactly comm.size elements as data to be scattered; so you need to distribute your data between processes yourself. Something like this will do:

    if rank == 0:
        data = [i for i in range(8)]
    # dividing data into chunks
        chunks = [[] for _ in range(size)]
        for i, chunk in enumerate(data):
            chunks[i % size].append(chunk)
    else:
        data = None
        chunks = None
    data = comm.scatter(chunks, root=0)
    print str(rank) + ': ' + str(data)
    
    [physics@tornado] ~/utils> mpirun -np 3 ./mpi.py 
    2: [2, 5]
    0: [0, 3, 6]
    1: [1, 4, 7]
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题