ThreadHive.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright (C) 2009-2023, 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. using namespace anki;
  10. namespace {
  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, [[maybe_unused]] U32 threadId, [[maybe_unused]] ThreadHive& hive, [[maybe_unused]] ThreadHiveSemaphore* sem)
  27. {
  28. ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
  29. ctx->m_countAtomic.fetchSub(2);
  30. }
  31. static void incNumber(void* arg, [[maybe_unused]] U32 threadId, [[maybe_unused]] ThreadHive& hive, [[maybe_unused]] 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, [[maybe_unused]] U32 threadId, [[maybe_unused]] ThreadHive& hive, [[maybe_unused]] 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, [[maybe_unused]] U32 threadId, [[maybe_unused]] ThreadHive& hive, [[maybe_unused]] 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. } // namespace
  51. ANKI_TEST(Util, ThreadHive)
  52. {
  53. const U32 threadCount = 32;
  54. HeapMemoryPool pool(allocAligned, nullptr);
  55. ThreadHive hive(threadCount, &pool);
  56. // Simple test
  57. if(1)
  58. {
  59. ThreadHiveTestContext ctx;
  60. ctx.m_countAtomic.setNonAtomically(0);
  61. const U INITIAL_TASK_COUNT = 100;
  62. for(U i = 0; i < INITIAL_TASK_COUNT; ++i)
  63. {
  64. hive.submitTask(incNumber, &ctx);
  65. }
  66. hive.waitAllTasks();
  67. ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.getNonAtomically(), INITIAL_TASK_COUNT * 2);
  68. }
  69. // Depedency tests
  70. if(1)
  71. {
  72. ThreadHiveTestContext ctx;
  73. ctx.m_count = 0;
  74. ThreadHiveTask task;
  75. task.m_callback = taskToWaitOn;
  76. task.m_argument = &ctx;
  77. task.m_signalSemaphore = hive.newSemaphore(1);
  78. hive.submitTasks(&task, 1);
  79. const U DEP_TASKS = 10;
  80. ThreadHiveTask dtasks[DEP_TASKS];
  81. ThreadHiveSemaphore* sem = hive.newSemaphore(DEP_TASKS);
  82. for(U i = 0; i < DEP_TASKS; ++i)
  83. {
  84. dtasks[i].m_callback = taskToWait;
  85. dtasks[i].m_argument = &ctx;
  86. dtasks[i].m_waitSemaphore = task.m_signalSemaphore;
  87. dtasks[i].m_signalSemaphore = sem;
  88. }
  89. hive.submitTasks(&dtasks[0], DEP_TASKS);
  90. // Again
  91. ThreadHiveTask dtasks2[DEP_TASKS];
  92. for(U i = 0; i < DEP_TASKS; ++i)
  93. {
  94. dtasks2[i].m_callback = taskToWait;
  95. dtasks2[i].m_argument = &ctx;
  96. dtasks2[i].m_waitSemaphore = sem;
  97. }
  98. hive.submitTasks(&dtasks2[0], DEP_TASKS);
  99. hive.waitAllTasks();
  100. ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.getNonAtomically(), DEP_TASKS * 2 + 10);
  101. }
  102. // Fuzzy test
  103. if(1)
  104. {
  105. ThreadHiveTestContext ctx;
  106. ctx.m_count = 0;
  107. I number = 0;
  108. ThreadHiveSemaphore* sem = nullptr;
  109. const U SUBMISSION_COUNT = 100;
  110. const U TASK_COUNT = 1000;
  111. for(U i = 0; i < SUBMISSION_COUNT; ++i)
  112. {
  113. for(U j = 0; j < TASK_COUNT; ++j)
  114. {
  115. Bool cb = rand() % 2;
  116. number = (cb) ? number + 2 : number - 2;
  117. ThreadHiveTask task;
  118. task.m_callback = (cb) ? incNumber : decNumber;
  119. task.m_argument = &ctx;
  120. task.m_signalSemaphore = hive.newSemaphore(1);
  121. if((rand() % 3) == 0 && j > 0 && sem)
  122. {
  123. task.m_waitSemaphore = sem;
  124. }
  125. hive.submitTasks(&task, 1);
  126. if((rand() % 7) == 0)
  127. {
  128. sem = task.m_signalSemaphore;
  129. }
  130. }
  131. sem = nullptr;
  132. hive.waitAllTasks();
  133. }
  134. ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.getNonAtomically(), number);
  135. }
  136. }
  137. namespace {
  138. class FibTask
  139. {
  140. public:
  141. Atomic<U64>* m_sum;
  142. StackAllocator<U8> m_alloc;
  143. U64 m_n;
  144. FibTask(Atomic<U64>* sum, StackAllocator<U8>& alloc, U64 n)
  145. : m_sum(sum)
  146. , m_alloc(alloc)
  147. , m_n(n)
  148. {
  149. }
  150. void doWork(ThreadHive& hive)
  151. {
  152. if(m_n > 1)
  153. {
  154. FibTask* a = m_alloc.newInstance<FibTask>(m_sum, m_alloc, m_n - 1);
  155. FibTask* b = m_alloc.newInstance<FibTask>(m_sum, m_alloc, m_n - 2);
  156. Array<ThreadHiveTask, 2> tasks;
  157. tasks[0].m_callback = tasks[1].m_callback = FibTask::callback;
  158. tasks[0].m_argument = a;
  159. tasks[1].m_argument = b;
  160. hive.submitTasks(&tasks[0], tasks.getSize());
  161. }
  162. else
  163. {
  164. m_sum->fetchAdd(m_n);
  165. }
  166. }
  167. static void callback(void* arg, [[maybe_unused]] U32 taskId, ThreadHive& hive, [[maybe_unused]] ThreadHiveSemaphore* sem)
  168. {
  169. static_cast<FibTask*>(arg)->doWork(hive);
  170. }
  171. };
  172. static U64 fib(U64 n)
  173. {
  174. if(n > 1)
  175. {
  176. return fib(n - 1) + fib(n - 2);
  177. }
  178. else
  179. {
  180. return n;
  181. }
  182. }
  183. } // namespace
  184. ANKI_TEST(Util, ThreadHiveBench)
  185. {
  186. static const U FIB_N = 32;
  187. const U32 threadCount = getCpuCoresCount();
  188. HeapMemoryPool pool(allocAligned, nullptr);
  189. ThreadHive hive(threadCount, true);
  190. StackAllocator<U8> salloc(allocAligned, nullptr, 1024);
  191. Atomic<U64> sum = {0};
  192. FibTask task(&sum, salloc, FIB_N);
  193. auto timeA = HighRezTimer::getCurrentTime();
  194. hive.submitTask(FibTask::callback, &task);
  195. hive.waitAllTasks();
  196. auto timeB = HighRezTimer::getCurrentTime();
  197. const U64 serialFib = fib(FIB_N);
  198. auto timeC = HighRezTimer::getCurrentTime();
  199. ANKI_TEST_LOGI("Total time %fms. Ground truth %fms", (timeB - timeA) * 1000.0, (timeC - timeB) * 1000.0);
  200. ANKI_TEST_EXPECT_EQ(sum.getNonAtomically(), serialFib);
  201. }