ThreadHive.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Copyright (C) 2009-present, 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. ThreadHive hive(threadCount);
  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. namespace {
  137. class FibTask
  138. {
  139. public:
  140. Atomic<U64>* m_sum;
  141. StackAllocator<U8> m_alloc;
  142. U64 m_n;
  143. FibTask(Atomic<U64>* sum, StackAllocator<U8>& alloc, U64 n)
  144. : m_sum(sum)
  145. , m_alloc(alloc)
  146. , m_n(n)
  147. {
  148. }
  149. void doWork(ThreadHive& hive)
  150. {
  151. if(m_n > 1)
  152. {
  153. FibTask* a = m_alloc.newInstance<FibTask>(m_sum, m_alloc, m_n - 1);
  154. FibTask* b = m_alloc.newInstance<FibTask>(m_sum, m_alloc, m_n - 2);
  155. Array<ThreadHiveTask, 2> tasks;
  156. tasks[0].m_callback = tasks[1].m_callback = FibTask::callback;
  157. tasks[0].m_argument = a;
  158. tasks[1].m_argument = b;
  159. hive.submitTasks(&tasks[0], tasks.getSize());
  160. }
  161. else
  162. {
  163. m_sum->fetchAdd(m_n);
  164. }
  165. }
  166. static void callback(void* arg, [[maybe_unused]] U32 taskId, ThreadHive& hive, [[maybe_unused]] ThreadHiveSemaphore* sem)
  167. {
  168. static_cast<FibTask*>(arg)->doWork(hive);
  169. }
  170. };
  171. static U64 fib(U64 n)
  172. {
  173. if(n > 1)
  174. {
  175. return fib(n - 1) + fib(n - 2);
  176. }
  177. else
  178. {
  179. return n;
  180. }
  181. }
  182. } // namespace
  183. ANKI_TEST(Util, ThreadHiveBench)
  184. {
  185. static const U FIB_N = 32;
  186. const U32 threadCount = getCpuCoresCount();
  187. HeapMemoryPool pool(allocAligned, nullptr);
  188. ThreadHive hive(threadCount, true);
  189. StackAllocator<U8> salloc(allocAligned, nullptr, 1024);
  190. Atomic<U64> sum = {0};
  191. FibTask task(&sum, salloc, FIB_N);
  192. auto timeA = HighRezTimer::getCurrentTime();
  193. hive.submitTask(FibTask::callback, &task);
  194. hive.waitAllTasks();
  195. auto timeB = HighRezTimer::getCurrentTime();
  196. const U64 serialFib = fib(FIB_N);
  197. auto timeC = HighRezTimer::getCurrentTime();
  198. ANKI_TEST_LOGI("Total time %fms. Ground truth %fms", (timeB - timeA) * 1000.0, (timeC - timeB) * 1000.0);
  199. ANKI_TEST_EXPECT_EQ(sum.getNonAtomically(), serialFib);
  200. }