Understanding kernel divergence on GPUs

When are IF and SWITCH conditional/branching flow sufficient to create divergence which will impact utilization?
For example,
if I just have
if (x > 0) y = 1 ? y = 2;
is this bad?
what about if I have several of these short statements consecutively?
if (x> 0) y = 1 ? y = 2;
if (a> 0) b = 1 ? b = 2:
if (c> 0) d = 1 ? d = 2;
if (e> 0) f = 1 ? f = 2;
is this starting to be a potential issue?
or is it required that the separate conditional sections are much longer (many more instructions) before workgroup/SIMD divergence will start to cause performance to degrade?
Is there some general rule here or deeper level we can look to understand the underlying mechanism(s) resulting in the undesirable divergence?

1 Like

Iā€™d start with looking at this.

The best general rule is to assume that the compiler is not stupid and that the implementation is capable of executing simple conditionals reasonably. If you have concerns about specific, actual code, then profile it on the machines of interest.

Trying to prematurely optimize GPU code is usually a waste of time.

1 Like

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.