0

OpenCL 1.2: I am running a sequence of kernels in two separate command queues so that they can be scheduled concurrently, synchronising at the end. There are separate data buffers been used in these queues.

I started by using the same kernel objects in the separate queues. However, that seemed to get the data buffers all mixed up between the two queues. I looked up but could not find anything in the references regarding this. In particular, I see nothing explicitly mentioned in the clSetKernelArgs() page regarding this. There is a note saying

Users may not rely on a kernel object to retain objects specified as argument values to the kernel.

which I am not sure applies to this case.

So my devised solution is to inline the kernel code and make two separate kernel functions that call this code for each one of the kernels in the two parallel queues. This fixed the issue.

However: (1) I was not happy with this, and so I tested again on different devices. Data buffers are jumbled up in the Intel HD630 GPU, but not in the AMD Radeon Pro 560 (where all is good). (2) This solution does not scale. If I want to implement a larger amount of task parallelism using the same context, doing separate kernels for each parallel stream is no good. I have not tested two separate contexts, I supposed it would work, but then it would mean copying data from one context to the other at the sync point, which defeats the whole exercise.

Has anyone tried this, or have any insights on the issue?

1 Answer 1

0

So my devised solution is to inline the kernel code and make two separate kernel functions that call this code for each one of the kernels in the two parallel queues

You don't need to do that. You can achieve the same effect by simply creating multiple cl_kernel objects from the same cl_program. Simply call clCreateKernel multiple times with the same arguments. If it helps, you can think of cl_kernel as a struct that contains 1) a pointer to some executable code, and 2) storage for arguments. A cl_kernel does not actually "own" the code; the program does.

However, that seemed to get the data buffers all mixed up between the two queues

Are you aware that there are no implicit guarantees on the order of command execution across queues ?

Example: if you have one in-order cl_command_queue, enqueing commands A and then B in it guarantees A will execute before B. However, if you have two command queues C1 and C2, if you enqueue A into C1, then enqueue B into C2, then B can execute before A (or A before B).

If you need to enforce a specific order, you must use events. Like this:

cl_event ev;
cl_kernel A, B;
cl_command_queue C1, C2;
...
clEnqueueNDR(C1, A, ... , &ev);
clEnqueueNDR(C2, B, ..., 1, &ev, NULL);
clReleaseEvent(ev);

This guarantees B executes after A.

1
  • There is no need to enforce execution order, only at the sync point, which is OK, as I have already put in synchronisation there. I should have mentioned that I tried two kernels from the same code but was getting segfaults somewhere (I think at the setArg calls). However, I did not insist, so I should go back and try it again.
    – vlazzarini
    Sep 18, 2019 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.