load_balancer.c 4.7 KB

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