ERROR: pyopencl: creating context for specific dev

2020-03-01 03:26发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2020-03-01 04:03

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]]
查看更多
登录 后发表回答