You can use groupshared variables to make them syncronized
No he can't. The keyword groupshared
allows multiple threads inside a thread group to share data, not between the different thread groups. It's this way, because the thread groups might get executed by different streaming multiprocessors, while a single thread group is executed on a single streaming multiprocessor, where all the threads can use specialized on-chip memory to efficiently share data.
It actually depends on your implementation. A normal shader for blurring doesn't really require synchronization between the individual threads, since every thread just needs to gather its data. A better implementation might be, that you want to use groupshared memory as some kind of cache for the row of the texture, so that every thread only needs to perform a single texture fetch. Since groupshared memory is only available inside a thread group, you can only use up to 1024 threads. The only solution would be to convert your algorithm into a more iterative algorithm. Just work on 2 pixels per thread and use a groupshared array with 2048 elements. This works and is not even slower, since a thread group is not what the actual hardware executes in parallel. The driver splits your thread group into units of 32 or 64 threads called Warps or Wavefronts that get executed iteratively
(clarification: all the threads of a warp get executed in parallel, but the different warps get executed iteratively). So 2 thread groups of 1024 would be executed as 32 wavefronts or 64 warps in an iterative manner. My solution of just a single thread group and 1024 threads per thread group gets converted into just 16 wavefronts or 32 warps, but they all do the twice the amount of work. So in the end your algorithm is just as parallel, as it would be if you would use 2 thread groups. As long as a thread group consists of at least 8 warps (recommendation of NVIDIA) you can always remove some of your parallelisation without a decrease in performance. As far as I understand NVIDIA's Kepler architecture, they now begin to execute multiple warps in parallel (6, if I'm correct), so this solution might not be the best for the future. But it's as good as it can get with DirectX 11 unfortunately.
If you would actually want to work on more than 2048 pixels, you would need to use register memory to cache your pixels, since you would need more than the maximum of 32 KB group shared memory. Let's say you would want to work with 4096 pixels. You could store 4 pixels per thread inside its registers and always expose 2 of them in the group shared memory. You just need to synchronize the threads and always expose the pixels you want to access from other threads. Groupshared memory is just a way to share data. Register memory is way larger than just 32 KB.
Edited by CryZe, 20 December 2012 - 08:05 AM.