ERROR: pyopencl: creating context for specific device

人走茶凉 提交于 2019-11-30 16:20:05

问题


I want to create context for specific device on my platform. But I am getting an error.

Code:

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

The error i am getting:

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'

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.

Thank you


回答1:


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)

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


来源:https://stackoverflow.com/questions/12995859/error-pyopencl-creating-context-for-specific-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!