feft_lp_policy.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 - 2013 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_lp.h"
  17. #include "sc_hypervisor_policy.h"
  18. #include <starpu_config.h>
  19. #include <sys/time.h>
  20. int resize_no = 0;
  21. #ifdef STARPU_HAVE_GLPK_H
  22. static void _try_resizing(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  23. {
  24. /* for vite */
  25. /* printf("resize_no = %d\n", resize_no); */
  26. /* starpu_trace_user_event(resize_no++); */
  27. int ns = sched_ctxs == NULL ? sc_hypervisor_get_nsched_ctxs() : nsched_ctxs;
  28. unsigned *curr_sched_ctxs = sched_ctxs == NULL ? sc_hypervisor_get_sched_ctxs() : sched_ctxs;
  29. unsigned curr_nworkers = nworkers == -1 ? starpu_worker_get_count() : (unsigned)nworkers;
  30. struct types_of_workers *tw = sc_hypervisor_get_types_of_workers(workers, curr_nworkers);
  31. int nw = tw->nw;
  32. double nworkers_per_ctx[ns][nw];
  33. int total_nw[nw];
  34. sc_hypervisor_group_workers_by_type(tw, total_nw);
  35. struct timeval start_time;
  36. struct timeval end_time;
  37. gettimeofday(&start_time, NULL);
  38. double vmax = sc_hypervisor_lp_get_nworkers_per_ctx(ns, nw, nworkers_per_ctx, total_nw, tw);
  39. gettimeofday(&end_time, NULL);
  40. long diff_s = end_time.tv_sec - start_time.tv_sec;
  41. long diff_us = end_time.tv_usec - start_time.tv_usec;
  42. __attribute__((unused)) float timing = (float)(diff_s*1000000 + diff_us)/1000;
  43. if(vmax != 0.0)
  44. {
  45. int nworkers_per_ctx_rounded[nsched_ctxs][nw];
  46. sc_hypervisor_lp_round_double_to_int(ns, nw, nworkers_per_ctx, nworkers_per_ctx_rounded);
  47. // sc_hypervisor_lp_redistribute_resources_in_ctxs(ns, nw, nworkers_per_ctx_rounded, nworkers_per_ctx, curr_sched_ctxs, tw);
  48. sc_hypervisor_lp_distribute_resources_in_ctxs(curr_sched_ctxs, ns, nw, nworkers_per_ctx_rounded, nworkers_per_ctx, workers, curr_nworkers, tw);
  49. sc_hypervisor_lp_share_remaining_resources(ns, curr_sched_ctxs, curr_nworkers, workers);
  50. }
  51. }
  52. static void feft_lp_handle_poped_task(__attribute__((unused))unsigned sched_ctx, __attribute__((unused))int worker,
  53. __attribute__((unused))struct starpu_task *task, __attribute__((unused))uint32_t footprint)
  54. {
  55. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  56. if(ret != EBUSY)
  57. {
  58. unsigned criteria = sc_hypervisor_get_resize_criteria();
  59. if(criteria != SC_NOTHING && criteria == SC_SPEED)
  60. {
  61. if(sc_hypervisor_check_speed_gap_btw_ctxs())
  62. {
  63. _try_resizing(NULL, -1, NULL, -1);
  64. }
  65. }
  66. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  67. }
  68. }
  69. static void feft_lp_size_ctxs(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  70. {
  71. int ns = sched_ctxs == NULL ? sc_hypervisor_get_nsched_ctxs() : nsched_ctxs;
  72. unsigned *curr_sched_ctxs = sched_ctxs == NULL ? sc_hypervisor_get_sched_ctxs() : sched_ctxs;
  73. unsigned curr_nworkers = nworkers == -1 ? starpu_worker_get_count() : (unsigned)nworkers;
  74. struct types_of_workers *tw = sc_hypervisor_get_types_of_workers(workers, curr_nworkers);
  75. int nw = tw->nw;
  76. double nworkers_per_type[ns][nw];
  77. int total_nw[nw];
  78. sc_hypervisor_group_workers_by_type(tw, total_nw);
  79. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  80. struct sc_hypervisor_wrapper* sc_w = NULL;
  81. int s = 0;
  82. for(s = 0; s < nsched_ctxs; s++)
  83. {
  84. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  85. sc_w->to_be_sized = 1;
  86. }
  87. double vmax = sc_hypervisor_lp_get_nworkers_per_ctx(ns, nw, nworkers_per_type, total_nw, tw);
  88. if(vmax != 0.0)
  89. {
  90. // printf("********size\n");
  91. /* int i; */
  92. /* for( i = 0; i < nsched_ctxs; i++) */
  93. /* { */
  94. /* printf("ctx %d/worker type %d: n = %lf \n", i, 0, nworkers_per_type[i][0]); */
  95. /* #ifdef STARPU_USE_CUDA */
  96. /* int ncuda = starpu_worker_get_count_by_type(STARPU_CUDA_WORKER); */
  97. /* if(ncuda != 0) */
  98. /* printf("ctx %d/worker type %d: n = %lf \n", i, 1, nworkers_per_type[i][1]); */
  99. /* #endif */
  100. /* } */
  101. int nworkers_per_type_rounded[ns][nw];
  102. sc_hypervisor_lp_round_double_to_int(ns, nw, nworkers_per_type, nworkers_per_type_rounded);
  103. /* for( i = 0; i < nsched_ctxs; i++) */
  104. /* { */
  105. /* printf("ctx %d/worker type %d: n = %d \n", i, 0, nworkers_per_type_rounded[i][0]); */
  106. /* #ifdef STARPU_USE_CUDA */
  107. /* int ncuda = starpu_worker_get_count_by_type(STARPU_CUDA_WORKER); */
  108. /* if(ncuda != 0) */
  109. /* printf("ctx %d/worker type %d: n = %d \n", i, 1, nworkers_per_type_rounded[i][1]); */
  110. /* #endif */
  111. /* } */
  112. unsigned has_workers = 0;
  113. int s;
  114. for(s = 0; s < ns; s++)
  115. {
  116. int nworkers_ctx = sc_hypervisor_get_nworkers_ctx(curr_sched_ctxs[s], STARPU_ANY_WORKER);
  117. if(nworkers_ctx != 0)
  118. {
  119. has_workers = 1;
  120. break;
  121. }
  122. }
  123. if(has_workers)
  124. sc_hypervisor_lp_redistribute_resources_in_ctxs(ns, nw, nworkers_per_type_rounded, nworkers_per_type, curr_sched_ctxs, tw);
  125. else
  126. sc_hypervisor_lp_distribute_resources_in_ctxs(sched_ctxs, ns, nw, nworkers_per_type_rounded, nworkers_per_type, workers, curr_nworkers, tw);
  127. }
  128. printf("finished size ctxs\n");
  129. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  130. }
  131. static void feft_lp_handle_idle_cycle(unsigned sched_ctx, int worker)
  132. {
  133. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  134. if(ret != EBUSY)
  135. {
  136. unsigned criteria = sc_hypervisor_get_resize_criteria();
  137. if(criteria != SC_NOTHING && criteria == SC_IDLE)
  138. {
  139. if(sc_hypervisor_check_idle(sched_ctx, worker))
  140. _try_resizing(NULL, -1, NULL, -1);
  141. }
  142. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  143. }
  144. }
  145. static void feft_lp_resize_ctxs(unsigned *sched_ctxs, int nsched_ctxs ,
  146. int *workers, int nworkers)
  147. {
  148. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  149. if(ret != EBUSY)
  150. {
  151. struct sc_hypervisor_wrapper* sc_w = NULL;
  152. int s = 0;
  153. for(s = 0; s < nsched_ctxs; s++)
  154. {
  155. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  156. if((sc_w->submitted_flops + (0.1*sc_w->total_flops)) < sc_w->total_flops)
  157. {
  158. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  159. return;
  160. }
  161. }
  162. _try_resizing(sched_ctxs, nsched_ctxs, workers, nworkers);
  163. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  164. }
  165. }
  166. struct sc_hypervisor_policy feft_lp_policy = {
  167. .size_ctxs = feft_lp_size_ctxs,
  168. .resize_ctxs = feft_lp_resize_ctxs,
  169. .handle_poped_task = feft_lp_handle_poped_task,
  170. .handle_pushed_task = NULL,
  171. .handle_idle_cycle = feft_lp_handle_idle_cycle,
  172. .handle_idle_end = NULL,
  173. .handle_post_exec_hook = NULL,
  174. .handle_submitted_job = NULL,
  175. .end_ctx = NULL,
  176. .custom = 0,
  177. .name = "feft_lp"
  178. };
  179. #endif /* STARPU_HAVE_GLPK_H */