3Dave
3Dave

Reputation: 29051

OpenCL - Initializing program-scope variables from the host?

I have some general parameters declared as a global (__constant) struct, like so:

typedef struct
{
    int a;
    int b;
    float c;
    /// blah blah

} SomeParams;

__constant SomeParams Parameters;

in the kernel, I need to use it like so:

__kernel void Foo()
{
    int a = Parameters.a;
    /// do something useful...
}

I'm not sure how I can initialize the value of Parameters from the host before I execute the kernel.

I have no problem creating buffers, etc for kernel arguments, but since this isn't a kernel argument, what do I need to do?

I'm using the Cloo C#/OpenCL bindings, but even a raw CL API would be helpful.

Upvotes: 3

Views: 3528

Answers (1)

Jonathan DeCarlo
Jonathan DeCarlo

Reputation: 2818

As far as I know (but I wouldn't swear by this), you can't initialize variables from the host code that are declared in that way (with one exception, see below). You could declare a variable and initialize it like this:

__constant float pi = 3.14f;

You could also do something like this:

Kernel: __constant float width = WIDTH

Host: Build the kernel with a -D build parameter defining the value of WIDTH.

What I have done in the past is have the constant variable as a kernel parameter.

__kernel void Foo(__constant SomeParams Parameters)
{
    int a = Parameters.a;
    /// do something useful...
}

Then you can allocate and set the value just like any other kernel argument.

Upvotes: 6

Related Questions