2017-05-30 50 views
0

我正在解決遊戲職位。我以3000人左右的組合生成它們。在我最快的機器上,每個可以從6分鐘起。在我決定使用不同的方法之前,我有一個運行了兩個星期,所以我想在一個小時之後放棄(按處理器速度縮放),然後再細分問題。如何在多核機器上管理Python 3中的多個問題

代碼本身很小。它可以選擇使用一個高速緩存,約2倍加速,但當然緩存可以使用大量內存。

現在,我正在手動管理所有這些,並且我無法接近保持機器繁忙。

我有2個Core i-7,一個16核Xeon(超過1000個nVidia GPU內核)和一些小型機器,但我很樂意從一臺計算機解決方案開始。

有一個sqlite3數據庫已經在使用來保存結果,它也可以是解決方案的一部分。

有足夠的Python工具讓我想知道要探索哪一個。我一直在尋找線程,多進程,子進程和pyzmq包。

坦率地說,我對這個過度思考,並可能會使用一些方向。

+0

具體什麼你問?你可能想看看[問]。 –

+0

如果我足夠熟悉,我會是。我試圖給出我想要做的事情的一般描述,這些工具爲實現它提供了一些希望。 – 4dummies

回答

1

我不熟悉GPU編程,但在CPU端,您可能需要在os模塊中分叉。當你的程序分叉時,會發生什麼情況,在概念上,最終會產生程序原始實例(父進程)的克隆(子)。如果你運行demo_forking函數,你會發現你可以飽和最多HELPERS核心。希望這足以起步;你必須弄清楚如何在多個進程中分配你的問題。總體來說,我一直與Python和桌面編程脫節了一段時間,但由於其一些侷限性,Python不會成爲我編程多核系統的首選。話雖如此,如果你已經知道Python,那麼也許它是最適合你的應用的。

#!/usr/bin/env python3 
import time 

HELPERS = 10 # If set to the number of CPU cores or greater, can saturate the core. 

def helper(i): 
    '''Do part of the work. 

    This thread just eats up CPU cycles for i-seconds.''' 
    print('Working on part', i, 'of the task.') 
    start = time.time() 
    while time.time() < start + i: 
     pass 

    print('Done part', i, ' of the task.') 

def demo_threading(): 
    '''Use threads system to run tasks in parallel. 

    Because Python is really only single-threaded, this doesn't saturate a 
    multi-core system; it can only saturate a single core.''' 
    import threading 

    threads = [] 
    for i in range(HELPERS): 
     thd = threading.Thread(target=helper, args=[i]) 
     thd.start() 

    # Wait for all the helper threads to stop. 
    [thd.join() for thd in threads] 

def demo_forking(): 
    '''Use forking to run tasks in parallel.''' 
    import os 

    processes = [] 
    for i in range(HELPERS): 
     process = os.fork() 
     # We now have two identical copies of the process. 
     if process == 0: 
      # This is the child process. 
      helper(i) 
      return   # Don't spawn more processes from the child process. 
     else: 
      # This is the parent process. 
      processes.append(process) 

    # Wait for all the child processes to stop. 
    [os.waitpid(pid, 0) for pid in processes] 

http://python-notes.curiousefficiency.org/en/latest/python3/multicore_python.html

+0

'multiprocessing'封裝了分叉動作,使您不必進行系統調用和檢查進程ID。 –