logoalt Hacker News

tvbusy • today at 7:20 AM • 1 reply • view on HN

For me, if-branching is the hardest to wrap my head around. "If"s are so natural to CPU programming that it's second nature to any developer but on GPU, it's expensive.


Replies

__del__ • today at 7:45 AM

instead of iterating over an array and if-branching to one of two paths for each item, conceive of your conditionals as logic operations which test for set membership (i.e. "is this array item a member of the set i care about?") and with this array mask of "do the thing or don't do the thing" flags, you can mutate your data 0 or 1 times.

the key for me is thinking in terms of sets—much of your conditional logic is probably about sorting things into sets anyway.