load_balancer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016 Inria
  4. * Copyright (C) 2017 CNRS
  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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <starpu.h>
  20. #include <starpu_mpi.h>
  21. #include <starpu_scheduler.h>
  22. #include <common/utils.h>
  23. #include <starpu_mpi_lb.h>
  24. #include "policy/load_balancer_policy.h"
  25. static struct load_balancer_policy *defined_policy = NULL;
  26. typedef void (*_post_exec_hook_func_t)(struct starpu_task *task, unsigned sched_ctx_id);
  27. static _post_exec_hook_func_t saved_post_exec_hook[STARPU_NMAX_SCHED_CTXS];
  28. static void post_exec_hook_wrapper(struct starpu_task *task, unsigned sched_ctx_id)
  29. {
  30. //fprintf(stderr,"I am called ! \n");
  31. if (defined_policy && defined_policy->finished_task_entry_point)
  32. defined_policy->finished_task_entry_point();
  33. if (saved_post_exec_hook[sched_ctx_id])
  34. saved_post_exec_hook[sched_ctx_id](task, sched_ctx_id);
  35. }
  36. static struct load_balancer_policy *predefined_policies[] =
  37. {
  38. &load_heat_propagation_policy,
  39. NULL
  40. };
  41. void starpu_mpi_lb_init(const char *lb_policy_name, struct starpu_mpi_lb_conf *itf)
  42. {
  43. int ret;
  44. const char *policy_name = starpu_getenv("STARPU_MPI_LB");
  45. if (!policy_name)
  46. policy_name = lb_policy_name;
  47. if (!policy_name || (strcmp(policy_name, "help") == 0))
  48. {
  49. _STARPU_MSG("Warning : load balancing is disabled for this run.\n");
  50. _STARPU_MSG("Use the STARPU_MPI_LB = <name> environment variable to use a load balancer.\n");
  51. _STARPU_MSG("Available load balancers :\n");
  52. struct load_balancer_policy **policy;
  53. for(policy=predefined_policies ; *policy!=NULL ; policy++)
  54. {
  55. struct load_balancer_policy *p = *policy;
  56. fprintf(stderr," - %s\n", p->policy_name);
  57. }
  58. return;
  59. }
  60. if (policy_name)
  61. {
  62. struct load_balancer_policy **policy;
  63. for(policy=predefined_policies ; *policy!=NULL ; policy++)
  64. {
  65. struct load_balancer_policy *p = *policy;
  66. if (p->policy_name)
  67. {
  68. if (strcmp(policy_name, p->policy_name) == 0)
  69. {
  70. /* we found a policy with the requested name */
  71. defined_policy = p;
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. if (!defined_policy)
  78. {
  79. _STARPU_MSG("Error : no load balancer with the name %s. Load balancing will be disabled for this run.\n", policy_name);
  80. return;
  81. }
  82. ret = defined_policy->init(itf);
  83. if (ret != 0)
  84. {
  85. _STARPU_MSG("Error (%d) in %s->init: invalid starpu_mpi_lb_conf. Load balancing will be disabled for this run.\n", ret, defined_policy->policy_name);
  86. return;
  87. }
  88. /* starpu_register_hook(submitted_task, defined_policy->submitted_task_entry_point); */
  89. if (defined_policy->submitted_task_entry_point)
  90. starpu_mpi_pre_submit_hook_register(defined_policy->submitted_task_entry_point);
  91. /* starpu_register_hook(finished_task, defined_policy->finished_task_entry_point); */
  92. if (defined_policy->finished_task_entry_point)
  93. {
  94. int i;
  95. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  96. {
  97. struct starpu_sched_policy *sched_policy = starpu_sched_ctx_get_sched_policy(i);
  98. if (sched_policy)
  99. {
  100. _STARPU_DEBUG("Setting post_exec_hook for scheduling context %d %s (%d)\n", i, sched_policy->policy_name, STARPU_NMAX_SCHED_CTXS);
  101. saved_post_exec_hook[i] = sched_policy->post_exec_hook;
  102. sched_policy->post_exec_hook = post_exec_hook_wrapper;
  103. }
  104. else
  105. saved_post_exec_hook[i] = NULL;
  106. }
  107. }
  108. return;
  109. }
  110. void starpu_mpi_lb_shutdown()
  111. {
  112. if (!defined_policy)
  113. return;
  114. int ret = defined_policy->deinit();
  115. if (ret != 0)
  116. {
  117. _STARPU_MSG("Error (%d) in %s->deinit\n", ret, defined_policy->policy_name);
  118. return;
  119. }
  120. /* starpu_unregister_hook(submitted_task, defined_policy->submitted_task_entry_point); */
  121. if (defined_policy->submitted_task_entry_point)
  122. starpu_mpi_pre_submit_hook_unregister();
  123. /* starpu_unregister_hook(finished_task, defined_policy->finished_task_entry_point); */
  124. if (defined_policy->finished_task_entry_point)
  125. {
  126. int i;
  127. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  128. {
  129. if (saved_post_exec_hook[i])
  130. {
  131. struct starpu_sched_policy *sched_policy = starpu_sched_ctx_get_sched_policy(i);
  132. sched_policy->post_exec_hook = saved_post_exec_hook[i];
  133. saved_post_exec_hook[i] = NULL;
  134. }
  135. }
  136. }
  137. defined_policy = NULL;
  138. }