Pass condition as argument to an OpenCL kernel

I'm trying to write an OpenCL kernel that takes a condition, i.e., a boolean value, as an argument. My objective with this is to write a kernel that behaves in the following way:

__kernel void operation(
    __global double *vec,
    const bool condition,  // is this possible?
    const int n)
{
    // ...
    if(condition){
        // do this ...
    }
    else{
        // do that ...
    }
}

I've found this thread that says it is not possible to do what I want, but the discussion is a bit outdated and I was wondering if anything has changed.

So this is basically it. I'm completely open for suggestions.

Thanks in advance!

Upvotes: 0

Views: 312

Answers (1)

ProjectPhysX
ProjectPhysX

Reputation: 5764

Basically you can do exactly that. However bool (1-bit) is not allowed as kernel parameter. You have to use char (8-bit) or int (32-bit) instead as data type for the condition variable. If you set the condition value to 0 (false) or 1 (true), you can just plug it into the if(condition) as is.

I assume condition in your case is the same value for all threads. For performance reasons, I would advise to make two separate kernels, one for the if branch and one for the else branch, and move the condition to the C++ side to enqueue either of the two kernels.

Upvotes: 2

Related Questions