sc_config.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012 INRIA
  4. *
  5. * StarPU 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. * StarPU 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 <sc_hypervisor_intern.h>
  17. static struct sc_hypervisor_policy_config* _create_config(void)
  18. {
  19. struct sc_hypervisor_policy_config *config = (struct sc_hypervisor_policy_config *)malloc(sizeof(struct sc_hypervisor_policy_config));
  20. config->min_nworkers = -1;
  21. config->max_nworkers = -1;
  22. config->new_workers_max_idle = -1.0;
  23. config->ispeed_ctx_sample = 0.0;
  24. config->time_sample = 0.5;
  25. int i;
  26. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  27. {
  28. config->granularity = -1;
  29. config->priority[i] = -1;
  30. config->fixed_workers[i] = -1;
  31. config->max_idle[i] = -1.0;
  32. config->min_working[i] = -1.0;
  33. config->ispeed_w_sample[i] = 0.0;
  34. }
  35. return config;
  36. }
  37. static void _update_config(struct sc_hypervisor_policy_config *old, struct sc_hypervisor_policy_config* new)
  38. {
  39. old->min_nworkers = new->min_nworkers != -1 ? new->min_nworkers : old->min_nworkers ;
  40. old->max_nworkers = new->max_nworkers != -1 ? new->max_nworkers : old->max_nworkers ;
  41. old->new_workers_max_idle = new->new_workers_max_idle != -1.0 ? new->new_workers_max_idle : old->new_workers_max_idle;
  42. old->granularity = new->granularity != -1 ? new->granularity : old->granularity;
  43. int i;
  44. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  45. {
  46. old->priority[i] = new->priority[i] != -1 ? new->priority[i] : old->priority[i];
  47. old->fixed_workers[i] = new->fixed_workers[i] != -1 ? new->fixed_workers[i] : old->fixed_workers[i];
  48. old->max_idle[i] = new->max_idle[i] != -1.0 ? new->max_idle[i] : old->max_idle[i];
  49. old->min_working[i] = new->min_working[i] != -1.0 ? new->min_working[i] : old->min_working[i];
  50. }
  51. }
  52. void sc_hypervisor_set_config(unsigned sched_ctx, void *config)
  53. {
  54. if(hypervisor.sched_ctx_w[sched_ctx].config != NULL && config != NULL)
  55. {
  56. _update_config(hypervisor.sched_ctx_w[sched_ctx].config, config);
  57. }
  58. else
  59. {
  60. hypervisor.sched_ctx_w[sched_ctx].config = config;
  61. }
  62. return;
  63. }
  64. void _add_config(unsigned sched_ctx)
  65. {
  66. struct sc_hypervisor_policy_config *config = _create_config();
  67. config->min_nworkers = 0;
  68. config->max_nworkers = starpu_worker_get_count();
  69. config->new_workers_max_idle = MAX_IDLE_TIME;
  70. int i;
  71. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  72. {
  73. config->granularity = 1;
  74. config->priority[i] = 0;
  75. config->fixed_workers[i] = 0;
  76. config->max_idle[i] = MAX_IDLE_TIME;
  77. config->min_working[i] = MIN_WORKING_TIME;
  78. }
  79. sc_hypervisor_set_config(sched_ctx, config);
  80. }
  81. void _remove_config(unsigned sched_ctx)
  82. {
  83. sc_hypervisor_set_config(sched_ctx, NULL);
  84. }
  85. struct sc_hypervisor_policy_config* sc_hypervisor_get_config(unsigned sched_ctx)
  86. {
  87. return hypervisor.sched_ctx_w[sched_ctx].config;
  88. }
  89. static struct sc_hypervisor_policy_config* _ctl(unsigned sched_ctx, va_list varg_list, unsigned later)
  90. {
  91. struct sc_hypervisor_policy_config *config = NULL;
  92. if(later)
  93. config = _create_config();
  94. else
  95. config = sc_hypervisor_get_config(sched_ctx);
  96. assert(config != NULL);
  97. int arg_type;
  98. int i;
  99. int *workerids;
  100. int nworkers;
  101. while ((arg_type = va_arg(varg_list, int)) != SC_HYPERVISOR_NULL)
  102. {
  103. switch(arg_type)
  104. {
  105. case SC_HYPERVISOR_MAX_IDLE:
  106. workerids = va_arg(varg_list, int*);
  107. nworkers = va_arg(varg_list, int);
  108. double max_idle = va_arg(varg_list, double);
  109. for(i = 0; i < nworkers; i++)
  110. config->max_idle[workerids[i]] = max_idle;
  111. break;
  112. case SC_HYPERVISOR_MIN_WORKING:
  113. workerids = va_arg(varg_list, int*);
  114. nworkers = va_arg(varg_list, int);
  115. double min_working = va_arg(varg_list, double);
  116. for(i = 0; i < nworkers; i++)
  117. config->min_working[workerids[i]] = min_working;
  118. break;
  119. case SC_HYPERVISOR_PRIORITY:
  120. workerids = va_arg(varg_list, int*);
  121. nworkers = va_arg(varg_list, int);
  122. int priority = va_arg(varg_list, int);
  123. for(i = 0; i < nworkers; i++)
  124. config->priority[workerids[i]] = priority;
  125. break;
  126. case SC_HYPERVISOR_MIN_WORKERS:
  127. config->min_nworkers = va_arg(varg_list, unsigned);
  128. break;
  129. case SC_HYPERVISOR_MAX_WORKERS:
  130. config->max_nworkers = va_arg(varg_list, unsigned);
  131. break;
  132. case SC_HYPERVISOR_GRANULARITY:
  133. config->granularity = va_arg(varg_list, unsigned);
  134. break;
  135. case SC_HYPERVISOR_FIXED_WORKERS:
  136. workerids = va_arg(varg_list, int*);
  137. nworkers = va_arg(varg_list, int);
  138. for(i = 0; i < nworkers; i++)
  139. config->fixed_workers[workerids[i]] = 1;
  140. break;
  141. case SC_HYPERVISOR_NEW_WORKERS_MAX_IDLE:
  142. config->new_workers_max_idle = va_arg(varg_list, double);
  143. break;
  144. case SC_HYPERVISOR_ISPEED_W_SAMPLE:
  145. workerids = va_arg(varg_list, int*);
  146. nworkers = va_arg(varg_list, int);
  147. double sample = va_arg(varg_list, double);
  148. for(i = 0; i < nworkers; i++)
  149. config->ispeed_w_sample[workerids[i]] = sample;
  150. break;
  151. case SC_HYPERVISOR_ISPEED_CTX_SAMPLE:
  152. config->ispeed_ctx_sample = va_arg(varg_list, double);
  153. break;
  154. case SC_HYPERVISOR_TIME_SAMPLE:
  155. config->time_sample = va_arg(varg_list, double);
  156. break;
  157. /* not important for the strateg, needed just to jump these args in the iteration of the args */
  158. case SC_HYPERVISOR_TIME_TO_APPLY:
  159. va_arg(varg_list, int);
  160. break;
  161. case SC_HYPERVISOR_MIN_TASKS:
  162. va_arg(varg_list, int);
  163. break;
  164. }
  165. }
  166. return later ? config : NULL;
  167. }
  168. void sc_hypervisor_ctl(unsigned sched_ctx, ...)
  169. {
  170. va_list varg_list;
  171. va_start(varg_list, sched_ctx);
  172. int arg_type;
  173. int stop = 0;
  174. int task_tag = -1;
  175. while ((arg_type = va_arg(varg_list, int)) != SC_HYPERVISOR_NULL)
  176. {
  177. switch(arg_type)
  178. {
  179. case SC_HYPERVISOR_TIME_TO_APPLY:
  180. task_tag = va_arg(varg_list, int);
  181. stop = 1;
  182. break;
  183. case SC_HYPERVISOR_MIN_TASKS:
  184. hypervisor.min_tasks = va_arg(varg_list, int);
  185. hypervisor.check_min_tasks[sched_ctx] = 1;
  186. break;
  187. }
  188. if(stop) break;
  189. }
  190. va_end(varg_list);
  191. va_start(varg_list, sched_ctx);
  192. /* if config not null => save hypervisor configuration and consider it later */
  193. struct sc_hypervisor_policy_config *config = _ctl(sched_ctx, varg_list, (task_tag > 0));
  194. if(config != NULL)
  195. {
  196. struct configuration_entry *entry;
  197. entry = malloc(sizeof *entry);
  198. STARPU_ASSERT(entry != NULL);
  199. entry->task_tag = task_tag;
  200. entry->configuration = config;
  201. STARPU_PTHREAD_MUTEX_LOCK(&hypervisor.conf_mut[sched_ctx]);
  202. HASH_ADD_INT(hypervisor.configurations[sched_ctx], task_tag, entry);
  203. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.conf_mut[sched_ctx]);
  204. }
  205. va_end(varg_list);
  206. }