optimize performance by putting the cpu intensive code after cuda kernel.