component_mct.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Simon Archipoff
  5. * Copyright (C) 2020 Télécom-Sud Paris
  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_sched_component.h>
  19. #include <starpu_perfmodel.h>
  20. #include "helper_mct.h"
  21. #include <float.h>
  22. #include <core/sched_policy.h>
  23. #include <core/task.h>
  24. static int mct_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  25. {
  26. STARPU_ASSERT(component && task && starpu_sched_component_is_mct(component));
  27. struct _starpu_mct_data * d = component->data;
  28. struct starpu_sched_component * best_component;
  29. /* Estimated task duration for each child */
  30. double estimated_lengths[component->nchildren];
  31. /* Estimated transfer duration for each child */
  32. double estimated_transfer_length[component->nchildren];
  33. /* Estimated transfer+task termination for each child */
  34. double estimated_ends_with_task[component->nchildren];
  35. /* estimated energy */
  36. double local_energy[component->nchildren];
  37. /* Minimum transfer+task termination of the task over all workers */
  38. double min_exp_end_of_task;
  39. /* Maximum termination of the already-scheduled tasks over all workers */
  40. double max_exp_end_of_workers;
  41. unsigned suitable_components[component->nchildren];
  42. unsigned nsuitable_components;
  43. nsuitable_components = starpu_mct_compute_execution_times(component, task,
  44. estimated_lengths, estimated_transfer_length, suitable_components);
  45. /* If no suitable components were found, it means that the perfmodel of
  46. * the task had been purged since it has been pushed on the mct component.
  47. * We should send a push_fail message to its parent so that it will
  48. * be able to reschedule the task properly. */
  49. if(nsuitable_components == 0)
  50. return 1;
  51. /* Entering critical section to make sure no two workers
  52. make scheduling decisions at the same time */
  53. STARPU_COMPONENT_MUTEX_LOCK(&d->scheduling_mutex);
  54. starpu_mct_compute_expected_times(component, task, estimated_lengths, estimated_transfer_length,
  55. estimated_ends_with_task, &min_exp_end_of_task, &max_exp_end_of_workers, suitable_components, nsuitable_components);
  56. /* Compute the energy, if provided*/
  57. starpu_mct_compute_energy(component, task, local_energy, suitable_components, nsuitable_components);
  58. int best_icomponent = starpu_mct_get_best_component(d, task, estimated_lengths, estimated_transfer_length,
  59. estimated_ends_with_task, local_energy, min_exp_end_of_task, max_exp_end_of_workers, suitable_components, nsuitable_components);
  60. /* If no best component is found, it means that the perfmodel of
  61. * the task had been purged since it has been pushed on the mct component.
  62. * We should send a push_fail message to its parent so that it will
  63. * be able to reschedule the task properly. */
  64. if(best_icomponent == -1)
  65. {
  66. STARPU_COMPONENT_MUTEX_UNLOCK(&d->scheduling_mutex);
  67. return 1;
  68. }
  69. best_component = component->children[best_icomponent];
  70. if(starpu_sched_component_is_worker(best_component))
  71. {
  72. best_component->can_pull(best_component);
  73. STARPU_COMPONENT_MUTEX_UNLOCK(&d->scheduling_mutex);
  74. return 1;
  75. }
  76. starpu_sched_task_break(task);
  77. int ret = starpu_sched_component_push_task(component, best_component, task);
  78. /* I can now exit the critical section: Pushing the task below ensures that its execution
  79. time will be taken into account for subsequent scheduling decisions */
  80. STARPU_COMPONENT_MUTEX_UNLOCK(&d->scheduling_mutex);
  81. return ret;
  82. }
  83. static void mct_component_deinit_data(struct starpu_sched_component * component)
  84. {
  85. STARPU_ASSERT(starpu_sched_component_is_mct(component));
  86. struct _starpu_mct_data * d = component->data;
  87. STARPU_PTHREAD_MUTEX_DESTROY(&d->scheduling_mutex);
  88. free(d);
  89. }
  90. int starpu_sched_component_is_mct(struct starpu_sched_component * component)
  91. {
  92. return component->push_task == mct_push_task;
  93. }
  94. struct starpu_sched_component * starpu_sched_component_mct_create(struct starpu_sched_tree *tree, struct starpu_sched_component_mct_data * params)
  95. {
  96. struct starpu_sched_component * component = starpu_sched_component_create(tree, "mct");
  97. struct _starpu_mct_data *data = starpu_mct_init_parameters(params);
  98. component->data = data;
  99. STARPU_PTHREAD_MUTEX_INIT(&data->scheduling_mutex, NULL);
  100. component->push_task = mct_push_task;
  101. component->deinit_data = mct_component_deinit_data;
  102. return component;
  103. }