worker_list.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2014 Université de Bordeaux
  4. * Copyright (C) 2012-2013 CNRS
  5. * Copyright (C) 2011-2013 INRIA
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. static unsigned list_has_next(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  20. {
  21. int nworkers = workers->nworkers;
  22. STARPU_ASSERT(it != NULL);
  23. unsigned ret = it->cursor < nworkers ;
  24. if(!ret) it->cursor = 0;
  25. return ret;
  26. }
  27. static int list_get_next(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  28. {
  29. int *workerids = (int *)workers->workerids;
  30. int nworkers = (int)workers->nworkers;
  31. STARPU_ASSERT(it->cursor < nworkers);
  32. int ret = workerids[it->cursor++];
  33. return ret;
  34. }
  35. static unsigned list_has_next_master(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  36. {
  37. int nworkers = workers->nmasters;
  38. STARPU_ASSERT(it != NULL);
  39. unsigned ret = it->cursor < nworkers ;
  40. if(!ret) it->cursor = 0;
  41. return ret;
  42. }
  43. static int list_get_next_master(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  44. {
  45. int *workerids = (int *)workers->masters;
  46. int nworkers = (int)workers->nmasters;
  47. STARPU_ASSERT_MSG(it->cursor < nworkers, "cursor %d nworkers %d\n", it->cursor, nworkers);
  48. int ret = workerids[it->cursor++];
  49. return ret;
  50. }
  51. static unsigned _worker_belongs_to_ctx(struct starpu_worker_collection *workers, int workerid)
  52. {
  53. int *workerids = (int *)workers->workerids;
  54. unsigned nworkers = workers->nworkers;
  55. unsigned i;
  56. for(i = 0; i < nworkers; i++)
  57. {
  58. if(workerids[i] == workerid)
  59. return 1;
  60. }
  61. return 0;
  62. }
  63. static int list_add(struct starpu_worker_collection *workers, int worker)
  64. {
  65. int *workerids = (int *)workers->workerids;
  66. unsigned *nworkers = &workers->nworkers;
  67. STARPU_ASSERT(*nworkers < STARPU_NMAXWORKERS);
  68. if(!_worker_belongs_to_ctx(workers, worker))
  69. {
  70. workerids[(*nworkers)++] = worker;
  71. return worker;
  72. }
  73. else
  74. return -1;
  75. }
  76. static int _get_first_free_worker(int *workerids, int nworkers)
  77. {
  78. int i;
  79. for(i = 0; i < nworkers; i++)
  80. if(workerids[i] == -1)
  81. return i;
  82. return -1;
  83. }
  84. /* rearange array of workerids in order not to have {-1, -1, 5, -1, 7}
  85. and have instead {5, 7, -1, -1, -1}
  86. it is easier afterwards to iterate the array
  87. */
  88. static void _rearange_workerids(int *workerids, int old_nworkers)
  89. {
  90. int first_free_id = -1;
  91. int i;
  92. for(i = 0; i < old_nworkers; i++)
  93. {
  94. if(workerids[i] != -1)
  95. {
  96. first_free_id = _get_first_free_worker(workerids, old_nworkers);
  97. if(first_free_id != -1)
  98. {
  99. workerids[first_free_id] = workerids[i];
  100. workerids[i] = -1;
  101. }
  102. }
  103. }
  104. }
  105. static int list_remove(struct starpu_worker_collection *workers, int worker)
  106. {
  107. int *workerids = (int *)workers->workerids;
  108. unsigned nworkers = workers->nworkers;
  109. int *masters = (int *)workers->masters;
  110. unsigned nmasters = workers->nmasters;
  111. unsigned i;
  112. int found_worker = -1;
  113. for(i = 0; i < nworkers; i++)
  114. {
  115. if(workerids[i] == worker)
  116. {
  117. workerids[i] = -1;
  118. found_worker = worker;
  119. break;
  120. }
  121. }
  122. _rearange_workerids(workerids, nworkers);
  123. if(found_worker != -1)
  124. workers->nworkers--;
  125. int found_master = -1;
  126. for(i = 0; i < nmasters; i++)
  127. {
  128. if(masters[i] == worker)
  129. {
  130. masters[i] = -1;
  131. found_master = worker;
  132. break;
  133. }
  134. }
  135. _rearange_workerids(masters, nmasters);
  136. if(found_master != -1)
  137. workers->nmasters--;
  138. return found_worker;
  139. }
  140. static void _init_workers(int *workerids)
  141. {
  142. unsigned i;
  143. unsigned nworkers = starpu_worker_get_count();
  144. for(i = 0; i < nworkers; i++)
  145. workerids[i] = -1;
  146. return;
  147. }
  148. static void list_init(struct starpu_worker_collection *workers)
  149. {
  150. int *workerids = (int*)malloc(STARPU_NMAXWORKERS * sizeof(int));
  151. int *masters = (int*)malloc(STARPU_NMAXWORKERS * sizeof(int));
  152. _init_workers(workerids);
  153. _init_workers(masters);
  154. workers->workerids = (void*)workerids;
  155. workers->nworkers = 0;
  156. workers->masters = (void*)masters;
  157. workers->nmasters = 0;
  158. return;
  159. }
  160. static void list_deinit(struct starpu_worker_collection *workers)
  161. {
  162. free(workers->workerids);
  163. free(workers->masters);
  164. }
  165. static void list_init_iterator(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  166. {
  167. it->cursor = 0;
  168. int *workerids = (int *)workers->workerids;
  169. unsigned nworkers = workers->nworkers;
  170. unsigned i;
  171. int nm = 0;
  172. for(i = 0; i < nworkers; i++)
  173. {
  174. if(!starpu_worker_is_slave(workerids[i]))
  175. ((int*)workers->masters)[nm++] = workerids[i];
  176. }
  177. workers->nmasters = nm;
  178. }
  179. struct starpu_worker_collection worker_list =
  180. {
  181. .has_next = list_has_next,
  182. .get_next = list_get_next,
  183. .has_next_master = list_has_next_master,
  184. .get_next_master = list_get_next_master,
  185. .add = list_add,
  186. .remove = list_remove,
  187. .init = list_init,
  188. .deinit = list_deinit,
  189. .init_iterator = list_init_iterator,
  190. .type = STARPU_WORKER_LIST
  191. };