fixed dumb for loop problem in gradient calculation that made cpu and gpu versions run 5x slower than they should