并行计算 - Shuffle

Parallel Computing - Shuffle

我正在寻找并行洗牌数组。我发现做一个类似于双调排序但随机(50/50)重新排序的算法会导致均匀分布,但前提是数组是 2 的幂。我考虑过 Yates Fisher Shuffle 但我可以看不出如何并行化它以避免 O(N) 计算。

有什么建议吗?

谢谢!

最近有一篇关于此的清晰明确的论文 here 和参考文献,尤其是 Shun et al 2015 值得一读。

但基本上您可以使用 sort -R 中使用的相同类型的方法来执行此操作:通过为每一行提供一个随机键值并根据该键进行排序来进行随机播放。有很多方法可以很好地进行并行分布式排序。

这是 python + MPI 中使用奇偶排序的基本版本;如果 P 是处理器的数量,它会经过 P 个通信步骤。你可以做得更好,但这很容易理解; this question.

中对此进行了讨论
from __future__ import print_function
import sys
import random
from mpi4py import MPI

comm = MPI.COMM_WORLD

def exchange(localdata, sendrank, recvrank):
    """
    Perform a merge-exchange with a neighbour;
    sendrank sends local data to recvrank,
    which merge-sorts it, and then sends lower
    data back to the lower-ranked process and
    keeps upper data
    """
    rank = comm.Get_rank()
    assert rank == sendrank or rank == recvrank
    assert sendrank < recvrank

    if rank == sendrank:
        comm.send(localdata, dest=recvrank)
        newdata = comm.recv(source=recvrank)
    else:
        bothdata = list(localdata)
        otherdata = comm.recv(source=sendrank)
        bothdata = bothdata + otherdata
        bothdata.sort()
        comm.send(bothdata[:len(otherdata)], dest=sendrank)
        newdata = bothdata[len(otherdata):]
    return newdata

def print_by_rank(data, rank, nprocs):
    """ crudely attempt to print data coherently """
    for proc in range(nprocs):
        if proc == rank:
            print(str(rank)+": "+str(data))
            comm.barrier()
    return

def odd_even_sort(data):
    rank = comm.Get_rank()
    nprocs = comm.Get_size()
    data.sort()
    for step in range(1, nprocs+1):
        if ((rank + step) % 2) == 0:
            if rank < nprocs - 1:
                data = exchange(data, rank, rank+1)
        elif rank > 0:
            data = exchange(data, rank-1, rank)
    return data

def main():
    # everyone get their data
    rank = comm.Get_rank()
    nprocs = comm.Get_size()
    n_per_proc = 5
    data = list(range(n_per_proc*rank, n_per_proc*(rank+1)))

    if rank == 0:
        print("Original:")
    print_by_rank(data, rank, nprocs)

    # tag your data with random values
    data = [(random.random(), item) for item in data]

    # now sort it by these random tags
    data = odd_even_sort(data)

    if rank == 0:
        print("Shuffled:")
    print_by_rank([x for _, x in data], rank, nprocs)

    return 0


if __name__ == "__main__":
    sys.exit(main())

运行 给出:

$ mpirun -np 5 python mergesort_shuffle.py
Original:
0: [0, 1, 2, 3, 4]
1: [5, 6, 7, 8, 9]
2: [10, 11, 12, 13, 14]
3: [15, 16, 17, 18, 19]
4: [20, 21, 22, 23, 24]

Shuffled:
0: [19, 17, 4, 20, 9]
1: [23, 12, 3, 2, 8]
2: [14, 6, 13, 15, 1]
3: [11, 0, 22, 16, 18]
4: [5, 10, 21, 7, 24]