starpu_barrier.c 4.2 KB

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