1

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

Yash
  • 689
  • 4
  • 11
  • 26

1 Answers1

7

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]]
K. Brafford
  • 3,755
  • 2
  • 26
  • 30
  • 2
    thank you very much. You solved my problem. By the way, This is also working: platform = cl.get_platforms(); my_gpu_devices = platform[0].get_devices(cl.device_type.GPU); ctx = cl.Context([my_gpu_devices[0]]) – Yash Oct 22 '12 at 07:18
  • 2
    Yash's version works for me too. K. Brafford, I think there's an error in your last code line. Actually I had to change the Context creation to ctx = l.Context(devices=[my_gpu_devices[0]]) – ms611 Mar 20 '14 at 21:41