IJobParallelFor add some native array without batching

Hello! Maybe I did not quite understand the logic of the work IJobParallelFor or something I missed, but I have the following question. Can we somehow transfer an NativeArray to job without batching. For example:

struct SomeParallelJob : IJobParallelFor
    {
        public NativeArray<CustomStruct> dontBatchMe; //<-- I do not want to batching this array
        public NativeArray<int>          batchMeBaby;

        public void Execute(int index)
        {
            int tmp = batchMeBaby[index];

            for (int i = 0; i < dontBatchMe.Length; i++)
            {
                tmp += dontBatchMe[i].someFieldFromStruct;
            }

            batchMeBaby[index] = tmp;
        }
    }

dontBatchMe should be marked [ReadOnly] in your example above.

1 Like

Oh, I suspected it, but I did not have time to check it! Thank you for confirming my guess :slight_smile:

And you can still 1 question, so as not to create a new thread. Can we get one output from Job? Approximately how here (Now I’m using a native array of long 1 for this):

struct SomeJob : IJob
    {
        [ReadOnly] public NativeArray<int> data;
        public int counter;
        public void Execute()
        {
            for (int i = 0; i < data.Length; i++)
            {
                counter++;
            }
        }
    }
    protected override void OnUpdate()
    {
        NativeArray<int> data = new NativeArray<int>(100000, Allocator.TempJob);
        var someJob = new SomeJob
        {
            data = data,
        };
        JobHandle handle = someJob.Schedule(data.Length, 1250);
        handle.Complete();
        Debug.Log("Count: " + someJob.counter); // <--- this value always default int (0)
        data.Dispose();
    }

Now I’m using a native array of long 1 for this

That’s an acceptable approach. (Which is just a pointer to some allocated, presumably TempJob, memory.) - Any solution that’s specific to one int would just be identical to that anyway. (Although you could make an NativeInt container if you wanted, it’s probably doesn’t buy you anything.)

2 Likes

Ok :slight_smile: Thank you for ansewers :slight_smile: