#Get length of output in previous job in scheduled job

1 messages · Page 1 of 1 (latest)

quiet hare
#

Hi! I have a job that generates an arbitrary amount of data into a NativeStream, and I have a second job which then reads this stream into a NativeArray. I can do this without scheduling the second job and just waiting for the first one to finish, but I'd rather a more elegant solution.

I need to setup the NativeArray with the count of the NativeStream after the first job completes and before the second job runs. Is this not possible if I schedule the second job after the first?

crystal mantle
#

You can, with IJobParallelForDefer

#
var stream = new NativeStream(1, WorldUpdateAllocator);
var handle = new TestJ
{
    Stream = stream.AsWriter()
}.Schedule();

var  cnt = new NativeReference<int>(0, WorldUpdateAllocator);
int* ptr = cnt.GetUnsafePtr();

// Not necessary if you're willing to modify collections.
handle = new Counter
{
    Stream = stream.AsReader(),
    Count  = cnt
}.Schedule(handle);

handle = new TestJD
{
    Stream = stream.AsReader()
}.Schedule(ptr, 1, handle);```
#
private struct TestJD : IJobParallelForDefer
{
    [ReadOnly] public NativeStream.Reader Stream;

    public void Execute(int index)
    {
        Stream.BeginForEachIndex(index);
        // Do stuff.
    }
}

private struct Counter : IJob
{
    [ReadOnly] public NativeStream.Reader  Stream;
    public NativeReference<int> Count;

    public void Execute()
    {
        // It's an integer but buried inside UnsafeStream.
        // This job just establishes an accessible value.
        // If you want to skip this job, you must modify the 
        //   collections package to make this return by reference 
        //   and then you can establish a pointer for this in your IJPFD job scheduling.
        Count.Value = Stream.ForEachCount;
    }
}

private struct TestJ : IJob
{
    public NativeStream.Writer Stream;

    public void Execute()
    {
        Stream.Write(15);
    }
}```