#Any tips how to convert my job into parallel ?

1 messages · Page 1 of 1 (latest)

wheat pike
#

Building a lookup table for a grid system.

[BurstCompile] public struct BuildGridLookUpTable2 : IJob
    {
        [ReadOnly]      public NativeArray<int3> indexReference;
        [WriteOnly]     public NativeArray<int3> gridTable;
                        public int gridCount;
                        public int renderCount;

        public void Execute()
        {
            int  count = 0,
                 index = 0,
                 value = 0,
                 valueLast;

            for (int j = 0; j < gridCount; ++j)
            {
                gridTable[ j ] = default;
            }
            
            valueLast = indexReference[ 0 ][ 0 ];

            for( int i = 0; i < renderCount; ++i )
            {
                value = indexReference[ i ][ 0 ];

                if( value > gridCount - 1 )
                {
                    gridTable[ valueLast ] = new int3( count , index , 0 );

                    return;
                }

                if( value == valueLast ) count ++ ;

                else
                {
                    gridTable[ valueLast ] = new int3( count , index , 0 );

                    index = i;
                    count = 0;
                    i -- ;
                }

                if( i == renderCount - 1 )
                {
                    gridTable[ valueLast ] = new int3( count , index, 0 );
                }

                valueLast = value;
            }
        }
    }
mighty osprey
#

The initial look to zero out the output array can likely be a main-thread pre-pass; it will be memory-bound and is unlikely to benefit too much from running in parallel.

My first stab would probably be an IJobParallelFor (over the outer renderCount loop), writing to NativeList.AsParallelWriter() instead of a NativeArray. Is the order of the output data important, or just that it all gets into the gridTable at some point?

wheat pike
#

im a bit confused how to sync valueLast between threads