Ejemplo n.º 1
0
    unsafe public long Calculate(Dictionary <int, NativeArray <int> > map)
    {
        NativeArray <ulong> pointers = new NativeArray <ulong>(map.Count, Allocator.TempJob);
        NativeArray <int>   len      = new NativeArray <int>(map.Count, Allocator.TempJob);
        NativeArray <long>  subSums  = new NativeArray <long>(map.Count, Allocator.TempJob);
        NativeArray <long>  totalSum = new NativeArray <long>(1, Allocator.TempJob);


        // hopefully this is fast for what I need
        int i = 0;

        foreach (var item in map)
        {
            NativeArray <int> array = item.Value;
            len[i] = array.Length;
            void *ptr = array.GetUnsafePtr();
            pointers[i++] = (ulong)ptr;
        }



        JobHandle handle1 = new CalculateMultiJob
        {
            longPointers = pointers,
            arrayLengths = len,
            subSums      = subSums
        }.Schedule(map.Count, 1);



        JobHandle handle2 = new sumUpArrayVals
        {
            subSums = subSums,
            output  = totalSum
        }.Schedule(handle1);


        // If possible this should either go into a yeild pattern or complete in lateUpdate


        handle2.Complete();


        long sum = totalSum[0];


        pointers.Dispose();
        len.Dispose();
        subSums.Dispose();
        totalSum.Dispose();

        return(sum);
    }
    unsafe public long Calculate(Dictionary <int, NativeArray <int> > map)
    {
        // This relies on being called in the main thread

        // trick the job system into using ulong as a holder for void*
        _pointers = new NativeArray <ulong>(map.Count, Allocator.TempJob);
        _subSums  = new NativeArray <long>(map.Count, Allocator.TempJob);

        // Scalars seem to need to be in Native Structures
        _lens     = new NativeArray <int>(map.Count, Allocator.TempJob);
        _totalSum = new NativeArray <long>(1, Allocator.TempJob);


        int i = 0;

        foreach (var item in map)
        {
            NativeArray <int> array = item.Value;
            _lens[i] = array.Length;
            void *ptr = array.GetUnsafePtr();
            _pointers[i++] = (ulong)ptr;
        }



        // create a job to calculate each Array in parallel
        JobHandle handle1 = new CalculateMultiJob
        {
            longPointers = _pointers,
            arrayLengths = _lens,
            subSums      = _subSums // subsums will be the output passed to next job
        }.Schedule(map.Count, 1);


        // Consolidate all the sub sums into one master sum and schedule
        JobHandle handle2 = new sumUpArrayVals
        {
            subSums = _subSums,
            output  = _totalSum
        }.Schedule(handle1);

        pendingJob = true;
        handle     = handle2;

        return(-1);
    }