OpenCL returns random results

Discussion in 'Mac Programming' started by Stratoukos, Jan 8, 2010.

  1. Stratoukos macrumors member

    Jul 15, 2008
    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:

    __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);
    	for(int i=0; i<32; i++) {
    		prevVal = retVal[id];
    		retVal[id] = retVal[id] + carry[id];
    		carry[id+1] = ((retVal[id]<prevVal)?1:0);
    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.
  2. Berlepsch macrumors 6502


    Oct 22, 2007
    You are using carry[id] inside the for loop here:
    	retVal[id] = retVal[id] + carry[id];
    but your assignments are to carry[id+1] only. So for id==0, carry[0] is uninitialized (assuming that get_global_id(0) returns uints).
  3. Stratoukos thread starter macrumors member

    Jul 15, 2008
    Thanks so much! I can't believe I missed that for so long.

Share This Page