Simplest Python K-Way Merging Sort|最简单的Python k路归并排序
想做这个好长时间了,因为有一篇Dreamworks的论文《Coherent Out-of-Core Point-Based Global Illumination》提到了这个,一直没时间做,于是今天抽了十几分钟搞了一下,基于Python的,非常简单。由于基于点云的GI已经毫无疑问的不会使用了,所以Weta Digital的PantaRay和Dreamworks的这些方法都会直接宣布作废,权当一个过渡阶段(从REYES到纯RT)的折中方法了。
k是线程数目,n是每个线程处理的数字个数。可以直接替换为外部的IO访问代码,每一个线程读取外部待排序数据的索引,写出各自排序后的结果,主线程再来读取所有的文件做一次归并输出即可。这里用了Python的heapq,C++的话直接就是std::priority_queue,也非常方便。
1 import heapq 2 import random 3 import threading 4 5 # k threads 6 k = 4 7 8 # Generate n numbers for each thread to process. 9 n = 10 10 11 def GenerateNumbers(a, b): 12 numbers = [] 13 for i in xrange(n): 14 numbers.append(random.randint(a, b)) 15 return numbers 16 17 class SortingThread(threading.Thread): 18 def __init__(self): 19 super(SortingThread, self).__init__() 20 self.numbers = GenerateNumbers(1, 100) 21 22 def run(self): 23 self.numbers.sort() 24 25 if __name__ == '__main__': 26 # Parallel sort with multiple threads. 27 # 28 sortingThreads = [] 29 for i in xrange(k): 30 sortingThreads.append(SortingThread()) 31 for i in xrange(k): 32 sortingThreads[i].start() 33 for i in xrange(k): 34 sortingThreads[i].join() 35 print i , sortingThreads[i].numbers 36 37 # Use heap to output the final sorted list. 38 # We fill the heap with several numbers firstly 39 # 40 theHeap = [] 41 for i in xrange(1): 42 for t in xrange(len(sortingThreads)): 43 number = sortingThreads[t].numbers[i] 44 heapq.heappush(theHeap, number) 45 46 sortedNumbers = [] 47 48 # In the while loop, people may replace the code by reading file routines. 49 # Since IO is always the bottleneck, so single thread should be enough. 50 p = 1 51 while (len(theHeap) > 0): 52 if p < len(sortingThreads[t].numbers): 53 for t in xrange(len(sortingThreads)): 54 number = sortingThreads[t].numbers[p] 55 heapq.heappush(theHeap, number) 56 p += 1 57 sortedNumbers.append(heapq.heappop(theHeap)) 58 59 # Done 60 print sortedNumbers