ThreadHive.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Tests/Framework/Framework.h>
  6. #include <anki/util/ThreadHive.h>
  7. #include <anki/util/HighRezTimer.h>
  8. #include <anki/util/System.h>
  9. namespace anki
  10. {
  11. class ThreadHiveTestContext
  12. {
  13. public:
  14. ThreadHiveTestContext()
  15. {
  16. }
  17. ~ThreadHiveTestContext()
  18. {
  19. }
  20. union
  21. {
  22. Atomic<I32> m_countAtomic;
  23. I32 m_count;
  24. };
  25. };
  26. static void decNumber(void* arg, U32, ThreadHive& hive, ThreadHiveSemaphore* sem)
  27. {
  28. ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
  29. ctx->m_countAtomic.fetchSub(2);
  30. }
  31. static void incNumber(void* arg, U32, ThreadHive& hive, ThreadHiveSemaphore* sem)
  32. {
  33. ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
  34. ctx->m_countAtomic.fetchAdd(4);
  35. hive.submitTask(decNumber, arg);
  36. }
  37. static void taskToWaitOn(void* arg, U32, ThreadHive& hive, ThreadHiveSemaphore* sem)
  38. {
  39. ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
  40. HighRezTimer::sleep(1.0);
  41. ctx->m_count = 10;
  42. HighRezTimer::sleep(0.1);
  43. }
  44. static void taskToWait(void* arg, U32 threadId, ThreadHive& hive, ThreadHiveSemaphore* sem)
  45. {
  46. ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
  47. U prev = ctx->m_countAtomic.fetchAdd(1);
  48. ANKI_TEST_EXPECT_GEQ(prev, 10);
  49. }
  50. ANKI_TEST(Util, ThreadHive)
  51. {
  52. const U32 threadCount = 32;
  53. HeapAllocator<U8> alloc(allocAligned, nullptr);
  54. ThreadHive hive(threadCount, alloc);
  55. // Simple test
  56. if(1)
  57. {
  58. ThreadHiveTestContext ctx;
  59. ctx.m_countAtomic.setNonAtomically(0);
  60. const U INITIAL_TASK_COUNT = 100;
  61. for(U i = 0; i < INITIAL_TASK_COUNT; ++i)
  62. {
  63. hive.submitTask(incNumber, &ctx);
  64. }
  65. hive.waitAllTasks();
  66. ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.getNonAtomically(), INITIAL_TASK_COUNT * 2);
  67. }
  68. // Depedency tests
  69. if(1)
  70. {
  71. ThreadHiveTestContext ctx;
  72. ctx.m_count = 0;
  73. ThreadHiveTask task;
  74. task.m_callback = taskToWaitOn;
  75. task.m_argument = &ctx;
  76. task.m_signalSemaphore = hive.newSemaphore(1);
  77. hive.submitTasks(&task, 1);
  78. const U DEP_TASKS = 10;
  79. ThreadHiveTask dtasks[DEP_TASKS];
  80. ThreadHiveSemaphore* sem = hive.newSemaphore(DEP_TASKS);
  81. for(U i = 0; i < DEP_TASKS; ++i)
  82. {
  83. dtasks[i].m_callback = taskToWait;
  84. dtasks[i].m_argument = &ctx;
  85. dtasks[i].m_waitSemaphore = task.m_signalSemaphore;
  86. dtasks[i].m_signalSemaphore = sem;
  87. }
  88. hive.submitTasks(&dtasks[0], DEP_TASKS);
  89. // Again
  90. ThreadHiveTask dtasks2[DEP_TASKS];
  91. for(U i = 0; i < DEP_TASKS; ++i)
  92. {
  93. dtasks2[i].m_callback = taskToWait;
  94. dtasks2[i].m_argument = &ctx;
  95. dtasks2[i].m_waitSemaphore = sem;
  96. }
  97. hive.submitTasks(&dtasks2[0], DEP_TASKS);
  98. hive.waitAllTasks();
  99. ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.getNonAtomically(), DEP_TASKS * 2 + 10);
  100. }
  101. // Fuzzy test
  102. if(1)
  103. {
  104. ThreadHiveTestContext ctx;
  105. ctx.m_count = 0;
  106. I number = 0;
  107. ThreadHiveSemaphore* sem = nullptr;
  108. const U SUBMISSION_COUNT = 100;
  109. const U TASK_COUNT = 1000;
  110. for(U i = 0; i < SUBMISSION_COUNT; ++i)
  111. {
  112. for(U j = 0; j < TASK_COUNT; ++j)
  113. {
  114. Bool cb = rand() % 2;
  115. number = (cb) ? number + 2 : number - 2;
  116. ThreadHiveTask task;
  117. task.m_callback = (cb) ? incNumber : decNumber;
  118. task.m_argument = &ctx;
  119. task.m_signalSemaphore = hive.newSemaphore(1);
  120. if((rand() % 3) == 0 && j > 0 && sem)
  121. {
  122. task.m_waitSemaphore = sem;
  123. }
  124. hive.submitTasks(&task, 1);
  125. if((rand() % 7) == 0)
  126. {
  127. sem = task.m_signalSemaphore;
  128. }
  129. }
  130. sem = nullptr;
  131. hive.waitAllTasks();
  132. }
  133. ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.getNonAtomically(), number);
  134. }
  135. }
  136. class FibTask
  137. {
  138. public:
  139. Atomic<U64>* m_sum;
  140. StackAllocator<U8> m_alloc;
  141. U64 m_n;
  142. FibTask(Atomic<U64>* sum, StackAllocator<U8>& alloc, U64 n)
  143. : m_sum(sum)
  144. , m_alloc(alloc)
  145. , m_n(n)
  146. {
  147. }
  148. void doWork(ThreadHive& hive)
  149. {
  150. if(m_n > 1)
  151. {
  152. FibTask* a = m_alloc.newInstance<FibTask>(m_sum, m_alloc, m_n - 1);
  153. FibTask* b = m_alloc.newInstance<FibTask>(m_sum, m_alloc, m_n - 2);
  154. Array<ThreadHiveTask, 2> tasks;
  155. tasks[0].m_callback = tasks[1].m_callback = FibTask::callback;
  156. tasks[0].m_argument = a;
  157. tasks[1].m_argument = b;
  158. hive.submitTasks(&tasks[0], tasks.getSize());
  159. }
  160. else
  161. {
  162. m_sum->fetchAdd(m_n);
  163. }
  164. }
  165. static void callback(void* arg, U32, ThreadHive& hive, ThreadHiveSemaphore* sem)
  166. {
  167. static_cast<FibTask*>(arg)->doWork(hive);
  168. }
  169. };
  170. static U64 fib(U64 n)
  171. {
  172. if(n > 1)
  173. {
  174. return fib(n - 1) + fib(n - 2);
  175. }
  176. else
  177. {
  178. return n;
  179. }
  180. }
  181. ANKI_TEST(Util, ThreadHiveBench)
  182. {
  183. static const U FIB_N = 32;
  184. const U32 threadCount = getCpuCoresCount();
  185. HeapAllocator<U8> alloc(allocAligned, nullptr);
  186. ThreadHive hive(threadCount, alloc, true);
  187. StackAllocator<U8> salloc(allocAligned, nullptr, 1024);
  188. Atomic<U64> sum = {0};
  189. FibTask task(&sum, salloc, FIB_N);
  190. auto timeA = HighRezTimer::getCurrentTime();
  191. hive.submitTask(FibTask::callback, &task);
  192. hive.waitAllTasks();
  193. auto timeB = HighRezTimer::getCurrentTime();
  194. const U64 serialFib = fib(FIB_N);
  195. auto timeC = HighRezTimer::getCurrentTime();
  196. ANKI_TEST_LOGI("Total time %fms. Ground truth %fms", (timeB - timeA) * 1000.0, (timeC - timeB) * 1000.0);
  197. ANKI_TEST_EXPECT_EQ(sum.getNonAtomically(), serialFib);
  198. }
  199. } // end namespace anki