I have two unsigned long arrays of 32 elements representing a large integer and I am trying to add them together using OpenCL. This is the kernel I have written:
The op1 and op2 arguments are READ_ONLY buffers that represent the two operands and the retVal argument is a READ_WRITE buffer that will contain the result after this kernel executes.
For some reason a random number is added in the first, and sometimes the second, element of the array. The strange thing is that the random number remains the same for some time. Most times the number added is 18446744073185321056 in the first element and 30 in the second. So if I add 1+0 I get 18446744073185321057 in the first element, 30 in the second and all others are 0. If I add 5+3 I get 18446744073185321064 in the first, 30 in the second and so on. Sometimes a different number is added and sometimes no number is added at all, so I get the correct results back.
I have tried reading the op1 and op2 buffers immediately after writing them and the contain the correct data. So the problem must be either in the kernel or when I am reading the retVal buffer.
I am on a MacPro 3,1 (early 2008) with a flashed 4870 running 10.6.2, but the same thing happens, with different random numbers, on a MacBook Pro 4,1 (early 2008) with a GeForce 8600M GT also running 10.6.2.
Obviously the code I've written is much longer than this, but I couldn't post everything here. If you need anything else just ask for it.
Thanks or your help.
Code:
__kernel void addUnsigned(
__global unsigned long* op1,
__global unsigned long* op2,
__global unsigned long* retVal) {
int id = (int)get_global_id(0);
__local int carry[33];
__private unsigned long prevVal;
retVal[id] = op1[id] + op2[id];
carry[id+1] = ((retVal[id]<op1[id])?1:0);
barrier(CLK_LOCAL_MEM_FENCE);
for(int i=0; i<32; i++) {
prevVal = retVal[id];
retVal[id] = retVal[id] + carry[id];
carry[id+1] = ((retVal[id]<prevVal)?1:0);
barrier(CLK_LOCAL_MEM_FENCE);
}
}
The op1 and op2 arguments are READ_ONLY buffers that represent the two operands and the retVal argument is a READ_WRITE buffer that will contain the result after this kernel executes.
For some reason a random number is added in the first, and sometimes the second, element of the array. The strange thing is that the random number remains the same for some time. Most times the number added is 18446744073185321056 in the first element and 30 in the second. So if I add 1+0 I get 18446744073185321057 in the first element, 30 in the second and all others are 0. If I add 5+3 I get 18446744073185321064 in the first, 30 in the second and so on. Sometimes a different number is added and sometimes no number is added at all, so I get the correct results back.
I have tried reading the op1 and op2 buffers immediately after writing them and the contain the correct data. So the problem must be either in the kernel or when I am reading the retVal buffer.
I am on a MacPro 3,1 (early 2008) with a flashed 4870 running 10.6.2, but the same thing happens, with different random numbers, on a MacBook Pro 4,1 (early 2008) with a GeForce 8600M GT also running 10.6.2.
Obviously the code I've written is much longer than this, but I couldn't post everything here. If you need anything else just ask for it.
Thanks or your help.