Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NativeParallelMultiHashMapTests_InJobs.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using NUnit.Framework;
  2. using System.Collections.Generic;
  3. using Unity.Jobs;
  4. using Unity.Burst;
  5. using Unity.Collections;
  6. using Assert = FastAssert;
  7. namespace Unity.Collections.Tests
  8. {
  9. internal class NativeParallelMultiHashMapTests_InJobs : NativeParallelMultiHashMapTestsFixture
  10. {
  11. [Test]
  12. public void NativeParallelMultiHashMap_Read_And_Write()
  13. {
  14. var hashMap = new NativeParallelMultiHashMap<int, int>(hashMapSize, CommonRwdAllocator.Handle);
  15. var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  16. var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  17. var writeData = new MultiHashMapWriteParallelForJob()
  18. {
  19. hashMap = hashMap.AsParallelWriter(),
  20. status = writeStatus,
  21. keyMod = hashMapSize,
  22. };
  23. var readData = new MultiHashMapReadParallelForJob()
  24. {
  25. hashMap = hashMap,
  26. values = readValues,
  27. keyMod = writeData.keyMod,
  28. };
  29. var writeJob = writeData.Schedule(hashMapSize, 1);
  30. var readJob = readData.Schedule(hashMapSize, 1, writeJob);
  31. readJob.Complete();
  32. for (int i = 0; i < hashMapSize; ++i)
  33. {
  34. Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
  35. Assert.AreEqual(1, readValues[i], "Job failed to read from hash map");
  36. }
  37. hashMap.Dispose();
  38. writeStatus.Dispose();
  39. readValues.Dispose();
  40. }
  41. [Test]
  42. [TestRequiresCollectionChecks]
  43. public void NativeParallelMultiHashMap_Read_And_Write_Full()
  44. {
  45. var hashMap = new NativeParallelMultiHashMap<int, int>(hashMapSize / 2, CommonRwdAllocator.Handle);
  46. var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  47. var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  48. var writeData = new MultiHashMapWriteParallelForJob()
  49. {
  50. hashMap = hashMap.AsParallelWriter(),
  51. status = writeStatus,
  52. keyMod = hashMapSize,
  53. };
  54. var readData = new MultiHashMapReadParallelForJob()
  55. {
  56. hashMap = hashMap,
  57. values = readValues,
  58. keyMod = writeData.keyMod,
  59. };
  60. var writeJob = writeData.Schedule(hashMapSize, 1);
  61. var readJob = readData.Schedule(hashMapSize, 1, writeJob);
  62. readJob.Complete();
  63. var missing = new Dictionary<int, bool>();
  64. for (int i = 0; i < hashMapSize; ++i)
  65. {
  66. if (writeStatus[i] == -2)
  67. {
  68. missing[i] = true;
  69. Assert.AreEqual(-1, readValues[i], "Job read a value form hash map which should not be there");
  70. }
  71. else
  72. {
  73. Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
  74. Assert.AreEqual(1, readValues[i], "Job failed to read from hash map");
  75. }
  76. }
  77. Assert.AreEqual(hashMapSize - hashMapSize / 2, missing.Count, "Wrong indices written to hash map");
  78. hashMap.Dispose();
  79. writeStatus.Dispose();
  80. readValues.Dispose();
  81. }
  82. [Test]
  83. public void NativeParallelMultiHashMap_Key_Collisions()
  84. {
  85. var hashMap = new NativeParallelMultiHashMap<int, int>(hashMapSize, CommonRwdAllocator.Handle);
  86. var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  87. var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  88. var writeData = new MultiHashMapWriteParallelForJob()
  89. {
  90. hashMap = hashMap.AsParallelWriter(),
  91. status = writeStatus,
  92. keyMod = 16,
  93. };
  94. var readData = new MultiHashMapReadParallelForJob()
  95. {
  96. hashMap = hashMap,
  97. values = readValues,
  98. keyMod = writeData.keyMod,
  99. };
  100. var writeJob = writeData.Schedule(hashMapSize, 1);
  101. var readJob = readData.Schedule(hashMapSize, 1, writeJob);
  102. readJob.Complete();
  103. for (int i = 0; i < hashMapSize; ++i)
  104. {
  105. Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
  106. Assert.AreEqual(hashMapSize / readData.keyMod, readValues[i], "Job failed to read from hash map");
  107. }
  108. hashMap.Dispose();
  109. writeStatus.Dispose();
  110. readValues.Dispose();
  111. }
  112. [BurstCompile(CompileSynchronously = true)]
  113. struct AddMultiIndex : IJobParallelFor
  114. {
  115. public NativeParallelMultiHashMap<int, int>.ParallelWriter hashMap;
  116. public void Execute(int index)
  117. {
  118. hashMap.Add(index, index);
  119. }
  120. }
  121. [Test]
  122. public void NativeParallelMultiHashMap_TryMultiAddScalabilityConcurrent()
  123. {
  124. for (int count = 0; count < 1024; count++)
  125. {
  126. var hashMap = new NativeParallelMultiHashMap<int, int>(count, CommonRwdAllocator.Handle);
  127. var addIndexJob = new AddMultiIndex
  128. {
  129. hashMap = hashMap.AsParallelWriter()
  130. };
  131. var addIndexJobHandle = addIndexJob.Schedule(count, 64);
  132. addIndexJobHandle.Complete();
  133. hashMap.Dispose();
  134. }
  135. }
  136. }
  137. }