Results 1 to 3 of 3

Thread: fast buffer initialization with zeros

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    29

    fast buffer initialization with zeros

    What is the fastest way to initialize a buffer with zeros? I need a buffer which is 1/8 filled with data and 7/8 with zeros. The data is a copy from another buffer. The buffers can be several 100MB big and I have to do this quite often. My current solution is to allocate a buffer eight times the size of the source buffer, copy the data from the source and initialize the rest with zeros. But if there is e.g. a way to initialize the whole buffer at the same time with zeros, I can save some write operations.

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

    Re: fast buffer initialization with zeros

    Quote Originally Posted by MaximS
    What is the fastest way to initialize a buffer with zeros? I need a buffer which is 1/8 filled with data and 7/8 with zeros. The data is a copy from another buffer. The buffers can be several 100MB big and I have to do this quite often. My current solution is to allocate a buffer eight times the size of the source buffer, copy the data from the source and initialize the rest with zeros. But if there is e.g. a way to initialize the whole buffer at the same time with zeros, I can save some write operations.
    Options:
    a) Since you know most of the elements are zero, just hard-code the zero as an out-of-range value when reading the source array - i.e. if you wrote the kernel that uses this data.
    b) writing a kernel which does something similar. one work item per output item, and do a range-test on whether you load it with the source value or zero. Might have to experiment with persistent kernels and the data-size to get peak performance on a given device.
    c) try using the opencl 1.2 fillbuffer and copy routines.
    d) If this buffer isn't written to itself (e.g. not an 'in place' algorithm), you could just keep one around pre-initialised to zero, and just copy the significant data into it (or even better, have the previous stage write to it directly) when you need to run the given kernel.

    But in short: no there's no magic 'clear memory' function other than doing it element by element.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    29

    Re: fast buffer initialization with zeros

    Thanks for the answer. Option a is the best I think.

Similar Threads

  1. OpenCL image2d_t writing mostly zeros
    By Aflixion in forum OpenCL
    Replies: 1
    Last Post: 07-18-2012, 02:03 PM
  2. Fast array initialization
    By nachovall in forum OpenCL
    Replies: 4
    Last Post: 03-16-2011, 12:15 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
  •