Reputation: 125
I am very new to OpenCL (started today). I am having a hard time in realizing the kernel code for adding numbers of an array in the following fashion: If A[]=[1,2,3,4,5,6,7,8,9,10], then the sum should be sum[]=[4,10,16];
i.e, sum[i]=A[i]+A[i+2];
i=i+3;
I have tried the following code but it does not seem to work.
std::string kernel_code =
" void kernel simple_add(global const int* A, global const int* B, global int* C){ "
" int x =0;"
" int i =get_global_id(0);"
" SUM[x]=A[i]+A[i+2];"
" i=i+3;
" x++;"
" } ";
I am certain this is not the way to do it. Suggestions in this regard would be appreciated.
Upvotes: 2
Views: 195
Reputation: 5087
You should calculate i as if the work item were the only one in the set. gid is the global id of the work item, and i needs to be three times that. You also don't need to modify gid or i after they are used. This is not optimal, but it will get you the correct answer.
void kernel simple_add(global const int* A, global const int* SUM){
int gid = get_global_id(0);
int i = gid * 3;
SUM[gid]=A[i]+A[i+2];
}
Upvotes: 1