Changing the target device of a Queue

Hello all,
I would like to change the target device during runtime. For example, Let’s say I create a queue as -

cl::sycl::queue Queue(amd_selector)

and run a couple of kernels with this and now want to change my target device, let’s say an intel device. Is there any way I can re-initialize the same Queue as Queue(intel_selector). I essentially want to keep my Queue the same but make it target different devices at runtime like an AMD GPU in the start and shift to Intel iGPU and so on.

So is there any way I could possibly write a set_target_device method. I am not entirely sure if this is possible or not, but since it all compiles down to SPIR/SPIR-V, I could possibly take the intermediate representation and put that on different devices? (in a manner of speaking).

TIA

Hi @atharva362,

You can rebind queues in the sense that something like:

cl::sycl::queue Queue(amd_selector{});
Queue = cl::sycl::queue(intel_selector{});

will absolutely work, but I would strongly recommend keeping at least one queue per-device. The code I’ve posted is effectively destroying the old queue, creating a new one, then copying it into the old name you had (i.e. copy-assignment).

I would instead (say) create a queue for each device on the system, and dispatch kernels dynamically to those queues, depending on what you want to run on. You could even map between devices and queues or something for convenience (i.e. std::unordered_map<sycl::device, sycl::queue>).

Mutating the state of a queue doesn’t really map to the underlying APIs all that well, so it’s not something that is exposed. You are, in general, much better off creating the state you will need at the start of the program, and keeping it alive until you won’t be using it any more. Queues are also designed to be cheap to copy, so you can pass them to functions that want to enqueue kernels without too much worry. They are, however, expensive to create.

Hello @duncan, Thanks a lot for your replying.
I had read that creating a queue is an expensive task, but I still wanted to provide multiple vendor support.

Maybe creating multiple Queues(can prove to be expensive?), for now, I will just initialize a queue with a device and then reinitialize it using if requested.
Thanks a lot for your help.