问题描述
我想为平台上的特定设备创建上下文.但是我遇到了错误.
I want to create context for specific device on my platform. But I am getting an error.
代码:
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__'
如果我使用该程序,则可以在没有任何错误和警告的情况下进行编译和执行:
The program compiles and executes without any errors and warnings if i use:
ctx = cl.create_some_context()
但是,每次使用此功能执行程序时,我都必须手动选择设备类型.我可以设置以下环境变量
But I will have to manually select the device type every-time I execute the program using this function.I can set the following environmental variable
PYOPENCL_CTX='0'
使用此方法,我将无法根据需求为可用的不同设备创建上下文.对于我创建的所有上下文,默认情况下它将设置为设备0.
Using this I will not be able to create contexts for different devices available based on the requirement. It will be by default set to device 0 for all the contexts that I create.
有人可以帮我解决这个问题.
Can someone please help me with this problem.
谢谢
推荐答案
根据PyOpenCL文档,上下文获取设备列表,而不是特定设备.
According to the PyOpenCL documentation, Context takes a list of devices, not a specific device.
如果将上下文创建代码更改为此:
If you change your context creation code to this:
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
列表,例如:
It should work. If you really want to limit the choice to only one device, you can manipulate the my_gpu_devices
list, for instance:
my_gpu_devices = [platform[0].get_devices(device_type=cl.device_type.GPU)[0]]
这篇关于错误:pyopencl:为特定设备创建上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!