work_stealing_policy.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <core/policies/work_stealing_policy.h>
  17. /* save the general machine configuration */
  18. //static struct starpu_machine_config_s *machineconfig;
  19. static unsigned nworkers;
  20. static unsigned rr_worker;
  21. static struct starpu_jobq_s *queue_array[STARPU_NMAXWORKERS];
  22. /* keep track of the work performed from the beginning of the algorithm to make
  23. * better decisions about which queue to select when stealing or deferring work
  24. */
  25. static unsigned performed_total = 0;
  26. //static unsigned performed_local[16];
  27. #ifdef USE_OVERLOAD
  28. static float overload_metric(unsigned id)
  29. {
  30. float execution_ratio = 0.0f;
  31. if (performed_total > 0) {
  32. execution_ratio = _starpu_get_deque_nprocessed(queue_array[id])/performed_total;
  33. }
  34. unsigned performed_queue;
  35. performed_queue = _starpu_get_deque_nprocessed(queue_array[id]);
  36. float current_ratio = 0.0f;
  37. if (performed_queue > 0) {
  38. current_ratio = _starpu_get_deque_njobs(queue_array[id])/performed_queue;
  39. }
  40. return (current_ratio - execution_ratio);
  41. }
  42. /* who to steal work to ? */
  43. static struct starpu_jobq_s *select_victimq(void)
  44. {
  45. struct starpu_jobq_s *q;
  46. unsigned attempts = nworkers;
  47. unsigned worker = rr_worker;
  48. do {
  49. if (overload_metric(worker) > 0.0f)
  50. {
  51. q = queue_array[worker];
  52. return q;
  53. }
  54. else {
  55. worker = (worker + 1)%nworkers;
  56. }
  57. } while(attempts-- > 0);
  58. /* take one anyway ... */
  59. q = queue_array[rr_worker];
  60. rr_worker = (rr_worker + 1 )%nworkers;
  61. return q;
  62. }
  63. static struct starpu_jobq_s *select_workerq(void)
  64. {
  65. struct starpu_jobq_s *q;
  66. unsigned attempts = nworkers;
  67. unsigned worker = rr_worker;
  68. do {
  69. if (overload_metric(worker) < 0.0f)
  70. {
  71. q = queue_array[worker];
  72. return q;
  73. }
  74. else {
  75. worker = (worker + 1)%nworkers;
  76. }
  77. } while(attempts-- > 0);
  78. /* take one anyway ... */
  79. q = queue_array[rr_worker];
  80. rr_worker = (rr_worker + 1 )%nworkers;
  81. return q;
  82. }
  83. #else
  84. /* who to steal work to ? */
  85. static struct starpu_jobq_s *select_victimq(void)
  86. {
  87. struct starpu_jobq_s *q;
  88. q = queue_array[rr_worker];
  89. rr_worker = (rr_worker + 1 )%nworkers;
  90. return q;
  91. }
  92. /* when anonymous threads submit tasks,
  93. * we need to select a queue where to dispose them */
  94. static struct starpu_jobq_s *select_workerq(void)
  95. {
  96. struct starpu_jobq_s *q;
  97. q = queue_array[rr_worker];
  98. rr_worker = (rr_worker + 1 )%nworkers;
  99. return q;
  100. }
  101. #endif
  102. static starpu_job_t ws_pop_task(struct starpu_jobq_s *q)
  103. {
  104. starpu_job_t j;
  105. j = _starpu_deque_pop_task(q);
  106. if (j) {
  107. /* there was a local task */
  108. performed_total++;
  109. return j;
  110. }
  111. /* we need to steal someone's job */
  112. struct starpu_jobq_s *victimq;
  113. victimq = select_victimq();
  114. if (!_starpu_jobq_trylock(victimq))
  115. {
  116. j = _starpu_deque_pop_task(victimq);
  117. _starpu_jobq_unlock(victimq);
  118. STARPU_TRACE_WORK_STEALING(q, j);
  119. performed_total++;
  120. }
  121. return j;
  122. }
  123. static struct starpu_jobq_s *init_ws_deque(void)
  124. {
  125. struct starpu_jobq_s *q;
  126. q = _starpu_create_deque();
  127. q->push_task = _starpu_deque_push_task;
  128. q->push_prio_task = _starpu_deque_push_prio_task;
  129. q->pop_task = ws_pop_task;
  130. q->who = 0;
  131. queue_array[nworkers++] = q;
  132. return q;
  133. }
  134. static void initialize_ws_policy(struct starpu_machine_config_s *config,
  135. __attribute__ ((unused)) struct starpu_sched_policy_s *_policy)
  136. {
  137. nworkers = 0;
  138. rr_worker = 0;
  139. //machineconfig = config;
  140. _starpu_setup_queues(_starpu_init_deque_queues_mechanisms, init_ws_deque, config);
  141. }
  142. static struct starpu_jobq_s *get_local_queue_ws(struct starpu_sched_policy_s *policy __attribute__ ((unused)))
  143. {
  144. struct starpu_jobq_s *queue;
  145. queue = pthread_getspecific(policy->local_queue_key);
  146. if (!queue) {
  147. queue = select_workerq();
  148. }
  149. STARPU_ASSERT(queue);
  150. return queue;
  151. }
  152. struct starpu_sched_policy_s _starpu_sched_ws_policy = {
  153. .init_sched = initialize_ws_policy,
  154. .deinit_sched = NULL,
  155. .starpu_get_local_queue = get_local_queue_ws,
  156. .policy_name = "ws",
  157. .policy_description = "work stealing"
  158. };