component_best_implementation.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Simon Archipoff
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <float.h>
  18. #include <starpu_sched_component.h>
  19. #include <starpu_scheduler.h>
  20. #include <core/workers.h>
  21. /* return true if workerid can execute task, and fill task->predicted and task->predicted_transfer
  22. * according to best implementation predictions
  23. */
  24. static int find_best_impl(unsigned sched_ctx_id, struct starpu_task * task, int workerid)
  25. {
  26. double len = DBL_MAX;
  27. int best_impl = -1;
  28. unsigned impl;
  29. if (!task->cl->model)
  30. {
  31. /* No perfmodel, first available will be fine */
  32. int can_execute = starpu_worker_can_execute_task_first_impl(workerid, task, &impl);
  33. STARPU_ASSERT(can_execute);
  34. best_impl = impl;
  35. len = 0.0;
  36. }
  37. else
  38. {
  39. struct starpu_perfmodel_arch* archtype = starpu_worker_get_perf_archtype(workerid, sched_ctx_id);
  40. for(impl = 0; impl < STARPU_MAXIMPLEMENTATIONS; impl++)
  41. {
  42. if(starpu_worker_can_execute_task(workerid, task, impl))
  43. {
  44. double d = starpu_task_expected_length(task, archtype, impl);
  45. if(isnan(d))
  46. {
  47. best_impl = impl;
  48. len = 0.0;
  49. break;
  50. }
  51. if(d < len)
  52. {
  53. len = d;
  54. best_impl = impl;
  55. }
  56. }
  57. }
  58. }
  59. if(best_impl == -1)
  60. return 0;
  61. task->predicted = len;
  62. task->predicted_transfer = starpu_task_expected_data_transfer_time_for(task, workerid);
  63. starpu_task_set_implementation(task, best_impl);
  64. return 1;
  65. }
  66. /* set implementation, task->predicted and task->predicted_transfer with the first worker of workers that can execute that task
  67. * or have to be calibrated
  68. */
  69. static void select_best_implementation_and_set_preds(unsigned sched_ctx_id, struct starpu_bitmap * workers, struct starpu_task * task)
  70. {
  71. int workerid;
  72. for(workerid = starpu_bitmap_first(workers);
  73. -1 != workerid;
  74. workerid = starpu_bitmap_next(workers, workerid))
  75. if(find_best_impl(sched_ctx_id, task, workerid))
  76. break;
  77. }
  78. static int best_implementation_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  79. {
  80. STARPU_ASSERT(component->nchildren == 1);
  81. select_best_implementation_and_set_preds(component->tree->sched_ctx_id, &component->workers_in_ctx, task);
  82. return starpu_sched_component_push_task(component,component->children[0],task);
  83. }
  84. int starpu_sched_component_is_best_implementation(struct starpu_sched_component * component)
  85. {
  86. return component->push_task == best_implementation_push_task;
  87. }
  88. static struct starpu_task * best_implementation_pull_task(struct starpu_sched_component * component, struct starpu_sched_component * from STARPU_ATTRIBUTE_UNUSED)
  89. {
  90. struct starpu_task * task = NULL;
  91. unsigned i;
  92. for(i=0; i < component->nparents; i++)
  93. {
  94. if(component->parents[i] == NULL)
  95. continue;
  96. else
  97. {
  98. task = starpu_sched_component_pull_task(component->parents[i], component);
  99. if(task)
  100. break;
  101. }
  102. }
  103. if(task)
  104. /* this worker can execute this task as it was returned by a pop*/
  105. (void)find_best_impl(component->tree->sched_ctx_id, task, starpu_worker_get_id_check());
  106. return task;
  107. }
  108. struct starpu_sched_component * starpu_sched_component_best_implementation_create(struct starpu_sched_tree *tree, void *arg)
  109. {
  110. (void)arg;
  111. struct starpu_sched_component * component = starpu_sched_component_create(tree, "best_impl");
  112. component->push_task = best_implementation_push_task;
  113. component->pull_task = best_implementation_pull_task;
  114. return component;
  115. }