BLI_task_performance_test.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. #include "BLI_ressource_strings.h"
  4. #include "atomic_ops.h"
  5. #define GHASH_INTERNAL_API
  6. extern "C" {
  7. #include "BLI_utildefines.h"
  8. #include "BLI_listbase.h"
  9. #include "BLI_mempool.h"
  10. #include "BLI_task.h"
  11. #include "PIL_time.h"
  12. #include "MEM_guardedalloc.h"
  13. }
  14. /* *** Parallel iterations over double-linked list items. *** */
  15. #define NUM_RUN_AVERAGED 100
  16. static uint gen_pseudo_random_number(uint num)
  17. {
  18. /* Note: this is taken from BLI_ghashutil_uinthash(), don't want to depend on external code that
  19. * might change here... */
  20. num += ~(num << 16);
  21. num ^= (num >> 5);
  22. num += (num << 3);
  23. num ^= (num >> 13);
  24. num += ~(num << 9);
  25. num ^= (num >> 17);
  26. /* Make final number in [65 - 16385] range. */
  27. return ((num & 255) << 6) + 1;
  28. }
  29. static void task_listbase_light_iter_func(void *UNUSED(userdata), Link *item, int index)
  30. {
  31. LinkData *data = (LinkData *)item;
  32. data->data = POINTER_FROM_INT(POINTER_AS_INT(data->data) + index);
  33. }
  34. static void task_listbase_light_membarrier_iter_func(void *userdata, Link *item, int index)
  35. {
  36. LinkData *data = (LinkData *)item;
  37. int *count = (int *)userdata;
  38. data->data = POINTER_FROM_INT(POINTER_AS_INT(data->data) + index);
  39. atomic_sub_and_fetch_uint32((uint32_t *)count, 1);
  40. }
  41. static void task_listbase_heavy_iter_func(void *UNUSED(userdata), Link *item, int index)
  42. {
  43. LinkData *data = (LinkData *)item;
  44. /* 'Random' number of iterations. */
  45. const uint num = gen_pseudo_random_number((uint)index);
  46. for (uint i = 0; i < num; i++) {
  47. data->data = POINTER_FROM_INT(POINTER_AS_INT(data->data) + ((i % 2) ? -index : index));
  48. }
  49. }
  50. static void task_listbase_heavy_membarrier_iter_func(void *userdata, Link *item, int index)
  51. {
  52. LinkData *data = (LinkData *)item;
  53. int *count = (int *)userdata;
  54. /* 'Random' number of iterations. */
  55. const uint num = gen_pseudo_random_number((uint)index);
  56. for (uint i = 0; i < num; i++) {
  57. data->data = POINTER_FROM_INT(POINTER_AS_INT(data->data) + ((i % 2) ? -index : index));
  58. }
  59. atomic_sub_and_fetch_uint32((uint32_t *)count, 1);
  60. }
  61. static void task_listbase_test_do(ListBase *list,
  62. const int num_items,
  63. int *num_items_tmp,
  64. const char *id,
  65. TaskParallelListbaseFunc func,
  66. const bool use_threads,
  67. const bool check_num_items_tmp)
  68. {
  69. double averaged_timing = 0.0;
  70. for (int i = 0; i < NUM_RUN_AVERAGED; i++) {
  71. const double init_time = PIL_check_seconds_timer();
  72. BLI_task_parallel_listbase(list, num_items_tmp, func, use_threads);
  73. averaged_timing += PIL_check_seconds_timer() - init_time;
  74. /* Those checks should ensure us all items of the listbase were processed once, and only once -
  75. * as expected. */
  76. if (check_num_items_tmp) {
  77. EXPECT_EQ(*num_items_tmp, 0);
  78. }
  79. LinkData *item;
  80. int j;
  81. for (j = 0, item = (LinkData *)list->first; j < num_items && item != NULL;
  82. j++, item = item->next) {
  83. EXPECT_EQ(POINTER_AS_INT(item->data), j);
  84. item->data = POINTER_FROM_INT(0);
  85. }
  86. EXPECT_EQ(num_items, j);
  87. *num_items_tmp = num_items;
  88. }
  89. printf("\t%s: done in %fs on average over %d runs\n",
  90. id,
  91. averaged_timing / NUM_RUN_AVERAGED,
  92. NUM_RUN_AVERAGED);
  93. }
  94. static void task_listbase_test(const char *id, const int nbr, const bool use_threads)
  95. {
  96. printf("\n========== STARTING %s ==========\n", id);
  97. ListBase list = {NULL, NULL};
  98. LinkData *items_buffer = (LinkData *)MEM_calloc_arrayN(nbr, sizeof(*items_buffer), __func__);
  99. BLI_threadapi_init();
  100. int num_items = 0;
  101. for (int i = 0; i < nbr; i++) {
  102. BLI_addtail(&list, &items_buffer[i]);
  103. num_items++;
  104. }
  105. int num_items_tmp = num_items;
  106. task_listbase_test_do(&list,
  107. num_items,
  108. &num_items_tmp,
  109. "Light iter",
  110. task_listbase_light_iter_func,
  111. use_threads,
  112. false);
  113. task_listbase_test_do(&list,
  114. num_items,
  115. &num_items_tmp,
  116. "Light iter with mem barrier",
  117. task_listbase_light_membarrier_iter_func,
  118. use_threads,
  119. true);
  120. task_listbase_test_do(&list,
  121. num_items,
  122. &num_items_tmp,
  123. "Heavy iter",
  124. task_listbase_heavy_iter_func,
  125. use_threads,
  126. false);
  127. task_listbase_test_do(&list,
  128. num_items,
  129. &num_items_tmp,
  130. "Heavy iter with mem barrier",
  131. task_listbase_heavy_membarrier_iter_func,
  132. use_threads,
  133. true);
  134. MEM_freeN(items_buffer);
  135. BLI_threadapi_exit();
  136. printf("========== ENDED %s ==========\n\n", id);
  137. }
  138. TEST(task, ListBaseIterNoThread10k)
  139. {
  140. task_listbase_test("ListBase parallel iteration - Single thread - 10000 items", 10000, false);
  141. }
  142. TEST(task, ListBaseIter10k)
  143. {
  144. task_listbase_test("ListBase parallel iteration - Threaded - 10000 items", 10000, true);
  145. }
  146. TEST(task, ListBaseIterNoThread100k)
  147. {
  148. task_listbase_test("ListBase parallel iteration - Single thread - 100000 items", 100000, false);
  149. }
  150. TEST(task, ListBaseIter100k)
  151. {
  152. task_listbase_test("ListBase parallel iteration - Threaded - 100000 items", 100000, true);
  153. }