1

I am trying to write a code in C++, but after some search on the internet, I found one OpenCL based code is doing exactly the same thing as I want to do in C++. But since this is the first time I see a OpenCL code, I don't know how to change the following functions into c++:

const __global float4 *in_buf;

int x = get_global_id(0);
int y = get_global_id(1);

float result = y * get_global_size(0);

Is 'const __global float4 *in_buf' equivalent to 'const float *in_buf' in c++? And how to change the above other functions? Could anyone help? Thanks.

2 Answers 2

3

In general, you should take a look at the OpenCL specification (I'm assuming it's written in OpenCL 1.x) to better understand functions, types and how a kernel works.

Specifically for your question:

get_global_id returns the id of the current work item, and get_global_size returns the total number of work items. Since an OpenCL work-item is roughly equivalent to a single iteration in a sequential language, the equivalent of OpenCL's:

int x = get_global_id(0);
int y = get_global_id(1);
// do something with x and y
float result = y * get_global_size(0);

Will be C's:

for (int x = 0; x < dim0; x++) {
    for (int y = 0; y < dim1; y++) {
        // do something with x and y
        float result = y * dim0;
    }
}

As for float4 it's a vector type of 4 floats, roughly equivalent to C's float[4] (except that it supports many additional operators, such as vector arithmetic). Of course in this case it's a buffer, so an appropriate type would be float** or float[4]* - or better yet, just pack them together into a float* buffer and then load 4 at a time.

Feel free to ignore the __global modifier.

Sign up to request clarification or add additional context in comments.

2 Comments

+1; but I'd say a float4 is more closely equivalent to std::array<float, 4>- it can be assigned and copied.
Maybe it is equivalent to __m128 intrinsic variable?
2

const __global float4 *in_buf is not equivalent to const float *in_buf. The OpenCL uses vector variables, e.g. floatN, where N is e.g. 2,4,8. So float4 is in fact struct { float w, float x, float y, float z} with lot of tricks available to express vector operations.

get_global_id(0) gives you the iterator variable, so essentially replace every get_global_id(dim) with for(int x = 0; x< max[dim]; x++)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.