2
votes

I have a question about averaging over blocks. I have input data with size of 256*512, and I put 256 threads in one block, so there are 512 blocks total. The output should be with size of 256, which each element is the average of the same thread ID among different blocks. So in other words, thread 1 from all 512 blocks are averaged and result is given to thread 1 of output array. Similarly, thread 2 from all 512 blocks are averaged and given to thread 2 of output array. I know doing average inside a block by using shared memory is pretty fast and efficient, but that's not the condition here. Using NPP functions allow me to do it within a for loop, while it is really time consuming. Can anybody give suggestions of how to do the average over blocks efficiently? Thanks a lot!

1
Can't you switch threads and blocks so that you can average over threads in a block? - chaohuang

1 Answers

2
votes

WHy not flip it round, do 512 threads per block and do a shared memory sum-reduction to compute the sum within a block then divide by 512 and store to global memory?

You could also code this quickly using thrust, using a reduce-by-key operation.