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.
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.
Thanks for the answer. Option a is the best I think.