Results 1 to 4 of 4

Thread: Array in global memory - strange results

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2

    Array in global memory - strange results

    I am attempting to implement an algorithm to solve ODEs in OpenCL. I've been struggling with the best way to do this, and I have an idea in mind that I think should work in theory, but when I actually try to start with just a simple case I'm having unexpected results. I would very much appreciate any help.

    Here's what I have:

    Code :
    __kernel void calculate(__global float *n_array, __global float *n_avg, int step)
    {
      int j = get_global_id(0);
      n_array[j] += n_array[j] * some constants;
      n_avg[step] += n_array[j];
    }

    I'm executing this with 1000 global work items (so that "j" ranges from 0-999) and the program is called 1000 times in a for loop (so that this kernel gets executes a total of 1000*1000 times). I place values in "n_array" initially with clEnqueueWriteBuffer and this is working correctly. The issue is that n_avg ends up with very unexpected values. I expect is has something to do with being shared memory and perhaps the timing is such that values aren't being written properly. When I pick out a single value of j (i.e. if(j==10) ), it works exactly as expected. The issues is adding all the values to the n_avg[step] variable, say I have 1000 values of n_array[j] = 1, I would expect n_avg[step] to equal 1000 (since I added 1 to it 1000 times), but this is not always what is occurring. I know I may have not been clear here, I am still learning OpenCL so I don't know exactly how to frame my question. If you think you can help but need more information, please let me know and I will do my best to provide it. Thank you very much for taking the time to look at my question.

  2. #2
    Senior Member
    Join Date
    Oct 2012
    Posts
    102

    Re: Array in global memory - strange results

    n_avg[step] is accessed concurrently by all work-items so this cannot work.

    You have to split up your kernel into two kernels: in the first one, do the computation of the vector n_array, then in the second one, do a reduction of n_array.

    You can learn about parallel reduction in this introductory article for instance:

    http://developer.amd.com/Resources/d...eductions.aspx

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    2

    Re: Array in global memory - strange results

    It seems like that link you provided is down, any chance you could explain what it said? Thanks!

  4. #4
    Senior Member
    Join Date
    Aug 2011
    Posts
    271

    Re: Array in global memory - strange results

    Quote Originally Posted by phaethon
    It seems like that link you provided is down, any chance you could explain what it said? Thanks!
    Just do an internets search for parallel reduction, parallel sum, and see where that leads you. It is a widely researched and documented topic.

Similar Threads

  1. Profiling issues and strange results
    By sunny_engineer_204 in forum OpenCL
    Replies: 0
    Last Post: 05-14-2012, 02:49 PM
  2. Strange results
    By Rui in forum OpenCL
    Replies: 2
    Last Post: 04-07-2010, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •