Results 1 to 6 of 6

Thread: Summing up all elements of a buffer

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    4

    Summing up all elements of a buffer

    I cant quite figure out the best way to sum up all the elements of buffer.

    I have tried the following kernels but neither sums up the buffers properly.

    Code :
    __kernel void vector_sum(__global int *A,__global int *sum) {
        // Get the index of the current element to be processed
        int i = get_global_id(0);
     
        // Do the operation
        sum[0] = A[i] + sum[0];
    }

    Code :
    __kernel void vector_sum(__global int *A,int sum) {
        // Get the index of the current element to be processed
        int i = get_global_id(0);
     
        // Do the operation
        sum = A[i] + sum;
    }


    Does anyone have an idea for the best way to do this?

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

    Re: Summing up all elements of a buffer

    Does anyone have an idea for the best way to do this?
    This is not surprisingly, a very common problem on parallel architectures. One that is well studied and basically 'solved'.

    Do a search on 'parallel reduction', or 'parallel prefix sum': you will find dozens of hits for code and algorithms which should point you in the right direction. The sdk's of every vendor include examples as well.

    Your proposed solutions indicate you're not understanding the basic parallel execution programming model either. You need to acquaint yourself with that if you have any hope of getting anywhere with opencl. The opencl specification and all of the vendor introductory material are good guides.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    4

    Re: Summing up all elements of a buffer

    thanks

    I found this resource on the amd site
    http://developer.amd.com/documentati...eductions.aspx

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    25

    Re: Summing up all elements of a buffer

    If they're global ints, you can use atomics right?

    http://www.khronos.org/registry/cl/s...tomic_add.html

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    4

    Re: Summing up all elements of a buffer

    Quote Originally Posted by homemade-jam
    If they're global ints, you can use atomics right?

    http://www.khronos.org/registry/cl/s...tomic_add.html
    Is that supported in all OpenCL devices?

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

    Re: Summing up all elements of a buffer

    Quote Originally Posted by homemade-jam
    If they're global ints, you can use atomics right?

    http://www.khronos.org/registry/cl/s...tomic_add.html
    Global atomics in general are really really slow.

    And even if they weren't really slow, they would still be slow.

    They introduce a serial bottleneck which may span 20+ CPU cores so it doesn't matter how fast they are - they will be a bottleneck.

Similar Threads

  1. Replies: 9
    Last Post: 12-05-2011, 11:36 AM
  2. accessing elements of cl::Buffer?
    By qwer in forum OpenCL
    Replies: 1
    Last Post: 01-31-2011, 11:24 AM

Posting Permissions

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