component_best_implementation.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 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 <starpu_sched_component.h>
  18. #include <starpu_scheduler.h>
  19. #include <float.h>
  20. /* return true if workerid can execute task, and fill task->predicted and task->predicted_transfer
  21. * according to best implementation predictions
  22. */
  23. static int find_best_impl(unsigned sched_ctx_id, struct starpu_task * task, int workerid)
  24. {
  25. double len = DBL_MAX;
  26. int best_impl = -1;
  27. int impl;
  28. for(impl = 0; impl < STARPU_MAXIMPLEMENTATIONS; impl++)
  29. {
  30. if(starpu_worker_can_execute_task(workerid, task, impl))
  31. {
  32. struct starpu_perfmodel_arch* archtype = starpu_worker_get_perf_archtype(workerid, sched_ctx_id);
  33. double d = starpu_task_expected_length(task, archtype, impl);
  34. if(isnan(d))
  35. {
  36. best_impl = impl;
  37. len = 0.0;
  38. break;
  39. }
  40. if(d < len)
  41. {
  42. len = d;
  43. best_impl = impl;
  44. }
  45. }
  46. }
  47. if(best_impl == -1)
  48. return 0;
  49. int memory_node = starpu_worker_get_memory_node(workerid);
  50. task->predicted = len;
  51. task->predicted_transfer = starpu_task_expected_data_transfer_time(memory_node, task);
  52. starpu_task_set_implementation(task, best_impl);
  53. return 1;
  54. }
  55. /* set implementation, task->predicted and task->predicted_transfer with the first worker of workers that can execute that task
  56. * or have to be calibrated
  57. */
  58. static void select_best_implementation_and_set_preds(unsigned sched_ctx_id, struct starpu_bitmap * workers, struct starpu_task * task)
  59. {
  60. int workerid;
  61. for(workerid = starpu_bitmap_first(workers);
  62. -1 != workerid;
  63. workerid = starpu_bitmap_next(workers, workerid))
  64. if(find_best_impl(sched_ctx_id, task, workerid))
  65. break;
  66. }
  67. static int best_implementation_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  68. {
  69. STARPU_ASSERT(component->nchildren == 1);
  70. select_best_implementation_and_set_preds(component->tree->sched_ctx_id, component->workers_in_ctx, task);
  71. return starpu_sched_component_push_task(component,component->children[0],task);
  72. }
  73. int starpu_sched_component_is_best_implementation(struct starpu_sched_component * component)
  74. {
  75. return component->push_task == best_implementation_push_task;
  76. }
  77. static struct starpu_task * best_implementation_pull_task(struct starpu_sched_component * component)
  78. {
  79. struct starpu_task * task = NULL;
  80. int i;
  81. for(i=0; i < component->nparents; i++)
  82. {
  83. if(component->parents[i] == NULL)
  84. continue;
  85. else
  86. {
  87. task = starpu_sched_component_pull_task(component->parents[i], component);
  88. if(task)
  89. break;
  90. }
  91. }
  92. if(task)
  93. /* this worker can execute this task as it was returned by a pop*/
  94. (void)find_best_impl(component->tree->sched_ctx_id, task, starpu_worker_get_id());
  95. return task;
  96. }
  97. struct starpu_sched_component * starpu_sched_component_best_implementation_create(struct starpu_sched_tree *tree, void * arg STARPU_ATTRIBUTE_UNUSED)
  98. {
  99. struct starpu_sched_component * component = starpu_sched_component_create(tree, "best_impl");
  100. component->push_task = best_implementation_push_task;
  101. component->pull_task = best_implementation_pull_task;
  102. return component;
  103. }