Simple loop not being vectorised?

I have this simple loop as below which the compiler and the burst inspector is indicating that it isn’t being vectorised. And I’m kind of confused and not sure what I’m doing wrong, it seems close to the examples in the burst docs. I’m using an IJobParallelFor.

for (int a = 0; a < PotentialMoveCells.Length; a++)
            {
                Unity.Burst.CompilerServices.Loop.ExpectVectorized();
                PotentialMoveCells[a] = OutsideMoveCells[a];

            }

What are the types of the MoveCells?

1 Like

Maybe Burst doesn’t know that the arrays are not aliased, you can check it with ExpectNotAliased method Method ExpectNotAliased | Burst | 1.6.6, then use the NoAlias attribute to let it know.

The Job system already knows that the Native Containers can’t alias, unless you used NativeDisableContainerSafetyRestriction attribute on them, there’s more info on this blog post Unity Blog

1 Like

Its basically this:

public struct CellData : IComponentData
{
    public float3 worldPos;
    public int2 gridIndex;
    public byte cost;
    public ushort bestCost;
}

Is it perhaps too large of a struct.

I didn’t think about aliasing, I’ll look into that thanks.

Does the struct have a StructLayout attribute? If not try adding this before the struct:

 [StructLayout(LayoutKind.Sequential)]

If that doesn’t do the trick I would assume it cannot be vectorized because you’re mixing types (float3, int2, etc). You could apply a trick by using two float4 whereas w in worldPos is 0 (unused) and the other float4 encodes the int index (x, y) and the two cost values (z, w).

Maybe it also works with a float3 or float4 for worldpos, and an int4 for the index and costs.

Oh and if any of this works, let us know. I’m curious to know if vectorization can be “fixed” in this case in such a manner.

2 Likes

I tried it didn’t work, but thanks anyway. I’m probably going to leave this on the back burner for pushing performance. Its like 0.03ms each loop and 2ms of each update which is pretty good I think. I need to get the total sum which is 20ms down as much as I can and I’m not going to get much of that with this loop. I was mostly just curious as to why it wasn’t vectorising, it must be too many types perhaps.

Are PotentialMoveCells and the other one just NativeArray’s?

Yep they are.

I need an array that is local to each job thread. I can’t use nativelist I’ve had issue’s with declaring a ton of those in jobs before.

I messed around a bit and managed to get it to alias I think. Its gone down to 0.002ms average each loop, and chopped of 2ms of the overall time, that’s way more than I expected.

I still get the not vectorized warning but that seems good enough to me. Just gotta figure out how to optimise or get rid of the ifs that are taking up the majority of the time now.

2 Likes