feft_lp_policy.c 6.4 KB

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