How to make this piece of code faster? optimising it or using multiple threads?

I tried having the inner for-loop (for h in upagents) on the GPU. But, I couldn’t find alternatives to a few commands that are in there. I posted about it here - Rewriting function on CPU for execution on GPU