Пример #1
0
    private void DoExample()
    {
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        // Instantiate and initialize
        SimpleJob firstJob = new SimpleJob
        {
            a      = 5f,
            result = resultArray
        };

        AnotherJob secondJob = new AnotherJob
        {
            result = resultArray
        };

        // Schedule
        JobHandle firstHandle  = firstJob.Schedule();
        JobHandle secondHandle = firstJob.Schedule(firstHandle);

        // other tasks to run in Main Thread in parallel

        // Confirm that jobs are completed
        firstHandle.Complete();
        secondHandle.Complete();

        float resultingValue = resultArray[0];

        Debug.LogError("Result = " + resultingValue);

        resultArray.Dispose();
    }
Пример #2
0
    private void DoExample()
    {
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        // 1. Instantiate Job
        SimpleJob myJob = new SimpleJob
        {
            // 2. Initialize Job
            a      = 5.0f,
            result = resultArray
        };
        AnotherJob secondJob = new AnotherJob
        {
            result = resultArray
        };

        // 3. Shedule Job
        JobHandle handle       = myJob.Schedule();
        JobHandle secondHandle = secondJob.Schedule(handle); // !! передаем дескриптор во вторую работу

        // 4. Complete Job
        handle.Complete();  // работу следует завершить прежде, чем пользоваться результатами
        secondHandle.Complete();

        float resultingValue = resultArray[0];

        Debug.Log("result = " + resultingValue);

        resultArray.Dispose();
    }
Пример #3
0
    void Start()
    {
        theData    = new NativeArray <float>(1, Allocator.TempJob);
        theData[0] = 2;

        // Initialize a Job
        SimpleJob simpleJob = new SimpleJob
        {
            number = numberToAdd,
            data   = theData
        };

        // Schedule the job for execution.
        simpleJobHandle = simpleJob.Schedule();

        // Actually run the job.
        JobHandle.ScheduleBatchedJobs();

        // Wait...
        simpleJobHandle.Complete();

        // Check if completed or not and used data from the job result.
        if (simpleJobHandle.IsCompleted)
        {
            Debug.Log(simpleJob.data[0]);
        }

        theData.Dispose();
    }
Пример #4
0
    private void runDemo()
    {
        // Simpl job declaration with the data
        SimpleJob simpleJob = new SimpleJob {
            number = myNumber,
            data   = myData
        };

        //Schedule a simple job (added in the quee, not running)
        simpleJobHandle = simpleJob.Schedule();

        // run the schedule job
        JobHandle.ScheduleBatchedJobs();

        // wait for the job to be completed
        simpleJobHandle.Complete();

        if (simpleJobHandle.IsCompleted)
        {
            Debug.Log(simpleJob.data[0]);
        }

        // Free memory
        freeMemoryData();
    }
    private void DoExample()
    {
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        // Instatiate and Initialize
        SimpleJob myJob = new SimpleJob()
        {
            a      = 5f,
            result = resultArray
        };

        DifferentJob myJob2 = new DifferentJob()
        {
            result = resultArray
        };

        // Schedule
        JobHandle myHandle  = myJob.Schedule();
        JobHandle myHandle2 = myJob2.Schedule(myHandle); //myHandle is automatically completed within schedule

        // Other task to run in the Main thread

        //myHandle.Complete();
        myHandle2.Complete();

        float resultValue = resultArray[0];

        Debug.Log(resultValue);

        resultArray.Dispose();
    }
    void ExtendedExample()
    {
        NativeArray <float> result = new NativeArray <float>(1, Allocator.Persistent);

        SimpleJob job = new SimpleJob()
        {
            result = result
        };

        JobExecution execution = JobHelper.AddScheduledJob(job, job.Schedule(), (jobExecutor) => {
            /// OnJobComplete delegate returns 'jobExecutor' with usefull data.
            if (jobExecutor.FramesTaken == -1)
            {
                Debug.Log("Job has completed immediatelly!");
            }
            else
            {
                Debug.LogFormat("Job has completed in {0}s and {1} frames!", jobExecutor.Duration, jobExecutor.FramesTaken);
            }

            // Result is available. LateUpdate() context.
            Debug.Log(result[0]);

            // You can dispose NativeContainers container within the jobexecution immediately after completion.
            // This is required if your set your container allocator as Temp or TempJob, dont Dispose if you use Persistent allocation.
            // Otherwise no need to call Dispose, the JobHelper will execute Dispose() on all completed jobs on OnDestroy()
            jobExecutor.Dispose();
        },
                                                           // If you want to complete this job immediately in the LateUpdate() check this to true.
                                                           // But make sure to schedule the job before, to leave some room for workers to do its work.
                                                           completeImmediatelly: false);

        // There is also an option to complete the job whenever you want.
        Debug.Log(execution.Complete());
    }
Пример #7
0
        public void ScheduleSimpleJob()
        {
            didDispose = 0;
            SimpleJob job = new SimpleJob()
            {
                a = 5,
                b = 10
            };

            NativeArray <int> jobResult = new NativeArray <int>(SimpleJob.N, Allocator.TempJob);

            job.result = jobResult;

            JobHandle handle = job.Schedule();

            handle.Complete();

#if !UNITY_DOTSPLAYER
            // TODO: Understand / fix why the editor tests don't run quite the same code path.
            job.mDisposable.Dispose();
#endif

            Assert.AreEqual(1, didDispose);

            for (int i = 0; i < SimpleJob.N; ++i)
            {
                Assert.AreEqual(15, jobResult[i]);
            }

            jobResult.Dispose();
        }
Пример #8
0
    private void Start()
    {
        NativeArray <int> result    = new NativeArray <int>(1, Allocator.TempJob);
        SimpleJob         simpleJob = new SimpleJob {
            a = 1, b = 2, result = result
        };
        JobHandle jobHandle = simpleJob.Schedule();

        jobHandle.Complete();

        Debug.Log(result[0]);

        result.Dispose();
    }
Пример #9
0
    // Start is called before the first frame update
    void Start()
    {
        SimpleJob simpleJob = new SimpleJob
        {
            number = myNumber,
            data   = myData,
        };

        ParallelJob parallelJob = new ParallelJob
        {
            number = myNumber,
            data   = myData,
        };

        TransformJob transformJob = new TransformJob
        {
            number    = myNumber,
            data      = myData,
            deltaTime = Time.deltaTime,
        };

        simpleJobHandle    = simpleJob.Schedule();
        parallelJobHandle  = parallelJob.Schedule(myData.Length, 32, simpleJobHandle);
        transformJobHandle = transformJob.Schedule(transformAccessArray, parallelJobHandle);

        // dependency like a->b->c...

        JobHandle.ScheduleBatchedJobs();

        simpleJobHandle.Complete();
        parallelJobHandle.Complete();
        transformJobHandle.Complete();

        if (simpleJobHandle.IsCompleted)
        {
            Debug.Log("simple job result " + simpleJob.data[0]);
        }

        if (parallelJobHandle.IsCompleted)
        {
            for (int i = 0; i < myData.Length; ++i)
            {
                Debug.Log("parallel job result " + parallelJob.data[i]);
            }
        }
        myData.Dispose();
        transformAccessArray.Dispose();
    }
    void SimpleExample()
    {
        NativeArray <float> result = new NativeArray <float>(1, Allocator.Persistent);

        SimpleJob job = new SimpleJob()
        {
            result = result
        };

        JobExecution execution = JobHelper.AddScheduledJob(job, job.Schedule(), (jobExecutor) => {
            Debug.LogFormat("Job has completed in {0}s and {1} frames!", jobExecutor.Duration, jobExecutor.FramesTaken);

            // Result is available. LateUpdate() context.
            Debug.Log(result[0]);
        });
    }
Пример #11
0
    private void Start()
    {
        SimpleJob simpleJob = new SimpleJob
        {
            number = myNumber,
            data   = myData,
        };

        simpleJobHandle = simpleJob.Schedule();
        simpleJobHandle.Complete();

        if (simpleJobHandle.IsCompleted)
        {
            Debug.Log(simpleJob.data[0]);
        }
    }
Пример #12
0
    private void Start()
    {
        NativeArray <int> array = new NativeArray <int>(1, Allocator.TempJob);

        SimpleJob job = new SimpleJob()
        {
            a      = 1,
            b      = 2,
            result = array,
        };

        JobHandle handle = job.Schedule();

        handle.Complete();

        Debug.Log(job.result[0]);
        array.Dispose();
    }
Пример #13
0
    // Start is called before the first frame update
    private void Start()
    {
        // В целях необходимости синхронизации данных между основным потоком и Job-ом
        var result = new NativeArray <int>(1, Allocator.TempJob);
        var job    = new SimpleJob
        {
            A      = 1,
            B      = 2,
            Result = result
        };

        var jobHandle = job.Schedule();

        jobHandle.Complete();

        Debug.Log(job.Result[0]);
        result.Dispose();
    }
Пример #14
0
    private void DoExample()
    {
        // create & set aside data as early as possible:
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        // instantiate the job
        //SimpleJob myJob = new SimpleJob();
        // initialize the job instance with data
        //myJob.a = 5;
        //myJob.result = new NativeArray<float>(1, Allocator.TempJob);

        // Do the above but using object initializer syntax:
        SimpleJob myJob = new SimpleJob
        {
            a      = 5f,
            result = resultArray
        };
        AnotherJob secondJob = new AnotherJob
        {
            result = resultArray
        };
        // Allocator controls how long the job's data lasts. Most common options are:
        // Temp = 1 frame
        // TempJob = lifetime of Job or 4 frames, whichever comes first
        // Persistent = as long as you need

        // schedule the job instance into the CPU
        JobHandle handle       = myJob.Schedule();
        JobHandle secondHandle = secondJob.Schedule(handle); // requires handle to complete before running!

        // ...
        // other tasks to run in Main Thread in parallel with other jobs:
        // ...

        //handle.Complete(); // dependency of handles implies the first one already completes
        secondHandle.Complete();

        // job must complete before accessing job data, especially native container data!
        float resultValue = resultArray[0];

        Debug.Log($"Result was {resultValue}");         // gets value from native container, so we can access it
        Debug.Log($"myJob.a has a value of {myJob.a}"); // gets value from copy of job data, does not
        resultArray.Dispose();
    }
Пример #15
0
    // Update is called once per frame
    void Update()
    {
        NativeArray <float> result = new NativeArray <float>(1, Allocator.TempJob);

        SimpleJob simpleJob = new SimpleJob
        {
            a         = 1,
            deltaTime = Time.deltaTime,
            result    = result
        };

        JobHandle jobHandle = simpleJob.Schedule();

        jobHandle.Complete();

        jobsText.text = result[0].ToString();

        result.Dispose();
    }
Пример #16
0
    private void DoExample()
    {
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        SimpleJob myJob = new SimpleJob {
            a      = 5f,
            result = resultArray
        };

        JobHandle handle = myJob.Schedule();

        handle.Complete();

        float resultingValue = resultArray[0];

        Debug.Log(resultingValue);

        resultArray.Dispose();
    }
Пример #17
0
    private void Start()
    {
        SimpleJob simpleJob = new SimpleJob
        {
            number = myNumber,
            data   = myData
        };

        //agenda o job, mas num executa!
        myHandle = simpleJob.Schedule();
        //roda o job agendado
        JobHandle.ScheduleBatchedJobs();
        //aguarda o job completar
        myHandle.Complete();

        if (myHandle.IsCompleted)
        {
            Debug.Log(simpleJob.data[0]);
        }
    }
Пример #18
0
    private void DoExample()
    {
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        //Instantiate a job
        SimpleJob myJob = new SimpleJob
        {
            //Initialize data to work with
            a      = 5f,
            result = resultArray
        };

        AnotherJob secondJob = new AnotherJob();

        secondJob.result = resultArray;



        //Initialize data to work with
        //myJob.a = 5f;
        //myJob.result = new NativeArray<float>(1, Allocator.TempJob);



        //Schedule the job
        JobHandle handle       = myJob.Schedule();
        JobHandle secondHandle = secondJob.Schedule(handle);



        //Other tasks within the main thread
        //handle.Complete();
        secondHandle.Complete();

        float resultingValue = resultArray[0];

        Debug.Log("Result: " + resultingValue);

        resultArray.Dispose();
    }
Пример #19
0
    // Start is called before the first frame update
    void Start()
    {
        data    = new NativeArray <float>(1, Allocator.Persistent);
        data[0] = 2;
        SimpleJob simpleJob = new SimpleJob
        {
            number = this.number,
            data   = this.data
        };

        simpleJobHandle = simpleJob.Schedule();

        JobHandle.ScheduleBatchedJobs();

        simpleJobHandle.Complete();

        if (simpleJobHandle.IsCompleted)
        {
            Debug.Log(simpleJob.data[0]);
        }

        data.Dispose();
    }
Пример #20
0
        public void ScheduleSimpleJob()
        {
            SimpleJob job = new SimpleJob()
            {
                a = 5,
                b = 10
            };

            NativeArray <int> jobResult = new NativeArray <int>(SimpleJob.N, Allocator.TempJob);

            job.result = jobResult;

            JobHandle handle = job.Schedule();

            handle.Complete();

            for (int i = 0; i < SimpleJob.N; ++i)
            {
                Assert.AreEqual(15, jobResult[i]);
            }

            jobResult.Dispose();
        }
Пример #21
0
    private void DoExample()
    {
        // Allocate Memorys
        NativeArray <float> resultArray = new NativeArray <float>(1, Allocator.TempJob);

        // 1. instantiate
        SimpleJob firstJob = new SimpleJob
        {
            // 2. initialize
            a      = 5f,
            arrays = resultArray
        };

        AnotherJob secondJob = new AnotherJob
        {
            arrays = resultArray
        };

        // 3. schedule. Best Utilize, Schedule Early
        JobHandle fhandle = firstJob.Schedule();
        JobHandle shandle = secondJob.Schedule(fhandle);

        // other tasks to run in Main Thread in parallel. Best Utilize, Complete late
        // Complete 전, Native Variable에 접근할 경우 Error 발생

        // fhandle.Complete(); <- shandle이 대신함
        shandle.Complete();

        float resultingValue = resultArray[0];

        Debug.Log("Result = " + resultingValue);
        Debug.Log("job A = " + firstJob.a);

        // reAllocate Memorys
        resultArray.Dispose();
    }