ThreadHive.cpp 5.2 KB

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