我正在開發一個項目,將計算任務分配給多個python進程,每個進程與其自己的CUDA設備相關聯。OS X上的PyCuda /多處理問題10.8
當產卵的子過程,我用下面的代碼:
import pycuda.driver as cuda
class ComputeServer(object):
def _init_workers(self):
self.workers = []
cuda.init()
for device_id in range(cuda.Device.count()):
print "initializing device {}".format(device_id)
worker = CudaWorker(device_id)
worker.start()
self.workers.append(worker)
的CudaWorker在另一個文件中定義如下:
from multiprocessing import Process
import pycuda.driver as cuda
class CudaWorker(Process):
def __init__(self, device_id):
Process.__init__(self)
self.device_id = device_id
def run(self):
self._init_cuda_context()
while True:
# process requests here
def _init_cuda_context(self):
# the following line fails
cuda.init()
device = cuda.Device(self.device_id)
self.cuda_context = device.make_context()
當我運行在Windows 7或Linux的代碼,我沒有問題。當OSX 10.8.2,Cuda的5.0和PyCuda 2012.1上運行我的MacBook Pro的代碼我收到以下錯誤:
Process CudaWorker-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/Users/tombnorwood/pymodules/computeserver/worker.py", line 32, in run
self._init_cuda_context()
File "/Users/tombnorwood/pymodules/computeserver/worker.py", line 38, in _init_cuda_context
cuda.init()
RuntimeError: cuInit failed: no device
我有運行PyCuda腳本,而無需在我的Mac分叉新工藝沒有問題。我只在產生一個新的進程時纔會遇到這個問題。
有沒有人遇到過這個問題?
我懷疑這與以下事實有關: OS X有一大堆核心框架,不能在'fork'之後使用,並且PyCuda或CUDA本身都依賴於其中的一個... – abarnert
我實際上也認爲這種情況也是如此。有沒有辦法解決?這真的很煩人。 – tnorwood
如果是這樣,最簡單的解決方法是執行一個新的Python解釋器,而不是繼續使用分叉的解釋器。有一個補丁版本的'multiprocessing'圍繞某處這麼做。 (它可能有一天會被添加到主幹中作爲一個選項,但它永遠不會是默認的,因爲這會使得OS X'multiprocessing'比POSIX更像Windows。)如果你想要,而且你找不到它或者弄清楚如何自己做(實際上這很簡單),我可以爲它挖掘它。 – abarnert