ThreadHive.cpp 5.5 KB

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