public void ParallelForJobCanUseParallelPtr()
        {
            using (NativeArray <int> array = new NativeArray <int>(
                       3,
                       Allocator.TempJob))
            {
                NativeArray <int> arrayCopy = array;
                arrayCopy[0] = 10;
                arrayCopy[1] = 20;
                arrayCopy[2] = 30;

                using (NativePerJobThreadIntPtr sum = new NativePerJobThreadIntPtr(
                           Allocator.TempJob))
                {
                    ParallelForTestJob job = new ParallelForTestJob
                    {
                        Array = array,
                        Sum   = sum.GetParallel()
                    };
                    job.Run(array.Length);

                    Assert.That(sum.Value, Is.EqualTo(60));
                }
            }
        }
 public void ConstructorDefaultsValueToZero()
 {
     using (NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                Allocator.Temp))
     {
         Assert.That(intPtr.Value, Is.EqualTo(0));
     }
 }
 public void ConstructorSetsInitialValue()
 {
     using (NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                Allocator.Temp, 123))
     {
         Assert.That(intPtr.Value, Is.EqualTo(123));
     }
 }
        public void DisposeMakesUnusable()
        {
            NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                Allocator.Temp);

            intPtr.Dispose();
            Assert.That(
                () => intPtr.Value = 10,
                Throws.TypeOf <InvalidOperationException>());
        }
 public void DisposeRequiresReadAccess()
 {
     using (NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                Allocator.Temp))
     {
         AssertRequiresReadOrWriteAccess(
             intPtr,
             intPtr.Dispose);
     }
 }
 public void ParallelAddRequiresReadAccess()
 {
     using (NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                Allocator.Temp))
     {
         NativePerJobThreadIntPtr.Parallel parallel = intPtr.GetParallel();
         AssertRequiresReadOrWriteAccess(
             intPtr,
             () => parallel.Add(10));
     }
 }
 public void ParallelDecrementRequiresReadAccess()
 {
     using (NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                Allocator.Temp))
     {
         NativePerJobThreadIntPtr.Parallel parallel = intPtr.GetParallel();
         AssertRequiresReadOrWriteAccess(
             intPtr,
             parallel.Decrement);
     }
 }
 public void SetValueRequiresReadAccess()
 {
     using (NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                Allocator.Temp))
     {
         NativePerJobThreadIntPtr copy = intPtr;
         AssertRequiresReadOrWriteAccess(
             intPtr,
             () => copy.Value = 123);
     }
 }
 public void GetValueRequiresReadAccess()
 {
     using (NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                Allocator.Temp))
     {
         int value;
         AssertRequiresReadOrWriteAccess(
             intPtr,
             () => value = intPtr.Value);
     }
 }
        public void IsCreatedOnlyReturnsTrueBeforeDispose()
        {
            NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                Allocator.Temp);

            Assert.That(intPtr.IsCreated, Is.True);

            intPtr.Dispose();

            Assert.That(intPtr.IsCreated, Is.False);
        }
        public void GetValueReturnsWhatSetValueSets()
        {
            using (NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                       Allocator.Temp))
            {
                NativePerJobThreadIntPtr copy = intPtr;
                copy.Value = 123;

                Assert.That(intPtr.Value, Is.EqualTo(123));
            }
        }
        public void ParallelDecrementIncrementsValue()
        {
            using (NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                       Allocator.Temp,
                       123))
            {
                NativePerJobThreadIntPtr.Parallel parallel = intPtr.GetParallel();
                parallel.Decrement();

                Assert.That(intPtr.Value, Is.EqualTo(122));
            }
        }
        public void CanDeallocateOnJobCompletion()
        {
            NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                Allocator.TempJob);
            var job = new DeallocateOnJobCompletionJob {
                IntPtr = intPtr
            };

            job.Run();

            Assert.That(
                () => intPtr.Value = 10,
                Throws.TypeOf <InvalidOperationException>());
        }
        public void ParallelAddOffsetsValue()
        {
            using (NativePerJobThreadIntPtr intPtr = new NativePerJobThreadIntPtr(
                       Allocator.Temp,
                       123))
            {
                NativePerJobThreadIntPtr.Parallel parallel = intPtr.GetParallel();
                parallel.Add(5);

                Assert.That(intPtr.Value, Is.EqualTo(128));

                parallel.Add(-15);

                Assert.That(intPtr.Value, Is.EqualTo(113));
            }
        }
 private static void AssertRequiresReadOrWriteAccess(
     NativePerJobThreadIntPtr intPtr,
     Action action)
 {
     intPtr.TestUseOnlySetAllowReadAndWriteAccess(false);
     try
     {
         Assert.That(
             () => action(),
             Throws.TypeOf <InvalidOperationException>());
     }
     finally
     {
         intPtr.TestUseOnlySetAllowReadAndWriteAccess(true);
     }
 }
Exemplo n.º 16
0
    // Warm up the job system

    static void WarmUpJobSystem()
    {
        // Create native collections

        NativeArray <int> sum   = new NativeArray <int>(1, Allocator.TempJob);
        NativeArray <int> array = new NativeArray <int>(
            4,
            Allocator.TempJob);
        NativeList <int> list = new NativeList <int>(
            4,
            Allocator.TempJob);
        NativeLinkedList <int> linkedList = new NativeLinkedList <int>(
            4,
            Allocator.TempJob);
        NativeChunkedList <int> chunkedList = new NativeChunkedList <int>(
            4,
            4,
            Allocator.TempJob);
        NativeHashSet <int> hashSet = new NativeHashSet <int>(
            4,
            Allocator.TempJob);
        NativeIntPtr             nativeIntPtr             = new NativeIntPtr(Allocator.TempJob);
        NativePerJobThreadIntPtr nativePerJobThreadIntPtr = new NativePerJobThreadIntPtr(
            Allocator.TempJob);

        // Create jobs

        ListAddJob listAddJob = new ListAddJob
        {
            List = list
        };
        LinkedListAddJob linkedListAddJob = new LinkedListAddJob
        {
            List = linkedList
        };
        ChunkedListAddJob chunkedListAddJob = new ChunkedListAddJob
        {
            List = chunkedList
        };
        HashSetAddJob hashSetAddJob = new HashSetAddJob
        {
            Set = hashSet
        };
        ArrayIterateJob arrayIterateJob = new ArrayIterateJob
        {
            Array = array,
            Sum   = sum
        };
        ListIterateJob listIterateJob = new ListIterateJob
        {
            List = list,
            Sum  = sum
        };
        LinkedListIterateJob linkedListIterateJob = new LinkedListIterateJob
        {
            List = linkedList,
            Sum  = sum
        };
        ChunkedListIterateJob chunkedListIterateJob = new ChunkedListIterateJob
        {
            List = chunkedList,
            Sum  = sum
        };
        ArrayIterateJobParallelFor arrayIterateJobParallelFor = new ArrayIterateJobParallelFor
        {
            Array = array,
            Sum   = sum
        };
        LinkedListIterateJobParallelFor linkedListIterateJobParallelFor = new LinkedListIterateJobParallelFor
        {
            List = linkedList,
            Sum  = sum
        };
        ChunkedListIterateJobParallelFor chunkedListIterateJobParallelFor = new ChunkedListIterateJobParallelFor
        {
            List = chunkedList,
            Sum  = sum
        };
        LinkedListInsertJob linkedListInsertJob = new LinkedListInsertJob
        {
            LinkedList = linkedList
        };
        ChunkedListInsertJob chunkedListInsertJob = new ChunkedListInsertJob
        {
            List = chunkedList
        };
        LinkedListRemoveJob linkedListRemoveJob = new LinkedListRemoveJob
        {
            List = linkedList
        };
        ChunkedListRemoveJob chunkedListRemoveJob = new ChunkedListRemoveJob
        {
            List = chunkedList
        };
        HashSetRemoveJob hashSetRemoveJob = new HashSetRemoveJob
        {
            Set = hashSet
        };
        NativeIntPtrParallelJob nativeIntPtrParallelJob = new NativeIntPtrParallelJob
        {
            Array = array,
            Sum   = nativeIntPtr.GetParallel()
        };
        NativePerJobThreadIntPtrParallelJob nativePerJobThreadIntPtrParallelJob = new NativePerJobThreadIntPtrParallelJob
        {
            Array = array,
            Sum   = nativePerJobThreadIntPtr.GetParallel()
        };

        // Run jobs

        listAddJob.Run();
        linkedListAddJob.Run();
        chunkedListAddJob.Run();
        hashSetAddJob.Run();
        arrayIterateJob.Run();
        listIterateJob.Run();
        linkedListIterateJob.Run();
        chunkedListIterateJob.Run();
        arrayIterateJobParallelFor.Run(array.Length);
        linkedListIterateJobParallelFor.Run(linkedList.Length);
        chunkedListIterateJobParallelFor.RunRanged(chunkedList.Length);
        list.Clear();
        linkedList.Clear();
        chunkedList.Clear();
        hashSet.Clear();
        linkedListInsertJob.Run();
        chunkedListInsertJob.Run();
        linkedListRemoveJob.Run();
        chunkedListRemoveJob.Run();
        hashSetRemoveJob.Run();
        nativeIntPtrParallelJob.Run(array.Length);
        nativePerJobThreadIntPtrParallelJob.Run(array.Length);

        // Dispose native collections

        sum.Dispose();
        array.Dispose();
        list.Dispose();
        linkedList.Dispose();
        chunkedList.Dispose();
        hashSet.Dispose();
        nativeIntPtr.Dispose();
        nativePerJobThreadIntPtr.Dispose();
    }
Exemplo n.º 17
0
    // Run the test

    void Start()
    {
        WarmUpJobSystem();

        const int size =
#if UNITY_EDITOR
            1000
#else
            10000
#endif
        ;

        const int chunkSize           = 1024;
        const int numElementsPerChunk = chunkSize / sizeof(int);

        // Create native collections

        NativeArray <int> sum   = new NativeArray <int>(1, Allocator.TempJob);
        NativeArray <int> array = new NativeArray <int>(
            size,
            Allocator.TempJob);
        NativeList <int> list = new NativeList <int>(
            0,
            Allocator.TempJob);
        NativeLinkedList <int> linkedList = new NativeLinkedList <int>(
            0,
            Allocator.TempJob);
        NativeChunkedList <int> chunkedList = new NativeChunkedList <int>(
            numElementsPerChunk,
            0,
            Allocator.TempJob);
        NativeHashSet <int> hashSet = new NativeHashSet <int>(
            0,
            Allocator.TempJob);
        NativeIntPtr             nativeIntPtr             = new NativeIntPtr(Allocator.TempJob);
        NativePerJobThreadIntPtr nativePerJobThreadIntPtr = new NativePerJobThreadIntPtr(
            Allocator.TempJob);

        // Run add jobs

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

        ListAddJob listAddJob = new ListAddJob
        {
            List             = list,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        listAddJob.Run();
        long listAddTicks = sw.ElapsedTicks;

        LinkedListAddJob linkedListAddJob = new LinkedListAddJob
        {
            List             = linkedList,
            NumNodesToInsert = size
        };

        sw.Reset();
        sw.Start();
        linkedListAddJob.Run();
        long linkedListAddTicks = sw.ElapsedTicks;

        ChunkedListAddJob chunkedListAddJob = new ChunkedListAddJob
        {
            List             = chunkedList,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        chunkedListAddJob.Run();
        long chunkedListAddTicks = sw.ElapsedTicks;

        HashSetAddJob hashSetAddJob = new HashSetAddJob
        {
            Set = hashSet,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        hashSetAddJob.Run();
        long hashSetAddTicks = sw.ElapsedTicks;

        // Run iterate jobs

        ArrayIterateJob arrayIterateJob = new ArrayIterateJob
        {
            Array = array,
            Sum   = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        arrayIterateJob.Run();
        long arrayIterateTicks = sw.ElapsedTicks;

        ListIterateJob listIterateJob = new ListIterateJob
        {
            List = list,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        listIterateJob.Run();
        long listIterateTicks = sw.ElapsedTicks;

        LinkedListIterateJob linkedListIterateJob = new LinkedListIterateJob
        {
            List = linkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        linkedListIterateJob.Run();
        long linkedListIterateTicks = sw.ElapsedTicks;

        ChunkedListIterateJob chunkedListIterateJob = new ChunkedListIterateJob
        {
            List = chunkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        chunkedListIterateJob.Run();
        long chunkedListIterateTicks = sw.ElapsedTicks;

        ArrayIterateJobParallelFor arrayIterateJobParallelFor = new ArrayIterateJobParallelFor
        {
            Array = array,
            Sum   = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        arrayIterateJobParallelFor.Run(size);
        long arrayIterateParallelForTicks = sw.ElapsedTicks;

        linkedList.PrepareForParallelForJob();
        LinkedListIterateJobParallelFor linkedListIterateJobParallelFor = new LinkedListIterateJobParallelFor
        {
            List = linkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        linkedListIterateJobParallelFor.Run(size);
        long linkedListIterateParallelForTicks = sw.ElapsedTicks;

        chunkedList.PrepareForParallelForJob();
        ChunkedListIterateJobParallelFor chunkedListIterateJobParallelFor = new ChunkedListIterateJobParallelFor
        {
            List = chunkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        chunkedListIterateJobParallelFor.RunRanged(size);
        long chunkedListIterateParallelForTicks = sw.ElapsedTicks;

        // Clear native collections

        list.Clear();
        linkedList.Clear();
        chunkedList.Clear();

        // Run insert jobs

        LinkedListInsertJob linkedListInsertJob = new LinkedListInsertJob
        {
            LinkedList       = linkedList,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        linkedListInsertJob.Run();
        long linkedListInsertTicks = sw.ElapsedTicks;

        ChunkedListInsertJob chunkedListInsertJob = new ChunkedListInsertJob
        {
            List             = chunkedList,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        chunkedListInsertJob.Run();
        long chunkedListInsertTicks = sw.ElapsedTicks;

        // Run remove jobs

        LinkedListRemoveJob linkedListRemoveJob = new LinkedListRemoveJob
        {
            List = linkedList,
            NumElementsToRemove = size
        };

        sw.Reset();
        sw.Start();
        linkedListRemoveJob.Run();
        long linkedListRemoveTicks = sw.ElapsedTicks;

        ChunkedListRemoveJob chunkedListRemoveJob = new ChunkedListRemoveJob
        {
            List = chunkedList,
            NumElementsToRemove = size
        };

        sw.Reset();
        sw.Start();
        chunkedListRemoveJob.Run();
        long chunkedListRemoveTicks = sw.ElapsedTicks;

        HashSetRemoveJob hashSetRemoveJob = new HashSetRemoveJob
        {
            Set = hashSet,
            NumElementsToRemove = size
        };

        sw.Reset();
        sw.Start();
        hashSetRemoveJob.Run();
        long hashSetRemoveTicks = sw.ElapsedTicks;

        // Run NativeIntPtr and NativePerJobThreadIntPtr jobs

        NativeIntPtrParallelJob nativeIntPtrParallelJob = new NativeIntPtrParallelJob
        {
            Array = array,
            Sum   = nativeIntPtr.GetParallel()
        };

        sw.Reset();
        sw.Start();
        nativeIntPtrParallelJob.Run(size);
        long nativeIntPtrTicks = sw.ElapsedTicks;

        NativePerJobThreadIntPtrParallelJob nativePerJobThreadIntPtrParallelJob = new NativePerJobThreadIntPtrParallelJob
        {
            Array = array,
            Sum   = nativePerJobThreadIntPtr.GetParallel()
        };

        sw.Reset();
        sw.Start();
        nativePerJobThreadIntPtrParallelJob.Run(size);
        long nativePerJobThreadIntPtrTicks = sw.ElapsedTicks;

        // Report results
        Debug.Log(
            "Operation,Job Type,NativeArray,NativeList,NativeLinkedList,NativeChunkedList,NativeHashSet\n" +
            "Add,Single," + "n/a" + "," + listAddTicks + "," + linkedListAddTicks + "," + chunkedListAddTicks + "," + hashSetAddTicks + "\n" +
            "Iterate,Single," + arrayIterateTicks + "," + listIterateTicks + "," + linkedListIterateTicks + "," + chunkedListIterateTicks + "," + "n/a" + "\n" +
            "Iterate,ParallelFor," + arrayIterateParallelForTicks + "," + "n/a" + "," + linkedListIterateParallelForTicks + "," + chunkedListIterateParallelForTicks + "," + "n/a" + "\n" +
            "Insert,Single," + "n/a" + "," + "n/a" + "," + linkedListInsertTicks + "," + chunkedListInsertTicks + "," + "n/a" + "\n" +
            "Remove,Single," + "n/a" + "," + "n/a" + "," + linkedListRemoveTicks + "," + chunkedListRemoveTicks + "," + hashSetRemoveTicks);
        Debug.Log(
            "Operation,Job Type,NativeIntPtr,NativePerJobThreadIntPtr\n" +
            "Sum,ParallelFor," + nativeIntPtrTicks + "," + nativePerJobThreadIntPtrTicks);

        // Dispose native collections
        sum.Dispose();
        array.Dispose();
        list.Dispose();
        linkedList.Dispose();
        chunkedList.Dispose();
        hashSet.Dispose();
        nativeIntPtr.Dispose();
        nativePerJobThreadIntPtr.Dispose();

        // Quit
#if UNITY_EDITOR
        EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
    }