错误:pyopencl:为特定设备创建上下文

时间:2012-10-21 07:29:05

标签: python opencl 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。

有人可以帮我解决这个问题。

谢谢

1 个答案:

答案 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]]