feft_lp_policy.c 6.8 KB

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