2012-10-21 24 views
0

我想爲我的平臺上的特定設備創建上下文。但我得到一個錯誤。錯誤:pyopencl:爲特定設備創建上下文

代碼:

import pyopencl as cl 
platform = cl.get_platforms() 
devices = platform[0].get_devices(cl.device_type.GPU) 
ctx = cl.Context(devices[0]) 

我得到的錯誤:

Traceback (most recent call last): 
    File "D:\Programming\Programs_OpenCL_Python\Matrix Multiplication\3\main3.py", line 16, in <module> 
    ctx = cl.Context(devices[0]) 
AttributeError: 'Device' object has no attribute '__iter__' 

的程序編譯,如果我使用沒有任何錯誤和警告執行:

ctx = cl.create_some_context() 

但我會每次使用此功能執行程序時都必須手動選擇設備類型。 我可以設置以下環境變量

PYOPENCL_CTX='0' 

使用此我將無法創建基於需求提供不同的設備上下文。對於我創建的所有上下文,它將默認設置爲設備0。

有人可以幫助我解決這個問題。

謝謝

回答

5

按照PyOpenCL資料,Context需要的設備列表,而不是特定的設備。

如果你改變你的上下文創建的代碼如下:

platform = cl.get_platforms() 
my_gpu_devices = platform[0].get_devices(device_type=cl.device_type.GPU) 
ctx = cl.Context(devices=my_gpu_devices) 

它應該工作。如果你真的想限制選擇只有一個設備,你可以操縱my_gpu_devices列表,例如:

my_gpu_devices = [platform[0].get_devices(device_type=cl.device_type.GPU)[0]] 
+2

非常感謝你。你解決了我的問題。順便說一下,這也是工作: platform = cl.get_platforms(); my_gpu_devices = platform [0] .get_devices(cl.device_type.GPU); ctx = cl.Context([my_gpu_devices [0]]) – Yash

+1

Yash的版本也適用於我。 K. Brafford,我認爲你的最後一行代碼有錯誤。實際上,我必須將上下文創建更改爲ctx = l.Context(devices = [my_gpu_devices [0]]) – Marius