The code is slightly modified from the original code that @pascanur sent me.  In his code he was using the shared variable to group all index that have the same class and then calling step to do all those at once.

I changed the code to work the way it does now and saw that there was no performance difference to doing so on the CPU , but we can avoid a lot of copies on the GPU c_code by doing it this way.

It is possible that doing it the way you propose would be faster.