starpu_barrier.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017-2021 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. #define __COMMON_UTILS_H__
  17. #define _STARPU_MALLOC(p, s) do {p = malloc(s);} while (0)
  18. #define _STARPU_CALLOC(p, n, s) do {p = calloc(n, s);} while (0)
  19. #define _STARPU_REALLOC(p, s) do {p = realloc(p, s);} while (0)
  20. #define STARPU_HG_DISABLE_CHECKING(v) ((void) 0)
  21. #define STARPU_HG_ENABLE_CHECKING(v) ((void) 0)
  22. #define ANNOTATE_HAPPENS_AFTER(v) ((void) 0)
  23. #define ANNOTATE_HAPPENS_BEFORE(v) ((void) 0)
  24. #define STARPU_DEBUG_PREFIX "[starpu]"
  25. #ifdef STARPU_VERBOSE
  26. # define _STARPU_DEBUG(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); fflush(stderr); }} while(0)
  27. #else
  28. # define _STARPU_DEBUG(fmt, ...) do { } while (0)
  29. #endif
  30. #define STARPU_UYIELD() ((void)0)
  31. #ifndef NOCONFIG
  32. #include <common/config.h>
  33. #else
  34. #ifndef _GNU_SOURCE
  35. #define _GNU_SOURCE 1
  36. #endif
  37. // Assuming recent simgrid
  38. #define STARPU_HAVE_SIMGRID_MSG_H
  39. #define STARPU_HAVE_SIMGRID_SEMAPHORE_H
  40. #define STARPU_HAVE_SIMGRID_MUTEX_H
  41. #define STARPU_HAVE_SIMGRID_COND_H
  42. #define STARPU_HAVE_SIMGRID_BARRIER_H
  43. #define STARPU_HAVE_XBT_SYNCHRO_H
  44. #define HAVE_SIMGRID_GET_CLOCK
  45. #define HAVE_SG_ACTOR_SLEEP_FOR
  46. #define HAVE_SG_CFG_SET_INT
  47. #endif
  48. #include <unistd.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <limits.h>
  52. #include <math.h>
  53. #include <common/barrier.h>
  54. #ifdef STARPU_HAVE_SIMGRID_MSG_H
  55. #include <simgrid/msg.h>
  56. #else
  57. #include <msg/msg.h>
  58. #endif
  59. #include <simgrid/modelchecker.h>
  60. #ifdef STARPU_HAVE_XBT_SYNCHRO_H
  61. #include <xbt/synchro.h>
  62. #else
  63. #include <xbt/synchro_core.h>
  64. #endif
  65. /* common/thread.c references these, but doesn't need to have them working anyway */
  66. int
  67. _starpu_simgrid_thread_start(int argc, char *argv[])
  68. {
  69. return 0;
  70. }
  71. size_t _starpu_default_stack_size = 8192;
  72. void
  73. _starpu_simgrid_set_stack_size(size_t stack_size)
  74. {
  75. }
  76. starpu_sg_host_t _starpu_simgrid_get_host_by_name(const char *name)
  77. {
  78. }
  79. static void _starpu_clock_gettime(struct timespec *ts)
  80. {
  81. #ifdef HAVE_SIMGRID_GET_CLOCK
  82. double now = simgrid_get_clock();
  83. #else
  84. double now = MSG_get_clock();
  85. #endif
  86. ts->tv_sec = floor(now);
  87. ts->tv_nsec = floor((now - ts->tv_sec) * 1000000000);
  88. }
  89. void starpu_sleep(float nb_sec)
  90. {
  91. #ifdef HAVE_SG_ACTOR_SLEEP_FOR
  92. sg_actor_sleep_for(nb_sec);
  93. #else
  94. MSG_process_sleep(nb_sec);
  95. #endif
  96. }
  97. #include <common/barrier.c>
  98. #undef STARPU_DEBUG
  99. int starpu_worker_get_id(void) { return 0; }
  100. static inline unsigned _starpu_worker_mutex_is_sched_mutex(int workerid, starpu_pthread_mutex_t *mutex) { return 0; }
  101. #include <common/thread.c>
  102. #ifndef NTHREADS
  103. #define NTHREADS 2
  104. #endif
  105. #ifndef NITERS
  106. #define NITERS 1
  107. #endif
  108. struct _starpu_barrier barrier;
  109. int worker(int argc, char *argv[])
  110. {
  111. unsigned iter;
  112. for (iter = 0; iter < NITERS; iter++)
  113. {
  114. MC_assert(barrier.count <= NTHREADS);
  115. _starpu_barrier_wait(&barrier);
  116. }
  117. return 0;
  118. }
  119. int master(int argc, char *argv[])
  120. {
  121. unsigned i;
  122. _starpu_barrier_init(&barrier, NTHREADS);
  123. for (i = 0; i < NTHREADS; i++)
  124. {
  125. char *s;
  126. asprintf(&s, "%d\n", i);
  127. char **args = malloc(sizeof(char*)*2);
  128. args[0] = s;
  129. args[1] = NULL;
  130. MSG_process_create_with_arguments("test", worker, NULL, MSG_host_self(), 1, args);
  131. }
  132. return 0;
  133. }
  134. #undef main
  135. int main(int argc, char *argv[])
  136. {
  137. if (argc < 3)
  138. {
  139. fprintf(stderr,"usage: %s platform.xml host\n", argv[0]);
  140. exit(EXIT_FAILURE);
  141. }
  142. srand48(0);
  143. MSG_init(&argc, argv);
  144. #ifdef HAVE_SG_CFG_SET_INT
  145. sg_cfg_set_int("contexts/stack-size", 128);
  146. #elif SIMGRID_VERSION_MAJOR < 3 || (SIMGRID_VERSION_MAJOR == 3 && SIMGRID_VERSION_MINOR < 13)
  147. extern xbt_cfg_t _sg_cfg_set;
  148. xbt_cfg_set_int(_sg_cfg_set, "contexts/stack-size", 128);
  149. #else
  150. xbt_cfg_set_int("contexts/stack-size", 128);
  151. #endif
  152. MSG_create_environment(argv[1]);
  153. MSG_process_create("master", master, NULL, MSG_get_host_by_name(argv[2]));
  154. MSG_main();
  155. return 0;
  156. }