public float Calculate()
    {
        // Schedule the job on each frame to make sure that it will be compiled async on the next frame
        _array[0] = 0.0f;
        // Launch synchronous job
        var job = new MyJob {
            Result = _array
        };

        job.Schedule().Complete();
        var rotation = job.Result[0];

        // Launch an async compilation
        var asyncJobNoOptim = new MyJobWithDefaultOptimizations()
        {
            Result = _arrayAsyncJobDefault
        };
        var asyncJobFastOptim = new MyJobWithFastOptimizations()
        {
            Result = _arrayAsyncJobFast
        };
        var asyncJobNoOptimHandle   = asyncJobNoOptim.Schedule();
        var asyncJobFastOptimHandle = asyncJobFastOptim.Schedule();

        // Wait for async completion
        asyncJobNoOptimHandle.Complete();
        asyncJobFastOptimHandle.Complete();

        return(rotation);
    }
Ejemplo n.º 2
0
    public void jobTesting()
    {
        NativeArray <float> result = new NativeArray <float>(1, Allocator.TempJob);

        // Setup the data for job #1
        MyJob jobData = new MyJob();

        jobData.a      = 10;
        jobData.b      = 10;
        jobData.result = result;

        // Schedule job #1
        JobHandle firstHandle = jobData.Schedule();

        // Setup the data for job #2
        AddOneJob incJobData = new AddOneJob();

        incJobData.result = result;

        // Schedule job #2
        JobHandle secondHandle = incJobData.Schedule(firstHandle);

        // Wait for job #2 to complete
        secondHandle.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        float aPlusB = result[0];

        Debug.Log(aPlusB);
        // Free the memory allocated by the result array
        result.Dispose();
    }
Ejemplo n.º 3
0
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Y))
        {
            float startTime = Time.realtimeSinceStartup;
            for (int i = 0; i < myNumbers.Length; i++)
            {
                myNumbers[i] = Sigmoid(myNumbers[i]);
            }
            Debug.Log("Without jobs: " + (Time.realtimeSinceStartup - startTime));
        }
        if (Input.GetKeyDown(KeyCode.U))
        {
            float startTime = Time.realtimeSinceStartup;
            NativeArray <float> numbersToCompute = new NativeArray <float>(myNumbers.Length, Allocator.TempJob);
            numbersToCompute.CopyFrom(myNumbers);
            MyJob myJob = new MyJob()
            {
                numbers = numbersToCompute
            };

            JobHandle jobHandle = myJob.Schedule(numbersToCompute.Length, 64);
            jobHandle.Complete();

            Debug.Log("With jobs: " + (Time.realtimeSinceStartup - startTime));
            numbersToCompute.Dispose();
        }
    }
Ejemplo n.º 4
0
    // Start is called before the first frame update
    void Start()
    {
        // Create a native array of a single float to store the result. This example waits for the job to complete for illustration purposes
        NativeArray <float> result = new NativeArray <float>(1, Allocator.TempJob);

        // Set up the job data
        MyJob jobData = new MyJob();

        jobData.a      = 10;
        jobData.b      = 10;
        jobData.result = result;

        // Schedule the job
        JobHandle handle = jobData.Schedule();

        // Wait for the job to complete
        handle.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        float aPlusB = result[0];

        // Free the memory allocated by the result array
        result.Dispose();

        TextManager.Instance.Set($"Sum is {aPlusB}");
    }
Ejemplo n.º 5
0
        void Update()
        {
            if (_segments == null)
            {
                return;
            }

            _segments.Dependency.Complete();

            if (_segments.buffer.Length != _numSegments || _everyFrame)
            {
                _segments.buffer.Length = _numSegments;

                // scheduel new job:
                var job = new MyJob {
                    Transform   = transform.localToWorldMatrix,
                    NumSegments = _numSegments,
                    Segments    = _segments.buffer,
                    Offset      = Time.time,
                    Frequency   = _frequency,
                };
                _segments.Dependency = job.Schedule(
                    arrayLength:                    _numSegments,
                    innerloopBatchCount:    64,
                    dependsOn:                              _segments.Dependency
                    );
            }
        }
Ejemplo n.º 6
0
 void Update()
 {
     //按下鼠标左键后
     if (Input.GetMouseButtonDown(0))
     {
         //随机设置坐标
         NativeArray <Vector3> position = new NativeArray <Vector3>(cubes.Length, Allocator.Persistent);
         for (int i = 0; i < position.Length; i++)
         {
             position[i] = Vector3.one * i;
         }
         //设置Transform
         TransformAccessArray transformArray = new TransformAccessArray(cubes);
         //启动工作线程
         MyJob job = new MyJob()
         {
             position = position
         };
         JobHandle jobHandle = job.Schedule(transformArray);
         //等待工作线程结束
         jobHandle.Complete();
         Debug.Log("工作线程结束");
         //结束
         transformArray.Dispose();
         position.Dispose();
     }
 }
Ejemplo n.º 7
0
    void StartMyJob()
    {
        // Create a native array of a single float to store the result. This example waits for the job to complete for illustration purposes
        NativeArray <float> result = new NativeArray <float>(1, Allocator.TempJob);

        // Set up the job data
        MyJob jobData = new MyJob();

        jobData.a      = 10;
        jobData.b      = 10;
        jobData.result = result;

        JobB jobB = new JobB();

        jobB.result = result;

        // Schedule the job
        JobHandle handle = jobData.Schedule();
        JobHandle hB     = jobB.Schedule(handle);

        // Wait for the job to complete
        hB.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        float aPlusB = result[0];

        Debug.Log(aPlusB);

        // Free the memory allocated by the result array
        result.Dispose();
    }
Ejemplo n.º 8
0
        /// Start
        /// <summary>
        /// Invoked on Start
        /// </summary>
        void Start()
        {
            // populate input and output native arrays
            var input  = new NativeArray <float>(10, Allocator.Persistent);
            var output = new NativeArray <float>(1, Allocator.Persistent);

            for (int i = 0; i < input.Length; i++)
            {
                input[i] = 1.0f * i;
            }

            // create a job
            var job = new MyJob
            {
                Input  = input,
                Output = output
            };

            // schedule and complete
            job.Schedule().Complete();
            Debug.Log("The result of the sum is: " + output[0]);

            // dispose all native parts
            input.Dispose();
            output.Dispose();
        }
Ejemplo n.º 9
0
    public void TestBurstCompile()
    {
        //using (NativeArray<float> inputArray = new NativeArray<float>(10, Allocator.Persistent))
        //{
        //    using (NativeArray<float> outputArray = new NativeArray<float>(1, Allocator.Persistent))
        //    {
        //        for (int i =0;i < inputArray.Length;++i)
        //        {
        //            inputArray[i] = 1.0f * i;
        //        }

        //    }
        //}

        NativeArray <float> inputArray  = new NativeArray <float>(10, Allocator.Persistent);
        NativeArray <float> outputArray = new NativeArray <float>(1, Allocator.Persistent);

        var job = new MyJob
        {
            Input  = inputArray,
            Output = outputArray,
        };

        for (int i = 0; i < inputArray.Length; ++i)
        {
            inputArray[i] = 1.0f * i;
        }
        job.Schedule().Complete();
        Debug.Log("The result of the sum is: " + outputArray[0]);
        inputArray.Dispose();
        outputArray.Dispose();
    }
Ejemplo n.º 10
0
    private void Start()
    {
        // Create a native array of a single float to store the result in. This example waits for the job to complete
        NativeArray <float> result = new NativeArray <float>(1, Allocator.TempJob);

        // Setup the data for job #1
        MyJob jobData = new MyJob();

        jobData.a      = 10;
        jobData.b      = 10;
        jobData.result = result;

        // Schedule job #1
        JobHandle firstHandle = jobData.Schedule();

        // Setup the data for job #2
        AddOneJob incJobData = new AddOneJob();

        incJobData.result = result;

        // Schedule job #2
        JobHandle secondHandle = incJobData.Schedule(firstHandle);

        // Wait for job #2 to complete
        secondHandle.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        float aPlusB = result[0];

        // Free the memory allocated by the result array
        result.Dispose();

        Debug.Log($"O valor de sei la o que é {aPlusB}");
    }
Ejemplo n.º 11
0
    void jobTest()
    {
        NativeArray <float> result = new NativeArray <float>(1, Allocator.TempJob);

        // Set up the job data
        MyJob jobData = new MyJob
        {
            a      = 10,
            b      = 10,
            result = result
        };

        // Schedule the job
        JobHandle handle = jobData.Schedule();

        // Wait for the job to complete
        handle.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        float aPlusB = result[0];

        Debug.Log(result[0]);
        // Free the memory allocated by the result array
        result.Dispose();
    }
    void ScheduleTestJob()
    {
        // Create a native array of a single float to store the result. This example waits for the job to complete for illustration purposes
        NativeArray <float> result = new NativeArray <float>(1, Allocator.TempJob);

        MyJob tempJob = new MyJob();

        tempJob.a      = 10;
        tempJob.b      = 10;
        tempJob.result = result;

        JobHandle tempJobHandle = tempJob.Schedule();

        // Setup the data for job #2
        AddOneJob addOneJob = new AddOneJob();

        addOneJob.result = result;

        JobHandle secondJobHandle = addOneJob.Schedule(tempJobHandle);

        secondJobHandle.Complete();

        float resultValue = result[0];

        Debug.Log("Result of the myJob: " + resultValue);

        result.Dispose();
    }
Ejemplo n.º 13
0
    //多个jobs依赖关系
    public void SchedulingTwoJobs()
    {
        NativeArray <float> result = new NativeArray <float>(1, Allocator.TempJob);
        MyJob jobData = new MyJob();

        jobData.a      = 10;
        jobData.b      = 10;
        jobData.result = result;
        // 设置第一个任务开关
        JobHandle firstHandle = jobData.Schedule();

        //设置第二个任务
        AddOneJob incJobData = new AddOneJob();

        incJobData.result = result;

        // UnityEngine.Debug.Log(result[0]);
        //设置依赖
        JobHandle secondHandle = incJobData.Schedule(firstHandle);

        //等待完成
        secondHandle.Complete();

        float aPlusB = result[0];

        UnityEngine.Debug.Log(aPlusB);

        //释放资源
        result.Dispose();
    }
Ejemplo n.º 14
0
    // Start is called before the first frame update
    void Start()
    {
        // Create a native array of a single float to store the result. This example waits for the job to complete for illustration purposes
        NativeArray <float> result = new NativeArray <float>(1, Allocator.TempJob);

        // Set up the job data
        MyJob jobData = new MyJob();

        //jobData.obj = new TestA();
        jobData.a      = 10;
        jobData.b      = 10;
        jobData.result = result;

        // Schedule the job
        JobHandle handle = jobData.Schedule();

        // Wait for the job to complete
        handle.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        float aPlusB = result[0];

        // Free the memory allocated by the result array
        result.Dispose();

        var sw    = new System.Diagnostics.Stopwatch();
        var a     = new NativeArray <long>(4096, Allocator.Temp);
        var b     = new long[4096];
        var count = 10000000;

        Random.InitState(0);
        long s       = 0;
        var  monitor = new object();

        sw.Restart();
        for (int i = 0; i < count; i++)
        {
            var index = Random.Range(0, 4096);
            b[index] = Random.Range(int.MinValue, int.MaxValue);
            s       += b[index];
        }
        sw.Stop();
        Debug.Log($"{sw.ElapsedTicks}, {sw.ElapsedMilliseconds}, {s}"); // 0,0000509

        Random.InitState(0);
        s = 0;
        sw.Restart();
        for (int i = 0; i < count; i++)
        {
            lock (monitor)
            {
                var index = Random.Range(0, 4096);
                b[index] = Random.Range(int.MinValue, int.MaxValue);
                s       += b[index];
            }
        }
        sw.Stop();
        Debug.Log($"{sw.ElapsedTicks}, {sw.ElapsedMilliseconds}, {s}");
    }
Ejemplo n.º 15
0
    // Update is called once per frame
    void Update()
    {
        jobHandle.Complete();

        job.dir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

        jobHandle = job.Schedule(transAccessArr);
    }
Ejemplo n.º 16
0
        protected override JobHandle OnUpdate(JobHandle handle)
        {
            var job = new MyJob {
                TranslationType = GetArchetypeChunkComponentType <Translation>(true /* isReadOnly */),
            };

            handle = job.Schedule(m_Query, handle);
            return(handle);
        }
Ejemplo n.º 17
0
 public IEnumerator JobSystem() => UniTask.ToCoroutine(async() =>
 {
     var job = new MyJob()
     {
         loopCount = 999, inOut = new NativeArray <int>(1, Allocator.TempJob)
     };
     JobHandle.ScheduleBatchedJobs();
     await job.Schedule();
     job.inOut[0].Should().Be(999);
     job.inOut.Dispose();
 });
Ejemplo n.º 18
0
        public async UniTask JobSystem()
        {
            var job = new MyJob()
            {
                loopCount = 999, inOut = new NativeArray <int>(1, Allocator.TempJob)
            };
            await job.Schedule();

            job.inOut[0].Is(999);
            job.inOut.Dispose();
        }
Ejemplo n.º 19
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            var job = new MyJob()
            {
                Commands = this.commands.CreateCommandBuffer().ToConcurrent()
            };
            var handle = job.Schedule(this.query, inputDeps);

            this.commands.AddJobHandleForProducer(handle);
            return(handle);
        }
        void Update()
        {
            _batch.Dependency.Complete();

            var buffer = _batch.buffer;

            buffer.Length = 3;
            var job = new MyJob {
                Buffer       = buffer.AsArray(),
                LocalToWorld = transform.localToWorldMatrix
            };

            _batch.Dependency = job.Schedule(_batch.Dependency);
        }
    public void JobFunctionPointerTest()
    {
        var funcPtr = BurstCompiler.CompileFunctionPointer <DoThingDelegate>(DoThing);

        var job   = new MyJob();
        int value = 5;

        job.Blah            = &value;
        job.FunctionPointer = funcPtr;

        job.Schedule().Complete();

        Assert.AreEqual(6, value);
    }
Ejemplo n.º 22
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            var commandBuffer = _entityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();

            var handle = inputDeps;
            var job    = new MyJob {
                CommandBuffer = commandBuffer,
                Time          = UTJ.Time.GetCurrent(),
            };

            handle = job.Schedule(this, dependsOn: handle);
            _entityCommandBufferSystem.AddJobHandleForProducer(handle);

            return(handle);
        }
        protected override unsafe JobHandle OnUpdate(JobHandle handle)
        {
            _batchMatrices.Clear();

            var chunkArray = _query.CreateArchetypeChunkArray(Allocator.TempJob);
            var job        = new MyJob {
                Time           = UTJ.Time.GetCurrent(),
                DistortionType = GetArchetypeChunkComponentType <DistortionComponent>(),
                ChunkArray     = chunkArray,
                Matrices       = _batchMatrices,
            };

            handle = job.Schedule(handle);
            _renderDistortionSystem.AddJobHandleForProducer(handle);
            return(handle);
        }
Ejemplo n.º 24
0
        protected override JobHandle OnUpdate(JobHandle handle)
        {
            var job = new MyJob {
                MCollisionWorld = _buildPhysicsWorldSystem.PhysicsWorld.CollisionWorld,
                TranslationType = GetArchetypeChunkComponentType <Translation>(true /* isReadOnly */),
                RotationType    = GetArchetypeChunkComponentType <Rotation>(true /* isReadOnly */),
                GroundHeightInfoComponentType = GetArchetypeChunkComponentType <GroundHeightInfoComponent>(true /* isReadOnly */),
                GroundHeightComponentType     = GetArchetypeChunkComponentType <GroundHeightComponent>(false /* isReadOnly */),
            };

            handle = job.Schedule(_query, handle);

            _collisionSystem.AddDependingJobHandle(handle);

            return(handle);
        }
Ejemplo n.º 25
0
    // Start is called before the first frame update
    void Start()
    {
        valuesList = new List <NativeArray <int> >();
        handles    = new List <JobHandle>();

        for (int i = 0; i < 10; i++)
        {
            valuesList.Add(new NativeArray <int>((new List <int> {
                i * 1, i * 2, i * 3, i * 4, i * 5
            }).ToArray(), Allocator.TempJob));

            MyJob job = new MyJob();
            job.a      = i;
            job.values = valuesList[valuesList.Count - 1];
            handles.Add(job.Schedule());
        }
    }
Ejemplo n.º 26
0
    // Start is called before the first frame update
    void Start()
    {
        var results = new NativeArray <float>(1, Allocator.TempJob);
        var job     = new MyJob
        {
            a       = 1, b = 123,
            results = results
        };

        var h = job.Schedule();

        h.Complete();

        //job.Run();
        Debug.Log(results[0]);

        results.Dispose();
    }
Ejemplo n.º 27
0
    // Update is called once per frame
    void Update()
    {
        NativeArray <float> array = new NativeArray <float>(1, Allocator.TempJob);
        MyJob job = new MyJob();

        job.a      = 1;
        job.b      = 3;
        job.result = array;

        JobHandle handle = job.Schedule();

        handle.Complete();
        var data = job.result[0];

        Debug.LogError(data);

        array.Dispose();
    }
Ejemplo n.º 28
0
        protected override JobHandle OnUpdate(JobHandle handle)
        {
            _batchMatrices.Clear();

            var chunkArray = _query.CreateArchetypeChunkArray(Allocator.TempJob);
            var job        = new MyJob {
                TranslationType      = GetArchetypeChunkComponentType <Translation>(),
                CachedBeamMatrixType = GetArchetypeChunkComponentType <CachedBeamMatrix>(),
                BeamType             = GetArchetypeChunkComponentType <BeamComponent>(),
                ChunkArray           = chunkArray,
                Matrices             = _batchMatrices,
            };

            handle = job.Schedule(handle);
            _renderBeamSystem.AddJobHandleForProducer(handle);

            return(handle);
        }
Ejemplo n.º 29
0
    private void Start()
    {
        NativeArray <float> res = new NativeArray <float>(1, Allocator.TempJob);

        job = new MyJob {
            a = 10, b = 5, result = res
        };

        var handler = job.Schedule();

        handler.Complete();

        float eheh = res[0];

        print(eheh);

        res.Dispose();
    }
Ejemplo n.º 30
0
    //单个jobs
    public void SchedulingJobs()
    {
        NativeArray <float> result = new NativeArray <float>(1, Allocator.TempJob);
        MyJob jobData = new MyJob();

        jobData.a      = 10;
        jobData.b      = 10;
        jobData.result = result;

        //开始一个任务
        JobHandle handle = jobData.Schedule();

        //等待job完成
        handle.Complete();

        float aPlusB = result[0];

        UnityEngine.Debug.Log("result:" + aPlusB);
        //释放内存中的result
        result.Dispose();
    }