ThreadHive.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright (C) 2009-2016, 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)
  27. {
  28. ThreadHiveTestContext* ctx = static_cast<ThreadHiveTestContext*>(arg);
  29. ctx->m_countAtomic.fetchSub(2);
  30. }
  31. static void incNumber(void* arg, U32, ThreadHive& hive)
  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)
  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)
  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 = 4;
  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.set(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.get(), 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. hive.submitTasks(&task, 1);
  77. const U DEP_TASKS = 10;
  78. ThreadHiveTask dtasks[DEP_TASKS];
  79. for(U i = 0; i < DEP_TASKS; ++i)
  80. {
  81. dtasks[i].m_callback = taskToWait;
  82. dtasks[i].m_argument = &ctx;
  83. dtasks[i].m_inDependencies = WeakArray<ThreadHiveDependencyHandle>(&task.m_outDependency, 1);
  84. }
  85. hive.submitTasks(&dtasks[0], DEP_TASKS);
  86. // Again
  87. ThreadHiveTask dtasks2[DEP_TASKS];
  88. for(U i = 0; i < DEP_TASKS; ++i)
  89. {
  90. dtasks2[i].m_callback = taskToWait;
  91. dtasks2[i].m_argument = &ctx;
  92. dtasks2[i].m_inDependencies = WeakArray<ThreadHiveDependencyHandle>(&dtasks[i].m_outDependency, 1);
  93. }
  94. hive.submitTasks(&dtasks2[0], DEP_TASKS);
  95. hive.waitAllTasks();
  96. ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.get(), DEP_TASKS * 2 + 10);
  97. }
  98. // Fuzzy test
  99. if(1)
  100. {
  101. ThreadHiveTestContext ctx;
  102. ctx.m_count = 0;
  103. I number = 0;
  104. ThreadHiveDependencyHandle dep = 0;
  105. const U SUBMISSION_COUNT = 100;
  106. const U TASK_COUNT = 1000;
  107. for(U i = 0; i < SUBMISSION_COUNT; ++i)
  108. {
  109. for(U j = 0; j < TASK_COUNT; ++j)
  110. {
  111. Bool cb = rand() % 2;
  112. number = (cb) ? number + 2 : number - 2;
  113. ThreadHiveTask task;
  114. task.m_callback = (cb) ? incNumber : decNumber;
  115. task.m_argument = &ctx;
  116. if((rand() % 3) == 0 && j > 0 && dep)
  117. {
  118. task.m_inDependencies = WeakArray<ThreadHiveDependencyHandle>(&dep, 1);
  119. }
  120. hive.submitTasks(&task, 1);
  121. if((rand() % 7) == 0)
  122. {
  123. dep = task.m_outDependency;
  124. }
  125. }
  126. dep = 0;
  127. hive.waitAllTasks();
  128. }
  129. ANKI_TEST_EXPECT_EQ(ctx.m_countAtomic.get(), number);
  130. }
  131. }
  132. class FibTask
  133. {
  134. public:
  135. Atomic<U64>* m_sum;
  136. StackAllocator<U8> m_alloc;
  137. U64 m_n;
  138. FibTask(Atomic<U64>* sum, StackAllocator<U8>& alloc, U64 n)
  139. : m_sum(sum)
  140. , m_alloc(alloc)
  141. , m_n(n)
  142. {
  143. }
  144. void doWork(ThreadHive& hive)
  145. {
  146. if(m_n > 1)
  147. {
  148. FibTask* a = m_alloc.newInstance<FibTask>(m_sum, m_alloc, m_n - 1);
  149. FibTask* b = m_alloc.newInstance<FibTask>(m_sum, m_alloc, m_n - 2);
  150. Array<ThreadHiveTask, 2> tasks;
  151. tasks[0].m_callback = tasks[1].m_callback = FibTask::callback;
  152. tasks[0].m_argument = a;
  153. tasks[1].m_argument = b;
  154. hive.submitTasks(&tasks[0], tasks.getSize());
  155. }
  156. else
  157. {
  158. m_sum->fetchAdd(m_n);
  159. }
  160. }
  161. static void callback(void* arg, U32, ThreadHive& hive)
  162. {
  163. static_cast<FibTask*>(arg)->doWork(hive);
  164. }
  165. };
  166. static U64 fib(U64 n)
  167. {
  168. if(n > 1)
  169. {
  170. return fib(n - 1) + fib(n - 2);
  171. }
  172. else
  173. {
  174. return n;
  175. }
  176. }
  177. ANKI_TEST(Util, ThreadHiveBench)
  178. {
  179. static const U FIB_N = 32;
  180. const U32 threadCount = getCpuCoresCount();
  181. HeapAllocator<U8> alloc(allocAligned, nullptr);
  182. ThreadHive hive(threadCount, alloc);
  183. StackAllocator<U8> salloc(allocAligned, nullptr, 1024);
  184. Atomic<U64> sum = {0};
  185. FibTask task(&sum, salloc, FIB_N);
  186. HighRezTimer timer;
  187. timer.start();
  188. hive.submitTask(FibTask::callback, &task);
  189. hive.waitAllTasks();
  190. timer.stop();
  191. ANKI_TEST_LOGI("Total time %fms", timer.getElapsedTime() * 1000.0);
  192. const U64 serialFib = fib(FIB_N);
  193. ANKI_TEST_EXPECT_EQ(sum.get(), serialFib);
  194. }
  195. } // end namespace anki