worker_list.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Université de Bordeaux 1
  4. * Copyright (C) 2012-2013 Centre National de la Recherche Scientifique
  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. #include <pthread.h>
  20. static unsigned list_has_next(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  21. {
  22. int nworkers = (int)workers->nworkers;
  23. STARPU_ASSERT(it != NULL);
  24. unsigned ret = it->cursor < nworkers ;
  25. if(!ret) it->cursor = 0;
  26. return ret;
  27. }
  28. static int list_get_next(struct starpu_worker_collection *workers, struct starpu_sched_ctx_iterator *it)
  29. {
  30. int *workerids = (int *)workers->workerids;
  31. int nworkers = (int)workers->nworkers;
  32. STARPU_ASSERT(it->cursor < nworkers);
  33. int ret = workerids[(it->cursor)++];
  34. return ret;
  35. }
  36. static unsigned _worker_belongs_to_ctx(struct starpu_worker_collection *workers, int workerid)
  37. {
  38. int *workerids = (int *)workers->workerids;
  39. unsigned nworkers = workers->nworkers;
  40. unsigned i;
  41. for(i = 0; i < nworkers; i++)
  42. {
  43. if(workerids[i] == workerid)
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. static int list_add(struct starpu_worker_collection *workers, int worker)
  49. {
  50. int *workerids = (int *)workers->workerids;
  51. unsigned *nworkers = &workers->nworkers;
  52. STARPU_ASSERT(*nworkers < STARPU_NMAXWORKERS - 1);
  53. if(!_worker_belongs_to_ctx(workers, worker))
  54. {
  55. workerids[(*nworkers)++] = worker;
  56. return worker;
  57. }
  58. else
  59. return -1;
  60. }
  61. static int _get_first_free_worker(int *workerids, int nworkers)
  62. {
  63. int i;
  64. for(i = 0; i < nworkers; i++)
  65. if(workerids[i] == -1)
  66. return i;
  67. return -1;
  68. }
  69. /* rearange array of workerids in order not to have {-1, -1, 5, -1, 7}
  70. and have instead {5, 7, -1, -1, -1}
  71. it is easier afterwards to iterate the array
  72. */
  73. static void _rearange_workerids(int *workerids, int old_nworkers)
  74. {
  75. int first_free_id = -1;
  76. int i;
  77. for(i = 0; i < old_nworkers; i++)
  78. {
  79. if(workerids[i] != -1)
  80. {
  81. first_free_id = _get_first_free_worker(workerids, old_nworkers);
  82. if(first_free_id != -1)
  83. {
  84. workerids[first_free_id] = workerids[i];
  85. workerids[i] = -1;
  86. }
  87. }
  88. }
  89. }
  90. static int list_remove(struct starpu_worker_collection *workers, int worker)
  91. {
  92. int *workerids = (int *)workers->workerids;
  93. unsigned nworkers = workers->nworkers;
  94. int found_worker = -1;
  95. unsigned i;
  96. for(i = 0; i < nworkers; i++)
  97. {
  98. if(workerids[i] == worker)
  99. {
  100. workerids[i] = -1;
  101. found_worker = worker;
  102. break;
  103. }
  104. }
  105. _rearange_workerids(workerids, nworkers);
  106. if(found_worker != -1)
  107. workers->nworkers--;
  108. return found_worker;
  109. }
  110. static void _init_workers(int *workerids)
  111. {
  112. unsigned i;
  113. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  114. workerids[i] = -1;
  115. return;
  116. }
  117. static void list_init(struct starpu_worker_collection *workers)
  118. {
  119. int *workerids = (int*)malloc(STARPU_NMAXWORKERS * sizeof(int));
  120. _init_workers(workerids);
  121. workers->workerids = (void*)workerids;
  122. workers->nworkers = 0;
  123. return;
  124. }
  125. static void list_deinit(struct starpu_worker_collection *workers)
  126. {
  127. free(workers->workerids);
  128. }
  129. static void list_init_iterator(struct starpu_worker_collection *workers STARPU_ATTRIBUTE_UNUSED, struct starpu_sched_ctx_iterator *it)
  130. {
  131. *((int*)it) = 0;
  132. }
  133. struct starpu_worker_collection worker_list =
  134. {
  135. .has_next = list_has_next,
  136. .get_next = list_get_next,
  137. .add = list_add,
  138. .remove = list_remove,
  139. .init = list_init,
  140. .deinit = list_deinit,
  141. .init_iterator = list_init_iterator,
  142. .type = STARPU_WORKER_LIST
  143. };