simgrid_cpp.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-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 <starpu.h>
  17. #include <core/simgrid.h>
  18. #include <common/config.h>
  19. #ifdef STARPU_SIMGRID
  20. #if SIMGRID_VERSION >= 32190
  21. #include <simgrid/simix.hpp>
  22. #else
  23. #include <simgrid/simix.h>
  24. #endif
  25. #include <smpi/smpi.h>
  26. /* thread_create function which implements inheritence of MPI privatization */
  27. /* See https://github.com/simgrid/simgrid/issues/139 */
  28. typedef struct
  29. {
  30. void_f_pvoid_t code;
  31. void *userparam;
  32. #if SIMGRID_VERSION < 32501
  33. void *father_data;
  34. #endif
  35. } thread_data_t;
  36. #if SIMGRID_VERSION >= 32501
  37. static void *_starpu_simgrid_xbt_thread_create_wrapper(void *arg)
  38. {
  39. thread_data_t *t = (thread_data_t *) arg;
  40. /* FIXME: Ugly work-around for bug in simgrid: the MPI context is not properly set at MSG process startup */
  41. starpu_sleep(0.000001);
  42. #ifdef HAVE_SMPI_THREAD_CREATE
  43. /* Make this actor inherit SMPI data from father actor */
  44. SMPI_thread_create();
  45. #endif
  46. t->code(t->userparam);
  47. free(t);
  48. return NULL;
  49. }
  50. #else
  51. #if SIMGRID_VERSION >= 32190
  52. static void _starpu_simgrid_xbt_thread_create_wrapper(void)
  53. #else
  54. static int _starpu_simgrid_xbt_thread_create_wrapper(int argc STARPU_ATTRIBUTE_UNUSED, char *argv[] STARPU_ATTRIBUTE_UNUSED)
  55. #endif
  56. {
  57. /* FIXME: Ugly work-around for bug in simgrid: the MPI context is not properly set at MSG process startup */
  58. starpu_sleep(0.000001);
  59. #ifdef HAVE_SMX_ACTOR_T
  60. smx_actor_t
  61. #else
  62. smx_process_t
  63. #endif
  64. self = SIMIX_process_self();
  65. #if SIMGRID_VERSION < 31300
  66. thread_data_t *t = (thread_data_t *) SIMIX_process_self_get_data(self);
  67. #else
  68. thread_data_t *t = (thread_data_t *) SIMIX_process_self_get_data();
  69. #endif
  70. simcall_process_set_data(self, t->father_data);
  71. t->code(t->userparam);
  72. simcall_process_set_data(self, NULL);
  73. free(t);
  74. #if SIMGRID_VERSION < 32190
  75. return 0;
  76. #endif
  77. }
  78. #endif
  79. void _starpu_simgrid_xbt_thread_create(const char *name, starpu_pthread_attr_t *attr, void_f_pvoid_t code, void *param)
  80. {
  81. #if SIMGRID_VERSION >= 32501
  82. starpu_pthread_t t;
  83. thread_data_t *res = (thread_data_t *) malloc(sizeof(thread_data_t));
  84. res->userparam = param;
  85. res->code = code;
  86. starpu_pthread_create_on(name, &t, attr, _starpu_simgrid_xbt_thread_create_wrapper, res, sg_host_self());
  87. #else
  88. if (attr && attr->stacksize)
  89. _starpu_simgrid_set_stack_size(attr->stacksize);
  90. #if SIMGRID_VERSION >= 32190 || defined(HAVE_SIMCALL_PROCESS_CREATE) || defined(simcall_process_create)
  91. #ifdef HAVE_SMX_ACTOR_T
  92. smx_actor_t process STARPU_ATTRIBUTE_UNUSED;
  93. #else
  94. smx_process_t process STARPU_ATTRIBUTE_UNUSED;
  95. #endif
  96. thread_data_t *res = (thread_data_t *) malloc(sizeof(thread_data_t));
  97. res->userparam = param;
  98. res->code = code;
  99. #if SIMGRID_VERSION < 31300
  100. res->father_data = SIMIX_process_self_get_data(SIMIX_process_self());
  101. #else
  102. res->father_data = SIMIX_process_self_get_data();
  103. #endif
  104. #if SIMGRID_VERSION < 31200
  105. simcall_process_create(&process,
  106. #else
  107. process = simcall_process_create(
  108. #endif
  109. name,
  110. _starpu_simgrid_xbt_thread_create_wrapper, res,
  111. #if SIMGRID_VERSION < 31400
  112. SIMIX_host_self_get_name(),
  113. #else
  114. # if defined(HAVE_SG_HOST_SELF) || defined(sg_host_self)
  115. sg_host_self(),
  116. # else
  117. SIMIX_host_self(),
  118. # endif
  119. #endif
  120. #if SIMGRID_VERSION < 31500 || SIMGRID_VERSION == 31559
  121. -1.0,
  122. #endif
  123. #if SIMGRID_VERSION < 32190
  124. 0, NULL,
  125. #endif
  126. /*props */ NULL
  127. #if SIMGRID_VERSION < 31500 || SIMGRID_VERSION == 31559
  128. , 0
  129. #endif
  130. );
  131. #else
  132. STARPU_ABORT_MSG("Can't run StarPU-Simgrid-MPI with a Simgrid version which does not provide simcall_process_create and does not fix https://github.com/simgrid/simgrid/issues/139 , sorry.");
  133. #endif
  134. if (attr && attr->stacksize)
  135. _starpu_simgrid_set_stack_size(_starpu_default_stack_size);
  136. #endif
  137. }
  138. #endif