Yas*_*ash 0 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])
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
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__'
Run Code Online (Sandbox Code Playgroud)
如果我使用以下程序编译并执行没有任何错误和警告:
ctx = cl.create_some_context()
Run Code Online (Sandbox Code Playgroud)
但是每次执行程序时我都必须手动选择设备类型.我可以设置以下环境变量
PYOPENCL_CTX='0'
Run Code Online (Sandbox Code Playgroud)
使用此功能,我将无法根据要求为不同的设备创建上下文.对于我创建的所有上下文,它将默认设置为设备0.
有人可以帮我解决这个问题.
谢谢
根据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)
Run Code Online (Sandbox Code Playgroud)
它应该工作.如果您确实希望将选择限制为仅一个设备,则可以操作my_gpu_devices
列表,例如:
my_gpu_devices = [platform[0].get_devices(device_type=cl.device_type.GPU)[0]]
Run Code Online (Sandbox Code Playgroud)